Compare commits

...

6 commits

Author SHA1 Message Date
fc9718f9a1 Merge pull request 'generell aliases and small fixes' (#15) from dev into main
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
Reviewed-on: #15
2023-03-02 14:36:59 +01:00
8210af9c6b
version
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
2023-03-02 14:36:13 +01:00
124c129c2b
generell Aliases
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-03-02 14:33:41 +01:00
6e38f4ee13
changed git ignore
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-03-01 16:37:17 +01:00
f8aa601fcf
readme fix
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-03-01 16:35:09 +01:00
d51ad10864
update connect var
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-03-01 16:32:34 +01:00
8 changed files with 76 additions and 13 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@ dist
node_modules node_modules
ltests ltests
sqlsave sqlsave
postgresHandler.ts postgresHandler.ts
.vscode

View file

@ -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",

View file

@ -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:

View file

@ -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;
}
} }

View file

@ -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;
} }
} }

View file

@ -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));

View file

@ -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]);

View file

@ -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;