Skip to content

Commit

Permalink
fix(sql): fix queries where value legitimately begins with $
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Jun 21, 2024
1 parent 81a1ab1 commit 087f2ba
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/sql/src/sql-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,7 @@ export class SqlBuilder {
return sql;
}
}

export class SqlReference {
constructor(public readonly field: string) {}
}
6 changes: 3 additions & 3 deletions packages/sql/src/sql-filter-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/sql/tests/sql-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -110,7 +110,7 @@ test('QueryToSql', () => {
const queryToSql = new SQLFilterBuilder(localAdapter, ReflectionClass.from(User), quoteId('user'), serializer, new SqlPlaceholderStrategy());

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 = ?)`);
Expand Down

0 comments on commit 087f2ba

Please sign in to comment.