dblang/src/alias.ts

51 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-02-18 14:47:44 +01:00
import { Attribute, DB, Table } from "./db"
import { Handler } from "./defaultHandler";
2023-02-18 11:45:23 +01:00
2023-02-18 14:47:44 +01:00
export class AttributeAlias {
name: string;
nameO: string;
table: TableAlias;
attr: Attribute;
constructor(attr: Attribute, table: TableAlias) {
this.name = attr.name;
this.nameO = attr.nameO;
this.attr = attr;
this.table = table;
}
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) + ")";
}
2023-02-18 11:45:23 +01:00
}
2023-02-18 14:47:44 +01:00
export class TableAlias {
dbLangTableName: string;
dbLangTableAttributes: { [key: string]: AttributeAlias; } = {};
dbLangDatabaseInstance: DB;
dbLangTableInstance: Table;
[key: string]: AttributeAlias | any;
constructor(name: string, table: Table) {
this.dbLangTableName = name;
this.dbLangTableInstance = table;
this.dbLangDatabaseInstance = table.dbLangDatabaseInstance;
let keys = Object.keys(table.dbLangTableAttributes);
for (let i = 0; i < keys.length; i++) {
const a = table.dbLangTableAttributes[keys[i]];
let attrAlias = new AttributeAlias(a, this);
this[a.name] = attrAlias;
this[a.nameO] = attrAlias;
this.dbLangTableAttributes[a.name] = attrAlias;
}
}
serialize(handler: Handler) {
return this.toString(handler);
}
toString(handler: Handler = this.dbLangDatabaseInstance.getHandler()) {
return handler.builders.escapeID(this.dbLangTableName);
}
2023-02-18 11:45:23 +01:00
}