dblang/src/dbStructure.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-01-29 22:11:24 +01:00
import { Attribute, Table } from "./db";
2023-01-23 21:19:18 +01:00
import { Handler } from "./defaultHandler";
2023-01-29 22:11:24 +01:00
import { QueryBuilder } from "./query";
2023-01-23 21:19:18 +01:00
import { allModifierInput, primaryData, serializeReturn } from "./types";
2023-01-24 10:40:16 +01:00
export class Datatype{
type: string;
args: primaryData[];
constructor(type:string, args: primaryData[]){
this.type = type;
this.args = args;
}
2023-01-29 22:11:24 +01:00
serialize(handler : Handler): QueryBuilder{
2023-01-24 10:40:16 +01:00
return handler.datatypes[this.type as keyof typeof handler.datatypes](this.args);
}
}
2023-01-23 21:19:18 +01:00
export abstract class Modifier {
2023-01-23 22:10:33 +01:00
t: string;
a: Array<allModifierInput>;
2023-01-23 21:19:18 +01:00
constructor(type: string, args: (allModifierInput)[]) {
this.t = type;
this.a = args;
}
2023-01-29 22:11:24 +01:00
serialize(handler : Handler): QueryBuilder {
2023-01-24 20:34:03 +01:00
return handler.modifiers[this.t as keyof typeof handler.modifiers](handler,this.a);
2023-01-23 21:19:18 +01:00
}
}
2023-01-23 22:10:33 +01:00
export class BooleanModifier extends Modifier { }
export class NumberModifier extends Modifier { }
export class StringModifier extends Modifier { }
2023-01-23 21:19:18 +01:00
2023-01-23 22:10:33 +01:00
export class Aggregation {
t: string;
a: Attribute;
2023-01-23 21:19:18 +01:00
constructor(type: string, args: Attribute) {
this.t = type;
this.a = args;
}
2023-01-29 22:11:24 +01:00
serialize(handler : Handler): QueryBuilder {
2023-01-24 20:34:03 +01:00
return handler.aggregations[this.t as keyof typeof handler.aggregations](handler,this.a);
2023-01-23 21:19:18 +01:00
}
}
2023-01-23 22:10:33 +01:00
export class Joins {
2023-01-23 21:19:18 +01:00
2023-01-29 22:11:24 +01:00
serialize(handler : Handler): QueryBuilder {
return new QueryBuilder();
2023-01-23 21:19:18 +01:00
}
2023-01-29 22:11:24 +01:00
}
export interface Constraint{
name:string;
serialize( handler: Handler) : QueryBuilder;
uses(attr:Attribute): boolean;
check(attr:Table): boolean | string;
}
export class checkConstraint implements Constraint{
checkQuery:BooleanModifier;
name: string;
constructor(name:string, check: BooleanModifier){
this.name = name;
this.checkQuery = check;
}
check(attr: Table): boolean {
2023-02-13 23:44:08 +01:00
return true;
2023-01-29 22:11:24 +01:00
}
uses(attr: Attribute): boolean {
throw new Error("Method not implemented.");
}
serialize(handler: Handler): QueryBuilder {
throw new Error("Method not implemented.");
}
}
export class uniqueConstraint implements Constraint{
name: string;
2023-02-13 23:44:08 +01:00
attrs: Attribute[];
constructor(name:string, attrs:Attribute[]){
2023-01-29 22:11:24 +01:00
this.name = name;
2023-02-13 23:44:08 +01:00
this.attrs = attrs;
2023-01-29 22:11:24 +01:00
}
check(table: Table): boolean|string {
2023-02-13 23:44:08 +01:00
for(let i = 0; i < this.attrs.length; i++){
if(this.attrs[i].ops.primaryKey) return "Can not combine unique Constraint and primary key";
2023-01-29 22:11:24 +01:00
}
return false;
}
uses(attr: Attribute): boolean {
2023-02-13 23:44:08 +01:00
return this.attrs.includes(attr);
2023-01-29 22:11:24 +01:00
}
serialize(handler: Handler): QueryBuilder {
throw new Error("Method not implemented.");
}
2023-01-23 21:19:18 +01:00
}