From 2f94997f4a4637fd57ffaa2fea79e4a5bea465da Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 18 Feb 2023 11:45:23 +0100 Subject: [PATCH] fixed auto_increment --- src/alias.ts | 9 +++++++++ src/db.ts | 6 +++--- src/defaultHandler.ts | 34 ++++++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/alias.ts diff --git a/src/alias.ts b/src/alias.ts new file mode 100644 index 0000000..501753c --- /dev/null +++ b/src/alias.ts @@ -0,0 +1,9 @@ +import { Attribute, Table } from "./db" + +export class TableAlias extends Table{ + +} + +export class AttributeAlias extends Attribute{ + +} \ No newline at end of file diff --git a/src/db.ts b/src/db.ts index 06af42b..1ea9169 100644 --- a/src/db.ts +++ b/src/db.ts @@ -17,18 +17,18 @@ export class DB { this.type = databaseType; if (databaseType == dbType.mariadb) { this.mariaPool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true }); - this.handler = new Handler(); + this.handler = new Handler(this); } /*else if (databaseType == dbType.postgres) { this.pgPool = new pg.Pool({ host, user, password, max: connectionLimit, database, idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000 }); - this.handler = new postgresHandler(); + this.handler = new postgresHandler(this); }*/ else throw new Error("Unsuported Database type!"); this.name = database; } async query(query: Query) { - console.log(query); + //console.log(query); if (this.type == dbType.mariadb) return await this.mariaPool.query(query); /*else if (this.type == dbType.postgres) { let res = await this.pgPool.query(query.sql, query.values); diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 0d7e63d..d5f9c3c 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -4,6 +4,10 @@ import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery import { allModifierInput, joinType, onAction, primaryData } from "./types" export class Handler { + db: DB; + constructor(db: DB){ + this.db = db; + } async syncDB(db: DB, handler: Handler, deleteInDB: boolean = false) { //Constraint Data let constraints = await db.query(handler.builders.query(handler.querys.listConstraints(handler, db))); @@ -28,6 +32,7 @@ export class Handler { const del = new QueryBuilder(); const create = new QueryBuilder(); + const connst = new QueryBuilder(); function checkUniqueIsVaild(n: string, table: string) { let t = db.getTable(table); @@ -166,7 +171,7 @@ export class Handler { let changePrimary = false; if (tableD == null) { create.appendEnding(handler.querys.create(handler, table)); - changePrimary = true; + //changePrimary = true; } else { let keys = Object.keys(table.dbLangTableAttributes); for (let j = 0; j < keys.length; j++) { @@ -205,19 +210,20 @@ export class Handler { for (let j = 0; j < table.dbLangConstrains.length; j++) { const c = table.dbLangConstrains[j]; if (c instanceof checkConstraint) { - create.appendEnding(handler.querys.addCheck(handler, table, c)); + connst.appendEnding(handler.querys.addCheck(handler, table, c)); } else if (c instanceof uniqueConstraint) { if (!checkUniqueExists(table, c)) { - create.appendEnding(handler.querys.addUnique(handler, table, c)); + connst.appendEnding(handler.querys.addUnique(handler, table, c)); } } else if (c instanceof foreignConstraint) { if (!checkForeignExists(table, c)) { - create.appendEnding(handler.querys.addForeignKey(handler, table, c)); + connst.appendEnding(handler.querys.addForeignKey(handler, table, c)); } } } } if (!create.isEmpty()) del.append(create); + if (!connst.isEmpty()) del.append(connst); if (!del.isEmpty()) await db.query(handler.builders.query(del)); } @@ -352,6 +358,15 @@ export class Handler { builder.append(a.serializeSettings(handler)); if (i + 1 < keys.length) builder.addCode(", "); } + let primAttr = Object.entries(table.dbLangTableAttributes) + .filter(([n, attr]) => !!attr.ops.primaryKey); + if (primAttr.length > 0) { + builder.addCode(", primary key ("); + builder.addCodeCommaSeperated( + primAttr.map(([n, attr]) => handler.builders.escapeID(n)) + ); + builder.addCode(")"); + } builder.addCode(")"); return builder; }, @@ -527,15 +542,18 @@ export class Handler { if (key.match(/^`.+`$/g)) return key; return `\`${key.replace(/`/g, '``')}\``; }, + escapeLiteral: (data: any): string => { + return this.db.mariaPool.escape(data); + }, compareDatatypes: (handler: Handler, attr: Datatype, curr: String) => { let qb = attr.serialize(handler); let sql = ""; for (let i = 0; i < qb.list.length; i++) { const [inject, data] = qb.list[i]; - sql += data; + if(inject)sql += handler.builders.escapeLiteral(data); + else sql += data; } - - return curr.split(" ").join("").startsWith(sql.split(" ").join("")); + return curr.split(", ").join(",").startsWith(sql.split(", ").join(",")); }, } @@ -724,7 +742,7 @@ export class Handler { builder.addCode(")"); return builder; }, - bool: dataTypeNoArg("bool"), + bool: dataTypeNoArg("tinyint"), bit: dataTypeNoArg("bit"), tinyint: dataTypeNoArg("tinyint"), smallint: dataTypeNoArg("smallint"),