dblang/src/query.ts

195 lines
6 KiB
TypeScript
Raw Normal View History

2023-02-27 16:33:27 +01:00
import { AttributeAlias } from "./alias.js";
import { Attribute, DB, Table } from "./db.js";
import { BooleanModifier, Modifier } from "./dbStructure.js";
import { Handler } from "./defaultHandler.js";
import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js";
2023-01-23 21:19:18 +01:00
export class Query {
2023-01-23 22:10:33 +01:00
sql: string;
values: primaryData[];
2023-02-14 16:45:21 +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-14 16:45:21 +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]);
2023-02-14 16:45:21 +01:00
if (i + 1 < data.length) this.list.push([false, comma]);
2023-02-13 23:44:08 +01:00
}
}
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]);
2023-02-14 16:45:21 +01:00
if (i + 1 < data.length) this.list.push([false, comma]);
2023-01-29 22:11:24 +01:00
}
}
append(qb: QueryBuilder) {
this.list.push(...qb.list);
}
2023-02-14 16:45:21 +01:00
appendEnding(qb: QueryBuilder) {
2023-02-22 11:16:36 +01:00
if (qb.isEmpty()) return;
2023-02-13 23:44:08 +01:00
this.append(qb);
2023-02-14 16:45:21 +01:00
this.list.push([false, ";"]);
2023-02-13 23:44:08 +01:00
}
2023-02-14 16:45:21 +01:00
isEmpty() {
2023-02-13 23:44:08 +01:00
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;
}
2023-02-18 14:47:44 +01:00
groupByD: (Attribute | AttributeAlias)[] = [];
groupBy(a: (Attribute | AttributeAlias)[]) {
2023-01-23 21:19:18 +01:00
this.groupByD = a;
return this;
}
2023-02-14 23:29:16 +01:00
havingD: BooleanModifier | null = null;
having(m: BooleanModifier) {
2023-01-23 21:19:18 +01:00
this.havingD = m;
return this;
}
2023-01-23 22:10:33 +01:00
limitD: number | null = null;
2023-02-15 16:22:40 +01:00
limitOffsetD: number | null = null;
limit(i: number, offset: number = 0) {
2023-01-23 22:10:33 +01:00
this.limitD = i;
2023-02-15 16:22:40 +01:00
this.limitOffsetD = offset;
2023-01-23 22:10:33 +01:00
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-22 11:16:36 +01:00
async query(db: DB, printQuery = false) {
2023-02-14 22:23:21 +01:00
const handler = db.getHandler();
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
2023-02-22 11:16:36 +01:00
let readResp = await db.query(s, printQuery);
2023-02-22 11:13:43 +01:00
return db.getHandler().responses.readResponse(readResp);
2023-02-14 22:23:21 +01:00
}
}
export class insertQuery {
attrs: Attribute[];
values: primaryData[][] = [];
select: selectQuery | null = null;
constructor(attrs: Attribute[]) {
if (attrs.length == 0) throw new Error("Insertion must be done in at least one Column.");
for (let i = 0; i < attrs.length; i++) {
if (attrs[i].table != attrs[0].table) throw new Error("Insertion Columns must be in one Table.");
}
this.attrs = attrs;
}
add(...data: primaryData[]) {
if (this.select != null) throw new Error("Can not add Values when using select!");
this.values.push(data);
return this;
}
addValues(...data: primaryData[][]) {
if (this.select != null) throw new Error("Can not add Values when using select!");
this.values.push(...data);
return this;
}
setSelect(state: selectQuery) {
if (this.values.length != 0) throw new Error("Can not add select when using values!");
this.select = state;
return this;
}
serialize(handler: Handler): QueryBuilder {
return handler.querys.insert(handler, this);
}
2023-02-22 11:16:36 +01:00
async query(db: DB, printQuery = false) {
2023-02-14 22:23:21 +01:00
const handler = db.getHandler();
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
2023-02-22 11:16:36 +01:00
let readResp = await db.query(s, printQuery);
2023-02-22 11:13:43 +01:00
return db.getHandler().responses.insertResponse(readResp);
2023-02-14 22:23:21 +01:00
}
}
export class updateQuery {
table: Table;
setD: ([Attribute, allModifierInput])[] = [];
whereD: BooleanModifier | null = null;
constructor(table: Table) {
this.table = table;
}
set(attr: Attribute, value: allModifierInput) {
if (this.table != attr.table) throw new Error("Can only edit columns of the updated table!");
this.setD.push([attr, value]);
return this;
}
where(w: BooleanModifier) {
this.whereD = w;
return this;
}
serialize(handler: Handler): QueryBuilder {
return handler.querys.update(handler, this);
}
2023-02-22 11:16:36 +01:00
async query(db: DB, printQuery = false) {
2023-02-14 22:23:21 +01:00
const handler = db.getHandler();
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
2023-02-22 11:16:36 +01:00
let readResp = await db.query(s, printQuery);
2023-02-22 11:13:43 +01:00
return db.getHandler().responses.writeResponse(readResp);
2023-02-14 22:23:21 +01:00
}
}
export class removeQuery {
table: Table;
whereD: BooleanModifier | null = null;
constructor(table: Table) {
this.table = table;
}
where(w: BooleanModifier) {
this.whereD = w;
return this;
}
serialize(handler: Handler): QueryBuilder {
return handler.querys.remove(handler, this);
}
2023-02-22 11:16:36 +01:00
async query(db: DB, printQuery = false) {
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);
2023-02-22 11:16:36 +01:00
let readResp = await db.query(s, printQuery);
2023-02-22 11:13:43 +01:00
return db.getHandler().responses.writeResponse(readResp);
2023-01-23 21:19:18 +01:00
}
}