Skip to content

Commit

Permalink
fix: isStringType for Nullable(String) and FixedString(N) (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed May 17, 2024
1 parent df50323 commit 6e1f86e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fixed `IN` operator escaping the entire string (specifically with `Nullable(String)`), also added `FixedString(N)` (#830)

## 4.0.7

- Upgrade dependencies
Expand Down
1 change: 1 addition & 0 deletions cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"mage_output_file.go"
],
"words": [
"fixedstring",
"singlequote",
"concats",
"Milli",
Expand Down
29 changes: 29 additions & 0 deletions src/data/sqlGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,17 @@ describe('getLimit', () => {
});
});

describe('is*Type', () => {
it.each<{ input: string, expected: boolean }>([
{ input: 'Nullable(String)', expected: true },
{ input: 'FixedString(1)', expected: true },
{ input: 'String', expected: true },
{ input: 'Array(String)', expected: false },
])('$input isStringType $expected', (c) => {
expect(_testExports.isStringType(c.input)).toEqual(c.expected);
});
});

describe('getFilters', () => {
it('returns empty filter array', () => {
const options = {} as QueryBuilderOptions;
Expand All @@ -538,6 +549,24 @@ describe('getFilters', () => {
expect(sql).toEqual(expectedSql);
});

it('returns correct IN clause for escaped and unescaped values', () => {
const options = {
filters: [
{
condition: 'AND',
filterType: 'custom',
key: 'col',
operator: FilterOperator.In,
type: 'string',
value: '1, (2), 3, some string, \'another string\', someFunction(123), "column reference"'.split(',')
}
]
} as QueryBuilderOptions;
const sql = _testExports.getFilters(options);
const expectedSql = `( col IN ('1', (2), '3', 'some string', 'another string', someFunction(123), "column reference") )`;
expect(sql).toEqual(expectedSql);
});

it('returns complex filter array', () => {
const options = {
columns: [{ name: 'hinted', hint: ColumnHint.Time }],
Expand Down
3 changes: 2 additions & 1 deletion src/data/sqlGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ const numberTypes = ['int', 'float', 'decimal'];
const isNumberType = (type: string): boolean => numberTypes.some(t => type?.toLowerCase().includes(t));
const isDateType = (type: string): boolean => type?.toLowerCase().startsWith('date') || type?.toLowerCase().startsWith('nullable(date');
// const isDateTimeType = (type: string): boolean => type?.toLowerCase().startsWith('datetime') || type?.toLowerCase().startsWith('nullable(datetime');
const isStringType = (type: string): boolean => type.toLowerCase() === 'string' && !(isBooleanType(type) || isNumberType(type) || isDateType(type));
const isStringType = (type: string): boolean => (type.toLowerCase() === 'string' || type.toLowerCase().startsWith('nullable(string') || type.toLowerCase().startsWith('fixedstring')) && !(isBooleanType(type) || isNumberType(type) || isDateType(type));
const isNullFilter = (operator: FilterOperator): boolean => operator === FilterOperator.IsNull || operator === FilterOperator.IsNotNull;
const isBooleanFilter = (type: string): boolean => isBooleanType(type);
const isNumberFilter = (type: string): boolean => isNumberType(type);
Expand Down Expand Up @@ -778,4 +778,5 @@ export const _testExports = {
getOrderBy,
getLimit,
getFilters,
isStringType,
};

0 comments on commit 6e1f86e

Please sign in to comment.