diff --git a/readme.md b/readme.md index 72fa516..197a59e 100644 --- a/readme.md +++ b/readme.md @@ -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) \ No newline at end of file diff --git a/src/db.ts b/src/db.ts index 1266a2a..d954eb5 100644 --- a/src/db.ts +++ b/src/db.ts @@ -16,39 +16,39 @@ export class DB { return Handler; } - newTable(name:string){ + newTable(name: string) { return new Table(name); } } export class Attribute { - name : string; - constructor(name: string){ + name: string; + constructor(name: string) { this.name = name; } - serialize(){ + serialize() { return this.toString(); } - toString(){ + toString() { return this.name; } } -export class Table{ - dbLangTableName : string; - dbLangTableAttributes:{ [key: string]: Attribute; } = {}; - [key:string]: Attribute | any - constructor(name: string){ +export class Table { + dbLangTableName: string; + dbLangTableAttributes: { [key: string]: Attribute; } = {}; + [key: string]: Attribute | any + constructor(name: string) { this.dbLangTableName = name; } - serialize(){ + serialize() { return this.toString(); } - toString(){ + toString() { return this.dbLangTableName; } - addAttribute(name:string,ops:{ + addAttribute(name: string, ops: { unique?: boolean, A_I?: boolean, default?: primaryData, @@ -59,12 +59,12 @@ export class Table{ onDelete?: onAction, onUpdate?: onAction } - },noErrorOnNameConflict = false){ + }, noErrorOnNameConflict = false) { let attr = new Attribute(name); this.dbLangTableAttributes[name] = attr; - if(["serialize", "toString","addAttribute","dbLangTableName","dbLangTableAttributes"].includes(name)){ - if(!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!"); - }else{ + if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes"].includes(name)) { + if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!"); + } else { this[name] = attr; } return attr; @@ -72,4 +72,4 @@ export class Table{ } export * from './funcs'; -export {onAction}; \ No newline at end of file +export { onAction }; \ No newline at end of file diff --git a/src/dbStructure.ts b/src/dbStructure.ts index 186b08e..ac3f88b 100644 --- a/src/dbStructure.ts +++ b/src/dbStructure.ts @@ -5,35 +5,35 @@ import { allModifierInput, primaryData, serializeReturn } from "./types"; export abstract class Modifier { - t : string; - a : Array; + t: string; + a: Array; constructor(type: string, args: (allModifierInput)[]) { this.t = type; this.a = args; } - serialize(handler = Handler):serializeReturn { + serialize(handler = Handler): serializeReturn { return handler.modifiers[this.t as keyof typeof handler.modifiers](this.a); } } -export class BooleanModifier extends Modifier{} -export class NumberModifier extends Modifier{} -export class StringModifier extends Modifier{} +export class BooleanModifier extends Modifier { } +export class NumberModifier extends Modifier { } +export class StringModifier extends Modifier { } -export class Aggregation{ - t : string; - a : Attribute; +export class Aggregation { + t: string; + a: Attribute; constructor(type: string, args: Attribute) { this.t = type; this.a = args; } - serialize(handler = Handler):serializeReturn{ + serialize(handler = Handler): serializeReturn { return handler.aggregations[this.t as keyof typeof handler.aggregations](this.a); } } -export class Joins{ +export class Joins { - serialize(handler = Handler):serializeReturn { - return ["",[]]; + serialize(handler = Handler): serializeReturn { + return ["", []]; } } \ No newline at end of file diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 9299829..11c3ff2 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -13,18 +13,22 @@ export class Handler { if (q.whereD) { let whereS = q.whereD.serialize(this); 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); args.push(...groupByS[1]); - sql += " group by "+groupByS[0]; + sql += " group by " + groupByS[0]; if (q.havingD) { let havingS = q.havingD.serialize(this); 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]; } @@ -44,7 +48,7 @@ export class Handler { eq: joinArg("=", this), plus: joinArg("+", this), minus: joinArg("-", this), - not: (a: allModifierInput[]):serializeReturn => { + not: (a: allModifierInput[]): serializeReturn => { let e = a[0]; if (e instanceof Attribute) return ["not (" + e + ")", []]; if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) { @@ -60,16 +64,16 @@ export class Handler { function joinArg(type: string, s: any) { return (a: (allModifierInput)[]): serializeReturn => { let args: primaryData[] = []; - let sql = "(" + a.map(d => { + let sql = a.map(d => { if (d instanceof Attribute) return d; if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) { let [sqli, argsi] = d.serialize(s); args.push(...(argsi.flat(Infinity))); - return sqli; + return "("+sqli+")"; } args.push(d); return "?"; - }).join(" " + type + " ") + ")"; + }).join(" " + type + " "); return [sql, args] } diff --git a/src/funcs.ts b/src/funcs.ts index 700f537..bb61e1a 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -4,19 +4,19 @@ import { selectQuery } from "./query"; import { allModifierInput, selectElements, selectFromElements } from "./types"; //modifiers -export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and",args); -export const or = (...args: (BooleanModifier)[]) => new BooleanModifier("or",args); -export const eq = (...args: (allModifierInput)[]) => new BooleanModifier("eq",args); -export const plus = (...args: (allModifierInput)[]) => new NumberModifier("plus",args); -export const minus = (...args: (allModifierInput)[]) => new NumberModifier("minus",args); +export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and", args); +export const or = (...args: (BooleanModifier)[]) => new BooleanModifier("or", args); +export const eq = (...args: (allModifierInput)[]) => new BooleanModifier("eq", args); +export const plus = (...args: (allModifierInput)[]) => new NumberModifier("plus", args); +export const minus = (...args: (allModifierInput)[]) => new NumberModifier("minus", args); //aggregations -export const count = (a:Attribute) => new Aggregation("count",a); -export const sum = (a:Attribute) => new Aggregation("sum",a); -export const avg = (a:Attribute) => new Aggregation("avg",a); -export const min = (a:Attribute) => new Aggregation("min",a); -export const max = (a:Attribute) => new Aggregation("max",a); +export const count = (a: Attribute) => new Aggregation("count", a); +export const sum = (a: Attribute) => new Aggregation("sum", a); +export const avg = (a: Attribute) => new Aggregation("avg", a); +export const min = (a: Attribute) => new Aggregation("min", a); +export const max = (a: Attribute) => new Aggregation("max", a); //query -export const select = (args: selectElements[],from: selectFromElements) => new selectQuery(args,from); +export const select = (args: selectElements[], from: selectFromElements) => new selectQuery(args, from); diff --git a/src/query.ts b/src/query.ts index 10e6252..b0f9c86 100644 --- a/src/query.ts +++ b/src/query.ts @@ -5,8 +5,8 @@ import { primaryData, selectElements, selectFromElements, serializeReturn } from export class Query { - sql : string; - values : primaryData[]; + sql: string; + values: primaryData[]; constructor(sql: string, values: primaryData[]) { this.sql = sql; this.values = values; @@ -14,13 +14,13 @@ export class Query { } export class selectQuery { - attr:selectElements[] = []; - from:selectFromElements; - constructor(a: selectElements[], from:selectFromElements) { + attr: selectElements[] = []; + from: selectFromElements; + constructor(a: selectElements[], from: selectFromElements) { this.attr.push(...a.flat(Infinity)); this.from = from ? from : null; } - whereD:BooleanModifier | null = null; + whereD: BooleanModifier | null = null; where(m: BooleanModifier) { this.whereD = m; return this; @@ -30,18 +30,23 @@ export class selectQuery { this.groupByD = a; return this; } - havingD:Modifier | null = null; + havingD: Modifier | null = null; having(m: Modifier) { this.havingD = m; 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); } query(db: DB) { const s = this.serialize(db.getHandler()); - return new Query(s[0],s[1]); + return new Query(s[0], s[1]); } } \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 0ecfc4e..24adafb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,7 +11,7 @@ export type selectFromElements = Table | Joins | null; export type serializeReturn = [string, primaryData[]]; -export enum onAction{ +export enum onAction { cascade, noAction, setNull,