Compare commits

..

No commits in common. "fc9718f9a12b57efb42af5cb8d8b1b285067235d" and "ea5954a9ff125b2011666cf0f7cf649cf9d75d0d" have entirely different histories.

8 changed files with 13 additions and 76 deletions

1
.gitignore vendored
View file

@ -3,4 +3,3 @@ node_modules
ltests ltests
sqlsave sqlsave
postgresHandler.ts postgresHandler.ts
.vscode

View file

@ -1,6 +1,6 @@
{ {
"name": "dblang", "name": "dblang",
"version": "0.8.0", "version": "0.7.0",
"description": "", "description": "",
"main": "dist/db.js", "main": "dist/db.js",
"types": "dist/db.d.ts", "types": "dist/db.d.ts",

View file

@ -13,7 +13,6 @@ 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
@ -30,8 +29,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,7 +130,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: Get requestet Data:

View file

@ -1,8 +1,5 @@
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;
@ -58,33 +55,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;
}
}

View file

@ -74,7 +74,6 @@ 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 { Alias, AttributeAlias, TableAlias } from "./alias.js"; import { 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"
@ -246,10 +246,7 @@ export class Handler {
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 Alias) { else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
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(")");
@ -329,8 +326,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 {
@ -709,8 +704,6 @@ 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 (");
@ -727,8 +720,6 @@ 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 {
@ -736,8 +727,6 @@ 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 {
@ -748,8 +737,6 @@ 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 {
@ -757,8 +744,6 @@ 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 {
@ -769,8 +754,6 @@ export class Handler {
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 (");
@ -790,8 +773,6 @@ 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 {
@ -890,8 +871,6 @@ 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,4 +1,3 @@
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";
@ -57,9 +56,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]);

View file

@ -1,12 +1,12 @@
import { Alias, AttributeAlias, TableAlias } from "./alias.js"; import { 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 | 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;