This commit is contained in:
parent
71f6fbf117
commit
e33dbcdfca
7 changed files with 72 additions and 60 deletions
|
@ -1 +1,4 @@
|
||||||
|
# DBlang
|
||||||
|
sql Querys with js ot ts Syntax.
|
||||||
|
|
||||||
[![status-badge](https://ci.jusax.de/api/badges/jusax23/dblang/status.svg)](https://ci.jusax.de/jusax23/dblang)
|
[![status-badge](https://ci.jusax.de/api/badges/jusax23/dblang/status.svg)](https://ci.jusax.de/jusax23/dblang)
|
36
src/db.ts
36
src/db.ts
|
@ -16,39 +16,39 @@ export class DB {
|
||||||
return Handler;
|
return Handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
newTable(name:string){
|
newTable(name: string) {
|
||||||
return new Table(name);
|
return new Table(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Attribute {
|
export class Attribute {
|
||||||
name : string;
|
name: string;
|
||||||
constructor(name: string){
|
constructor(name: string) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
serialize(){
|
serialize() {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
toString(){
|
toString() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class Table{
|
export class Table {
|
||||||
dbLangTableName : string;
|
dbLangTableName: string;
|
||||||
dbLangTableAttributes:{ [key: string]: Attribute; } = {};
|
dbLangTableAttributes: { [key: string]: Attribute; } = {};
|
||||||
[key:string]: Attribute | any
|
[key: string]: Attribute | any
|
||||||
constructor(name: string){
|
constructor(name: string) {
|
||||||
this.dbLangTableName = name;
|
this.dbLangTableName = name;
|
||||||
}
|
}
|
||||||
serialize(){
|
serialize() {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
toString(){
|
toString() {
|
||||||
return this.dbLangTableName;
|
return this.dbLangTableName;
|
||||||
}
|
}
|
||||||
addAttribute(name:string,ops:{
|
addAttribute(name: string, ops: {
|
||||||
unique?: boolean,
|
unique?: boolean,
|
||||||
A_I?: boolean,
|
A_I?: boolean,
|
||||||
default?: primaryData,
|
default?: primaryData,
|
||||||
|
@ -59,12 +59,12 @@ export class Table{
|
||||||
onDelete?: onAction,
|
onDelete?: onAction,
|
||||||
onUpdate?: onAction
|
onUpdate?: onAction
|
||||||
}
|
}
|
||||||
},noErrorOnNameConflict = false){
|
}, noErrorOnNameConflict = false) {
|
||||||
let attr = new Attribute(name);
|
let attr = new Attribute(name);
|
||||||
this.dbLangTableAttributes[name] = attr;
|
this.dbLangTableAttributes[name] = attr;
|
||||||
if(["serialize", "toString","addAttribute","dbLangTableName","dbLangTableAttributes"].includes(name)){
|
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes"].includes(name)) {
|
||||||
if(!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
|
if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
|
||||||
}else{
|
} else {
|
||||||
this[name] = attr;
|
this[name] = attr;
|
||||||
}
|
}
|
||||||
return attr;
|
return attr;
|
||||||
|
@ -72,4 +72,4 @@ export class Table{
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from './funcs';
|
export * from './funcs';
|
||||||
export {onAction};
|
export { onAction };
|
|
@ -5,35 +5,35 @@ import { allModifierInput, primaryData, serializeReturn } from "./types";
|
||||||
|
|
||||||
|
|
||||||
export abstract class Modifier {
|
export abstract class Modifier {
|
||||||
t : string;
|
t: string;
|
||||||
a : Array<allModifierInput>;
|
a: Array<allModifierInput>;
|
||||||
constructor(type: string, args: (allModifierInput)[]) {
|
constructor(type: string, args: (allModifierInput)[]) {
|
||||||
this.t = type;
|
this.t = type;
|
||||||
this.a = args;
|
this.a = args;
|
||||||
}
|
}
|
||||||
serialize(handler = Handler):serializeReturn {
|
serialize(handler = Handler): serializeReturn {
|
||||||
return handler.modifiers[this.t as keyof typeof handler.modifiers](this.a);
|
return handler.modifiers[this.t as keyof typeof handler.modifiers](this.a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class BooleanModifier extends Modifier{}
|
export class BooleanModifier extends Modifier { }
|
||||||
export class NumberModifier extends Modifier{}
|
export class NumberModifier extends Modifier { }
|
||||||
export class StringModifier extends Modifier{}
|
export class StringModifier extends Modifier { }
|
||||||
|
|
||||||
export class Aggregation{
|
export class Aggregation {
|
||||||
t : string;
|
t: string;
|
||||||
a : Attribute;
|
a: Attribute;
|
||||||
constructor(type: string, args: Attribute) {
|
constructor(type: string, args: Attribute) {
|
||||||
this.t = type;
|
this.t = type;
|
||||||
this.a = args;
|
this.a = args;
|
||||||
}
|
}
|
||||||
serialize(handler = Handler):serializeReturn{
|
serialize(handler = Handler): serializeReturn {
|
||||||
return handler.aggregations[this.t as keyof typeof handler.aggregations](this.a);
|
return handler.aggregations[this.t as keyof typeof handler.aggregations](this.a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class Joins{
|
export class Joins {
|
||||||
|
|
||||||
serialize(handler = Handler):serializeReturn {
|
serialize(handler = Handler): serializeReturn {
|
||||||
return ["",[]];
|
return ["", []];
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,18 +13,22 @@ export class Handler {
|
||||||
if (q.whereD) {
|
if (q.whereD) {
|
||||||
let whereS = q.whereD.serialize(this);
|
let whereS = q.whereD.serialize(this);
|
||||||
args.push(...whereS[1]);
|
args.push(...whereS[1]);
|
||||||
sql += " where "+whereS[0];
|
sql += " where " + whereS[0];
|
||||||
}
|
}
|
||||||
if (q.groupByD.length>0) {
|
if (q.groupByD.length > 0) {
|
||||||
let groupByS = joinArg(",", this)(q.groupByD);
|
let groupByS = joinArg(",", this)(q.groupByD);
|
||||||
args.push(...groupByS[1]);
|
args.push(...groupByS[1]);
|
||||||
sql += " group by "+groupByS[0];
|
sql += " group by " + groupByS[0];
|
||||||
if (q.havingD) {
|
if (q.havingD) {
|
||||||
let havingS = q.havingD.serialize(this);
|
let havingS = q.havingD.serialize(this);
|
||||||
args.push(...havingS[1]);
|
args.push(...havingS[1]);
|
||||||
sql += " having "+havingS[0];
|
sql += " having " + havingS[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (q.limitD != null) {
|
||||||
|
sql += " limit ?";
|
||||||
|
args.push(q.limitD);
|
||||||
|
}
|
||||||
|
|
||||||
return [sql, args];
|
return [sql, args];
|
||||||
}
|
}
|
||||||
|
@ -44,7 +48,7 @@ export class Handler {
|
||||||
eq: joinArg("=", this),
|
eq: joinArg("=", this),
|
||||||
plus: joinArg("+", this),
|
plus: joinArg("+", this),
|
||||||
minus: joinArg("-", this),
|
minus: joinArg("-", this),
|
||||||
not: (a: allModifierInput[]):serializeReturn => {
|
not: (a: allModifierInput[]): serializeReturn => {
|
||||||
let e = a[0];
|
let e = a[0];
|
||||||
if (e instanceof Attribute) return ["not (" + e + ")", []];
|
if (e instanceof Attribute) return ["not (" + e + ")", []];
|
||||||
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
|
@ -60,16 +64,16 @@ export class Handler {
|
||||||
function joinArg(type: string, s: any) {
|
function joinArg(type: string, s: any) {
|
||||||
return (a: (allModifierInput)[]): serializeReturn => {
|
return (a: (allModifierInput)[]): serializeReturn => {
|
||||||
let args: primaryData[] = [];
|
let args: primaryData[] = [];
|
||||||
let sql = "(" + a.map(d => {
|
let sql = a.map(d => {
|
||||||
if (d instanceof Attribute) return d;
|
if (d instanceof Attribute) return d;
|
||||||
if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
||||||
let [sqli, argsi] = d.serialize(s);
|
let [sqli, argsi] = d.serialize(s);
|
||||||
args.push(...(argsi.flat(Infinity)));
|
args.push(...(argsi.flat(Infinity)));
|
||||||
return sqli;
|
return "("+sqli+")";
|
||||||
}
|
}
|
||||||
args.push(d);
|
args.push(d);
|
||||||
return "?";
|
return "?";
|
||||||
}).join(" " + type + " ") + ")";
|
}).join(" " + type + " ");
|
||||||
return [sql, args]
|
return [sql, args]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
22
src/funcs.ts
22
src/funcs.ts
|
@ -4,19 +4,19 @@ import { selectQuery } from "./query";
|
||||||
import { allModifierInput, selectElements, selectFromElements } from "./types";
|
import { allModifierInput, selectElements, selectFromElements } from "./types";
|
||||||
|
|
||||||
//modifiers
|
//modifiers
|
||||||
export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and",args);
|
export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and", args);
|
||||||
export const or = (...args: (BooleanModifier)[]) => new BooleanModifier("or",args);
|
export const or = (...args: (BooleanModifier)[]) => new BooleanModifier("or", args);
|
||||||
export const eq = (...args: (allModifierInput)[]) => new BooleanModifier("eq",args);
|
export const eq = (...args: (allModifierInput)[]) => new BooleanModifier("eq", args);
|
||||||
export const plus = (...args: (allModifierInput)[]) => new NumberModifier("plus",args);
|
export const plus = (...args: (allModifierInput)[]) => new NumberModifier("plus", args);
|
||||||
export const minus = (...args: (allModifierInput)[]) => new NumberModifier("minus",args);
|
export const minus = (...args: (allModifierInput)[]) => new NumberModifier("minus", args);
|
||||||
|
|
||||||
//aggregations
|
//aggregations
|
||||||
|
|
||||||
export const count = (a:Attribute) => new Aggregation("count",a);
|
export const count = (a: Attribute) => new Aggregation("count", a);
|
||||||
export const sum = (a:Attribute) => new Aggregation("sum",a);
|
export const sum = (a: Attribute) => new Aggregation("sum", a);
|
||||||
export const avg = (a:Attribute) => new Aggregation("avg",a);
|
export const avg = (a: Attribute) => new Aggregation("avg", a);
|
||||||
export const min = (a:Attribute) => new Aggregation("min",a);
|
export const min = (a: Attribute) => new Aggregation("min", a);
|
||||||
export const max = (a:Attribute) => new Aggregation("max",a);
|
export const max = (a: Attribute) => new Aggregation("max", a);
|
||||||
|
|
||||||
//query
|
//query
|
||||||
export const select = (args: selectElements[],from: selectFromElements) => new selectQuery(args,from);
|
export const select = (args: selectElements[], from: selectFromElements) => new selectQuery(args, from);
|
||||||
|
|
23
src/query.ts
23
src/query.ts
|
@ -5,8 +5,8 @@ import { primaryData, selectElements, selectFromElements, serializeReturn } from
|
||||||
|
|
||||||
|
|
||||||
export class Query {
|
export class Query {
|
||||||
sql : string;
|
sql: string;
|
||||||
values : primaryData[];
|
values: primaryData[];
|
||||||
constructor(sql: string, values: primaryData[]) {
|
constructor(sql: string, values: primaryData[]) {
|
||||||
this.sql = sql;
|
this.sql = sql;
|
||||||
this.values = values;
|
this.values = values;
|
||||||
|
@ -14,13 +14,13 @@ export class Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class selectQuery {
|
export class selectQuery {
|
||||||
attr:selectElements[] = [];
|
attr: selectElements[] = [];
|
||||||
from:selectFromElements;
|
from: selectFromElements;
|
||||||
constructor(a: selectElements[], from:selectFromElements) {
|
constructor(a: selectElements[], from: selectFromElements) {
|
||||||
this.attr.push(...a.flat(Infinity));
|
this.attr.push(...a.flat(Infinity));
|
||||||
this.from = from ? from : null;
|
this.from = from ? from : null;
|
||||||
}
|
}
|
||||||
whereD:BooleanModifier | null = null;
|
whereD: BooleanModifier | null = null;
|
||||||
where(m: BooleanModifier) {
|
where(m: BooleanModifier) {
|
||||||
this.whereD = m;
|
this.whereD = m;
|
||||||
return this;
|
return this;
|
||||||
|
@ -30,18 +30,23 @@ export class selectQuery {
|
||||||
this.groupByD = a;
|
this.groupByD = a;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
havingD:Modifier | null = null;
|
havingD: Modifier | null = null;
|
||||||
having(m: Modifier) {
|
having(m: Modifier) {
|
||||||
this.havingD = m;
|
this.havingD = m;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
limitD: number | null = null;
|
||||||
|
limit(i: number) {
|
||||||
|
this.limitD = i;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
serialize(handler = Handler) : serializeReturn {
|
serialize(handler = Handler): serializeReturn {
|
||||||
return handler.querys.select(this);
|
return handler.querys.select(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
query(db: DB) {
|
query(db: DB) {
|
||||||
const s = this.serialize(db.getHandler());
|
const s = this.serialize(db.getHandler());
|
||||||
return new Query(s[0],s[1]);
|
return new Query(s[0], s[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,7 +11,7 @@ export type selectFromElements = Table | Joins | null;
|
||||||
export type serializeReturn = [string, primaryData[]];
|
export type serializeReturn = [string, primaryData[]];
|
||||||
|
|
||||||
|
|
||||||
export enum onAction{
|
export enum onAction {
|
||||||
cascade,
|
cascade,
|
||||||
noAction,
|
noAction,
|
||||||
setNull,
|
setNull,
|
||||||
|
|
Loading…
Reference in a new issue