Compare commits
No commits in common. "main" and "v0.4.0" have entirely different histories.
14 changed files with 114 additions and 356 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,4 +3,3 @@ node_modules
|
||||||
ltests
|
ltests
|
||||||
sqlsave
|
sqlsave
|
||||||
postgresHandler.ts
|
postgresHandler.ts
|
||||||
.vscode
|
|
6
package-lock.json
generated
6
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.9.6",
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.9.6",
|
"version": "1.0.0",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mariadb": "^3.0.2",
|
"mariadb": "^3.0.2",
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
"typescript": "^4.9.4"
|
"typescript": "^4.9.4"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 16"
|
"node": ">= 14"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@esbuild/android-arm": {
|
"node_modules/@esbuild/android-arm": {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.9.6",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/db.js",
|
"main": "dist/db.js",
|
||||||
"types": "dist/db.d.ts",
|
"types": "dist/db.d.ts",
|
||||||
"type": "module",
|
"type": "commonjs",
|
||||||
"directories": {
|
"directories": {
|
||||||
"dist": "dist"
|
"dist": "dist"
|
||||||
},
|
},
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
"pg": "^8.9.0"
|
"pg": "^8.9.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 16"
|
"node": ">= 14"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
|
|
22
readme.md
22
readme.md
|
@ -13,8 +13,6 @@ Features:
|
||||||
- [x] remove unused Schema
|
- [x] remove unused Schema
|
||||||
- [x] joins
|
- [x] joins
|
||||||
- [x] table alias
|
- [x] table alias
|
||||||
- [x] general aliases
|
|
||||||
- [x] unified response
|
|
||||||
- [ ] change Schema
|
- [ ] change Schema
|
||||||
- [ ] Key-Value Store Shortcut
|
- [ ] Key-Value Store Shortcut
|
||||||
- [ ] Views
|
- [ ] Views
|
||||||
|
@ -30,8 +28,7 @@ Supported Databses:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import { DB } from "dblang";
|
import { DB } from "dblang";
|
||||||
const db = new DB();
|
const db = new DB({
|
||||||
db.connect({
|
|
||||||
host: "localhost",
|
host: "localhost",
|
||||||
user: "root",
|
user: "root",
|
||||||
password: "0123456789",
|
password: "0123456789",
|
||||||
|
@ -132,20 +129,6 @@ _Aliases_:
|
||||||
```javascript
|
```javascript
|
||||||
const alias1 = TableA.createAlias("alias1");
|
const alias1 = TableA.createAlias("alias1");
|
||||||
//alias1 can be used like a Table in select Statements.
|
//alias1 can be used like a Table in select Statements.
|
||||||
const alias2 = alias(avg(TableA.A1), "someAvg");
|
|
||||||
```
|
|
||||||
|
|
||||||
Get requestet Data:
|
|
||||||
```javascript
|
|
||||||
let response = await select([TableA.A1, TableB.B1], innerJoinOn(eq(TableA.A1, Tableb.B1)))
|
|
||||||
.query(db);
|
|
||||||
|
|
||||||
response[0][TableA.A1];
|
|
||||||
response[0][TableB.B1];
|
|
||||||
|
|
||||||
response[i][TableA.A1];
|
|
||||||
response[i][TableB.B1]; // i < repsonse.length
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Insert:
|
#### Insert:
|
||||||
|
@ -241,7 +224,6 @@ BooleanModifier:
|
||||||
- `le(v1, v2, ...)`
|
- `le(v1, v2, ...)`
|
||||||
- `not(v1)`
|
- `not(v1)`
|
||||||
- `like(v1, v2)`
|
- `like(v1, v2)`
|
||||||
- `exists(s)`
|
|
||||||
|
|
||||||
NumberModifier:
|
NumberModifier:
|
||||||
- `plus(v1, v2, ...)`
|
- `plus(v1, v2, ...)`
|
||||||
|
@ -252,7 +234,7 @@ NumberModifier:
|
||||||
StringModifier:
|
StringModifier:
|
||||||
- `concat(v1, v2, ...)`
|
- `concat(v1, v2, ...)`
|
||||||
|
|
||||||
(v\* = string, number, boolean, null, Modifier, Aggregation, select Query, Attribute; s = selectQuery)
|
(v\* = string, number, boolean, null, Modifier, Aggregation, select Query, Attribute)
|
||||||
|
|
||||||
#### Joins
|
#### Joins
|
||||||
|
|
||||||
|
|
44
src/alias.ts
44
src/alias.ts
|
@ -1,8 +1,5 @@
|
||||||
import { Attribute, DB, Table } from "./db.js"
|
import { Attribute, DB, Table } from "./db"
|
||||||
import { Aggregation, Modifier } from "./dbStructure.js";
|
import { Handler } from "./defaultHandler";
|
||||||
import { Handler } from "./defaultHandler.js";
|
|
||||||
import { QueryBuilder, selectQuery } from "./query.js";
|
|
||||||
import { selectElements } from "./types.js";
|
|
||||||
|
|
||||||
export class AttributeAlias {
|
export class AttributeAlias {
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -18,15 +15,12 @@ export class AttributeAlias {
|
||||||
serialize(handler: Handler) {
|
serialize(handler: Handler) {
|
||||||
return handler.builders.escapeID(this.name);
|
return handler.builders.escapeID(this.name);
|
||||||
}
|
}
|
||||||
getString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
toString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
||||||
return this.table.serialize(handler) + "." + this.serialize(handler);
|
return this.table.serialize(handler) + "." + this.serialize(handler);
|
||||||
}
|
}
|
||||||
getStringFunc(handler: Handler) {
|
toStringFunc(handler: Handler) {
|
||||||
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
||||||
}
|
}
|
||||||
toString() {
|
|
||||||
return this.table.dbLangTableName + "_" + this.name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TableAlias {
|
export class TableAlias {
|
||||||
|
@ -58,33 +52,3 @@ export class TableAlias {
|
||||||
return handler.builders.escapeID(this.dbLangTableName);
|
return handler.builders.escapeID(this.dbLangTableName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Alias {
|
|
||||||
alias: string;
|
|
||||||
a: selectElements;
|
|
||||||
|
|
||||||
constructor(a: selectElements, alias: string) {
|
|
||||||
this.alias = alias;
|
|
||||||
this.a = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
serialize(handler: Handler) {
|
|
||||||
let builder = new QueryBuilder();
|
|
||||||
if (this.a instanceof Attribute || this.a instanceof AttributeAlias)
|
|
||||||
builder.addCode(this.a.getString(handler) + " " + handler.builders.escapeID(this.a.toString()));
|
|
||||||
else if (this.a instanceof Modifier || this.a instanceof selectQuery || this.a instanceof Aggregation || this.a instanceof Alias) {
|
|
||||||
builder.addCode("(");
|
|
||||||
builder.append(this.a.serialize(handler));
|
|
||||||
builder.addCode(")");
|
|
||||||
} else {
|
|
||||||
builder.addInjection(this.a);
|
|
||||||
}
|
|
||||||
return builder;
|
|
||||||
}
|
|
||||||
getString(handler: Handler) {
|
|
||||||
return this.serialize(handler);
|
|
||||||
}
|
|
||||||
toString() {
|
|
||||||
return this.alias;
|
|
||||||
}
|
|
||||||
}
|
|
62
src/db.ts
62
src/db.ts
|
@ -1,33 +1,24 @@
|
||||||
import mariadb from 'mariadb';
|
import mariadb from 'mariadb';
|
||||||
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure.js';
|
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure';
|
||||||
import { Handler } from './defaultHandler.js';
|
import { Handler } from './defaultHandler';
|
||||||
import { Query } from './query.js';
|
import { Query } from './query';
|
||||||
import { attributeSettings, extendedAttributeSettings, onAction, dbType, order } from './types.js';
|
import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types';
|
||||||
import { TableAlias } from "./alias.js"
|
import { TableAlias } from "./alias"
|
||||||
import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses.js"
|
import { insertResponse, readResponse, writeResponse } from "./responses"
|
||||||
//import { postgresHandler } from "./postgresHandler"
|
//import { postgresHandler } from "./postgresHandler"
|
||||||
|
|
||||||
declare module "mariadb" {
|
|
||||||
interface Pool{
|
|
||||||
on(ev: 'error', callback: () => void): Pool;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DB {
|
export class DB {
|
||||||
tables: Table[] = [];
|
tables: Table[] = [];
|
||||||
handler!: Handler;
|
handler: Handler;
|
||||||
name!: string;
|
name: string;
|
||||||
type!: dbType;
|
type: dbType;
|
||||||
mariaPool!: mariadb.Pool;
|
mariaPool!: mariadb.Pool;
|
||||||
private connected = false;
|
|
||||||
//pgPool!: pg.Pool;
|
//pgPool!: pg.Pool;
|
||||||
connect({ host, user, password, database, port = 3306, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, port?: number, connectionLimit?: number, databaseType?: dbType }) {
|
constructor({ host, user, password, database, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, connectionLimit?: number, databaseType?: dbType }) {
|
||||||
this.connected = true;
|
|
||||||
this.type = databaseType;
|
this.type = databaseType;
|
||||||
if (databaseType == dbType.mariadb) {
|
if (databaseType == dbType.mariadb) {
|
||||||
this.mariaPool = mariadb.createPool({ host, user, password, port, database, connectionLimit, multipleStatements: true });
|
this.mariaPool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
|
||||||
// catch errors
|
|
||||||
this.mariaPool.on("error",()=>{});
|
|
||||||
this.handler = new Handler(this);
|
this.handler = new Handler(this);
|
||||||
}
|
}
|
||||||
/*else if (databaseType == dbType.postgres) {
|
/*else if (databaseType == dbType.postgres) {
|
||||||
|
@ -39,8 +30,7 @@ export class DB {
|
||||||
this.name = database;
|
this.name = database;
|
||||||
}
|
}
|
||||||
async query(query: Query, printQuery = false) {
|
async query(query: Query, printQuery = false) {
|
||||||
if (!this.connected) throw new Error("Not connected yet!");
|
if(printQuery)console.log(query);
|
||||||
if (printQuery) console.log(query);
|
|
||||||
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
||||||
/*else if (this.type == dbType.postgres) {
|
/*else if (this.type == dbType.postgres) {
|
||||||
let res = await this.pgPool.query(query.sql, query.values);
|
let res = await this.pgPool.query(query.sql, query.values);
|
||||||
|
@ -50,12 +40,10 @@ export class DB {
|
||||||
else throw new Error("Invalid Database Type!");
|
else throw new Error("Invalid Database Type!");
|
||||||
}
|
}
|
||||||
getHandler() {
|
getHandler() {
|
||||||
if (!this.connected) throw new Error("Not connected yet!");
|
|
||||||
return this.handler;
|
return this.handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sync(force = false) {
|
async sync(force = false) {
|
||||||
if (!this.connected) throw new Error("Not connected yet!");
|
|
||||||
let handler = this.getHandler();
|
let handler = this.getHandler();
|
||||||
await handler.syncDB(this, handler, force);
|
await handler.syncDB(this, handler, force);
|
||||||
}
|
}
|
||||||
|
@ -71,10 +59,8 @@ export class DB {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
async close() {
|
async close() {
|
||||||
if (!this.connected) throw new Error("Not connected yet!");
|
|
||||||
if (this.type == dbType.mariadb && this.mariaPool) await this.mariaPool.end();
|
if (this.type == dbType.mariadb && this.mariaPool) await this.mariaPool.end();
|
||||||
//if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end();
|
//if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end();
|
||||||
this.connected = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,13 +90,13 @@ export class Attribute {
|
||||||
[this]
|
[this]
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if (ops.foreignKey != null) {
|
if (ops.foreginKey != null) {
|
||||||
table.addConstraint(new foreignConstraint(
|
table.addConstraint(new foreignConstraint(
|
||||||
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreignKey.link.name,
|
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreginKey.link.name,
|
||||||
[this],
|
[this],
|
||||||
[ops.foreignKey.link],
|
[ops.foreginKey.link],
|
||||||
ops.foreignKey.onDelete,
|
ops.foreginKey.onDelete,
|
||||||
ops.foreignKey.onUpdate
|
ops.foreginKey.onUpdate
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,15 +109,12 @@ export class Attribute {
|
||||||
serialize(handler: Handler) {
|
serialize(handler: Handler) {
|
||||||
return handler.builders.escapeID(this.name);
|
return handler.builders.escapeID(this.name);
|
||||||
}
|
}
|
||||||
getString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
toString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
||||||
return this.table.serialize(handler) + "." + this.serialize(handler);
|
return this.table.serialize(handler) + "." + this.serialize(handler);
|
||||||
}
|
}
|
||||||
toStringFunc(handler: Handler) {
|
toStringFunc(handler: Handler) {
|
||||||
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
||||||
}
|
}
|
||||||
toString() {
|
|
||||||
return this.table.dbLangTableName + "_" + this.name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -170,8 +153,7 @@ export class Table {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
addConstraint(c: Constraint) {
|
addConstraint(c: Constraint) {
|
||||||
let check = c.check(this);
|
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,7 +161,7 @@ export class Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from './funcs.js';
|
export * from './funcs';
|
||||||
export { onAction, order };
|
export { onAction };
|
||||||
export { dbType as databaseType }
|
export { dbType as databaseType }
|
||||||
export { readResponse, writeResponse, insertResponse, singleResponse }
|
export { readResponse, writeResponse, insertResponse }
|
|
@ -1,8 +1,7 @@
|
||||||
import { Attribute, Table } from "./db.js";
|
import { Attribute, Table } from "./db";
|
||||||
import { Handler } from "./defaultHandler.js";
|
import { Handler } from "./defaultHandler";
|
||||||
import { QueryBuilder } from "./query.js";
|
import { QueryBuilder } from "./query";
|
||||||
import { sha256 } from "./tools.js";
|
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types";
|
||||||
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types.js";
|
|
||||||
|
|
||||||
export class Datatype {
|
export class Datatype {
|
||||||
type: string;
|
type: string;
|
||||||
|
@ -111,7 +110,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -127,17 +125,14 @@ 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) prim = false;
|
if (this.attrs[i].ops.primaryKey) return "Can not combine unique Constraint and primary key";
|
||||||
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 {
|
||||||
|
@ -152,7 +147,6 @@ 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;
|
||||||
|
@ -166,7 +160,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.";
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import { Alias, AttributeAlias, TableAlias } from "./alias.js";
|
import { AttributeAlias, TableAlias } from "./alias";
|
||||||
import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db.js"
|
import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db"
|
||||||
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"
|
||||||
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js"
|
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query"
|
||||||
import { allModifierInput, joinType, onAction, order, primaryData } from "./types.js"
|
import { allModifierInput, joinType, onAction, primaryData } from "./types"
|
||||||
import { sha256 } from "./tools.js"
|
|
||||||
|
|
||||||
export class Handler {
|
export class Handler {
|
||||||
db: DB;
|
db: DB;
|
||||||
|
@ -121,21 +120,6 @@ 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];
|
||||||
|
@ -145,19 +129,7 @@ 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)
|
||||||
|
@ -170,6 +142,19 @@ 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];
|
||||||
|
@ -195,13 +180,7 @@ 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) {
|
||||||
if (a.ops.autoIncrement) {
|
|
||||||
a.ops.autoIncrement = false;
|
|
||||||
create.appendEnding(handler.querys.addColumn(handler, a));
|
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) ||
|
||||||
|
@ -209,6 +188,10 @@ 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;
|
||||||
|
@ -221,20 +204,11 @@ 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));
|
||||||
|
@ -267,24 +241,7 @@ export class Handler {
|
||||||
select: (handler: Handler, q: selectQuery): QueryBuilder => {
|
select: (handler: Handler, q: selectQuery): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("select ");
|
builder.addCode("select ");
|
||||||
let a = q.attr;
|
builder.append(joinArg(", ")(handler, q.attr));
|
||||||
for (let i = 0; i < a.length; i++) {
|
|
||||||
const d = a[i];
|
|
||||||
if (d instanceof Attribute || d instanceof AttributeAlias)
|
|
||||||
builder.addCode(d.getString(handler) + " " + handler.builders.escapeID(d.toString()));
|
|
||||||
else if (d instanceof Alias) {
|
|
||||||
builder.append(d.serialize(handler));
|
|
||||||
builder.addCode(handler.builders.escapeID(d.toString()));
|
|
||||||
} else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
|
||||||
builder.addCode("(");
|
|
||||||
builder.append(d.serialize(handler));
|
|
||||||
builder.addCode(")");
|
|
||||||
} else {
|
|
||||||
builder.addInjection(d);
|
|
||||||
}
|
|
||||||
if (i + 1 < a.length) builder.addCode(", ");
|
|
||||||
}
|
|
||||||
//builder.append(joinArg(", ")(handler, q.attr));
|
|
||||||
builder.addCode(` from `);
|
builder.addCode(` from `);
|
||||||
if (q.from == null) {
|
if (q.from == null) {
|
||||||
builder.addCode(" DUAL");
|
builder.addCode(" DUAL");
|
||||||
|
@ -307,25 +264,6 @@ 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 ");
|
||||||
|
@ -346,7 +284,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 IGNORE INTO ");
|
qb.addCode("INSERT 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)));
|
||||||
|
@ -374,8 +312,6 @@ export class Handler {
|
||||||
qb.addCode(s[0].serialize(handler));
|
qb.addCode(s[0].serialize(handler));
|
||||||
qb.addCode(" = (");
|
qb.addCode(" = (");
|
||||||
if (s[1] instanceof Attribute || s[1] instanceof AttributeAlias) qb.addCode(s[1].serialize(handler));
|
if (s[1] instanceof Attribute || s[1] instanceof AttributeAlias) qb.addCode(s[1].serialize(handler));
|
||||||
else if (s[1] instanceof Alias)
|
|
||||||
qb.addCode(handler.builders.escapeID(s[1].toString()));
|
|
||||||
else if (s[1] instanceof Modifier || s[1] instanceof selectQuery || s[1] instanceof Aggregation) {
|
else if (s[1] instanceof Modifier || s[1] instanceof selectQuery || s[1] instanceof Aggregation) {
|
||||||
qb.append(s[1].serialize(handler));
|
qb.append(s[1].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -616,7 +552,6 @@ export class Handler {
|
||||||
},
|
},
|
||||||
escapeID: (key: string): string => {
|
escapeID: (key: string): string => {
|
||||||
if (!key || key === "" || key.includes('\u0000')) throw new Error("Can not escape empty key or with null unicode!");
|
if (!key || key === "" || key.includes('\u0000')) throw new Error("Can not escape empty key or with null unicode!");
|
||||||
if (key.length > 64) key = sha256(key);
|
|
||||||
if (key.match(/^`.+`$/g)) return key;
|
if (key.match(/^`.+`$/g)) return key;
|
||||||
return `\`${key.replace(/`/g, '``')}\``;
|
return `\`${key.replace(/`/g, '``')}\``;
|
||||||
},
|
},
|
||||||
|
@ -642,11 +577,11 @@ export class Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
aggregations = {
|
aggregations = {
|
||||||
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.getString(handler) + ")" }]),
|
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]),
|
||||||
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.getString(handler) + ")" }]),
|
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]),
|
||||||
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.getString(handler) + ")" }]),
|
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.toString(handler) + ")" }]),
|
||||||
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.getString(handler) + ")" }]),
|
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.toString(handler) + ")" }]),
|
||||||
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.getString(handler) + ")" }]),
|
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.toString(handler) + ")" }]),
|
||||||
}
|
}
|
||||||
|
|
||||||
joins = {
|
joins = {
|
||||||
|
@ -655,7 +590,6 @@ export class Handler {
|
||||||
for (let i = 0; i < j.tables.length; i++) {
|
for (let i = 0; i < j.tables.length; i++) {
|
||||||
const t = j.tables[i];
|
const t = j.tables[i];
|
||||||
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
||||||
else if (t instanceof TableAlias) qb.addCode(t.serializeAlias(handler));
|
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(t.serialize(handler));
|
qb.append(t.serialize(handler));
|
||||||
|
@ -674,7 +608,6 @@ export class Handler {
|
||||||
using: (handler: Handler, j: usingJoin) => {
|
using: (handler: Handler, j: usingJoin) => {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
||||||
else if (j.tableA instanceof TableAlias) qb.addCode(j.tableA.serializeAlias(handler));
|
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableA.serialize(handler));
|
||||||
|
@ -685,11 +618,10 @@ export class Handler {
|
||||||
if (j.type == joinType.right) qb.addCode("right ");
|
if (j.type == joinType.right) qb.addCode("right ");
|
||||||
if (j.type == joinType.full) qb.addCode("full ");
|
if (j.type == joinType.full) qb.addCode("full ");
|
||||||
qb.addCode("join ");
|
qb.addCode("join ");
|
||||||
if (j.tableB instanceof Table) qb.addCode(j.tableB.serialize(handler));
|
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
||||||
else if (j.tableB instanceof TableAlias) qb.addCode(j.tableB.serializeAlias(handler));
|
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableB.serialize(handler));
|
qb.append(j.tableA.serialize(handler));
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
}
|
}
|
||||||
qb.addCode(" using (");
|
qb.addCode(" using (");
|
||||||
|
@ -700,7 +632,6 @@ export class Handler {
|
||||||
on: (handler: Handler, j: onJoin) => {
|
on: (handler: Handler, j: onJoin) => {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
||||||
else if (j.tableA instanceof TableAlias) qb.addCode(j.tableA.serializeAlias(handler));
|
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableA.serialize(handler));
|
||||||
|
@ -711,11 +642,10 @@ export class Handler {
|
||||||
if (j.type == joinType.right) qb.addCode("right ");
|
if (j.type == joinType.right) qb.addCode("right ");
|
||||||
if (j.type == joinType.full) qb.addCode("full ");
|
if (j.type == joinType.full) qb.addCode("full ");
|
||||||
qb.addCode("join ");
|
qb.addCode("join ");
|
||||||
if (j.tableB instanceof Table) qb.addCode(j.tableB.serialize(handler));
|
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
||||||
else if (j.tableB instanceof TableAlias) qb.addCode(j.tableB.serializeAlias(handler));
|
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableB.serialize(handler));
|
qb.append(j.tableA.serialize(handler));
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
}
|
}
|
||||||
qb.addCode(" on (");
|
qb.addCode(" on (");
|
||||||
|
@ -728,7 +658,6 @@ export class Handler {
|
||||||
for (let i = 0; i < j.tables.length; i++) {
|
for (let i = 0; i < j.tables.length; i++) {
|
||||||
const t = j.tables[i];
|
const t = j.tables[i];
|
||||||
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
||||||
else if (t instanceof TableAlias) qb.addCode(t.serializeAlias(handler));
|
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(t.serialize(handler));
|
qb.append(t.serialize(handler));
|
||||||
|
@ -754,9 +683,7 @@ export class Handler {
|
||||||
divide: joinArg("-/"),
|
divide: joinArg("-/"),
|
||||||
not: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
not: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
let e = a[0];
|
let e = a[0];
|
||||||
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "not (" + e.getString(handler) + ")" }])
|
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "not (" + e.toString(handler) + ")" }])
|
||||||
if (e instanceof Alias)
|
|
||||||
return new QueryBuilder([{ data: handler.builders.escapeID(e.toString()) }]);
|
|
||||||
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("not (");
|
builder.addCode("not (");
|
||||||
|
@ -772,18 +699,14 @@ export class Handler {
|
||||||
},
|
},
|
||||||
like: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
like: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].getString());
|
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].toString());
|
||||||
else if (a[0] instanceof Alias)
|
|
||||||
builder.addCode(handler.builders.escapeID(a[0].toString()));
|
|
||||||
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
||||||
builder.append(a[0].serialize(handler));
|
builder.append(a[0].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
builder.addInjection(a[0]);
|
builder.addInjection(a[0]);
|
||||||
}
|
}
|
||||||
builder.addCode(" LIKE ");
|
builder.addCode(" LIKE ");
|
||||||
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].getString());
|
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].toString());
|
||||||
else if (a[1] instanceof Alias)
|
|
||||||
builder.addCode(handler.builders.escapeID(a[1].toString()));
|
|
||||||
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
||||||
builder.append(a[1].serialize(handler));
|
builder.append(a[1].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -793,18 +716,14 @@ export class Handler {
|
||||||
},
|
},
|
||||||
regexp: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
regexp: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].getString());
|
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].toString());
|
||||||
else if (a[0] instanceof Alias)
|
|
||||||
builder.addCode(handler.builders.escapeID(a[0].toString()));
|
|
||||||
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
||||||
builder.append(a[0].serialize(handler));
|
builder.append(a[0].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
builder.addInjection(a[0]);
|
builder.addInjection(a[0]);
|
||||||
}
|
}
|
||||||
builder.addCode(" REGEXP ");
|
builder.addCode(" REGEXP ");
|
||||||
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].getString());
|
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].toString());
|
||||||
else if (a[1] instanceof Alias)
|
|
||||||
builder.addCode(handler.builders.escapeID(a[1].toString()));
|
|
||||||
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
||||||
builder.append(a[1].serialize(handler));
|
builder.append(a[1].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -812,32 +731,12 @@ export class Handler {
|
||||||
}
|
}
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
exists: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
|
||||||
let e = a[0];
|
|
||||||
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "exists (" + e.getString(handler) + ")" }])
|
|
||||||
if (e instanceof Alias)
|
|
||||||
return new QueryBuilder([{ data: handler.builders.escapeID(e.toString()) }]);
|
|
||||||
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
|
||||||
const builder = new QueryBuilder();
|
|
||||||
builder.addCode("exists (");
|
|
||||||
builder.append(e.serialize(handler));
|
|
||||||
builder.addCode(")");
|
|
||||||
return builder;
|
|
||||||
}
|
|
||||||
return new QueryBuilder([
|
|
||||||
{ data: "exists (" },
|
|
||||||
{ inject: true, data: e },
|
|
||||||
{ data: ")" }
|
|
||||||
]);
|
|
||||||
},
|
|
||||||
concat: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
concat: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("CONCAT(");
|
builder.addCode("CONCAT(");
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
const e = a[i];
|
const e = a[i];
|
||||||
if (e instanceof Attribute || e instanceof AttributeAlias) builder.addCode(e.getString());
|
if (e instanceof Attribute || e instanceof AttributeAlias) builder.addCode(e.toString());
|
||||||
else if (e instanceof Alias)
|
|
||||||
builder.addCode(handler.builders.escapeID(e.toString()));
|
|
||||||
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
builder.append(e.serialize(handler));
|
builder.append(e.serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -847,25 +746,7 @@ 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 = {
|
||||||
|
@ -953,9 +834,7 @@ function joinArg(type: string) {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
const d = a[i];
|
const d = a[i];
|
||||||
if (d instanceof Attribute || d instanceof AttributeAlias) builder.addCode(d.getString(handler));
|
if (d instanceof Attribute || d instanceof AttributeAlias) builder.addCode(d.toString(handler));
|
||||||
else if (d instanceof Alias)
|
|
||||||
builder.addCode(handler.builders.escapeID(d.toString()));
|
|
||||||
else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
||||||
builder.addCode("(");
|
builder.addCode("(");
|
||||||
builder.append(d.serialize(handler));
|
builder.append(d.serialize(handler));
|
||||||
|
|
15
src/funcs.ts
15
src/funcs.ts
|
@ -1,8 +1,7 @@
|
||||||
import { Alias } from "./alias.js";
|
import { Attribute, Table } from "./db";
|
||||||
import { Attribute, Table } from "./db.js";
|
import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure";
|
||||||
import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure.js";
|
import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query";
|
||||||
import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query.js";
|
import { allModifierInput, joinType, selectElements, selectFromElements } from "./types";
|
||||||
import { allModifierInput, joinType, selectElements, selectFromElements } from "./types.js";
|
|
||||||
|
|
||||||
//modifiers
|
//modifiers
|
||||||
export const and = (...args: BooleanModifier[]) => new BooleanModifier("and", args);
|
export const and = (...args: BooleanModifier[]) => new BooleanModifier("and", args);
|
||||||
|
@ -21,10 +20,7 @@ export const not = (arg: allModifierInput) => new BooleanModifier("not", [arg]);
|
||||||
export const like = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("like", [a, b]);
|
export const like = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("like", [a, b]);
|
||||||
export const regexp = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("regexp", [a, b]);
|
export const regexp = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("regexp", [a, b]);
|
||||||
|
|
||||||
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);
|
||||||
|
@ -58,9 +54,6 @@ export const insert = (...attrs: Attribute[]) => new insertQuery(attrs);
|
||||||
export const update = (table: Table) => new updateQuery(table);
|
export const update = (table: Table) => new updateQuery(table);
|
||||||
export const remove = (table: Table) => new removeQuery(table);
|
export const remove = (table: Table) => new removeQuery(table);
|
||||||
|
|
||||||
//alias
|
|
||||||
export const alias = (a: selectElements, name: string) => new Alias(a, name);
|
|
||||||
|
|
||||||
//datatypes
|
//datatypes
|
||||||
|
|
||||||
export const CHAR = (size: number) => new Datatype("char", [size]);
|
export const CHAR = (size: number) => new Datatype("char", [size]);
|
||||||
|
|
29
src/query.ts
29
src/query.ts
|
@ -1,8 +1,8 @@
|
||||||
import { AttributeAlias } from "./alias.js";
|
import { AttributeAlias } from "./alias";
|
||||||
import { Attribute, DB, Table } from "./db.js";
|
import { Attribute, DB, Table } from "./db";
|
||||||
import { BooleanModifier, Modifier } from "./dbStructure.js";
|
import { BooleanModifier, Modifier } from "./dbStructure";
|
||||||
import { Handler } from "./defaultHandler.js";
|
import { Handler } from "./defaultHandler";
|
||||||
import { allModifierInput, order, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js";
|
import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types";
|
||||||
|
|
||||||
|
|
||||||
export class Query {
|
export class Query {
|
||||||
|
@ -84,17 +84,6 @@ 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) {
|
||||||
|
@ -112,7 +101,7 @@ export class selectQuery {
|
||||||
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 handler.responses.readResponse(readResp);
|
return db.getHandler().responses.readResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,7 +139,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 handler.responses.insertResponse(readResp);
|
return db.getHandler().responses.insertResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +168,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 handler.responses.writeResponse(readResp);
|
return db.getHandler().responses.writeResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,6 +190,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 handler.responses.writeResponse(readResp);
|
return db.getHandler().responses.writeResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,36 +1,21 @@
|
||||||
import { AttributeAlias } from "./alias.js";
|
|
||||||
import { Attribute } from "./db.js";
|
|
||||||
import { primaryData } from "./types.js";
|
|
||||||
|
|
||||||
export class singleResponse {
|
export class readResponse extends Array{
|
||||||
[key: string]: primaryData | any;
|
constructor(rows:({[key: string]:any})[]){
|
||||||
constructor(r: object) {
|
|
||||||
Object.assign(this, r);
|
|
||||||
}
|
|
||||||
getVal(a: Attribute | AttributeAlias) {
|
|
||||||
return this[a.toString()];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class readResponse extends Array<singleResponse>{
|
|
||||||
constructor(rows: ({ [key: string]: primaryData })[]) {
|
|
||||||
super();
|
super();
|
||||||
for(let i = 0; i < rows.length; i++){
|
this.push(...rows);
|
||||||
this[i] = new singleResponse(rows[i])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class writeResponse {
|
export class writeResponse{
|
||||||
affectedRows: number;
|
affectedRows: number;
|
||||||
constructor(affectedRows: number) {
|
constructor(affectedRows: number){
|
||||||
this.affectedRows = affectedRows;
|
this.affectedRows = affectedRows;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class insertResponse extends writeResponse {
|
export class insertResponse extends writeResponse{
|
||||||
insertId: number;
|
insertId: number;
|
||||||
constructor(affectedRows: number, insertId: number) {
|
constructor(affectedRows: number, insertId: number){
|
||||||
super(affectedRows);
|
super(affectedRows);
|
||||||
this.insertId = insertId;
|
this.insertId = insertId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
import crypto from "crypto";
|
|
||||||
|
|
||||||
export const sha256 = (d: any) => crypto.createHash('sha256').update(String(d)).digest('base64');
|
|
21
src/types.ts
21
src/types.ts
|
@ -1,12 +1,12 @@
|
||||||
import { Alias, AttributeAlias, TableAlias } from "./alias.js";
|
import { AttributeAlias, TableAlias } from "./alias";
|
||||||
import { Attribute, Table } from "./db.js";
|
import { Attribute, Table } from "./db";
|
||||||
import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure.js";
|
import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure";
|
||||||
import { QueryBuilder, selectQuery } from "./query.js";
|
import { QueryBuilder, selectQuery } from "./query";
|
||||||
|
|
||||||
export type primaryData = string | number | boolean | null;
|
export type primaryData = string | number | boolean | null;
|
||||||
export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | AttributeAlias | Aggregation | Alias;
|
export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | AttributeAlias | Aggregation;
|
||||||
|
|
||||||
export type selectElements = primaryData | Attribute | AttributeAlias | Aggregation | selectQuery | Alias;
|
export type selectElements = primaryData | Attribute | AttributeAlias | Aggregation | selectQuery
|
||||||
export type selectFromElements = TableAlias | Table | Joins | null;
|
export type selectFromElements = TableAlias | Table | Joins | null;
|
||||||
export type joinElements = Table | Joins;
|
export type joinElements = Table | Joins;
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ export type attributeSettings = {
|
||||||
default?: primaryData,
|
default?: primaryData,
|
||||||
notNull?: boolean
|
notNull?: boolean
|
||||||
primaryKey?: boolean,
|
primaryKey?: boolean,
|
||||||
foreignKey?: {
|
foreginKey?: {
|
||||||
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,
|
||||||
foreignKey?: {
|
foreginKey?: {
|
||||||
link: Attribute,
|
link: Attribute,
|
||||||
onDelete?: onAction,
|
onDelete?: onAction,
|
||||||
onUpdate?: onAction
|
onUpdate?: onAction
|
||||||
|
@ -64,8 +64,3 @@ export enum dbType {
|
||||||
//mysql,
|
//mysql,
|
||||||
postgres,
|
postgres,
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum order {
|
|
||||||
ASC,
|
|
||||||
DESC
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES6",
|
"module": "CommonJS",
|
||||||
"module": "ES6",
|
|
||||||
"lib": ["es6"],
|
"lib": ["es6"],
|
||||||
"types": ["node"],
|
"types": ["node"],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
|
Loading…
Reference in a new issue