From cba4248727cae1addf53f868467407c6f9087a9e Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sun, 19 Feb 2023 21:01:02 +0100 Subject: [PATCH 1/3] regexp --- src/defaultHandler.ts | 17 +++++++++++++++++ src/funcs.ts | 1 + 2 files changed, 18 insertions(+) diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index d98d186..45394ff 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -708,6 +708,23 @@ export class Handler { } return builder; }, + regexp: (handler: Handler, a: allModifierInput[]): QueryBuilder => { + const builder = new QueryBuilder(); + if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].toString()); + else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) { + builder.append(a[0].serialize(handler)); + } else { + builder.addInjection(a[0]); + } + builder.addCode(" REGEXP "); + if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].toString()); + else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) { + builder.append(a[1].serialize(handler)); + } else { + builder.addInjection(a[1]); + } + return builder; + }, concat: (handler: Handler, a: allModifierInput[]): QueryBuilder => { const builder = new QueryBuilder(); builder.addCode("CONCAT("); diff --git a/src/funcs.ts b/src/funcs.ts index e5e5ac3..b567b05 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -18,6 +18,7 @@ export const divide = (...args: allModifierInput[]) => new NumberModifier("divid export const not = (arg: allModifierInput) => new BooleanModifier("not", [arg]); export const like = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("like", [a, b]); +export const regexp = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("regexp", [a, b]); export const concat = (...args: allModifierInput[]) => new StringModifier("concat", args); From f63c0cf6d0e24be1125c04aa05d4bb0472d92f7d Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 22 Feb 2023 11:13:43 +0100 Subject: [PATCH 2/3] responses --- src/db.ts | 4 +++- src/defaultHandler.ts | 8 +++++++- src/query.ts | 12 ++++++++---- src/responses.ts | 22 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/responses.ts diff --git a/src/db.ts b/src/db.ts index 5685448..0d2ab04 100644 --- a/src/db.ts +++ b/src/db.ts @@ -4,6 +4,7 @@ import { Handler } from './defaultHandler'; import { Query } from './query'; import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types'; import { TableAlias } from "./alias" +import { insertResponse, readResponse, writeResponse } from "./responses" //import { postgresHandler } from "./postgresHandler" @@ -162,4 +163,5 @@ export class Table { export * from './funcs'; export { onAction }; -export { dbType as databaseType } \ No newline at end of file +export { dbType as databaseType } +export { readResponse, writeResponse, insertResponse } \ No newline at end of file diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 45394ff..2c36030 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -1,5 +1,5 @@ import { AttributeAlias, TableAlias } from "./alias"; -import { Attribute, DB, Table } from "./db" +import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db" import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure" import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query" import { allModifierInput, joinType, onAction, primaryData } from "./types" @@ -570,6 +570,12 @@ export class Handler { }, } + responses = { + readResponse: (resp: any) => new readResponse(resp), + writeResponse: (resp: any) => new writeResponse(resp.affectedRows), + insertResponse: (resp: any) => new insertResponse(resp.affectedRows, resp.insertId), + } + aggregations = { count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]), sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]), diff --git a/src/query.ts b/src/query.ts index 29b713e..a69d5ef 100644 --- a/src/query.ts +++ b/src/query.ts @@ -100,7 +100,8 @@ export class selectQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.readResponse(readResp); } } @@ -137,7 +138,8 @@ export class insertQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.insertResponse(readResp); } } @@ -165,7 +167,8 @@ export class updateQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.writeResponse(readResp); } } @@ -186,6 +189,7 @@ export class removeQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.writeResponse(readResp); } } \ No newline at end of file diff --git a/src/responses.ts b/src/responses.ts new file mode 100644 index 0000000..d8f8ba1 --- /dev/null +++ b/src/responses.ts @@ -0,0 +1,22 @@ + +export class readResponse extends Array{ + constructor(rows:({[key: string]:any})[]){ + super(); + this.push(...rows); + } +} + +export class writeResponse{ + affectedRows: number; + constructor(affectedRows: number){ + this.affectedRows = affectedRows; + } +} + +export class insertResponse extends writeResponse{ + insertId: number; + constructor(affectedRows: number, insertId: number){ + super(affectedRows); + this.insertId = insertId; + } +} From 155e7028633fc3d6bdb1248590a6d77e6bf37a97 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 22 Feb 2023 11:16:36 +0100 Subject: [PATCH 3/3] query print --- src/db.ts | 4 ++-- src/query.ts | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/db.ts b/src/db.ts index 0d2ab04..860e3aa 100644 --- a/src/db.ts +++ b/src/db.ts @@ -29,8 +29,8 @@ export class DB { this.name = database; } - async query(query: Query) { - console.log(query); + async query(query: Query, printQuery = false) { + if(printQuery)console.log(query); if (this.type == dbType.mariadb) return await this.mariaPool.query(query); /*else if (this.type == dbType.postgres) { let res = await this.pgPool.query(query.sql, query.values); diff --git a/src/query.ts b/src/query.ts index a69d5ef..f5202c7 100644 --- a/src/query.ts +++ b/src/query.ts @@ -49,7 +49,7 @@ export class QueryBuilder { this.list.push(...qb.list); } appendEnding(qb: QueryBuilder) { - if(qb.isEmpty()) return; + if (qb.isEmpty()) return; this.append(qb); this.list.push([false, ";"]); } @@ -96,11 +96,11 @@ export class selectQuery { return handler.querys.select(handler, this); } - async query(db: DB) { + async query(db: DB, printQuery = false) { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - let readResp = await db.query(s); + let readResp = await db.query(s, printQuery); return db.getHandler().responses.readResponse(readResp); } } @@ -134,11 +134,11 @@ export class insertQuery { serialize(handler: Handler): QueryBuilder { return handler.querys.insert(handler, this); } - async query(db: DB) { + async query(db: DB, printQuery = false) { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - let readResp = await db.query(s); + let readResp = await db.query(s, printQuery); return db.getHandler().responses.insertResponse(readResp); } } @@ -163,11 +163,11 @@ export class updateQuery { serialize(handler: Handler): QueryBuilder { return handler.querys.update(handler, this); } - async query(db: DB) { + async query(db: DB, printQuery = false) { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - let readResp = await db.query(s); + let readResp = await db.query(s, printQuery); return db.getHandler().responses.writeResponse(readResp); } } @@ -185,11 +185,11 @@ export class removeQuery { serialize(handler: Handler): QueryBuilder { return handler.querys.remove(handler, this); } - async query(db: DB) { + async query(db: DB, printQuery = false) { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - let readResp = await db.query(s); + let readResp = await db.query(s, printQuery); return db.getHandler().responses.writeResponse(readResp); } } \ No newline at end of file