import mariadb from 'mariadb'; import { checkConstraint, Constraint, Datatype, uniqueConstraint } from './dbStructure'; import { Handler } from './defaultHandler'; import { Query, selectQuery } from './query'; import { attributeSettings, extendedAttributeSettings, onAction, primaryData, serializeReturn } from './types'; export class DB { tables:Table[] = []; handler: Handler; name: string; //pool:mariadb.Pool; constructor(/*{ host, user, password, database, connectionLimit = 5 }*/) { //this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true }); this.handler = new Handler(); this.name = "notimplemented" } async query(query: Query) { console.log(query); //return this.pool.query(query); } getHandler() { return this.handler; } sync(){ let handler = this.getHandler(); this.tables.forEach(t=>{ console.log(new Query(handler.builders.query(handler.querys.create(handler,t)))); }) } newTable(name: string) { let tabel = new Table(name,this); this.tables.push(tabel); return tabel; } } export class Attribute { name: string; table: Table; ops: attributeSettings; type: Datatype; constructor(name: string, table:Table, type: Datatype, ops: attributeSettings) { this.name = name; this.ops = ops; this.type = type; this.table = table; 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] )) } } serializeDatatype(handler : Handler){ return this.type.serialize(handler); } serializeSettings(handler : Handler){ return handler.builders.attributeSettings(handler,this); } serialize(handler : Handler) { return handler.builders.escapeID(this.name); } toString(handler : Handler = this.table.dbLangDatabaseInstance.getHandler()) { return this.table.serialize(handler)+"."+this.serialize(handler); } toStringFunc(handler : Handler){ return this.table.serialize(handler)+"("+this.serialize(handler)+")"; } } export class Table { dbLangTableName: string; dbLangTableAttributes: { [key: string]: Attribute; } = {}; dbLangDatabaseInstance: DB; dbLangConstrains: Constraint[] = []; [key: string]: Attribute | any constructor(name: string, db:DB) { this.dbLangTableName = name; this.dbLangDatabaseInstance = db; } serialize(handler : Handler) { return this.toString(handler); } toString(handler : Handler = this.dbLangDatabaseInstance.getHandler()) { return handler.builders.escapeID(this.dbLangTableName); } addAttribute(name: string, type: Datatype, ops: attributeSettings = {}, noErrorOnNameConflict = false) { if(this.dbLangTableAttributes[name] != null) throw new Error("You are tring to create an Attribute twise!"); let attr = new Attribute(name,this,type,ops); this.dbLangTableAttributes[name] = attr; if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes", "dbLangDatabaseInstance","addConstraint","addAttributes"].includes(name)) { if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!"); } else { this[name] = attr; } return attr; } 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); } } export * from './funcs'; export { onAction };