Merge pull request 'generell aliases and small fixes' (#15) from dev into main
Reviewed-on: #15
This commit is contained in:
commit
fc9718f9a1
8 changed files with 76 additions and 13 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,4 +2,5 @@ dist
|
||||||
node_modules
|
node_modules
|
||||||
ltests
|
ltests
|
||||||
sqlsave
|
sqlsave
|
||||||
postgresHandler.ts
|
postgresHandler.ts
|
||||||
|
.vscode
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "0.7.0",
|
"version": "0.8.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/db.js",
|
"main": "dist/db.js",
|
||||||
"types": "dist/db.d.ts",
|
"types": "dist/db.d.ts",
|
||||||
|
|
|
@ -13,6 +13,7 @@ Features:
|
||||||
- [x] remove unused Schema
|
- [x] remove unused Schema
|
||||||
- [x] joins
|
- [x] joins
|
||||||
- [x] table alias
|
- [x] table alias
|
||||||
|
- [x] generell aliases
|
||||||
- [x] unified response
|
- [x] unified response
|
||||||
- [ ] change Schema
|
- [ ] change Schema
|
||||||
- [ ] Key-Value Store Shortcut
|
- [ ] Key-Value Store Shortcut
|
||||||
|
@ -29,7 +30,8 @@ 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",
|
||||||
|
@ -130,6 +132,7 @@ _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:
|
Get requestet Data:
|
||||||
|
|
37
src/alias.ts
37
src/alias.ts
|
@ -1,5 +1,8 @@
|
||||||
import { Attribute, DB, Table } from "./db.js"
|
import { Attribute, DB, Table } from "./db.js"
|
||||||
|
import { Aggregation, Modifier } from "./dbStructure.js";
|
||||||
import { Handler } from "./defaultHandler.js";
|
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;
|
||||||
|
@ -21,8 +24,8 @@ export class AttributeAlias {
|
||||||
getStringFunc(handler: Handler) {
|
getStringFunc(handler: Handler) {
|
||||||
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
||||||
}
|
}
|
||||||
toString(){
|
toString() {
|
||||||
return this.table.dbLangTableName + "_" + this.name;
|
return this.table.dbLangTableName + "_" + this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,4 +57,34 @@ export class TableAlias {
|
||||||
toString(handler: Handler = this.dbLangDatabaseInstance.getHandler()) {
|
toString(handler: Handler = this.dbLangDatabaseInstance.getHandler()) {
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -74,6 +74,7 @@ export class DB {
|
||||||
if (!this.connected) throw new Error("Not connected yet!");
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { AttributeAlias, TableAlias } from "./alias.js";
|
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"
|
||||||
|
@ -244,9 +244,12 @@ export class Handler {
|
||||||
let a = q.attr;
|
let a = q.attr;
|
||||||
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)
|
if (d instanceof Attribute || d instanceof AttributeAlias)
|
||||||
builder.addCode(d.getString(handler)+" "+ handler.builders.escapeID(d.toString()));
|
builder.addCode(d.getString(handler) + " " + handler.builders.escapeID(d.toString()));
|
||||||
else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
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.addCode("(");
|
||||||
builder.append(d.serialize(handler));
|
builder.append(d.serialize(handler));
|
||||||
builder.addCode(")");
|
builder.addCode(")");
|
||||||
|
@ -326,6 +329,8 @@ 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 {
|
||||||
|
@ -704,6 +709,8 @@ export class Handler {
|
||||||
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.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) {
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("not (");
|
builder.addCode("not (");
|
||||||
|
@ -720,6 +727,8 @@ 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].getString());
|
||||||
|
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 {
|
||||||
|
@ -727,6 +736,8 @@ export class Handler {
|
||||||
}
|
}
|
||||||
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].getString());
|
||||||
|
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 {
|
||||||
|
@ -737,6 +748,8 @@ 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].getString());
|
||||||
|
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 {
|
||||||
|
@ -744,6 +757,8 @@ export class Handler {
|
||||||
}
|
}
|
||||||
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].getString());
|
||||||
|
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 {
|
||||||
|
@ -751,9 +766,11 @@ export class Handler {
|
||||||
}
|
}
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
exists: (handler: Handler, a: allModifierInput[]):QueryBuilder =>{
|
exists: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
let e = a[0];
|
let e = a[0];
|
||||||
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "exists (" + e.getString(handler) + ")" }])
|
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) {
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("exists (");
|
builder.addCode("exists (");
|
||||||
|
@ -773,6 +790,8 @@ export class Handler {
|
||||||
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.getString());
|
||||||
|
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 {
|
||||||
|
@ -871,6 +890,8 @@ function joinArg(type: string) {
|
||||||
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.getString(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));
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { Alias } from "./alias.js";
|
||||||
import { Attribute, Table } from "./db.js";
|
import { Attribute, Table } from "./db.js";
|
||||||
import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure.js";
|
import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure.js";
|
||||||
import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query.js";
|
import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query.js";
|
||||||
|
@ -56,6 +57,9 @@ 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]);
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import { AttributeAlias, TableAlias } from "./alias.js";
|
import { Alias, AttributeAlias, TableAlias } from "./alias.js";
|
||||||
import { Attribute, Table } from "./db.js";
|
import { Attribute, Table } from "./db.js";
|
||||||
import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure.js";
|
import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure.js";
|
||||||
import { QueryBuilder, selectQuery } from "./query.js";
|
import { QueryBuilder, selectQuery } from "./query.js";
|
||||||
|
|
||||||
export type primaryData = string | number | boolean | null;
|
export type primaryData = string | number | boolean | null;
|
||||||
export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | AttributeAlias | Aggregation;
|
export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | AttributeAlias | Aggregation | Alias;
|
||||||
|
|
||||||
export type selectElements = primaryData | Attribute | AttributeAlias | Aggregation | selectQuery
|
export type selectElements = primaryData | Attribute | AttributeAlias | Aggregation | selectQuery | Alias;
|
||||||
export type selectFromElements = TableAlias | Table | Joins | null;
|
export type selectFromElements = TableAlias | Table | Joins | null;
|
||||||
export type joinElements = Table | Joins;
|
export type joinElements = Table | Joins;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue