Aliases #3
3 changed files with 38 additions and 11 deletions
9
src/alias.ts
Normal file
9
src/alias.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import { Attribute, Table } from "./db"
|
||||||
|
|
||||||
|
export class TableAlias extends Table{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AttributeAlias extends Attribute{
|
||||||
|
|
||||||
|
}
|
|
@ -17,18 +17,18 @@ export class DB {
|
||||||
this.type = databaseType;
|
this.type = databaseType;
|
||||||
if (databaseType == dbType.mariadb) {
|
if (databaseType == dbType.mariadb) {
|
||||||
this.mariaPool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
|
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) {
|
/*else if (databaseType == dbType.postgres) {
|
||||||
this.pgPool = new pg.Pool({ host, user, password, max: connectionLimit, database, idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000 });
|
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!");
|
else throw new Error("Unsuported Database type!");
|
||||||
|
|
||||||
this.name = database;
|
this.name = database;
|
||||||
}
|
}
|
||||||
async query(query: Query) {
|
async query(query: Query) {
|
||||||
console.log(query);
|
//console.log(query);
|
||||||
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
||||||
/*else if (this.type == dbType.postgres) {
|
/*else if (this.type == dbType.postgres) {
|
||||||
let res = await this.pgPool.query(query.sql, query.values);
|
let res = await this.pgPool.query(query.sql, query.values);
|
||||||
|
|
|
@ -4,6 +4,10 @@ import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery
|
||||||
import { allModifierInput, joinType, onAction, primaryData } from "./types"
|
import { allModifierInput, joinType, onAction, primaryData } from "./types"
|
||||||
|
|
||||||
export class Handler {
|
export class Handler {
|
||||||
|
db: DB;
|
||||||
|
constructor(db: DB){
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
async syncDB(db: DB, handler: Handler, deleteInDB: boolean = false) {
|
async syncDB(db: DB, handler: Handler, deleteInDB: boolean = false) {
|
||||||
//Constraint Data
|
//Constraint Data
|
||||||
let constraints = await db.query(handler.builders.query(handler.querys.listConstraints(handler, db)));
|
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 del = new QueryBuilder();
|
||||||
const create = new QueryBuilder();
|
const create = new QueryBuilder();
|
||||||
|
const connst = new QueryBuilder();
|
||||||
|
|
||||||
function checkUniqueIsVaild(n: string, table: string) {
|
function checkUniqueIsVaild(n: string, table: string) {
|
||||||
let t = db.getTable(table);
|
let t = db.getTable(table);
|
||||||
|
@ -166,7 +171,7 @@ export class Handler {
|
||||||
let changePrimary = false;
|
let changePrimary = false;
|
||||||
if (tableD == null) {
|
if (tableD == null) {
|
||||||
create.appendEnding(handler.querys.create(handler, table));
|
create.appendEnding(handler.querys.create(handler, table));
|
||||||
changePrimary = true;
|
//changePrimary = true;
|
||||||
} else {
|
} else {
|
||||||
let keys = Object.keys(table.dbLangTableAttributes);
|
let keys = Object.keys(table.dbLangTableAttributes);
|
||||||
for (let j = 0; j < keys.length; j++) {
|
for (let j = 0; j < keys.length; j++) {
|
||||||
|
@ -205,19 +210,20 @@ export class Handler {
|
||||||
for (let j = 0; j < table.dbLangConstrains.length; j++) {
|
for (let j = 0; j < table.dbLangConstrains.length; j++) {
|
||||||
const c = table.dbLangConstrains[j];
|
const c = table.dbLangConstrains[j];
|
||||||
if (c instanceof checkConstraint) {
|
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) {
|
} else if (c instanceof uniqueConstraint) {
|
||||||
if (!checkUniqueExists(table, c)) {
|
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) {
|
} else if (c instanceof foreignConstraint) {
|
||||||
if (!checkForeignExists(table, c)) {
|
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 (!create.isEmpty()) del.append(create);
|
||||||
|
if (!connst.isEmpty()) del.append(connst);
|
||||||
if (!del.isEmpty()) await db.query(handler.builders.query(del));
|
if (!del.isEmpty()) await db.query(handler.builders.query(del));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,6 +358,15 @@ export class Handler {
|
||||||
builder.append(a.serializeSettings(handler));
|
builder.append(a.serializeSettings(handler));
|
||||||
if (i + 1 < keys.length) builder.addCode(", ");
|
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(")");
|
builder.addCode(")");
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
|
@ -527,15 +542,18 @@ export class Handler {
|
||||||
if (key.match(/^`.+`$/g)) return key;
|
if (key.match(/^`.+`$/g)) return key;
|
||||||
return `\`${key.replace(/`/g, '``')}\``;
|
return `\`${key.replace(/`/g, '``')}\``;
|
||||||
},
|
},
|
||||||
|
escapeLiteral: (data: any): string => {
|
||||||
|
return this.db.mariaPool.escape(data);
|
||||||
|
},
|
||||||
compareDatatypes: (handler: Handler, attr: Datatype, curr: String) => {
|
compareDatatypes: (handler: Handler, attr: Datatype, curr: String) => {
|
||||||
let qb = attr.serialize(handler);
|
let qb = attr.serialize(handler);
|
||||||
let sql = "";
|
let sql = "";
|
||||||
for (let i = 0; i < qb.list.length; i++) {
|
for (let i = 0; i < qb.list.length; i++) {
|
||||||
const [inject, data] = qb.list[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(")");
|
builder.addCode(")");
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
bool: dataTypeNoArg("bool"),
|
bool: dataTypeNoArg("tinyint"),
|
||||||
bit: dataTypeNoArg("bit"),
|
bit: dataTypeNoArg("bit"),
|
||||||
tinyint: dataTypeNoArg("tinyint"),
|
tinyint: dataTypeNoArg("tinyint"),
|
||||||
smallint: dataTypeNoArg("smallint"),
|
smallint: dataTypeNoArg("smallint"),
|
||||||
|
|
Loading…
Reference in a new issue