dblang/src/query.ts

191 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-02-18 14:47:44 +01:00
import { AttributeAlias } from "./alias";
2023-02-14 22:23:21 +01:00
import { Attribute, DB, Table } from "./db";
2023-01-23 21:19:18 +01:00
import { BooleanModifier, Modifier } from "./dbStructure";
import { Handler } from "./defaultHandler";
2023-02-14 22:23:21 +01:00
import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types";
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-18 14:47:44 +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-14 22:23:21 +01:00
async query(db: DB) {
const handler = db.getHandler();
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
return await db.query(s);
}
}
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);
}
async query(db: DB) {
const handler = db.getHandler();
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
return await db.query(s);
}
}
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);
}
async query(db: DB) {
const handler = db.getHandler();
const builder = this.serialize(handler);
const s = handler.builders.query(builder);
return await db.query(s);
}
}
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-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
}
}