diff --git a/packages/sql/src/sql-builder.ts b/packages/sql/src/sql-builder.ts index 864bbe7fa..f65f871f6 100644 --- a/packages/sql/src/sql-builder.ts +++ b/packages/sql/src/sql-builder.ts @@ -459,3 +459,7 @@ export class SqlBuilder { return sql; } } + +export class SqlReference { + constructor(public readonly field: string) {} +} diff --git a/packages/sql/src/sql-filter-builder.ts b/packages/sql/src/sql-filter-builder.ts index 3f99e6731..360ceb77e 100644 --- a/packages/sql/src/sql-filter-builder.ts +++ b/packages/sql/src/sql-filter-builder.ts @@ -20,6 +20,7 @@ import { } from '@deepkit/type'; import { SqlPlaceholderStrategy } from './platform/default-platform.js'; import { getPreparedEntity, PreparedAdapter, PreparedEntity } from './prepare.js'; +import { SqlReference } from './sql-builder.js'; type Filter = { [name: string]: any }; @@ -111,10 +112,9 @@ export class SQLFilterBuilder { else if (comparison === 'regex') return this.regexpComparator(this.quoteIdWithTable(fieldName), value); else throw new Error(`Comparator ${comparison} not supported.`); - const referenceValue = 'string' === typeof value && value[0] === '$'; let rvalue = ''; - if (referenceValue) { - rvalue = `${this.quoteIdWithTable(value.substr(1))}`; + if (value instanceof SqlReference) { + rvalue = `${this.quoteIdWithTable(value.field)}`; } else { if (value === undefined || value === null) { cmpSign = cmpSign === '!=' ? this.isNotNull() : this.isNull(); diff --git a/packages/sql/tests/sql-query.spec.ts b/packages/sql/tests/sql-query.spec.ts index ece7fa4ba..c3b70b92a 100644 --- a/packages/sql/tests/sql-query.spec.ts +++ b/packages/sql/tests/sql-query.spec.ts @@ -6,7 +6,7 @@ import { splitDotPath, sql, SQLQueryModel } from '../src/sql-adapter.js'; import { DefaultPlatform, SqlPlaceholderStrategy } from '../src/platform/default-platform.js'; import { SchemaParser } from '../src/reverse/schema-parser.js'; import { DatabaseModel } from '../src/schema/table.js'; -import { SqlBuilder } from '../src/sql-builder.js'; +import { SqlBuilder, SqlReference } from '../src/sql-builder.js'; import { PreparedAdapter } from '../src/prepare.js'; function quoteId(value: string): string { @@ -111,7 +111,7 @@ test('QueryToSql', () => { }); expect(queryToSql.convert({ id: 123 })).toBe(`user.id = ?`); - expect(queryToSql.convert({ id: '$id' })).toBe(`user.id = user.id`); + expect(queryToSql.convert({ id: new SqlReference('id') })).toBe(`user.id = user.id`); expect(queryToSql.convert({ username: 'Peter' })).toBe(`user.username = ?`); expect(queryToSql.convert({ id: 44, username: 'Peter' })).toBe(`(user.id = ? AND user.username = ?)`);