2023-01-23 21:19:18 +01:00
|
|
|
import { Attribute, DB } from "./db";
|
|
|
|
import { BooleanModifier, Modifier } from "./dbStructure";
|
|
|
|
import { Handler } from "./defaultHandler";
|
|
|
|
import { primaryData, selectElements, selectFromElements, serializeReturn } from "./types";
|
|
|
|
|
|
|
|
|
|
|
|
export class Query {
|
2023-01-23 22:10:33 +01:00
|
|
|
sql: string;
|
|
|
|
values: primaryData[];
|
2023-01-29 22:11:24 +01:00
|
|
|
constructor([sql, values]:serializeReturn) {
|
2023-01-23 21:19:18 +01:00
|
|
|
this.sql = sql;
|
|
|
|
this.values = values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 22:11:24 +01:00
|
|
|
export class QueryBuilder {
|
|
|
|
//injekt and data
|
|
|
|
list: ([boolean, primaryData])[] = [];
|
|
|
|
constructor(l?: ({ inject?: boolean, data: primaryData })[]) {
|
|
|
|
if (Array.isArray(l))
|
|
|
|
for (let i = 0; i < l.length; i++) {
|
|
|
|
const e = l[i];
|
|
|
|
this.list.push([e.inject ? true : false, e.data]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addCode(text: string) {
|
|
|
|
this.list.push([false, text]);
|
|
|
|
}
|
|
|
|
addInjection(data: primaryData) {
|
|
|
|
this.list.push([true, data]);
|
|
|
|
}
|
|
|
|
addInjectionCommaSeperated(data: primaryData[], comma = ", ") {
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
const e = data[i];
|
|
|
|
this.list.push([true, e]);
|
|
|
|
if(i+1<data.length)this.list.push([false, comma]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
append(qb: QueryBuilder) {
|
|
|
|
this.list.push(...qb.list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-23 21:19:18 +01:00
|
|
|
export class selectQuery {
|
2023-01-23 22:10:33 +01:00
|
|
|
attr: selectElements[] = [];
|
|
|
|
from: selectFromElements;
|
|
|
|
constructor(a: selectElements[], from: selectFromElements) {
|
2023-01-23 21:19:18 +01:00
|
|
|
this.attr.push(...a.flat(Infinity));
|
|
|
|
this.from = from ? from : null;
|
|
|
|
}
|
2023-01-23 22:10:33 +01:00
|
|
|
whereD: BooleanModifier | null = null;
|
2023-01-23 21:19:18 +01:00
|
|
|
where(m: BooleanModifier) {
|
|
|
|
this.whereD = m;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
groupByD: Attribute[] = [];
|
|
|
|
groupBy(a: Attribute[]) {
|
|
|
|
this.groupByD = a;
|
|
|
|
return this;
|
|
|
|
}
|
2023-01-23 22:10:33 +01:00
|
|
|
havingD: Modifier | null = null;
|
2023-01-23 21:19:18 +01:00
|
|
|
having(m: Modifier) {
|
|
|
|
this.havingD = m;
|
|
|
|
return this;
|
|
|
|
}
|
2023-01-23 22:10:33 +01:00
|
|
|
limitD: number | null = null;
|
|
|
|
limit(i: number) {
|
|
|
|
this.limitD = i;
|
|
|
|
return this;
|
|
|
|
}
|
2023-01-23 21:19:18 +01:00
|
|
|
|
2023-01-29 22:11:24 +01:00
|
|
|
serialize(handler: Handler): QueryBuilder {
|
|
|
|
return handler.querys.select(handler, this);
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
query(db: DB) {
|
2023-01-29 22:11:24 +01:00
|
|
|
const handler = db.getHandler();
|
|
|
|
const s = handler.builders.query(this.serialize(handler));
|
|
|
|
return db.query(new Query(s));
|
2023-01-23 21:19:18 +01:00
|
|
|
}
|
|
|
|
}
|