2023-01-24 20:34:03 +01:00
|
|
|
import { Attribute, DB, Table } from "./db"
|
2023-02-14 17:37:01 +01:00
|
|
|
import { Aggregation, checkConstraint, Constraint, Datatype, foreignConstraint, Modifier, uniqueConstraint } from "./dbStructure"
|
2023-02-14 22:23:21 +01:00
|
|
|
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query"
|
2023-02-14 17:37:01 +01:00
|
|
|
import { allModifierInput, onAction, primaryData, serializeReturn } from "./types"
|
2023-01-23 21:19:18 +01:00
|
|
|
|
|
|
|
export class Handler {
|
2023-02-13 23:44:08 +01:00
|
|
|
async syncDB(db: DB, handler: Handler, deleteInDB: boolean = false) {
|
|
|
|
console.log("start sync");
|
2023-01-24 20:34:03 +01:00
|
|
|
|
2023-02-13 23:44:08 +01:00
|
|
|
let gd = new QueryBuilder();
|
|
|
|
gd.addCode(`SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where CONSTRAINT_SCHEMA = `);
|
|
|
|
gd.addInjection(db.name);
|
|
|
|
gd.addCode(` or TABLE_SCHEMA = `);
|
|
|
|
gd.addInjection(db.name);
|
|
|
|
gd.addCode(`;select * from information_schema.table_constraints where CONSTRAINT_SCHEMA = `);
|
|
|
|
gd.addInjection(db.name)
|
|
|
|
gd.addCode(` or TABLE_SCHEMA = `);
|
|
|
|
gd.addInjection(db.name);
|
|
|
|
|
|
|
|
let [key, constraints] = await db.query(handler.builders.query(gd));
|
|
|
|
|
2023-02-14 17:37:01 +01:00
|
|
|
//Table List
|
2023-02-14 16:45:21 +01:00
|
|
|
let allTables = (await db.query(handler.builders.query(handler.querys.listTables(handler, db)))).map((d: any) => d.TABLE_NAME);
|
2023-02-13 23:44:08 +01:00
|
|
|
|
2023-02-14 17:37:01 +01:00
|
|
|
// gether Table Schematics
|
2023-02-14 16:45:21 +01:00
|
|
|
let tableData: { [key: string]: any; } = {};
|
2023-02-13 23:44:08 +01:00
|
|
|
for (let i = 0; i < db.tables.length; i++) {
|
|
|
|
const table = db.tables[i];
|
|
|
|
try {
|
|
|
|
const tableDataBuilder = new QueryBuilder();
|
|
|
|
tableDataBuilder.addCode("DESCRIBE ");
|
|
|
|
tableDataBuilder.addCode(table.serialize(handler));
|
2023-02-14 16:45:21 +01:00
|
|
|
tableData[table.dbLangTableName.toLowerCase()] =
|
|
|
|
Object.fromEntries((await db.query(handler.builders.query(tableDataBuilder))).map((d: any) => [d.Field.toLowerCase(), d]));
|
2023-02-13 23:44:08 +01:00
|
|
|
} catch (_) {
|
2023-02-14 16:45:21 +01:00
|
|
|
tableData[table.dbLangTableName.toLowerCase()] = null;
|
2023-02-13 23:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const del = new QueryBuilder();
|
|
|
|
const create = new QueryBuilder();
|
|
|
|
|
2023-02-14 16:45:21 +01:00
|
|
|
function checkUniqueIsVaild(n: string, table: string) {
|
|
|
|
let t = db.getTable(table);
|
|
|
|
if (t == null) return false;
|
|
|
|
for (let i = 0; i < t.dbLangConstrains.length; i++) {
|
|
|
|
const c = t.dbLangConstrains[i];
|
|
|
|
if (c.name == n.toLowerCase() && c instanceof uniqueConstraint) {
|
|
|
|
let attrs = c.attrs.map(a => a.name);
|
|
|
|
let found = 0;
|
|
|
|
for (let j = 0; j < key.length; j++) {
|
|
|
|
if (
|
|
|
|
key[j].CONSTRAINT_NAME == n
|
|
|
|
&& key[j].TABLE_NAME == table
|
|
|
|
) {
|
|
|
|
if (attrs.includes(key[j].COLUMN_NAME.toLowerCase())) found++;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found == attrs.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-14 17:37:01 +01:00
|
|
|
|
|
|
|
function checkForeignIsVaild(n: string, table: string) {
|
|
|
|
let t = db.getTable(table);
|
|
|
|
if (t == null) return false;
|
|
|
|
for (let i = 0; i < t.dbLangConstrains.length; i++) {
|
|
|
|
const c = t.dbLangConstrains[i];
|
|
|
|
if (c.name == n.toLowerCase() && c instanceof foreignConstraint) {
|
|
|
|
let fromAttrs = c.fromAttrs.map(a => a.name);
|
|
|
|
let toAttrs = c.toAttrs.map(a => a.name);
|
|
|
|
let refTable = c.toAttrs[0].table.dbLangTableName;
|
|
|
|
let found = 0;
|
|
|
|
for (let j = 0; j < key.length; j++) {
|
|
|
|
if (
|
|
|
|
key[j].CONSTRAINT_NAME == n
|
|
|
|
&& key[j].TABLE_NAME == table
|
|
|
|
&& key[j].REFERENCED_TABLE_NAME == refTable
|
|
|
|
) {
|
|
|
|
let inF = fromAttrs.indexOf(key[j].COLUMN_NAME.toLowerCase());
|
|
|
|
let inT = toAttrs.indexOf(key[j].REFERENCED_COLUMN_NAME.toLowerCase());
|
|
|
|
if (inF != -1 && inT == inF) found++;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found == fromAttrs.length && found == toAttrs.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:45:21 +01:00
|
|
|
function checkUniqueExists(tabel: Table, c: uniqueConstraint) {
|
|
|
|
let attrs = c.attrs.map(a => a.name);
|
|
|
|
let found = 0;
|
|
|
|
for (let j = 0; j < key.length; j++) {
|
|
|
|
if (
|
|
|
|
key[j].CONSTRAINT_NAME.toLowerCase() == c.name
|
|
|
|
&& key[j].TABLE_NAME == tabel.dbLangTableName
|
|
|
|
) {
|
|
|
|
if (attrs.includes(key[j].COLUMN_NAME.toLowerCase())) found++;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found == attrs.length;
|
|
|
|
}
|
2023-02-14 17:37:01 +01:00
|
|
|
function checkForeignExists(tabel: Table, c: foreignConstraint) {
|
|
|
|
let fromAttrs = c.fromAttrs.map(a => a.name);
|
|
|
|
let toAttrs = c.toAttrs.map(a => a.name);
|
|
|
|
let refTable = c.toAttrs[0].table.dbLangTableName;
|
|
|
|
let found = 0;
|
|
|
|
for (let j = 0; j < key.length; j++) {
|
|
|
|
if (
|
|
|
|
key[j].CONSTRAINT_NAME == c.name
|
|
|
|
&& key[j].TABLE_NAME == tabel.dbLangTableName
|
|
|
|
&& key[j].REFERENCED_TABLE_NAME == refTable
|
|
|
|
) {
|
|
|
|
let inF = fromAttrs.indexOf(key[j].COLUMN_NAME.toLowerCase());
|
|
|
|
let inT = toAttrs.indexOf(key[j].REFERENCED_COLUMN_NAME.toLowerCase());
|
|
|
|
if (inF != -1 && inT == inF) found++;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found == fromAttrs.length && found == toAttrs.length;
|
|
|
|
}
|
2023-02-14 16:45:21 +01:00
|
|
|
|
|
|
|
//delete check and unused/false(unique, foreign) constraints
|
2023-02-13 23:44:08 +01:00
|
|
|
for (let i = 0; i < constraints.length; i++) {
|
|
|
|
const c = constraints[i];
|
|
|
|
if (c.CONSTRAINT_TYPE == "CHECK") {
|
|
|
|
del.appendEnding(handler.querys.removeCheck(handler, c.TABLE_NAME, c.CONSTRAINT_NAME));
|
2023-02-14 16:45:21 +01:00
|
|
|
} else if (c.CONSTRAINT_TYPE == "UNIQUE") {
|
|
|
|
if (
|
|
|
|
(typeof tableData[c.TABLE_NAME.toLowerCase()] == "undefined" && deleteInDB)
|
|
|
|
|| !checkUniqueIsVaild(c.CONSTRAINT_NAME, c.TABLE_NAME)
|
|
|
|
) del.appendEnding(handler.querys.removeUnique(handler, c.TABLE_NAME, c.CONSTRAINT_NAME));
|
2023-02-14 17:37:01 +01:00
|
|
|
} else if (c.CONSTRAINT_TYPE == "FOREIGN KEY") {
|
|
|
|
if (
|
|
|
|
(typeof tableData[c.TABLE_NAME.toLowerCase()] == "undefined" && deleteInDB)
|
|
|
|
|| !checkForeignIsVaild(c.CONSTRAINT_NAME, c.TABLE_NAME)
|
|
|
|
) del.appendEnding(handler.querys.removeForeignKey(handler, c.TABLE_NAME, c.CONSTRAINT_NAME));
|
2023-02-13 23:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 16:45:21 +01:00
|
|
|
//delete unused Tables
|
|
|
|
if (deleteInDB) for (let i = 0; i < allTables.length; i++) {
|
|
|
|
if (typeof tableData[allTables[i].toLowerCase()] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
|
|
|
|
}
|
2023-02-13 23:44:08 +01:00
|
|
|
|
2023-02-14 16:45:21 +01:00
|
|
|
function freeForUpdate(attr: string, table: string) {
|
2023-02-13 23:44:08 +01:00
|
|
|
for (let i = 0; i < key.length; i++) {
|
|
|
|
const k = key[i];
|
|
|
|
if (
|
2023-02-14 16:45:21 +01:00
|
|
|
k.REFERENCED_TABLE_NAME == table
|
|
|
|
&& k.REFERENCED_COLUMN_NAME.toLowerCase() == attr.toLowerCase()
|
2023-02-13 23:44:08 +01:00
|
|
|
) {
|
|
|
|
del.appendEnding(handler.querys.removeForeignKey(handler, k.TABLE_NAME, k.CONSTRAINT_NAME));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//create tables
|
|
|
|
for (let i = 0; i < db.tables.length; i++) {
|
|
|
|
const table = db.tables[i];
|
2023-02-14 16:45:21 +01:00
|
|
|
const tableD = tableData[table.dbLangTableName.toLowerCase()];
|
|
|
|
//delete unused Columns
|
2023-02-14 17:37:01 +01:00
|
|
|
if (deleteInDB && tableD != null) {
|
2023-02-14 16:45:21 +01:00
|
|
|
let keys = Object.keys(tableD);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
if (table.dbLangTableAttributes[keys[i]] == null) {
|
|
|
|
freeForUpdate(keys[i].toLowerCase(), table.dbLangTableName);
|
|
|
|
create.appendEnding(handler.querys.removeColumn(handler, table, keys[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//add/mofify columns
|
2023-02-13 23:44:08 +01:00
|
|
|
let changePrimary = false;
|
|
|
|
if (tableD == null) {
|
|
|
|
create.appendEnding(handler.querys.create(handler, table));
|
|
|
|
changePrimary = true;
|
|
|
|
} else {
|
|
|
|
let keys = Object.keys(table.dbLangTableAttributes);
|
|
|
|
for (let j = 0; j < keys.length; j++) {
|
|
|
|
const a = table.dbLangTableAttributes[keys[j]];
|
|
|
|
const attrData = tableD[keys[j]];
|
|
|
|
if (attrData == null) {
|
|
|
|
create.appendEnding(handler.querys.addColumn(handler, a));
|
|
|
|
changePrimary = true;
|
|
|
|
} else if (
|
|
|
|
!handler.builders.compareDatatypes(handler, a.type, attrData.Type) ||
|
|
|
|
a.ops.default != attrData.Default ||
|
|
|
|
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey) != (attrData.Null == "NO") ||
|
|
|
|
(!!a.ops.autoIncrement) != (attrData.Extra == "auto_increment")
|
|
|
|
) {
|
2023-02-14 17:37:01 +01:00
|
|
|
/*console.log(!handler.builders.compareDatatypes(handler, a.type, attrData.Type), "|",
|
2023-02-13 23:44:08 +01:00
|
|
|
a.ops.default, attrData.Default, "|",
|
|
|
|
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey), (attrData.Null == "NO"), "|",
|
2023-02-14 17:37:01 +01:00
|
|
|
(!!a.ops.autoIncrement), (attrData.Extra == "auto_increment"));*/
|
2023-02-14 16:45:21 +01:00
|
|
|
freeForUpdate(a.name, a.table.dbLangTableName);
|
2023-02-13 23:44:08 +01:00
|
|
|
create.appendEnding(handler.querys.changeColumn(handler, a));
|
|
|
|
}
|
|
|
|
if (attrData == null) {
|
|
|
|
changePrimary = true;
|
|
|
|
} else {
|
|
|
|
if ((attrData.Key == "PRI") != (!!a.ops.primaryKey)) {
|
2023-02-14 16:45:21 +01:00
|
|
|
freeForUpdate(a.name, a.table.dbLangTableName);
|
2023-02-13 23:44:08 +01:00
|
|
|
changePrimary = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changePrimary) {
|
2023-02-14 16:45:21 +01:00
|
|
|
create.appendEnding(handler.querys.removePrimaryKey(handler, table.dbLangTableName));
|
2023-02-13 23:44:08 +01:00
|
|
|
create.appendEnding(handler.querys.addPrimaryKey(handler, table));
|
|
|
|
}
|
|
|
|
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));
|
2023-02-14 16:45:21 +01:00
|
|
|
} else if (c instanceof uniqueConstraint) {
|
|
|
|
if (!checkUniqueExists(table, c)) {
|
|
|
|
create.appendEnding(handler.querys.addUnique(handler, table, c));
|
|
|
|
}
|
2023-02-14 17:37:01 +01:00
|
|
|
} else if (c instanceof foreignConstraint) {
|
|
|
|
if (!checkForeignExists(table, c)) {
|
|
|
|
create.appendEnding(handler.querys.addForeignKey(handler, table, c));
|
|
|
|
}
|
2023-02-13 23:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!create.isEmpty()) del.append(create);
|
|
|
|
if (!del.isEmpty()) await db.query(handler.builders.query(del));
|
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
|
|
|
|
querys = {
|
2023-01-29 22:11:24 +01:00
|
|
|
select: (handler: Handler, q: selectQuery): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("select ");
|
|
|
|
builder.append(joinArg(", ")(handler, q.attr));
|
|
|
|
builder.addCode(` from ${q.from == null ? 'DUAL' : q.from.serialize(handler)}`);
|
2023-01-23 21:19:18 +01:00
|
|
|
if (q.whereD) {
|
2023-01-29 22:11:24 +01:00
|
|
|
builder.addCode(" where ");
|
|
|
|
builder.append(q.whereD.serialize(handler));
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-23 22:10:33 +01:00
|
|
|
if (q.groupByD.length > 0) {
|
2023-01-29 22:11:24 +01:00
|
|
|
builder.addCode(" group by ");
|
|
|
|
builder.append(joinArg(",")(handler, q.groupByD));
|
2023-01-23 21:19:18 +01:00
|
|
|
if (q.havingD) {
|
2023-01-29 22:11:24 +01:00
|
|
|
builder.addCode(" having ");
|
|
|
|
builder.append(q.havingD.serialize(handler));
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-29 22:11:24 +01:00
|
|
|
if (q.havingD) {
|
|
|
|
builder.addCode(" limit ");
|
|
|
|
builder.addInjection(q.limitD);
|
|
|
|
}
|
2023-02-13 23:44:08 +01:00
|
|
|
|
|
|
|
/*builder.setHandler((json)=>{
|
|
|
|
return json;
|
|
|
|
});*/
|
|
|
|
|
2023-01-29 22:11:24 +01:00
|
|
|
return builder;
|
2023-01-24 20:34:03 +01:00
|
|
|
},
|
2023-02-14 22:23:21 +01:00
|
|
|
insert: (hander: Handler, q: insertQuery): QueryBuilder => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("INSERT INTO ");
|
|
|
|
qb.addCode(q.attrs[0].table.serialize(hander));
|
|
|
|
qb.addCode("(");
|
|
|
|
qb.addCodeCommaSeperated(q.attrs.map(a => a.serialize(hander)));
|
|
|
|
qb.addCode(") ");
|
|
|
|
if (q.select == null) {
|
|
|
|
qb.addCode("VALUES ");
|
|
|
|
for (let i = 0; i < q.values.length; i++) {
|
|
|
|
qb.addCode("(");
|
|
|
|
qb.addInjectionCommaSeperated(q.values[i]);
|
|
|
|
qb.addCode(")");
|
|
|
|
if (i + 1 < q.values.length) qb.addCode(",");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qb.append(q.select.serialize(hander));
|
|
|
|
}
|
|
|
|
return qb;
|
|
|
|
},
|
|
|
|
update: (handler: Handler, q: updateQuery): QueryBuilder => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("UPDATE ");
|
|
|
|
qb.addCode(q.table.serialize(handler));
|
|
|
|
qb.addCode(" SET ");
|
|
|
|
for (let i = 0; i < q.setD.length; i++) {
|
|
|
|
const s = q.setD[i];
|
|
|
|
qb.addCode(s[0].serialize(handler));
|
|
|
|
qb.addCode(" = (");
|
|
|
|
if (s[1] instanceof Attribute) qb.addCode(s[1].serialize(handler));
|
|
|
|
else if (s[1] instanceof Modifier || s[1] instanceof selectQuery || s[1] instanceof Aggregation) {
|
|
|
|
qb.append(s[1].serialize(handler));
|
|
|
|
} else {
|
|
|
|
qb.addInjection(s[1]);
|
|
|
|
}
|
|
|
|
qb.addCode(")");
|
|
|
|
if (i + 1 < q.setD.length) qb.addCode(", ");
|
|
|
|
}
|
|
|
|
if (q.whereD) {
|
|
|
|
qb.addCode(" where ");
|
|
|
|
qb.append(q.whereD.serialize(handler));
|
|
|
|
}
|
|
|
|
return qb;
|
|
|
|
},
|
|
|
|
remove: (handler: Handler, q: removeQuery): QueryBuilder =>{
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("DELETE FROM ");
|
|
|
|
qb.addCode(q.table.serialize(handler));
|
|
|
|
if (q.whereD) {
|
|
|
|
qb.addCode(" where ");
|
|
|
|
qb.append(q.whereD.serialize(handler));
|
|
|
|
}
|
|
|
|
return qb;
|
|
|
|
},
|
2023-02-14 16:45:21 +01:00
|
|
|
listTables: (handler: Handler, db: DB) => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode(`SELECT * FROM information_schema.tables where TABLE_SCHEMA = `);
|
|
|
|
qb.addInjection(db.name);
|
|
|
|
qb.addCode(` and TABLE_TYPE = "BASE TABLE"`);
|
|
|
|
return qb;
|
|
|
|
},
|
2023-01-29 22:11:24 +01:00
|
|
|
create: (handler: Handler, table: Table): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode(`create table if not exists ${table.toString(handler)}(`);
|
|
|
|
let keys = Object.keys(table.dbLangTableAttributes);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const a = table.dbLangTableAttributes[keys[i]];
|
|
|
|
builder.addCode(a.serialize(handler) + " ");
|
|
|
|
builder.append(a.serializeSettings(handler));
|
|
|
|
if (i + 1 < keys.length) builder.addCode(", ");
|
|
|
|
}
|
|
|
|
builder.addCode(")");
|
|
|
|
return builder;
|
|
|
|
},
|
2023-02-14 16:45:21 +01:00
|
|
|
deleteTable: (handler: Handler, table: string): QueryBuilder => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode(`DROP TABLE `);
|
|
|
|
qb.addCode(handler.builders.escapeID(table));
|
|
|
|
qb.addCode(` CASCADE`);
|
|
|
|
return qb;
|
|
|
|
},
|
2023-02-13 23:44:08 +01:00
|
|
|
addColumn: (handler: Handler, attr: Attribute): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode(`alter table `);
|
|
|
|
builder.addCode(attr.table.toString(handler));
|
|
|
|
builder.addCode(` add if not exists `);
|
|
|
|
builder.addCode(attr.serialize(handler) + " ");
|
|
|
|
builder.append(attr.serializeSettings(handler));
|
|
|
|
return builder;
|
|
|
|
},
|
|
|
|
changeColumn: (handler: Handler, attr: Attribute): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode(`alter table `);
|
|
|
|
builder.addCode(attr.table.toString(handler));
|
|
|
|
builder.addCode(` change `);
|
|
|
|
builder.addCode(attr.serialize(handler) + " ");
|
|
|
|
builder.addCode(attr.serialize(handler) + " ");
|
|
|
|
builder.append(attr.serializeSettings(handler));
|
|
|
|
return builder;
|
|
|
|
},
|
2023-02-14 16:45:21 +01:00
|
|
|
removeColumn: (handler: Handler, tabel: Table, attr: string) => {
|
2023-02-13 23:44:08 +01:00
|
|
|
const builder = new QueryBuilder();
|
2023-02-14 16:45:21 +01:00
|
|
|
builder.addCode("ALTER TABLE ");
|
|
|
|
builder.addCode(tabel.serialize(handler));
|
|
|
|
builder.addCode(" DROP COLUMN ");
|
|
|
|
builder.addCode(handler.builders.escapeID(attr));
|
|
|
|
return builder;
|
|
|
|
},
|
2023-02-14 17:37:01 +01:00
|
|
|
addForeignKey: (handler: Handler, table: Table, c: foreignConstraint) => {
|
2023-02-14 16:45:21 +01:00
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("ALTER TABLE ");
|
2023-02-14 17:37:01 +01:00
|
|
|
builder.addCode(table.serialize(handler));
|
2023-02-14 16:45:21 +01:00
|
|
|
builder.addCode(" ADD CONSTRAINT ");
|
2023-02-14 17:37:01 +01:00
|
|
|
builder.addCode(handler.builders.escapeID(c.name));
|
|
|
|
builder.addCode(" FOREIGN KEY (");
|
|
|
|
builder.addCodeCommaSeperated(c.fromAttrs.map(a => a.serialize(handler)));
|
2023-02-14 16:45:21 +01:00
|
|
|
builder.addCode(") REFERENCES ");
|
2023-02-14 17:37:01 +01:00
|
|
|
builder.addCode(c.toAttrs[0].table.serialize(handler));
|
2023-02-14 16:45:21 +01:00
|
|
|
builder.addCode("(");
|
2023-02-14 17:37:01 +01:00
|
|
|
builder.addCodeCommaSeperated(c.toAttrs.map(a => a.serialize(handler)));
|
2023-02-14 16:45:21 +01:00
|
|
|
builder.addCode(")");
|
2023-02-14 17:37:01 +01:00
|
|
|
|
|
|
|
if (c.onUpdate == onAction.cascade) builder.addCode(" ON UPDATE CASCADE");
|
|
|
|
if (c.onUpdate == onAction.noAction) builder.addCode(" ON UPDATE NO ACTION");
|
|
|
|
if (c.onUpdate == onAction.setDefault) builder.addCode(" ON UPDATE SET DEFUALT");
|
|
|
|
if (c.onUpdate == onAction.setNull) builder.addCode(" ON UPDATE SET NULL");
|
|
|
|
|
|
|
|
if (c.onDelete == onAction.cascade) builder.addCode(" ON DELETE CASCADE");
|
|
|
|
if (c.onDelete == onAction.noAction) builder.addCode(" ON DELETE NO ACTION");
|
|
|
|
if (c.onDelete == onAction.setDefault) builder.addCode(" ON DELETE SET DEFUALT");
|
|
|
|
if (c.onDelete == onAction.setNull) builder.addCode(" ON DELETE SET NULL");
|
2023-02-13 23:44:08 +01:00
|
|
|
return builder;
|
|
|
|
},
|
|
|
|
removeForeignKey: (handler: Handler, tablenName: string, name: string) => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("ALTER TABLE ");
|
|
|
|
builder.addCode(handler.builders.escapeID(tablenName));
|
|
|
|
builder.addCode(" DROP FOREIGN KEY IF EXISTS ");
|
|
|
|
builder.addCode(handler.builders.escapeID(name));
|
|
|
|
builder.addCode(";ALTER TABLE ");
|
|
|
|
builder.addCode(handler.builders.escapeID(tablenName));
|
|
|
|
builder.addCode(" DROP INDEX IF EXISTS ");
|
|
|
|
builder.addCode(handler.builders.escapeID(name));
|
2023-01-24 20:34:03 +01:00
|
|
|
|
2023-02-13 23:44:08 +01:00
|
|
|
return builder;
|
|
|
|
},
|
|
|
|
addPrimaryKey: (handler: Handler, table: Table) => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("ALTER TABLE ");
|
|
|
|
qb.addCode(table.serialize(handler));
|
|
|
|
qb.addCode(" add PRIMARY KEY if not exists (");
|
|
|
|
qb.addCodeCommaSeperated(
|
|
|
|
Object.entries(table.dbLangTableAttributes)
|
|
|
|
.filter(([n, attr]) => !!attr.ops.primaryKey)
|
|
|
|
.map(([n, attr]) => handler.builders.escapeID(n))
|
|
|
|
);
|
|
|
|
qb.addCode(")");
|
|
|
|
return qb;
|
|
|
|
},
|
2023-02-14 16:45:21 +01:00
|
|
|
removePrimaryKey: (handler: Handler, table: string) => {
|
2023-02-13 23:44:08 +01:00
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("ALTER TABLE ");
|
2023-02-14 16:45:21 +01:00
|
|
|
qb.addCode(handler.builders.escapeID(table));
|
2023-02-13 23:44:08 +01:00
|
|
|
qb.addCode(" DROP INDEX IF EXISTS `PRIMARY`");
|
|
|
|
return qb;
|
|
|
|
},
|
|
|
|
removeCheck: (handler: Handler, table: string, name: string) => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("ALTER TABLE ");
|
|
|
|
qb.addCode(handler.builders.escapeID(table));
|
|
|
|
qb.addCode(" DROP CONSTRAINT IF EXISTS ");
|
|
|
|
qb.addCode(handler.builders.escapeID(name));
|
|
|
|
return qb;
|
|
|
|
},
|
|
|
|
addCheck: (handler: Handler, table: Table, c: checkConstraint) => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode(`ALTER TABLE `);
|
|
|
|
qb.addCode(table.serialize(handler));
|
|
|
|
qb.addCode(` ADD CONSTRAINT `);
|
|
|
|
qb.addCode(handler.builders.escapeID(c.name));
|
|
|
|
qb.addCode(` CHECK (`);
|
|
|
|
qb.append(c.checkQuery.serialize(handler));
|
|
|
|
qb.addCode(")");
|
|
|
|
return qb;
|
|
|
|
},
|
|
|
|
removeUnique: (handler: Handler, table: string, name: string) => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("ALTER TABLE ");
|
|
|
|
qb.addCode(handler.builders.escapeID(table));
|
|
|
|
qb.addCode(" DROP INDEX IF EXISTS ");
|
|
|
|
qb.addCode(handler.builders.escapeID(name));
|
|
|
|
return qb;
|
|
|
|
},
|
|
|
|
addUnique: (handler: Handler, table: Table, u: uniqueConstraint) => {
|
|
|
|
const qb = new QueryBuilder();
|
|
|
|
qb.addCode("ALTER TABLE ");
|
|
|
|
qb.addCode(table.serialize(handler));
|
|
|
|
qb.addCode(" ADD CONSTRAINT ");
|
|
|
|
qb.addCode(handler.builders.escapeID(u.name));
|
|
|
|
qb.addCode(" UNIQUE (");
|
|
|
|
qb.addCodeCommaSeperated(u.attrs.map(a => a.serialize(handler)));
|
|
|
|
qb.addCode(")");
|
|
|
|
return qb;
|
|
|
|
},
|
2023-01-29 22:11:24 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
|
|
|
|
builders = {
|
2023-02-13 23:44:08 +01:00
|
|
|
query: (qb: QueryBuilder): Query => {
|
2023-01-29 22:11:24 +01:00
|
|
|
let args: primaryData[] = [];
|
|
|
|
let sql = "";
|
|
|
|
for (let i = 0; i < qb.list.length; i++) {
|
|
|
|
const [inject, data] = qb.list[i];
|
|
|
|
if (inject) {
|
|
|
|
sql += "?";
|
|
|
|
args.push(data);
|
|
|
|
} else {
|
|
|
|
sql += data;
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 23:44:08 +01:00
|
|
|
return new Query([sql, args]);
|
2023-01-29 22:11:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
attributeSettings: (handler: Handler, a: Attribute): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.append(a.type.serialize(handler));
|
2023-01-24 20:34:03 +01:00
|
|
|
if (a.ops.autoIncrement) {
|
2023-01-29 22:11:24 +01:00
|
|
|
builder.addCode(" auto_increment");
|
2023-01-24 20:34:03 +01:00
|
|
|
}
|
|
|
|
if (a.ops.default != null) {
|
|
|
|
if (a.ops.autoIncrement || a.ops.primaryKey || a.ops.notNull)
|
|
|
|
throw new Error(`Can not set default when autoIncrement, primaryKey or notNull ist set on Attribute: ${a.toStringFunc(handler)}`);
|
2023-01-29 22:11:24 +01:00
|
|
|
builder.addCode(" default ");
|
|
|
|
builder.addInjection(a.ops.default);
|
2023-01-24 20:34:03 +01:00
|
|
|
}
|
|
|
|
if (a.ops.notNull != null) {
|
2023-01-29 22:11:24 +01:00
|
|
|
if (!a.ops.autoIncrement && !a.ops.primaryKey && a.ops.default == null) {
|
|
|
|
builder.addCode(" not null");
|
2023-02-13 23:44:08 +01:00
|
|
|
} else builder.addCode(" null");
|
|
|
|
} else builder.addCode(" null");
|
2023-01-29 22:11:24 +01:00
|
|
|
return builder;
|
2023-01-24 20:34:03 +01:00
|
|
|
},
|
2023-01-29 22:11:24 +01:00
|
|
|
escapeID: (key: string): string => {
|
|
|
|
if (!key || key === "" || key.includes('\u0000')) throw new Error("Can not escape empty key or with null unicode!");
|
2023-01-24 20:34:03 +01:00
|
|
|
if (key.match(/^`.+`$/g)) return key;
|
|
|
|
return `\`${key.replace(/`/g, '``')}\``;
|
2023-02-13 23:44:08 +01:00
|
|
|
},
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return curr.split(" ").join("").startsWith(sql.split(" ").join(""));
|
|
|
|
},
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:34:03 +01:00
|
|
|
aggregations = {
|
2023-01-29 22:11:24 +01:00
|
|
|
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]),
|
|
|
|
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]),
|
|
|
|
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.toString(handler) + ")" }]),
|
|
|
|
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.toString(handler) + ")" }]),
|
|
|
|
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.toString(handler) + ")" }]),
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:34:03 +01:00
|
|
|
modifiers = {
|
|
|
|
and: joinArg("and"),
|
|
|
|
or: joinArg("or"),
|
|
|
|
le: joinArg("<"),
|
|
|
|
leq: joinArg("<="),
|
|
|
|
eq: joinArg("="),
|
|
|
|
geq: joinArg(">="),
|
|
|
|
ge: joinArg(">"),
|
|
|
|
plus: joinArg("+"),
|
|
|
|
minus: joinArg("-"),
|
2023-02-14 18:13:06 +01:00
|
|
|
mult: joinArg("*"),
|
|
|
|
divide: joinArg("-/"),
|
2023-01-29 22:11:24 +01:00
|
|
|
not: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
2023-01-23 21:19:18 +01:00
|
|
|
let e = a[0];
|
2023-01-29 22:11:24 +01:00
|
|
|
if (e instanceof Attribute) return new QueryBuilder([{ data: "not (" + e.toString(handler) + ")" }])
|
2023-01-23 21:19:18 +01:00
|
|
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
2023-01-29 22:11:24 +01:00
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("not (");
|
|
|
|
builder.append(e.serialize(handler));
|
|
|
|
builder.addCode(")");
|
|
|
|
return builder;
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-29 22:11:24 +01:00
|
|
|
return new QueryBuilder([
|
|
|
|
{ data: "not(" },
|
|
|
|
{ inject: true, data: e },
|
|
|
|
{ data: ")" }
|
2023-02-14 18:13:06 +01:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
like: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
if (a[0] instanceof Attribute) builder.addCode(a[0].toString());
|
|
|
|
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
|
|
|
builder.append(a[0].serialize(handler));
|
2023-02-14 22:23:21 +01:00
|
|
|
} else {
|
2023-02-14 18:13:06 +01:00
|
|
|
builder.addInjection(a[0]);
|
|
|
|
}
|
|
|
|
builder.addCode(" LIKE ");
|
|
|
|
if (a[1] instanceof Attribute) builder.addCode(a[1].toString());
|
|
|
|
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
|
|
|
builder.append(a[1].serialize(handler));
|
2023-02-14 22:23:21 +01:00
|
|
|
} else {
|
2023-02-14 18:13:06 +01:00
|
|
|
builder.addInjection(a[1]);
|
|
|
|
}
|
|
|
|
return builder;
|
2023-02-14 22:23:21 +01:00
|
|
|
},
|
|
|
|
concat: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("CONCAT(");
|
|
|
|
for (let i = 0; i < a.length; i++) {
|
|
|
|
const e = a[i];
|
|
|
|
if (e instanceof Attribute) builder.addCode(e.toString());
|
|
|
|
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
|
|
|
builder.append(e.serialize(handler));
|
|
|
|
} else {
|
|
|
|
builder.addInjection(e);
|
|
|
|
}
|
|
|
|
if (i < a.length - 1) builder.addCode(", ");
|
|
|
|
}
|
|
|
|
builder.addCode(")");
|
|
|
|
return builder;
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 22:23:21 +01:00
|
|
|
|
2023-01-24 20:34:03 +01:00
|
|
|
datatypes = {
|
2023-01-24 10:40:16 +01:00
|
|
|
char: dataTypeSingleNum("char"),
|
|
|
|
varchar: dataTypeSingleNum("varchar"),
|
|
|
|
binary: dataTypeSingleNum("binary"),
|
|
|
|
varbinary: dataTypeSingleNum("varbinary"),
|
|
|
|
|
|
|
|
tinyblob: dataTypeNoArg("tinyblob"),
|
|
|
|
blob: dataTypeNoArg("blob"),
|
|
|
|
mediumblob: dataTypeNoArg("mediumblob"),
|
|
|
|
longblob: dataTypeNoArg("longblob"),
|
|
|
|
tinytext: dataTypeNoArg("tinytext"),
|
|
|
|
text: dataTypeNoArg("text"),
|
|
|
|
mediumtext: dataTypeNoArg("mediumtext"),
|
|
|
|
longtext: dataTypeNoArg("longtext"),
|
|
|
|
|
2023-01-29 22:11:24 +01:00
|
|
|
enum: (a: primaryData[]): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("enum(");
|
|
|
|
builder.addInjectionCommaSeperated(a);
|
|
|
|
builder.addCode(")");
|
|
|
|
return builder;
|
2023-01-24 10:40:16 +01:00
|
|
|
},
|
2023-01-29 22:11:24 +01:00
|
|
|
set: (a: primaryData[]): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
builder.addCode("set(");
|
|
|
|
builder.addInjectionCommaSeperated(a);
|
|
|
|
builder.addCode(")");
|
|
|
|
return builder;
|
2023-01-24 10:40:16 +01:00
|
|
|
},
|
|
|
|
bool: dataTypeNoArg("bool"),
|
|
|
|
bit: dataTypeNoArg("bit"),
|
|
|
|
tinyint: dataTypeNoArg("tinyint"),
|
|
|
|
smallint: dataTypeNoArg("smallint"),
|
|
|
|
mediumint: dataTypeNoArg("mediumint"),
|
|
|
|
int: dataTypeNoArg("int"),
|
|
|
|
bigint: dataTypeNoArg("bigint"),
|
|
|
|
|
|
|
|
float: dataTypeDblNum("float"),
|
|
|
|
double: dataTypeDblNum("double"),
|
|
|
|
decimal: dataTypeDblNum("decimal"),
|
|
|
|
|
2023-02-13 23:44:08 +01:00
|
|
|
date: dataTypeNoArg("date"),
|
2023-01-24 20:34:03 +01:00
|
|
|
datetime: dataTypeNoArg("datatime"),
|
2023-01-24 10:40:16 +01:00
|
|
|
timestamp: dataTypeNoArg("timestamp"),
|
|
|
|
time: dataTypeNoArg("time"),
|
|
|
|
year: dataTypeNoArg("year"),
|
|
|
|
}
|
2023-01-23 21:19:18 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-01-24 20:34:03 +01:00
|
|
|
function dataTypeNoArg(type: string) {
|
2023-01-29 22:11:24 +01:00
|
|
|
return (a: primaryData[]): QueryBuilder => {
|
|
|
|
return new QueryBuilder([{
|
|
|
|
inject: false,
|
|
|
|
data: type
|
|
|
|
}]);
|
2023-01-24 10:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
function dataTypeSingleNum(type: string) {
|
2023-01-29 22:11:24 +01:00
|
|
|
return (a: primaryData[]): QueryBuilder => {
|
|
|
|
return new QueryBuilder([
|
|
|
|
{ data: type + "(" },
|
|
|
|
{ inject: true, data: a[0] },
|
|
|
|
{ data: ")" }
|
|
|
|
]);
|
2023-01-24 10:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
function dataTypeDblNum(type: string) {
|
2023-01-29 22:11:24 +01:00
|
|
|
return (a: primaryData[]): QueryBuilder => {
|
|
|
|
return new QueryBuilder([
|
|
|
|
{ data: type + "(" },
|
|
|
|
{ inject: true, data: a[0] },
|
|
|
|
{ data: ", " },
|
|
|
|
{ inject: true, data: a[1] },
|
|
|
|
{ data: ")" }
|
|
|
|
]);
|
2023-01-24 10:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-24 20:34:03 +01:00
|
|
|
function joinArg(type: string) {
|
2023-01-29 22:11:24 +01:00
|
|
|
return (handler: Handler, a: (allModifierInput)[]): QueryBuilder => {
|
|
|
|
const builder = new QueryBuilder();
|
|
|
|
for (let i = 0; i < a.length; i++) {
|
|
|
|
const d = a[i];
|
|
|
|
if (d instanceof Attribute) builder.addCode(d.toString(handler));
|
|
|
|
else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
|
|
|
builder.addCode("(");
|
|
|
|
builder.append(d.serialize(handler));
|
|
|
|
builder.addCode(")");
|
|
|
|
} else {
|
|
|
|
builder.addInjection(d);
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-29 22:11:24 +01:00
|
|
|
if (i + 1 < a.length) builder.addCode(" " + type + " ");
|
|
|
|
}
|
|
|
|
return builder;
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
}
|