101 lines
No EOL
2.7 KiB
TypeScript
101 lines
No EOL
2.7 KiB
TypeScript
import { Attribute, DB } from "./db";
|
|
import { BooleanModifier, Modifier } from "./dbStructure";
|
|
import { Handler } from "./defaultHandler";
|
|
import { primaryData, selectElements, selectFromElements, serializeReturn } from "./types";
|
|
|
|
|
|
export class Query {
|
|
sql: string;
|
|
values: primaryData[];
|
|
constructor([sql, values]: serializeReturn) {
|
|
this.sql = sql;
|
|
this.values = values;
|
|
}
|
|
}
|
|
|
|
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]);
|
|
}
|
|
addCodeCommaSeperated(data: string[], comma = ", ") {
|
|
for (let i = 0; i < data.length; i++) {
|
|
const e = data[i];
|
|
this.list.push([false, e]);
|
|
if (i + 1 < data.length) this.list.push([false, comma]);
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
appendEnding(qb: QueryBuilder) {
|
|
this.append(qb);
|
|
this.list.push([false, ";"]);
|
|
}
|
|
isEmpty() {
|
|
return this.list.length == 0;
|
|
}
|
|
/*setHandler(fun:(d:any)=>any){
|
|
|
|
}*/
|
|
}
|
|
|
|
|
|
export class selectQuery {
|
|
attr: selectElements[] = [];
|
|
from: selectFromElements;
|
|
constructor(a: selectElements[], from: selectFromElements) {
|
|
this.attr.push(...a.flat(Infinity));
|
|
this.from = from ? from : null;
|
|
}
|
|
whereD: BooleanModifier | null = null;
|
|
where(m: BooleanModifier) {
|
|
this.whereD = m;
|
|
return this;
|
|
}
|
|
groupByD: Attribute[] = [];
|
|
groupBy(a: Attribute[]) {
|
|
this.groupByD = a;
|
|
return this;
|
|
}
|
|
havingD: Modifier | null = null;
|
|
having(m: Modifier) {
|
|
this.havingD = m;
|
|
return this;
|
|
}
|
|
limitD: number | null = null;
|
|
limit(i: number) {
|
|
this.limitD = i;
|
|
return this;
|
|
}
|
|
|
|
serialize(handler: Handler): QueryBuilder {
|
|
return handler.querys.select(handler, this);
|
|
}
|
|
|
|
async query(db: DB) {
|
|
const handler = db.getHandler();
|
|
const builder = this.serialize(handler);
|
|
const s = handler.builders.query(builder);
|
|
return await db.query(s);
|
|
}
|
|
} |