diff --git a/src/db.ts b/src/db.ts index fc9262c..bc3cb92 100644 --- a/src/db.ts +++ b/src/db.ts @@ -16,7 +16,7 @@ export class DB { this.name = database; } async query(query: Query) { - console.log(query); + //console.log(query); return await this.pool.query(query); } getHandler() { @@ -41,6 +41,9 @@ export class DB { for (let i = 0; i < this.tables.length; i++) if (this.tables[i].dbLangTableName == name) return this.tables[i]; return null; } + async close() { + if (this.pool) await this.pool.end(); + } } export class Attribute { @@ -69,7 +72,7 @@ export class Attribute { } if (ops.foreginKey != null) { table.addConstraint(new foreignConstraint( - table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_"+ops.foreginKey.link.name, + table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreginKey.link.name, [this], [ops.foreginKey.link], ops.foreginKey.onDelete, diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 8102338..450babb 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -465,7 +465,6 @@ export class Handler { }, } - aggregations = { 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) + ")" }]), @@ -524,6 +523,7 @@ export class Handler { return builder; } } + datatypes = { char: dataTypeSingleNum("char"), varchar: dataTypeSingleNum("varchar"), diff --git a/src/funcs.ts b/src/funcs.ts index d9e9dee..870d2d0 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -1,5 +1,5 @@ import { Attribute } from "./db"; -import { Aggregation, BooleanModifier, checkConstraint, Datatype, NumberModifier } from "./dbStructure"; +import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, NumberModifier, uniqueConstraint } from "./dbStructure"; import { selectQuery } from "./query"; import { allModifierInput, primaryData, selectElements, selectFromElements } from "./types"; @@ -71,8 +71,13 @@ export const YEAR = new Datatype("year", []); // Constraints //TODO: //primary key -export const foreginKey = (attrs: Attribute[], target: Attribute[]) => { }; -export const uniqueKey = (attr: Attribute[]) => { }; +export const foreignKey = (attrs: Attribute[], target: Attribute[]) => new foreignConstraint( + "foreign_constraint_" + + attrs.map(a => a.name + "_" + a.table.dbLangTableName).join("_") + + "_to_" + + target.map(a => a.name + "_" + a.table.dbLangTableName).join("_"), + attrs, target); +export const uniqueKey = (attrs: Attribute[]) => new uniqueConstraint("unique_constraint_" + attrs.map(a => a.name + "_" + a.table.dbLangTableName).join("_"), attrs); export const check = (name: string, mod: BooleanModifier) => new checkConstraint(name, mod);