dblang/src/defaultHandler.ts

136 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-01-23 21:19:18 +01:00
import { Attribute } from "./db"
import { Aggregation, Modifier } from "./dbStructure"
import { selectQuery } from "./query"
import { allModifierInput, primaryData, serializeReturn } from "./types"
export class Handler {
static querys = {
select: (q: selectQuery): serializeReturn => {
let args: primaryData[] = [];
let w = joinArg(", ", this)(q.attr);
args.push(...w[1]);
let sql = `select ${w[0]} from ${q.from == null ? 'DUAL' : q.from.serialize(this)}`;
if (q.whereD) {
let whereS = q.whereD.serialize(this);
args.push(...whereS[1]);
2023-01-23 22:10:33 +01:00
sql += " where " + whereS[0];
2023-01-23 21:19:18 +01:00
}
2023-01-23 22:10:33 +01:00
if (q.groupByD.length > 0) {
2023-01-23 21:19:18 +01:00
let groupByS = joinArg(",", this)(q.groupByD);
args.push(...groupByS[1]);
2023-01-23 22:10:33 +01:00
sql += " group by " + groupByS[0];
2023-01-23 21:19:18 +01:00
if (q.havingD) {
let havingS = q.havingD.serialize(this);
args.push(...havingS[1]);
2023-01-23 22:10:33 +01:00
sql += " having " + havingS[0];
2023-01-23 21:19:18 +01:00
}
}
2023-01-23 22:10:33 +01:00
if (q.limitD != null) {
sql += " limit ?";
args.push(q.limitD);
}
2023-01-23 21:19:18 +01:00
return [sql, args];
}
}
static aggregations = {
count: (a: Attribute): serializeReturn => ["count(" + a + ")", []],
sum: (a: Attribute): serializeReturn => ["sum(" + a + ")", []],
avg: (a: Attribute): serializeReturn => ["avg(" + a + ")", []],
min: (a: Attribute): serializeReturn => ["min(" + a + ")", []],
max: (a: Attribute): serializeReturn => ["max(" + a + ")", []],
}
static modifiers = {
and: joinArg("and", this),
or: joinArg("or", this),
eq: joinArg("=", this),
plus: joinArg("+", this),
minus: joinArg("-", this),
2023-01-23 22:10:33 +01:00
not: (a: allModifierInput[]): serializeReturn => {
2023-01-23 21:19:18 +01:00
let e = a[0];
if (e instanceof Attribute) return ["not (" + e + ")", []];
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
let [sqli, argsi] = e.serialize(this);
return ["not (" + sqli + ")", argsi]
}
return ["not (?)", [e]];
}
}
2023-01-24 10:40:16 +01:00
static datatypes = {
char: dataTypeSingleNum("char"),
varchar: dataTypeSingleNum("varchar"),
binary: dataTypeSingleNum("binary"),
varbinary: dataTypeSingleNum("varbinary"),
tinyblob: dataTypeNoArg("tinyblob"),
blob: dataTypeNoArg("blob"),
mediumblob: dataTypeNoArg("mediumblob"),
longblob: dataTypeNoArg("longblob"),
tinytext: dataTypeNoArg("tinytext"),
text: dataTypeNoArg("text"),
mediumtext: dataTypeNoArg("mediumtext"),
longtext: dataTypeNoArg("longtext"),
enum: (a: primaryData[]): serializeReturn =>{
return ["enum("+a.map(()=>"?").join(", ")+")",a];
},
set: (a: primaryData[]): serializeReturn =>{
return ["set("+a.map(()=>"?").join(", ")+")",a];
},
bool: dataTypeNoArg("bool"),
bit: dataTypeNoArg("bit"),
tinyint: dataTypeNoArg("tinyint"),
smallint: dataTypeNoArg("smallint"),
mediumint: dataTypeNoArg("mediumint"),
int: dataTypeNoArg("int"),
bigint: dataTypeNoArg("bigint"),
float: dataTypeDblNum("float"),
double: dataTypeDblNum("double"),
decimal: dataTypeDblNum("decimal"),
data: dataTypeNoArg("data"),
datatime: dataTypeNoArg("datatime"),
timestamp: dataTypeNoArg("timestamp"),
time: dataTypeNoArg("time"),
year: dataTypeNoArg("year"),
}
2023-01-23 21:19:18 +01:00
};
2023-01-24 10:40:16 +01:00
function dataTypeNoArg(type:string){
return (a : primaryData[]): serializeReturn =>{
return [type,[]];
}
}
function dataTypeSingleNum(type:string){
return (a : primaryData[]): serializeReturn =>{
return [type+"(?)",[a[0]]];
}
}
function dataTypeDblNum(type:string){
return (a : primaryData[]): serializeReturn =>{
return [type+"(?,?)",[a[0],a[1]]];
}
}
2023-01-23 21:19:18 +01:00
function joinArg(type: string, s: any) {
return (a: (allModifierInput)[]): serializeReturn => {
let args: primaryData[] = [];
2023-01-23 22:10:33 +01:00
let sql = a.map(d => {
2023-01-23 21:19:18 +01:00
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)));
2023-01-23 22:10:33 +01:00
return "("+sqli+")";
2023-01-23 21:19:18 +01:00
}
args.push(d);
return "?";
2023-01-23 22:10:33 +01:00
}).join(" " + type + " ");
2023-01-23 21:19:18 +01:00
return [sql, args]
}
}