Compare commits
17 commits
Author | SHA1 | Date | |
---|---|---|---|
3ed554f5ea | |||
25a38efdc6 | |||
575e9c2d4f | |||
f290b61f44 | |||
c2dc88062f | |||
0a1cb7c85f | |||
3a01244199 | |||
23ebf3c75a | |||
eed8d36c0b | |||
5d97a3211f | |||
56fb927c74 | |||
21b2bec2c4 | |||
1e396b5934 | |||
57544dffd2 | |||
c9f76a0799 | |||
d647fba5a9 | |||
dc7f68054d |
8 changed files with 128 additions and 42 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.7.0",
|
"version": "0.9.6",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.7.0",
|
"version": "0.9.6",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mariadb": "^3.0.2",
|
"mariadb": "^3.0.2",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.8.1",
|
"version": "0.9.6",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/db.js",
|
"main": "dist/db.js",
|
||||||
"types": "dist/db.d.ts",
|
"types": "dist/db.d.ts",
|
||||||
|
|
17
src/db.ts
17
src/db.ts
|
@ -2,7 +2,7 @@ import mariadb from 'mariadb';
|
||||||
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure.js';
|
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure.js';
|
||||||
import { Handler } from './defaultHandler.js';
|
import { Handler } from './defaultHandler.js';
|
||||||
import { Query } from './query.js';
|
import { Query } from './query.js';
|
||||||
import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types.js';
|
import { attributeSettings, extendedAttributeSettings, onAction, dbType, order } from './types.js';
|
||||||
import { TableAlias } from "./alias.js"
|
import { TableAlias } from "./alias.js"
|
||||||
import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses.js"
|
import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses.js"
|
||||||
//import { postgresHandler } from "./postgresHandler"
|
//import { postgresHandler } from "./postgresHandler"
|
||||||
|
@ -104,13 +104,13 @@ export class Attribute {
|
||||||
[this]
|
[this]
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if (ops.foreginKey != null) {
|
if (ops.foreignKey != null) {
|
||||||
table.addConstraint(new foreignConstraint(
|
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.foreignKey.link.name,
|
||||||
[this],
|
[this],
|
||||||
[ops.foreginKey.link],
|
[ops.foreignKey.link],
|
||||||
ops.foreginKey.onDelete,
|
ops.foreignKey.onDelete,
|
||||||
ops.foreginKey.onUpdate
|
ops.foreignKey.onUpdate
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,7 +170,8 @@ export class Table {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
addConstraint(c: Constraint) {
|
addConstraint(c: Constraint) {
|
||||||
c.check(this);
|
let check = c.check(this);
|
||||||
|
if(typeof check == "string") throw new Error(check);
|
||||||
this.dbLangConstrains.push(c);
|
this.dbLangConstrains.push(c);
|
||||||
}
|
}
|
||||||
createAlias(name: string) {
|
createAlias(name: string) {
|
||||||
|
@ -179,6 +180,6 @@ export class Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from './funcs.js';
|
export * from './funcs.js';
|
||||||
export { onAction };
|
export { onAction, order };
|
||||||
export { dbType as databaseType }
|
export { dbType as databaseType }
|
||||||
export { readResponse, writeResponse, insertResponse, singleResponse }
|
export { readResponse, writeResponse, insertResponse, singleResponse }
|
|
@ -1,6 +1,7 @@
|
||||||
import { Attribute, Table } from "./db.js";
|
import { Attribute, Table } from "./db.js";
|
||||||
import { Handler } from "./defaultHandler.js";
|
import { Handler } from "./defaultHandler.js";
|
||||||
import { QueryBuilder } from "./query.js";
|
import { QueryBuilder } from "./query.js";
|
||||||
|
import { sha256 } from "./tools.js";
|
||||||
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types.js";
|
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types.js";
|
||||||
|
|
||||||
export class Datatype {
|
export class Datatype {
|
||||||
|
@ -110,6 +111,7 @@ export class checkConstraint implements Constraint {
|
||||||
checkQuery: BooleanModifier;
|
checkQuery: BooleanModifier;
|
||||||
name: string;
|
name: string;
|
||||||
constructor(name: string, check: BooleanModifier) {
|
constructor(name: string, check: BooleanModifier) {
|
||||||
|
if (name.length > 64) name = sha256(name);
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
this.checkQuery = check;
|
this.checkQuery = check;
|
||||||
}
|
}
|
||||||
|
@ -125,14 +127,17 @@ export class uniqueConstraint implements Constraint {
|
||||||
name: string;
|
name: string;
|
||||||
attrs: Attribute[];
|
attrs: Attribute[];
|
||||||
constructor(name: string, attrs: Attribute[]) {
|
constructor(name: string, attrs: Attribute[]) {
|
||||||
|
if (name.length > 64) name = sha256(name);
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
this.attrs = attrs;
|
this.attrs = attrs;
|
||||||
}
|
}
|
||||||
check(table: Table): boolean | string {
|
check(table: Table): boolean | string {
|
||||||
|
let prim = true;
|
||||||
for (let i = 0; i < this.attrs.length; i++) {
|
for (let i = 0; i < this.attrs.length; i++) {
|
||||||
if (this.attrs[i].ops.primaryKey) return "Can not combine unique Constraint and primary key";
|
if (!this.attrs[i].ops.primaryKey) prim = false;
|
||||||
if (this.attrs[i].table != table) return "Referencing Attributes must be in host Table.";
|
if (this.attrs[i].table != table) return "Referencing Attributes must be in host Table.";
|
||||||
}
|
}
|
||||||
|
if (prim) return "Can not combine unique Constraint and primary key";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uses(attr: Attribute): boolean {
|
uses(attr: Attribute): boolean {
|
||||||
|
@ -147,6 +152,7 @@ export class foreignConstraint implements Constraint {
|
||||||
onUpdate: onAction;
|
onUpdate: onAction;
|
||||||
onDelete: onAction;
|
onDelete: onAction;
|
||||||
constructor(name: string, from: Attribute[], to: Attribute[], onDelete: onAction = onAction.nothing, onUpdate: onAction = onAction.nothing) {
|
constructor(name: string, from: Attribute[], to: Attribute[], onDelete: onAction = onAction.nothing, onUpdate: onAction = onAction.nothing) {
|
||||||
|
if (name.length > 64) name = sha256(name);
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
this.fromAttrs = from;
|
this.fromAttrs = from;
|
||||||
this.toAttrs = to;
|
this.toAttrs = to;
|
||||||
|
@ -160,7 +166,7 @@ export class foreignConstraint implements Constraint {
|
||||||
let table = this.toAttrs[0].table;
|
let table = this.toAttrs[0].table;
|
||||||
for (let i = 0; i < this.toAttrs.length; i++) {
|
for (let i = 0; i < this.toAttrs.length; i++) {
|
||||||
if (table != this.toAttrs[i].table) return "Referenced Attributes must be in one Table.";
|
if (table != this.toAttrs[i].table) return "Referenced Attributes must be in one Table.";
|
||||||
if (this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
|
if (!this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
|
||||||
}
|
}
|
||||||
for (let i = 0; i < this.fromAttrs.length; i++) {
|
for (let i = 0; i < this.fromAttrs.length; i++) {
|
||||||
if (this.fromAttrs[i].table != t) return "Referencing Attributes must be in host Table.";
|
if (this.fromAttrs[i].table != t) return "Referencing Attributes must be in host Table.";
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { Alias, AttributeAlias, TableAlias } from "./alias.js";
|
||||||
import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db.js"
|
import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db.js"
|
||||||
import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure.js"
|
import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure.js"
|
||||||
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js"
|
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js"
|
||||||
import { allModifierInput, joinType, onAction, primaryData } from "./types.js"
|
import { allModifierInput, joinType, onAction, order, primaryData } from "./types.js"
|
||||||
import { sha256 } from "./tools.js"
|
import { sha256 } from "./tools.js"
|
||||||
|
|
||||||
export class Handler {
|
export class Handler {
|
||||||
|
@ -121,6 +121,21 @@ export class Handler {
|
||||||
return found == fromAttrs.length && found == toAttrs.length;
|
return found == fromAttrs.length && found == toAttrs.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function freeForUpdate(attr: string, table: string, start = false, pool = del) {
|
||||||
|
for (let i = 0; i < key.length; i++) {
|
||||||
|
const k = key[i];
|
||||||
|
if (
|
||||||
|
((k.REFERENCED_TABLE_NAME ?? k.referenced_table_name) == table
|
||||||
|
&& (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name).toLowerCase() == attr.toLowerCase())
|
||||||
|
|| (start && (k.TABLE_NAME ?? k.table_name) == table
|
||||||
|
&& (k.COLUMN_NAME ?? k.column_name).toLowerCase() == attr.toLowerCase() && (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name) != null)
|
||||||
|
) {
|
||||||
|
pool.appendEnding(handler.querys.removeForeignKey(handler, (k.TABLE_NAME ?? k.table_name), (k.CONSTRAINT_NAME ?? k.constraint_name)));
|
||||||
|
key.splice(i--, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//delete check and unused/false(unique, foreign) constraints
|
//delete check and unused/false(unique, foreign) constraints
|
||||||
for (let i = 0; i < constraints.length; i++) {
|
for (let i = 0; i < constraints.length; i++) {
|
||||||
const c = constraints[i];
|
const c = constraints[i];
|
||||||
|
@ -130,7 +145,19 @@ export class Handler {
|
||||||
if (
|
if (
|
||||||
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|
||||||
|| !checkUniqueIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
|
|| !checkUniqueIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
|
||||||
) del.appendEnding(handler.querys.removeUnique(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
|
) {
|
||||||
|
const uTName = (c.TABLE_NAME ?? c.table_name);
|
||||||
|
const uCName = (c.CONSTRAINT_NAME ?? c.constraint_name);
|
||||||
|
for (let j = 0; j < key.length; j++) {
|
||||||
|
if (
|
||||||
|
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == uCName
|
||||||
|
&& (key[j].TABLE_NAME ?? key[j].table_name) == uTName
|
||||||
|
) {
|
||||||
|
freeForUpdate((key[j].COLUMN_NAME ?? key[j].column_name), uTName, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
del.appendEnding(handler.querys.removeUnique(handler, uTName, uCName));
|
||||||
|
}
|
||||||
} else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "FOREIGN KEY") {
|
} else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "FOREIGN KEY") {
|
||||||
if (
|
if (
|
||||||
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|
||||||
|
@ -143,19 +170,6 @@ export class Handler {
|
||||||
if (typeof tableData[allTables[i]] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
|
if (typeof tableData[allTables[i]] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
function freeForUpdate(attr: string, table: string) {
|
|
||||||
for (let i = 0; i < key.length; i++) {
|
|
||||||
const k = key[i];
|
|
||||||
if (
|
|
||||||
(k.REFERENCED_TABLE_NAME ?? k.referenced_table_name) == table
|
|
||||||
&& (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name).toLowerCase() == attr.toLowerCase()
|
|
||||||
) {
|
|
||||||
del.appendEnding(handler.querys.removeForeignKey(handler, (k.TABLE_NAME ?? k.table_name), (k.CONSTRAINT_NAME ?? k.constraint_name)));
|
|
||||||
key.splice(i--, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//create tables
|
//create tables
|
||||||
for (let i = 0; i < db.tables.length; i++) {
|
for (let i = 0; i < db.tables.length; i++) {
|
||||||
const table = db.tables[i];
|
const table = db.tables[i];
|
||||||
|
@ -181,7 +195,13 @@ export class Handler {
|
||||||
const a = table.dbLangTableAttributes[keys[j]];
|
const a = table.dbLangTableAttributes[keys[j]];
|
||||||
const attrData = tableD[keys[j]];
|
const attrData = tableD[keys[j]];
|
||||||
if (attrData == null) {
|
if (attrData == null) {
|
||||||
create.appendEnding(handler.querys.addColumn(handler, a));
|
if (a.ops.autoIncrement) {
|
||||||
|
a.ops.autoIncrement = false;
|
||||||
|
create.appendEnding(handler.querys.addColumn(handler, a));
|
||||||
|
a.ops.autoIncrement = true;
|
||||||
|
connst.appendEnding(handler.querys.addColumn(handler, a));
|
||||||
|
}
|
||||||
|
else create.appendEnding(handler.querys.addColumn(handler, a));
|
||||||
if (a.ops.primaryKey) changePrimary = true;
|
if (a.ops.primaryKey) changePrimary = true;
|
||||||
} else if (
|
} else if (
|
||||||
!handler.builders.compareDatatypes(handler, a.type, attrData.Type) ||
|
!handler.builders.compareDatatypes(handler, a.type, attrData.Type) ||
|
||||||
|
@ -189,10 +209,6 @@ export class Handler {
|
||||||
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey) != (attrData.Null == "NO") ||
|
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey) != (attrData.Null == "NO") ||
|
||||||
(!!a.ops.autoIncrement) != (attrData.Extra == "auto_increment")
|
(!!a.ops.autoIncrement) != (attrData.Extra == "auto_increment")
|
||||||
) {
|
) {
|
||||||
/*console.log(!handler.builders.compareDatatypes(handler, a.type, attrData.Type), "|",
|
|
||||||
a.ops.default, attrData.Default, "|",
|
|
||||||
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey), (attrData.Null == "NO"), "|",
|
|
||||||
(!!a.ops.autoIncrement), (attrData.Extra == "auto_increment"));*/
|
|
||||||
freeForUpdate(a.name, a.table.dbLangTableName);
|
freeForUpdate(a.name, a.table.dbLangTableName);
|
||||||
if (a.ops.autoIncrement) {
|
if (a.ops.autoIncrement) {
|
||||||
a.ops.autoIncrement = false;
|
a.ops.autoIncrement = false;
|
||||||
|
@ -205,11 +221,20 @@ export class Handler {
|
||||||
if (attrData != null) {
|
if (attrData != null) {
|
||||||
if ((attrData.Key == "PRI") != (!!a.ops.primaryKey)) {
|
if ((attrData.Key == "PRI") != (!!a.ops.primaryKey)) {
|
||||||
freeForUpdate(a.name, a.table.dbLangTableName);
|
freeForUpdate(a.name, a.table.dbLangTableName);
|
||||||
console.log("206");
|
|
||||||
changePrimary = true;
|
changePrimary = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (changePrimary) for (let j = 0; j < keys.length; j++) {
|
||||||
|
const a = table.dbLangTableAttributes[keys[j]];
|
||||||
|
if (a.ops.autoIncrement) {
|
||||||
|
a.ops.autoIncrement = false;
|
||||||
|
create.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
|
a.ops.autoIncrement = true;
|
||||||
|
connst.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
|
}
|
||||||
|
freeForUpdate(a.name, a.table.dbLangTableName, true, create);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (changePrimary) {
|
if (changePrimary) {
|
||||||
create.appendEnding(handler.querys.removePrimaryKey(handler, table.dbLangTableName));
|
create.appendEnding(handler.querys.removePrimaryKey(handler, table.dbLangTableName));
|
||||||
|
@ -282,6 +307,25 @@ export class Handler {
|
||||||
builder.append(q.havingD.serialize(handler));
|
builder.append(q.havingD.serialize(handler));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (q.orderByD.length > 0) {
|
||||||
|
builder.addCode(" order by ");
|
||||||
|
for (let i = 0; i < q.orderByD.length; i++) {
|
||||||
|
const d = q.orderByD[i];
|
||||||
|
if (d[0] instanceof Attribute || d[0] instanceof AttributeAlias) builder.addCode(d[0].getString(handler));
|
||||||
|
else if (d[0] instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(d.toString()));
|
||||||
|
else if (d[0] instanceof Modifier || d[0] instanceof selectQuery || d[0] instanceof Aggregation) {
|
||||||
|
builder.addCode("(");
|
||||||
|
builder.append(d[0].serialize(handler));
|
||||||
|
builder.addCode(")");
|
||||||
|
} else {
|
||||||
|
builder.addInjection(d[0]);
|
||||||
|
}
|
||||||
|
if(d[1] == order.DESC) builder.addCode(" DESC");
|
||||||
|
else builder.addCode(" ASC");
|
||||||
|
if (i + 1 < q.orderByD.length) builder.addCode(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (q.limitD != null) {
|
if (q.limitD != null) {
|
||||||
if (q.limitOffsetD == null) {
|
if (q.limitOffsetD == null) {
|
||||||
builder.addCode(" limit ");
|
builder.addCode(" limit ");
|
||||||
|
@ -302,7 +346,7 @@ export class Handler {
|
||||||
},
|
},
|
||||||
insert: (hander: Handler, q: insertQuery): QueryBuilder => {
|
insert: (hander: Handler, q: insertQuery): QueryBuilder => {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
qb.addCode("INSERT INTO ");
|
qb.addCode("INSERT IGNORE INTO ");
|
||||||
qb.addCode(q.attrs[0].table.serialize(hander));
|
qb.addCode(q.attrs[0].table.serialize(hander));
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.addCodeCommaSeperated(q.attrs.map(a => a.serialize(hander)));
|
qb.addCodeCommaSeperated(q.attrs.map(a => a.serialize(hander)));
|
||||||
|
@ -803,7 +847,25 @@ export class Handler {
|
||||||
}
|
}
|
||||||
builder.addCode(")");
|
builder.addCode(")");
|
||||||
return builder;
|
return builder;
|
||||||
}
|
},
|
||||||
|
coalesce: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
|
const builder = new QueryBuilder();
|
||||||
|
builder.addCode("COALESCE(");
|
||||||
|
for (let i = 0; i < a.length; i++) {
|
||||||
|
const e = a[i];
|
||||||
|
if (e instanceof Attribute || e instanceof AttributeAlias) builder.addCode(e.getString());
|
||||||
|
else if (e instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(e.toString()));
|
||||||
|
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
|
builder.append(e.serialize(handler));
|
||||||
|
} else {
|
||||||
|
builder.addInjection(e);
|
||||||
|
}
|
||||||
|
if (i < a.length - 1) builder.addCode(", ");
|
||||||
|
}
|
||||||
|
builder.addCode(")");
|
||||||
|
return builder;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
datatypes = {
|
datatypes = {
|
||||||
|
|
|
@ -24,6 +24,7 @@ export const regexp = (a: allModifierInput, b: allModifierInput) => new BooleanM
|
||||||
export const exists = (q: selectQuery) => new BooleanModifier("exists", [q]);
|
export const exists = (q: selectQuery) => new BooleanModifier("exists", [q]);
|
||||||
|
|
||||||
export const concat = (...args: allModifierInput[]) => new StringModifier("concat", args);
|
export const concat = (...args: allModifierInput[]) => new StringModifier("concat", args);
|
||||||
|
export const coalesce = (...args: allModifierInput[]) => new StringModifier("coalesce", args);
|
||||||
|
|
||||||
//aggregations
|
//aggregations
|
||||||
export const count = (a: Attribute) => new Aggregation("count", a);
|
export const count = (a: Attribute) => new Aggregation("count", a);
|
||||||
|
|
19
src/query.ts
19
src/query.ts
|
@ -2,7 +2,7 @@ import { AttributeAlias } from "./alias.js";
|
||||||
import { Attribute, DB, Table } from "./db.js";
|
import { Attribute, DB, Table } from "./db.js";
|
||||||
import { BooleanModifier, Modifier } from "./dbStructure.js";
|
import { BooleanModifier, Modifier } from "./dbStructure.js";
|
||||||
import { Handler } from "./defaultHandler.js";
|
import { Handler } from "./defaultHandler.js";
|
||||||
import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js";
|
import { allModifierInput, order, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js";
|
||||||
|
|
||||||
|
|
||||||
export class Query {
|
export class Query {
|
||||||
|
@ -84,6 +84,17 @@ export class selectQuery {
|
||||||
this.havingD = m;
|
this.havingD = m;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
orderByD: ([allModifierInput, order])[] = [];
|
||||||
|
addOrderBy(a: allModifierInput, o: order = order.ASC) {
|
||||||
|
this.orderByD.push([a, o]);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
orderBY(a: allModifierInput, o: order = order.ASC) {
|
||||||
|
this.orderByD = [[a, o]];
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
limitD: number | null = null;
|
limitD: number | null = null;
|
||||||
limitOffsetD: number | null = null;
|
limitOffsetD: number | null = null;
|
||||||
limit(i: number, offset: number = 0) {
|
limit(i: number, offset: number = 0) {
|
||||||
|
@ -139,7 +150,7 @@ export class insertQuery {
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
let readResp = await db.query(s, printQuery);
|
let readResp = await db.query(s, printQuery);
|
||||||
return db.getHandler().responses.insertResponse(readResp);
|
return handler.responses.insertResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +179,7 @@ export class updateQuery {
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
let readResp = await db.query(s, printQuery);
|
let readResp = await db.query(s, printQuery);
|
||||||
return db.getHandler().responses.writeResponse(readResp);
|
return handler.responses.writeResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,6 +201,6 @@ export class removeQuery {
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
let readResp = await db.query(s, printQuery);
|
let readResp = await db.query(s, printQuery);
|
||||||
return db.getHandler().responses.writeResponse(readResp);
|
return handler.responses.writeResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,7 +20,7 @@ export type attributeSettings = {
|
||||||
default?: primaryData,
|
default?: primaryData,
|
||||||
notNull?: boolean
|
notNull?: boolean
|
||||||
primaryKey?: boolean,
|
primaryKey?: boolean,
|
||||||
foreginKey?: {
|
foreignKey?: {
|
||||||
link: Attribute,
|
link: Attribute,
|
||||||
onDelete?: onAction,
|
onDelete?: onAction,
|
||||||
onUpdate?: onAction
|
onUpdate?: onAction
|
||||||
|
@ -35,7 +35,7 @@ export type extendedAttributeSettings = {
|
||||||
default?: primaryData,
|
default?: primaryData,
|
||||||
notNull?: boolean
|
notNull?: boolean
|
||||||
primaryKey?: boolean,
|
primaryKey?: boolean,
|
||||||
foreginKey?: {
|
foreignKey?: {
|
||||||
link: Attribute,
|
link: Attribute,
|
||||||
onDelete?: onAction,
|
onDelete?: onAction,
|
||||||
onUpdate?: onAction
|
onUpdate?: onAction
|
||||||
|
@ -64,3 +64,8 @@ export enum dbType {
|
||||||
//mysql,
|
//mysql,
|
||||||
postgres,
|
postgres,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum order {
|
||||||
|
ASC,
|
||||||
|
DESC
|
||||||
|
}
|
Loading…
Reference in a new issue