Adding some null controls.

This commit is contained in:
mike12345567 2022-12-02 16:17:56 +00:00
parent e366f850d2
commit 2d9dd1a667
5 changed files with 48 additions and 58 deletions

View File

@ -11,7 +11,7 @@ GO
CREATE TABLE products CREATE TABLE products
( (
id int IDENTITY(1,1), id int IDENTITY(1,1),
name varchar (20), name varchar (20) NOT NULL,
description varchar(30), description varchar(30),
CONSTRAINT pk_products PRIMARY KEY NONCLUSTERED (id) CONSTRAINT pk_products PRIMARY KEY NONCLUSTERED (id)
); );
@ -22,7 +22,7 @@ GO
CREATE TABLE tasks CREATE TABLE tasks
( (
taskid int IDENTITY(1,1), taskid int IDENTITY(1,1),
taskname varchar (20), taskname varchar (20) NOT NULL,
productid int, productid int,
CONSTRAINT pk_tasks PRIMARY KEY NONCLUSTERED (taskid), CONSTRAINT pk_tasks PRIMARY KEY NONCLUSTERED (taskid),
CONSTRAINT fk_products FOREIGN KEY (productid) REFERENCES products (id), CONSTRAINT fk_products FOREIGN KEY (productid) REFERENCES products (id),
@ -33,7 +33,7 @@ IF OBJECT_ID ('dbo.people', 'U') IS NOT NULL
GO GO
CREATE TABLE people CREATE TABLE people
( (
name varchar(30), name varchar(30) NOT NULL,
age varchar(20), age varchar(20),
CONSTRAINT pk_people PRIMARY KEY NONCLUSTERED (name, age) CONSTRAINT pk_people PRIMARY KEY NONCLUSTERED (name, age)
); );

View File

@ -1,14 +1,5 @@
SELECT 'CREATE DATABASE main' SELECT 'CREATE DATABASE main'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
CREATE SCHEMA test;
CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer');
CREATE TABLE Persons (
PersonID SERIAL PRIMARY KEY,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Belfast',
Type person_job
CREATE TABLE IF NOT EXISTS public."Employee" CREATE TABLE IF NOT EXISTS public."Employee"
( (
@ -19,18 +10,6 @@ CREATE TABLE IF NOT EXISTS public."Employee"
WITH ( WITH (
OIDS = FALSE OIDS = FALSE
); );
CREATE TABLE Tasks (
TaskID SERIAL PRIMARY KEY,
ExecutorID INT,
QaID INT,
Completed BOOLEAN,
TaskName varchar(255),
CONSTRAINT fkexecutor
FOREIGN KEY(ExecutorID)
REFERENCES Persons(PersonID),
CONSTRAINT fkqa
FOREIGN KEY(QaID)
REFERENCES Persons(PersonID)
INSERT INTO public."Employee" ("id", "name") VALUES (1, 'Alice'); INSERT INTO public."Employee" ("id", "name") VALUES (1, 'Alice');
INSERT INTO public."Employee" ("id", "name") VALUES (2, 'Bob'); INSERT INTO public."Employee" ("id", "name") VALUES (2, 'Bob');
@ -43,9 +22,6 @@ CREATE TABLE IF NOT EXISTS public."Skills"
WITH ( WITH (
OIDS = FALSE OIDS = FALSE
); );
CREATE TABLE Products (
ProductID SERIAL PRIMARY KEY,
ProductName varchar(255)
INSERT INTO public."Skills" ("id", "name") VALUES (1, 'Docker'); INSERT INTO public."Skills" ("id", "name") VALUES (1, 'Docker');
INSERT INTO public."Skills" ("id", "name") VALUES (2, 'Microservices'); INSERT INTO public."Skills" ("id", "name") VALUES (2, 'Microservices');
@ -61,33 +37,6 @@ CREATE TABLE IF NOT EXISTS public."jt_employee_skills_Skills_employee"
WITH ( WITH (
OIDS = FALSE OIDS = FALSE
); );
CREATE TABLE Products_Tasks (
ProductID INT NOT NULL,
TaskID INT NOT NULL,
CONSTRAINT fkProducts
FOREIGN KEY(ProductID)
REFERENCES Products(ProductID),
CONSTRAINT fkTasks
FOREIGN KEY(TaskID)
REFERENCES Tasks(TaskID),
PRIMARY KEY (ProductID, TaskID)
);
CREATE TABLE test.table1 (
id SERIAL PRIMARY KEY,
Name varchar(255)
);
INSERT INTO Persons (FirstName, LastName, Address, City, Type) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast', 'qa');
INSERT INTO Persons (FirstName, LastName, Address, City, Type) VALUES ('John', 'Smith', '64 Updown Road', 'Dublin', 'programmer');
INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (1, 2, 'assembling', TRUE);
INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (2, 1, 'processing', FALSE);
INSERT INTO Products (ProductName) VALUES ('Computers');
INSERT INTO Products (ProductName) VALUES ('Laptops');
INSERT INTO Products (ProductName) VALUES ('Chairs');
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (1, 1);
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (2, 1);
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (3, 1);
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (1, 2);
INSERT INTO test.table1 (Name) VALUES ('Test');
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (1, 1, 1); insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (1, 1, 1);
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (2, 1, 2); insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (2, 1, 2);

View File

@ -38,6 +38,34 @@ interface TablesResponse {
TABLE_TYPE: string TABLE_TYPE: string
} }
type MSSQLColumn = {
IS_COMPUTED: number
IS_IDENTITY: number
TABLE_CATALOG: string
TABLE_SCHEMA: string
TABLE_NAME: string
COLUMN_NAME: string
ORDINAL_POSITION: number
COLUMN_DEFAULT: null | any
IS_NULLABLE: "NO" | "YES"
DATA_TYPE: string
CHARACTER_MAXIMUM_LENGTH: null | number
CHARACTER_OCTET_LENGTH: null | number
NUMERIC_PRECISION: null | string
NUMERIC_PRECISION_RADIX: null | string
NUMERIC_SCALE: null | string
DATETIME_PRECISION: null | string
CHARACTER_SET_CATALOG: null | string
CHARACTER_SET_SCHEMA: null | string
CHARACTER_SET_NAME: null | string
COLLATION_CATALOG: null | string
COLLATION_SCHEMA: null | string
COLLATION_NAME: null | string
DOMAIN_CATALOG: null | string
DOMAIN_SCHEMA: null | string
DOMAIN_NAME: null | string
}
const SCHEMA: Integration = { const SCHEMA: Integration = {
docs: "https://github.com/tediousjs/node-mssql", docs: "https://github.com/tediousjs/node-mssql",
plus: true, plus: true,
@ -228,15 +256,20 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
// find primary key constraints // find primary key constraints
const constraints = await this.runSQL(this.getConstraintsSQL(tableName)) const constraints = await this.runSQL(this.getConstraintsSQL(tableName))
// find the computed and identity columns (auto columns) // find the computed and identity columns (auto columns)
const columns = await this.runSQL(this.getAutoColumnsSQL(tableName)) const columns: MSSQLColumn[] = await this.runSQL(
this.getAutoColumnsSQL(tableName)
)
const primaryKeys = constraints const primaryKeys = constraints
.filter( .filter(
(constraint: any) => constraint.CONSTRAINT_TYPE === "PRIMARY KEY" (constraint: any) => constraint.CONSTRAINT_TYPE === "PRIMARY KEY"
) )
.map((constraint: any) => constraint.COLUMN_NAME) .map((constraint: any) => constraint.COLUMN_NAME)
const autoColumns = columns const autoColumns = columns
.filter((col: any) => col.IS_COMPUTED || col.IS_IDENTITY) .filter(col => col.IS_COMPUTED || col.IS_IDENTITY)
.map((col: any) => col.COLUMN_NAME) .map(col => col.COLUMN_NAME)
const requiredColumns = columns
.filter(col => col.IS_NULLABLE === "NO")
.map(col => col.COLUMN_NAME)
let schema: TableSchema = {} let schema: TableSchema = {}
for (let def of definition) { for (let def of definition) {
@ -245,8 +278,11 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
continue continue
} }
schema[name] = { schema[name] = {
autocolumn: !!autoColumns.find((col: string) => col === name), autocolumn: !!autoColumns.find(col => col === name),
name: name, name: name,
constraints: {
presence: requiredColumns.find(col => col === name),
},
...convertSqlType(def.DATA_TYPE), ...convertSqlType(def.DATA_TYPE),
externalType: def.DATA_TYPE, externalType: def.DATA_TYPE,
} }

View File

@ -334,6 +334,7 @@ class OracleIntegration extends Sql implements DatasourcePlus {
fieldSchema = { fieldSchema = {
autocolumn: OracleIntegration.isAutoColumn(oracleColumn), autocolumn: OracleIntegration.isAutoColumn(oracleColumn),
name: columnName, name: columnName,
// TODO: add required constraint
...this.internalConvertType(oracleColumn), ...this.internalConvertType(oracleColumn),
} }
table.schema[columnName] = fieldSchema table.schema[columnName] = fieldSchema

View File

@ -260,6 +260,9 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
column.identity_start || column.identity_start ||
column.identity_increment column.identity_increment
) )
const constraints = {
presence: column.is_nullable === "NO",
}
const hasDefault = const hasDefault =
typeof column.column_default === "string" && typeof column.column_default === "string" &&
column.column_default.startsWith("nextval") column.column_default.startsWith("nextval")
@ -269,6 +272,7 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
tables[tableName].schema[columnName] = { tables[tableName].schema[columnName] = {
autocolumn: isAuto, autocolumn: isAuto,
name: columnName, name: columnName,
constraints,
...convertSqlType(column.data_type), ...convertSqlType(column.data_type),
externalType: column.data_type, externalType: column.data_type,
} }