add limit
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
jusax23 2023-01-23 22:10:33 +01:00
parent 71f6fbf117
commit e33dbcdfca
7 changed files with 72 additions and 60 deletions

View file

@ -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)

View file

@ -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};
export { onAction };

View file

@ -5,35 +5,35 @@ import { allModifierInput, primaryData, serializeReturn } from "./types";
export abstract class Modifier {
t : string;
a : Array<allModifierInput>;
t: string;
a: Array<allModifierInput>;
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 ["", []];
}
}

View file

@ -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]
}

View file

@ -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);

View file

@ -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]);
}
}

View file

@ -11,7 +11,7 @@ export type selectFromElements = Table | Joins | null;
export type serializeReturn = [string, primaryData[]];
export enum onAction{
export enum onAction {
cascade,
noAction,
setNull,