diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index f7c77a1..53a2380 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -4,10 +4,59 @@ import * as TomgUtils from "./../Utils"; import { RelationInfo } from "../models/RelationInfo"; import { ColumnInfo } from "../models/ColumnInfo"; import { ManyToMany } from "typeorm"; +import { + WithWidthColumnType, + WithPrecisionColumnType, + WithLengthColumnType +} from "./../../node_modules/typeorm/driver/types/ColumnTypes"; + /** * AbstractDriver */ export abstract class AbstractDriver { + ColumnTypesWithWidth: WithWidthColumnType[] = [ + "tinyint", + "smallint", + "mediumint", + "int", + "bigint" + ]; + ColumnTypesWithPrecision: WithPrecisionColumnType[] = [ + "float", + "double", + "dec", + "decimal", + "numeric", + "real", + "double precision", + "number", + "datetime", + "datetime2", + "datetimeoffset", + "time", + "time with time zone", + "time without time zone", + "timestamp", + "timestamp without time zone", + "timestamp with time zone", + "timestamp with local time zone" + ]; + ColumnTypesWithLength: WithLengthColumnType[] = [ + "character varying", + "varying character", + "nvarchar", + "character", + "native character", + "varchar", + "char", + "nchar", + "varchar2", + "nvarchar2", + "raw", + "binary", + "varbinary" + ]; + FindManyToManyRelations(dbModel: DatabaseModel) { let manyToManyEntities = dbModel.entities.filter(entity => { return ( diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 865d41e..f8af028 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -64,87 +64,48 @@ export class MssqlDriver extends AbstractDriver { switch (resp.DATA_TYPE) { case "bigint": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "bit": colInfo.ts_type = "boolean"; break; case "decimal": colInfo.ts_type = "number"; - colInfo.numericPrecision = resp.NUMERIC_PRECISION; - colInfo.numericScale = resp.NUMERIC_SCALE; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "int": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "money": colInfo.ts_type = "number"; break; case "numeric": colInfo.ts_type = "number"; - colInfo.numericPrecision = resp.NUMERIC_PRECISION; - colInfo.numericScale = resp.NUMERIC_SCALE; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "smallint": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "smallmoney": colInfo.ts_type = "number"; break; case "tinyint": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "float": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; - colInfo.numericPrecision = resp.NUMERIC_PRECISION; break; case "real": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "date": colInfo.ts_type = "Date"; break; case "datetime2": colInfo.ts_type = "Date"; - colInfo.numericPrecision = resp.NUMERIC_PRECISION; break; case "datetime": colInfo.ts_type = "Date"; break; case "datetimeoffset": colInfo.ts_type = "Date"; - colInfo.numericPrecision = resp.NUMERIC_PRECISION; break; case "smalldatetime": colInfo.ts_type = "Date"; @@ -154,54 +115,30 @@ export class MssqlDriver extends AbstractDriver { break; case "char": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "text": colInfo.ts_type = "string"; break; case "varchar": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "nchar": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "ntext": colInfo.ts_type = "string"; break; case "nvarchar": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "binary": colInfo.ts_type = "Buffer"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "image": colInfo.ts_type = "Buffer"; break; case "varbinary": colInfo.ts_type = "Buffer"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "hierarchyid": colInfo.ts_type = "string"; @@ -235,6 +172,25 @@ export class MssqlDriver extends AbstractDriver { break; } + if ( + this.ColumnTypesWithPrecision.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.numericPrecision = resp.NUMERIC_PRECISION; + colInfo.numericScale = resp.NUMERIC_SCALE; + } + if ( + this.ColumnTypesWithLength.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.lenght = + resp.CHARACTER_MAXIMUM_LENGTH > 0 + ? resp.CHARACTER_MAXIMUM_LENGTH + : null; + } + if (colInfo.sql_type) ent.Columns.push(colInfo); }); }); diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 01722fe..21d1431 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -64,66 +64,32 @@ export class MysqlDriver extends AbstractDriver { switch (resp.DATA_TYPE) { case "int": colInfo.ts_type = "number"; - colInfo.width = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "tinyint": if (resp.column_type == "tinyint(1)") { colInfo.width = 1; colInfo.ts_type = "boolean"; } else { - colInfo.width = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; colInfo.ts_type = "number"; } break; case "smallint": colInfo.ts_type = "number"; - colInfo.width = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "mediumint": colInfo.ts_type = "number"; - colInfo.width = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "bigint": colInfo.ts_type = "string"; - colInfo.width = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "float": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "double": colInfo.ts_type = "number"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "decimal": colInfo.ts_type = "string"; - colInfo.numericPrecision = resp.NUMERIC_PRECISION; - colInfo.numericScale = resp.NUMERIC_SCALE; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "date": colInfo.ts_type = "string"; @@ -145,10 +111,6 @@ export class MysqlDriver extends AbstractDriver { break; case "varchar": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.CHARACTER_MAXIMUM_LENGTH > 0 - ? resp.CHARACTER_MAXIMUM_LENGTH - : null; break; case "blob": colInfo.ts_type = "Buffer"; @@ -220,6 +182,37 @@ export class MysqlDriver extends AbstractDriver { ); break; } + if ( + this.ColumnTypesWithPrecision.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.numericPrecision = resp.NUMERIC_PRECISION; + colInfo.numericScale = resp.NUMERIC_SCALE; + } + if ( + this.ColumnTypesWithLength.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.lenght = + resp.CHARACTER_MAXIMUM_LENGTH > 0 + ? resp.CHARACTER_MAXIMUM_LENGTH + : null; + } + if ( + this.ColumnTypesWithWidth.some( + v => + v == colInfo.sql_type && + colInfo.ts_type != "boolean" + ) + ) { + colInfo.width = + resp.CHARACTER_MAXIMUM_LENGTH > 0 + ? resp.CHARACTER_MAXIMUM_LENGTH + : null; + } + if (colInfo.sql_type) ent.Columns.push(colInfo); }); }); diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index 41a769d..337fc4d 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -60,14 +60,12 @@ export class OracleDriver extends AbstractDriver { case "number": colInfo.ts_type = "number"; colInfo.sql_type = "int"; - colInfo.char_max_lenght = - resp[5] > 0 ? resp[5] : null; + colInfo.lenght = resp[5] > 0 ? resp[5] : null; break; case "varchar2": colInfo.ts_type = "number"; colInfo.sql_type = "smallint"; - colInfo.char_max_lenght = - resp[5] > 0 ? resp[5] : null; + colInfo.lenght = resp[5] > 0 ? resp[5] : null; break; default: TomgUtils.LogError( diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 8930382..2fa536c 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -91,13 +91,9 @@ export class PostgresDriver extends AbstractDriver { break; case "numeric": colInfo.ts_type = "string"; - colInfo.numericPrecision = resp.numeric_precision; - colInfo.numericScale = resp.numeric_scale; break; case "real": colInfo.ts_type = "number"; - colInfo.numericPrecision = resp.numeric_precision; - colInfo.numericScale = resp.numeric_scale; break; case "float": colInfo.ts_type = "number"; @@ -110,35 +106,21 @@ export class PostgresDriver extends AbstractDriver { break; case "double precision": colInfo.ts_type = "number"; - colInfo.numericPrecision = resp.numeric_precision; - colInfo.numericScale = resp.numeric_scale; break; case "money": colInfo.ts_type = "string"; break; case "character varying": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.character_maximum_length > 0 - ? resp.character_maximum_length - : null; break; case "varchar": colInfo.ts_type = "string"; break; case "character": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.character_maximum_length > 0 - ? resp.character_maximum_length - : null; break; case "char": colInfo.ts_type = "string"; - colInfo.char_max_lenght = - resp.character_maximum_length > 0 - ? resp.character_maximum_length - : null; break; case "text": colInfo.ts_type = "string"; @@ -279,6 +261,34 @@ export class PostgresDriver extends AbstractDriver { ); break; } + if ( + this.ColumnTypesWithPrecision.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.numericPrecision = resp.numeric_precision; + colInfo.numericScale = resp.numeric_scale; + } + if ( + this.ColumnTypesWithLength.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.lenght = + resp.character_maximum_length > 0 + ? resp.character_maximum_length + : null; + } + if ( + this.ColumnTypesWithWidth.some( + v => v == colInfo.sql_type + ) + ) { + colInfo.width = + resp.character_maximum_length > 0 + ? resp.character_maximum_length + : null; + } if (colInfo.sql_type) ent.Columns.push(colInfo); }); diff --git a/src/entity.mst b/src/entity.mst index a46d3c7..a04bd48 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -11,8 +11,8 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Man {{^relations}} @Column("{{sql_type}}",{ {{#is_generated}} generated:true,{{/is_generated}}{{#is_nullable}} nullable:true,{{/is_nullable}}{{^is_nullable}} - nullable:false,{{/is_nullable}}{{#char_max_lenght}} - length:{{.}},{{/char_max_lenght}}{{#width}} + nullable:false,{{/is_nullable}}{{#lenght}} + length:{{.}},{{/lenght}}{{#width}} width:{{.}},{{/width}}{{#default}} default:"{{.}}",{{/default}}{{#numericPrecision}} precision:{{.}},{{/numericPrecision}}{{#numericScale}} diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index f9067e4..ab22efc 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -18,7 +18,7 @@ export class ColumnInfo { | "string | string[]" | "any"; sql_type: string; - char_max_lenght: number | null = null; + lenght: number | null = null; width: number | null = null; isPrimary: boolean = false; is_generated: boolean = false; diff --git a/test/drivers/MssqlDriver.test.ts b/test/drivers/MssqlDriver.test.ts index 01bccef..bb48c1e 100644 --- a/test/drivers/MssqlDriver.test.ts +++ b/test/drivers/MssqlDriver.test.ts @@ -94,7 +94,7 @@ describe('MssqlDriver', function () { entities.push(y) var expected: EntityInfo[] = JSON.parse(JSON.stringify(entities)); expected[0].Columns.push({ - char_max_lenght: null, + lenght: null, default: 'a', is_nullable: true, isPrimary: false,