From 17685a44be671e7f59f349a9eed0349d125d6515 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 6 Sep 2020 15:38:54 +0200 Subject: [PATCH] Improve field disambiguation to allow raw values when starting with ",',` --- package.json | 2 +- src/db/ModelQuery.ts | 36 ++++++++++++++++++++---------------- test/ModelQuery.test.ts | 7 +++++++ 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 2c38121..2a8e0f6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.22.0-rc.4", + "version": "0.22.0-rc.6", "description": "Node web application framework and toolbelt.", "repository": "https://gitlab.com/ArisuOngaku/wms-core", "author": "Alice Gaudon ", diff --git a/src/db/ModelQuery.ts b/src/db/ModelQuery.ts index 9eb94ad..277acb0 100644 --- a/src/db/ModelQuery.ts +++ b/src/db/ModelQuery.ts @@ -18,7 +18,7 @@ export default class ModelQuery implements WhereFieldConsumer implements WhereFieldConsumer `\`${v}\``).join('.'), true, test, operator)); + this._leftJoinOn.push(new WhereFieldValue(inputToFieldOrValue(field1), inputToFieldOrValue(field2), true, test, operator)); return this; } @@ -96,7 +96,7 @@ export default class ModelQuery implements WhereFieldConsumer v.startsWith('`') ? v : `\`${v}\``).join('.'); + this._sortBy = raw ? field : inputToFieldOrValue(field); this._sortDirection = direction; return this; } @@ -119,7 +119,7 @@ export default class ModelQuery implements WhereFieldConsumer v.startsWith('`') ? v : `\`${v}\``).join('.'), + sortBy: raw ? sortBy : inputToFieldOrValue(sortBy), direction: direction, limit: limit, }; @@ -138,18 +138,11 @@ export default class ModelQuery implements WhereFieldConsumer { - let field = f.toString(); - + let fields = this.fields.map(f => { + const field = f.toString(); if (field.startsWith('(')) return f; // Skip sub-queries - - let parts = field.split('.'); - if (parts.length === 1) parts = [this.table, field]; // Add table disambiguation - - return parts.map(v => v.startsWith('`') || v === '*' ? v : `\`${v}\``).join('.'); - }); - - let fields = fieldArray.join(','); + return inputToFieldOrValue(field, this.table); + }).join(','); let join = ''; if (this._leftJoin) { @@ -316,6 +309,17 @@ export default class ModelQuery implements WhereFieldConsumer v === '*' ? v : `\`${v}\``).join('.'); +} + export interface ModelQueryResult extends Array { originalData?: any[]; pagination?: Pagination; @@ -357,7 +361,7 @@ class FieldValue { public toString(first: boolean = true): string { const valueStr = (this.raw || this.value === null || this.value instanceof ModelQuery) ? this.value : (Array.isArray(this.value) ? `(${'?'.repeat(this.value.length).split('').join(',')})` : '?'); - let field = this.field.split('.').map(p => `\`${p}\``).join('.'); + let field = inputToFieldOrValue(this.field); return `${first ? '' : ','}${field}${this.test}${valueStr}`; } diff --git a/test/ModelQuery.test.ts b/test/ModelQuery.test.ts index dbc5872..3cdd3d8 100644 --- a/test/ModelQuery.test.ts +++ b/test/ModelQuery.test.ts @@ -3,6 +3,13 @@ import ModelFactory from "../src/db/ModelFactory"; import Model from "../src/db/Model"; describe('Test ModelQuery', () => { + test('select', () => { + const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory, 'f1', '"Test" as f2') + .where('f4', 'v4'); + expect(query.toString(true)).toBe('SELECT `model`.`f1`,"Test" as f2 FROM `model` WHERE `f4`=?'); + expect(query.variables).toStrictEqual(['v4']); + }); + test('order by', () => { const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory) .sortBy('model.f2', 'ASC');