2023-01-23 21:19:18 +01:00
|
|
|
import mariadb from 'mariadb';
|
2023-01-24 10:40:16 +01:00
|
|
|
import { Datatype } 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';
|
|
|
|
import { attributeSettings, 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-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-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=>{
|
|
|
|
console.log(handler.querys.create(handler,t));
|
|
|
|
})
|
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;
|
|
|
|
}
|
|
|
|
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-23 22:10:33 +01:00
|
|
|
[key: string]: Attribute | any
|
2023-01-24 20:34:03 +01:00
|
|
|
constructor(name: string, table:DB) {
|
2023-01-23 21:19:18 +01:00
|
|
|
this.dbLangTableName = name;
|
2023-01-24 20:34:03 +01:00
|
|
|
this.dbLangDatabaseInstance = table;
|
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) {
|
|
|
|
let attr = new Attribute(name,this,type,ops);
|
2023-01-23 21:19:18 +01:00
|
|
|
this.dbLangTableAttributes[name] = attr;
|
2023-01-24 20:34:03 +01:00
|
|
|
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes", "dbLangDatabaseInstance"].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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export * from './funcs';
|
2023-01-23 22:10:33 +01:00
|
|
|
export { onAction };
|