fixed auto_increment
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
jusax23 2023-02-18 11:45:23 +01:00
parent af4208ed3a
commit 2f94997f4a
3 changed files with 38 additions and 11 deletions

9
src/alias.ts Normal file
View file

@ -0,0 +1,9 @@
import { Attribute, Table } from "./db"
export class TableAlias extends Table{
}
export class AttributeAlias extends Attribute{
}

View file

@ -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);

View file

@ -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"),