dblang/src/query.ts

52 lines
1.3 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-23 21:19:18 +01:00
constructor(sql: string, values: primaryData[]) {
this.sql = sql;
this.values = values;
}
}
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-23 22:10:33 +01:00
serialize(handler = Handler): serializeReturn {
2023-01-23 21:19:18 +01:00
return handler.querys.select(this);
}
query(db: DB) {
const s = this.serialize(db.getHandler());
2023-01-23 22:10:33 +01:00
return new Query(s[0], s[1]);
2023-01-23 21:19:18 +01:00
}
}