From 575e9c2d4f70318f242fa7e92bf45fee6c9ba18e Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 29 Mar 2023 21:09:05 +0200 Subject: [PATCH 1/2] order by --- src/db.ts | 4 ++-- src/defaultHandler.ts | 21 ++++++++++++++++++++- src/query.ts | 13 ++++++++++++- src/types.ts | 5 +++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/db.ts b/src/db.ts index f7b74fd..53408d5 100644 --- a/src/db.ts +++ b/src/db.ts @@ -2,7 +2,7 @@ import mariadb from 'mariadb'; import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure.js'; import { Handler } from './defaultHandler.js'; import { Query } from './query.js'; -import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types.js'; +import { attributeSettings, extendedAttributeSettings, onAction, dbType, order } from './types.js'; import { TableAlias } from "./alias.js" import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses.js" //import { postgresHandler } from "./postgresHandler" @@ -180,6 +180,6 @@ export class Table { } export * from './funcs.js'; -export { onAction }; +export { onAction, order }; export { dbType as databaseType } export { readResponse, writeResponse, insertResponse, singleResponse } \ No newline at end of file diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index db30121..492df71 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -2,7 +2,7 @@ import { Alias, AttributeAlias, TableAlias } from "./alias.js"; import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db.js" import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure.js" import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js" -import { allModifierInput, joinType, onAction, primaryData } from "./types.js" +import { allModifierInput, joinType, onAction, order, primaryData } from "./types.js" import { sha256 } from "./tools.js" export class Handler { @@ -307,6 +307,25 @@ export class Handler { builder.append(q.havingD.serialize(handler)); } } + if (q.orderByD.length > 0) { + builder.addCode(" order by "); + for (let i = 0; i < q.orderByD.length; i++) { + const d = q.orderByD[i]; + if (d[0] instanceof Attribute || d[0] instanceof AttributeAlias) builder.addCode(d[0].getString(handler)); + else if (d[0] instanceof Alias) + builder.addCode(handler.builders.escapeID(d.toString())); + else if (d[0] instanceof Modifier || d[0] instanceof selectQuery || d[0] instanceof Aggregation) { + builder.addCode("("); + builder.append(d[0].serialize(handler)); + builder.addCode(")"); + } else { + builder.addInjection(d[0]); + } + if(d[1] == order.DESC) builder.addCode(" DESC"); + else builder.addCode(" ASC"); + if (i + 1 < q.orderByD.length) builder.addCode(", "); + } + } if (q.limitD != null) { if (q.limitOffsetD == null) { builder.addCode(" limit "); diff --git a/src/query.ts b/src/query.ts index cf949f6..694a504 100644 --- a/src/query.ts +++ b/src/query.ts @@ -2,7 +2,7 @@ import { AttributeAlias } from "./alias.js"; import { Attribute, DB, Table } from "./db.js"; import { BooleanModifier, Modifier } from "./dbStructure.js"; import { Handler } from "./defaultHandler.js"; -import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js"; +import { allModifierInput, order, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js"; export class Query { @@ -84,6 +84,17 @@ export class selectQuery { this.havingD = m; return this; } + + orderByD: ([allModifierInput, order])[] = []; + addOrderBy(a: allModifierInput, o: order = order.ASC) { + this.orderByD.push([a, o]); + return this; + } + orderBY(a: allModifierInput, o: order = order.ASC) { + this.orderByD = [[a, o]]; + return this; + } + limitD: number | null = null; limitOffsetD: number | null = null; limit(i: number, offset: number = 0) { diff --git a/src/types.ts b/src/types.ts index e7d4f9f..97ffcd5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -63,4 +63,9 @@ export enum dbType { mariadb, //mysql, postgres, +} + +export enum order { + ASC, + DESC } \ No newline at end of file From 25a38efdc6e709457632fbdafc066b3d28c3cc28 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 29 Mar 2023 21:10:11 +0200 Subject: [PATCH 2/2] version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4933727..d2f6d44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dblang", - "version": "0.9.5", + "version": "0.9.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dblang", - "version": "0.9.5", + "version": "0.9.6", "license": "UNLICENSED", "dependencies": { "mariadb": "^3.0.2", diff --git a/package.json b/package.json index 85e65b8..550733b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dblang", - "version": "0.9.5", + "version": "0.9.6", "description": "", "main": "dist/db.js", "types": "dist/db.d.ts",