responses and fixes #10
5 changed files with 83 additions and 30 deletions
14
readme.md
14
readme.md
|
@ -13,6 +13,7 @@ Features:
|
||||||
- [x] remove unused Schema
|
- [x] remove unused Schema
|
||||||
- [x] joins
|
- [x] joins
|
||||||
- [x] table alias
|
- [x] table alias
|
||||||
|
- [x] unified response
|
||||||
- [ ] change Schema
|
- [ ] change Schema
|
||||||
- [ ] Key-Value Store Shortcut
|
- [ ] Key-Value Store Shortcut
|
||||||
- [ ] Views
|
- [ ] Views
|
||||||
|
@ -131,6 +132,19 @@ 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.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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:
|
||||||
```javascript
|
```javascript
|
||||||
import { select } from "dblang"
|
import { select } from "dblang"
|
||||||
|
|
|
@ -15,12 +15,15 @@ export class AttributeAlias {
|
||||||
serialize(handler: Handler) {
|
serialize(handler: Handler) {
|
||||||
return handler.builders.escapeID(this.name);
|
return handler.builders.escapeID(this.name);
|
||||||
}
|
}
|
||||||
toString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
getString(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) {
|
getStringFunc(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 {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { Handler } from './defaultHandler';
|
||||||
import { Query } from './query';
|
import { Query } from './query';
|
||||||
import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types';
|
import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types';
|
||||||
import { TableAlias } from "./alias"
|
import { TableAlias } from "./alias"
|
||||||
import { insertResponse, readResponse, writeResponse } from "./responses"
|
import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses"
|
||||||
//import { postgresHandler } from "./postgresHandler"
|
//import { postgresHandler } from "./postgresHandler"
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,12 +109,15 @@ export class Attribute {
|
||||||
serialize(handler: Handler) {
|
serialize(handler: Handler) {
|
||||||
return handler.builders.escapeID(this.name);
|
return handler.builders.escapeID(this.name);
|
||||||
}
|
}
|
||||||
toString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
getString(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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,4 +167,4 @@ export class Table {
|
||||||
export * from './funcs';
|
export * from './funcs';
|
||||||
export { onAction };
|
export { onAction };
|
||||||
export { dbType as databaseType }
|
export { dbType as databaseType }
|
||||||
export { readResponse, writeResponse, insertResponse }
|
export { readResponse, writeResponse, insertResponse, singleResponse }
|
|
@ -241,7 +241,21 @@ 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 ");
|
||||||
builder.append(joinArg(", ")(handler, q.attr));
|
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) {
|
||||||
|
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");
|
||||||
|
@ -577,11 +591,11 @@ export class Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
aggregations = {
|
aggregations = {
|
||||||
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]),
|
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.getString(handler) + ")" }]),
|
||||||
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]),
|
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.getString(handler) + ")" }]),
|
||||||
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.toString(handler) + ")" }]),
|
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.getString(handler) + ")" }]),
|
||||||
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.toString(handler) + ")" }]),
|
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.getString(handler) + ")" }]),
|
||||||
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.toString(handler) + ")" }]),
|
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.getString(handler) + ")" }]),
|
||||||
}
|
}
|
||||||
|
|
||||||
joins = {
|
joins = {
|
||||||
|
@ -590,6 +604,7 @@ 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));
|
||||||
|
@ -608,6 +623,7 @@ 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));
|
||||||
|
@ -618,10 +634,11 @@ 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.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableB instanceof Table) qb.addCode(j.tableB.serialize(handler));
|
||||||
|
else if (j.tableB instanceof TableAlias) qb.addCode(j.tableB.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableB.serialize(handler));
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
}
|
}
|
||||||
qb.addCode(" using (");
|
qb.addCode(" using (");
|
||||||
|
@ -632,6 +649,7 @@ 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));
|
||||||
|
@ -642,10 +660,11 @@ 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.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableB instanceof Table) qb.addCode(j.tableB.serialize(handler));
|
||||||
|
else if (j.tableB instanceof TableAlias) qb.addCode(j.tableB.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableB.serialize(handler));
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
}
|
}
|
||||||
qb.addCode(" on (");
|
qb.addCode(" on (");
|
||||||
|
@ -658,6 +677,7 @@ 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));
|
||||||
|
@ -683,7 +703,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.toString(handler) + ")" }])
|
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "not (" + e.getString(handler) + ")" }])
|
||||||
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 (");
|
||||||
|
@ -699,14 +719,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].toString());
|
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].getString());
|
||||||
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].toString());
|
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].getString());
|
||||||
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 {
|
||||||
|
@ -716,14 +736,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].toString());
|
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].getString());
|
||||||
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].toString());
|
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].getString());
|
||||||
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 {
|
||||||
|
@ -736,7 +756,7 @@ export class Handler {
|
||||||
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.toString());
|
if (e instanceof Attribute || e instanceof AttributeAlias) builder.addCode(e.getString());
|
||||||
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 {
|
||||||
|
@ -834,7 +854,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.toString(handler));
|
if (d instanceof Attribute || d instanceof AttributeAlias) builder.addCode(d.getString(handler));
|
||||||
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,8 +1,21 @@
|
||||||
|
import { AttributeAlias } from "./alias";
|
||||||
|
import { Attribute } from "./db";
|
||||||
|
import { primaryData } from "./types";
|
||||||
|
|
||||||
export class readResponse extends Array{
|
export class singleResponse {
|
||||||
constructor(rows:({[key: string]:any})[]){
|
[key: string]: primaryData | 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();
|
||||||
this.push(...rows);
|
this.push(...rows.map(r => new singleResponse(r)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue