dblang/src/dbStructure.ts

171 lines
5.1 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-02-15 13:58:19 +01:00
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types";
2023-01-23 21:19:18 +01:00
2023-02-14 16:45:21 +01:00
export class Datatype {
2023-01-24 10:40:16 +01:00
type: string;
args: primaryData[];
2023-02-14 16:45:21 +01:00
constructor(type: string, args: primaryData[]) {
2023-01-24 10:40:16 +01:00
this.type = type;
this.args = args;
}
2023-02-14 16:45:21 +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-02-14 16:45:21 +01:00
serialize(handler: Handler): QueryBuilder {
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-02-14 16:45:21 +01:00
serialize(handler: Handler): QueryBuilder {
return handler.aggregations[this.t as keyof typeof handler.aggregations](handler, this.a);
2023-01-23 21:19:18 +01:00
}
}
2023-02-15 13:58:19 +01:00
export interface Joins {
serialize(handler: Handler): QueryBuilder
}
export class joinNatural implements Joins{
tables: joinElements[];
type: joinType;
constructor(tables:joinElements[], type: joinType){
this.tables = tables;
this.type = type;
}
serialize(handler: Handler): QueryBuilder {
return handler.joins.natural(handler,this);
}
}
export class onJoin implements Joins{
tableA: joinElements;
tableB: joinElements;
type: joinType;
on: BooleanModifier;
constructor(tableA: joinElements, tableB: joinElements, type: joinType, on: BooleanModifier){
this.tableA = tableA;
this.tableB = tableB;
this.type = type;
this.on = on;
}
serialize(handler: Handler): QueryBuilder {
return handler.joins.on(handler,this);
}
}
export class usingJoin implements Joins{
tableA: joinElements;
tableB: joinElements;
type: joinType;
using: Attribute[];
constructor(tableA: joinElements, tableB: joinElements, type: joinType, using: Attribute[]){
this.tableA = tableA;
this.tableB = tableB;
this.type = type;
this.using = using;
}
serialize(handler: Handler): QueryBuilder {
return handler.joins.using(handler,this);
}
}
export class joinCross implements Joins{
tables: joinElements[];
constructor(tables:joinElements[]){
this.tables = tables;
}
2023-02-14 16:45:21 +01:00
serialize(handler: Handler): QueryBuilder {
2023-02-15 13:58:19 +01:00
return handler.joins.cross(handler,this);
2023-01-23 21:19:18 +01:00
}
2023-02-15 13:58:19 +01:00
2023-01-29 22:11:24 +01:00
}
2023-02-14 16:45:21 +01:00
export interface Constraint {
name: string;
uses(attr: Attribute): boolean;
check(table: Table): boolean | string;
2023-01-29 22:11:24 +01:00
}
2023-02-14 16:45:21 +01:00
export class checkConstraint implements Constraint {
checkQuery: BooleanModifier;
2023-01-29 22:11:24 +01:00
name: string;
2023-02-14 16:45:21 +01:00
constructor(name: string, check: BooleanModifier) {
this.name = name.toLowerCase();
2023-01-29 22:11:24 +01:00
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.");
}
}
2023-02-14 16:45:21 +01:00
export class uniqueConstraint implements Constraint {
2023-01-29 22:11:24 +01:00
name: string;
2023-02-13 23:44:08 +01:00
attrs: Attribute[];
2023-02-14 16:45:21 +01:00
constructor(name: string, attrs: Attribute[]) {
this.name = name.toLowerCase();
2023-02-13 23:44:08 +01:00
this.attrs = attrs;
2023-01-29 22:11:24 +01:00
}
2023-02-14 16:45:21 +01:00
check(table: Table): boolean | string {
for (let i = 0; i < this.attrs.length; i++) {
if (this.attrs[i].ops.primaryKey) return "Can not combine unique Constraint and primary key";
if (this.attrs[i].table != table) return "Referencing Attributes must be in host Table.";
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
}
2023-02-14 16:45:21 +01:00
}
export class foreignConstraint implements Constraint {
name: string;
fromAttrs: Attribute[];
toAttrs: Attribute[];
2023-02-14 17:37:01 +01:00
onUpdate: onAction;
onDelete: onAction;
constructor(name: string, from: Attribute[], to: Attribute[], onDelete: onAction = onAction.nothing, onUpdate: onAction = onAction.nothing) {
2023-02-14 16:45:21 +01:00
this.name = name.toLowerCase();
this.fromAttrs = from;
this.toAttrs = to;
2023-02-14 17:37:01 +01:00
this.onUpdate = onUpdate;
this.onDelete = onDelete;
2023-02-14 16:45:21 +01:00
}
uses(attr: Attribute): boolean {
throw new Error("Method not implemented.");
}
check(t: Table): string | boolean {
let table = this.toAttrs[0].table;
for (let i = 0; i < this.toAttrs.length; i++) {
if (table != this.toAttrs[i].table) return "Referenced Attributes must be in one Table.";
if (this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
}
for (let i = 0; i < this.fromAttrs.length; i++) {
if (this.fromAttrs[i].table != t) return "Referencing Attributes must be in host Table.";
}
return false;
}
2023-01-23 21:19:18 +01:00
}