From 124c129c2ba1a250eac6014623f24397886b4b99 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Thu, 2 Mar 2023 14:33:41 +0100 Subject: [PATCH] generell Aliases --- readme.md | 2 ++ src/alias.ts | 37 +++++++++++++++++++++++++++++++++++-- src/defaultHandler.ts | 31 ++++++++++++++++++++++++++----- src/funcs.ts | 4 ++++ src/types.ts | 6 +++--- 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index b61f31c..987b7f7 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,7 @@ Features: - [x] remove unused Schema - [x] joins - [x] table alias +- [x] generell aliases - [x] unified response - [ ] change Schema - [ ] Key-Value Store Shortcut @@ -131,6 +132,7 @@ _Aliases_: ```javascript const alias1 = TableA.createAlias("alias1"); //alias1 can be used like a Table in select Statements. +const alias2 = alias(avg(TableA.A1), "someAvg"); ``` Get requestet Data: diff --git a/src/alias.ts b/src/alias.ts index 38a8a58..60211bf 100644 --- a/src/alias.ts +++ b/src/alias.ts @@ -1,5 +1,8 @@ import { Attribute, DB, Table } from "./db.js" +import { Aggregation, Modifier } from "./dbStructure.js"; import { Handler } from "./defaultHandler.js"; +import { QueryBuilder, selectQuery } from "./query.js"; +import { selectElements } from "./types.js"; export class AttributeAlias { name: string; @@ -21,8 +24,8 @@ export class AttributeAlias { getStringFunc(handler: Handler) { return this.table.serialize(handler) + "(" + this.serialize(handler) + ")"; } - toString(){ - return this.table.dbLangTableName + "_" + this.name; + toString() { + return this.table.dbLangTableName + "_" + this.name; } } @@ -54,4 +57,34 @@ export class TableAlias { toString(handler: Handler = this.dbLangDatabaseInstance.getHandler()) { 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; + } } \ No newline at end of file diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index d37ded8..df56e8c 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -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 { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure.js" import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js" @@ -244,9 +244,12 @@ export class Handler { let a = 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 Modifier || d instanceof selectQuery || d instanceof Aggregation) { + 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(")"); @@ -326,6 +329,8 @@ export class Handler { qb.addCode(s[0].serialize(handler)); qb.addCode(" = ("); 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) { qb.append(s[1].serialize(handler)); } else { @@ -704,6 +709,8 @@ export class Handler { not: (handler: Handler, a: allModifierInput[]): QueryBuilder => { let e = a[0]; 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) { const builder = new QueryBuilder(); builder.addCode("not ("); @@ -720,6 +727,8 @@ export class Handler { like: (handler: Handler, a: allModifierInput[]): QueryBuilder => { const builder = new QueryBuilder(); 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) { builder.append(a[0].serialize(handler)); } else { @@ -727,6 +736,8 @@ export class Handler { } builder.addCode(" LIKE "); 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) { builder.append(a[1].serialize(handler)); } else { @@ -737,6 +748,8 @@ export class Handler { regexp: (handler: Handler, a: allModifierInput[]): QueryBuilder => { const builder = new QueryBuilder(); 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) { builder.append(a[0].serialize(handler)); } else { @@ -744,6 +757,8 @@ export class Handler { } builder.addCode(" REGEXP "); 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) { builder.append(a[1].serialize(handler)); } else { @@ -751,9 +766,11 @@ export class Handler { } return builder; }, - exists: (handler: Handler, a: allModifierInput[]):QueryBuilder =>{ + 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 ("); @@ -773,6 +790,8 @@ export class Handler { 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 { @@ -871,6 +890,8 @@ function joinArg(type: string) { for (let i = 0; i < a.length; i++) { const d = a[i]; 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) { builder.addCode("("); builder.append(d.serialize(handler)); diff --git a/src/funcs.ts b/src/funcs.ts index a11b831..2c8703c 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -1,3 +1,4 @@ +import { Alias } from "./alias.js"; import { Attribute, Table } from "./db.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"; @@ -56,6 +57,9 @@ export const insert = (...attrs: Attribute[]) => new insertQuery(attrs); export const update = (table: Table) => new updateQuery(table); export const remove = (table: Table) => new removeQuery(table); +//alias +export const alias = (a: selectElements, name: string) => new Alias(a, name); + //datatypes export const CHAR = (size: number) => new Datatype("char", [size]); diff --git a/src/types.ts b/src/types.ts index 4783874..d1c7b03 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,12 +1,12 @@ -import { AttributeAlias, TableAlias } from "./alias.js"; +import { Alias, AttributeAlias, TableAlias } from "./alias.js"; import { Attribute, Table } from "./db.js"; import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure.js"; import { QueryBuilder, selectQuery } from "./query.js"; 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 joinElements = Table | Joins;