2023-01-23 21:19:18 +01:00
|
|
|
import mariadb from 'mariadb';
|
2023-01-29 22:11:24 +01:00
|
|
|
import { checkConstraint, Constraint, Datatype, uniqueConstraint } from './dbStructure';
|
2023-01-23 21:19:18 +01:00
|
|
|
import { Handler } from './defaultHandler';
|
2023-01-24 20:34:03 +01:00
|
|
|
import { Query, selectQuery } from './query';
|
2023-01-29 22:11:24 +01:00
|
|
|
import { attributeSettings, extendedAttributeSettings, onAction, primaryData, serializeReturn } from './types';
|
2023-01-23 21:19:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
export class DB {
|
2023-01-24 20:34:03 +01:00
|
|
|
tables:Table[] = [];
|
|
|
|
handler: Handler;
|
2023-01-29 22:11:24 +01:00
|
|
|
name: string;
|
2023-01-23 21:19:18 +01:00
|
|
|
//pool:mariadb.Pool;
|
|
|
|
constructor(/*{ host, user, password, database, connectionLimit = 5 }*/) {
|
|
|
|
//this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
|
2023-01-24 20:34:03 +01:00
|
|
|
this.handler = new Handler();
|
2023-01-29 22:11:24 +01:00
|
|
|
this.name = "notimplemented"
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
async query(query: Query) {
|
2023-01-24 20:34:03 +01:00
|
|
|
console.log(query);
|
2023-01-23 21:19:18 +01:00
|
|
|
//return this.pool.query(query);
|
|
|
|
}
|
|
|
|
getHandler() {
|
2023-01-24 20:34:03 +01:00
|
|
|
return this.handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
sync(){
|
|
|
|
let handler = this.getHandler();
|
|
|
|
this.tables.forEach(t=>{
|
2023-01-29 22:11:24 +01:00
|
|
|
console.log(new Query(handler.builders.query(handler.querys.create(handler,t))));
|
2023-01-24 20:34:03 +01:00
|
|
|
})
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-23 22:10:33 +01:00
|
|
|
newTable(name: string) {
|
2023-01-24 20:34:03 +01:00
|
|
|
let tabel = new Table(name,this);
|
|
|
|
this.tables.push(tabel);
|
|
|
|
return tabel;
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Attribute {
|
2023-01-23 22:10:33 +01:00
|
|
|
name: string;
|
2023-01-24 20:34:03 +01:00
|
|
|
table: Table;
|
2023-01-24 10:40:16 +01:00
|
|
|
ops: attributeSettings;
|
|
|
|
type: Datatype;
|
2023-01-24 20:34:03 +01:00
|
|
|
constructor(name: string, table:Table, type: Datatype, ops: attributeSettings) {
|
2023-01-23 21:19:18 +01:00
|
|
|
this.name = name;
|
2023-01-24 10:40:16 +01:00
|
|
|
this.ops = ops;
|
|
|
|
this.type = type;
|
2023-01-24 20:34:03 +01:00
|
|
|
this.table = table;
|
2023-01-29 22:11:24 +01:00
|
|
|
|
|
|
|
if(ops.check != null){
|
|
|
|
table.addConstraint(new checkConstraint(
|
|
|
|
table.dbLangDatabaseInstance.name+"_"+table.dbLangTableName+"_"+name+"_check_constraint",
|
|
|
|
ops.check
|
|
|
|
))
|
|
|
|
}
|
|
|
|
if(ops.unique != null){
|
|
|
|
table.addConstraint(new uniqueConstraint(
|
|
|
|
table.dbLangDatabaseInstance.name+"_"+table.dbLangTableName+" "+name+"_unique_constraint",
|
|
|
|
[this]
|
|
|
|
))
|
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
}
|
|
|
|
serializeDatatype(handler : Handler){
|
|
|
|
return this.type.serialize(handler);
|
|
|
|
}
|
|
|
|
serializeSettings(handler : Handler){
|
|
|
|
return handler.builders.attributeSettings(handler,this);
|
2023-01-24 10:40:16 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
serialize(handler : Handler) {
|
|
|
|
return handler.builders.escapeID(this.name);
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
toString(handler : Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
|
|
|
return this.table.serialize(handler)+"."+this.serialize(handler);
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
toStringFunc(handler : Handler){
|
|
|
|
return this.table.serialize(handler)+"("+this.serialize(handler)+")";
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-23 22:10:33 +01:00
|
|
|
export class Table {
|
|
|
|
dbLangTableName: string;
|
|
|
|
dbLangTableAttributes: { [key: string]: Attribute; } = {};
|
2023-01-24 20:34:03 +01:00
|
|
|
dbLangDatabaseInstance: DB;
|
2023-01-29 22:11:24 +01:00
|
|
|
dbLangConstrains: Constraint[] = [];
|
2023-01-23 22:10:33 +01:00
|
|
|
[key: string]: Attribute | any
|
2023-01-29 22:11:24 +01:00
|
|
|
constructor(name: string, db:DB) {
|
2023-01-23 21:19:18 +01:00
|
|
|
this.dbLangTableName = name;
|
2023-01-29 22:11:24 +01:00
|
|
|
this.dbLangDatabaseInstance = db;
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
serialize(handler : Handler) {
|
|
|
|
return this.toString(handler);
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
toString(handler : Handler = this.dbLangDatabaseInstance.getHandler()) {
|
|
|
|
return handler.builders.escapeID(this.dbLangTableName);
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
2023-01-24 20:34:03 +01:00
|
|
|
addAttribute(name: string, type: Datatype, ops: attributeSettings = {}, noErrorOnNameConflict = false) {
|
2023-01-29 22:11:24 +01:00
|
|
|
if(this.dbLangTableAttributes[name] != null) throw new Error("You are tring to create an Attribute twise!");
|
2023-01-24 20:34:03 +01:00
|
|
|
let attr = new Attribute(name,this,type,ops);
|
2023-01-23 21:19:18 +01:00
|
|
|
this.dbLangTableAttributes[name] = attr;
|
2023-01-29 22:11:24 +01:00
|
|
|
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes", "dbLangDatabaseInstance","addConstraint","addAttributes"].includes(name)) {
|
2023-01-23 22:10:33 +01:00
|
|
|
if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
|
|
|
|
} else {
|
2023-01-23 21:19:18 +01:00
|
|
|
this[name] = attr;
|
|
|
|
}
|
|
|
|
return attr;
|
|
|
|
}
|
2023-01-29 22:11:24 +01:00
|
|
|
addAttributes(list:{[key: string]:extendedAttributeSettings}):{[key: string]:Attribute} {
|
|
|
|
return Object.fromEntries(Object.entries(list).map(([k,a])=>{
|
|
|
|
return [k,this.addAttribute(k,a.type,a)];
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
addConstraint(c:Constraint){
|
|
|
|
c.check(this);
|
|
|
|
this.dbLangConstrains.push(c);
|
|
|
|
}
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export * from './funcs';
|
2023-01-23 22:10:33 +01:00
|
|
|
export { onAction };
|