dblang/src/query.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

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])[] = [];
2023-02-13 23:44:08 +01:00
2023-01-29 22:11:24 +01:00
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]);
}
2023-02-13 23:44:08 +01:00
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]);
}
}
2023-01-29 22:11:24 +01:00
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-02-13 23:44:08 +01:00
appendEnding(qb: QueryBuilder){
this.append(qb);
this.list.push([false,";"]);
}
isEmpty(){
return this.list.length == 0;
}
/*setHandler(fun:(d:any)=>any){
}*/
2023-01-29 22:11:24 +01:00
}
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
}
2023-02-13 23:44:08 +01:00
async query(db: DB) {
2023-01-29 22:11:24 +01:00
const handler = db.getHandler();
2023-02-13 23:44:08 +01:00
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
return await db.query(s);
2023-01-23 21:19:18 +01:00
}
}