Skip to content

Commit

Permalink
Revert changes to ColumnNames and Column values (too many tricky gotc…
Browse files Browse the repository at this point in the history
…has).

Fix problem with AND concatenation.
Better IS NULL flow.
  • Loading branch information
panta82 committed Nov 7, 2021
1 parent 11aeb53 commit 66ed307
Showing 1 changed file with 11 additions and 32 deletions.
43 changes: 11 additions & 32 deletions src/db/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,8 @@ export class SQLFragment<RunResult = pg.QueryResult['rows'], Constraint = never>
// a ColumnNames-wrapped object -> quoted names in a repeatable order
// OR a ColumnNames-wrapped array -> quoted array values
const columnNames = Array.isArray(expression.value) ? expression.value :
Object.keys(expression.value).filter(key => (<any>expression.value)[key] !== undefined).sort();
Object.keys(expression.value).sort();
result.text += columnNames.map(k => `"${k}"`).join(', ');

} else if (expression instanceof ColumnValues) {
// a ColumnValues-wrapped object OR array
// -> values (in ColumnNames-matching order, if applicable) punted as SQLFragments or Parameters
Expand All @@ -382,23 +381,12 @@ export class SQLFragment<RunResult = pg.QueryResult['rows'], Constraint = never>
const
columnNames = <Column[]>Object.keys(expression.value).sort(),
columnValues = columnNames.map(k => (<any>expression.value)[k]);

let firstField = true;

for (let i = 0, len = columnValues.length; i < len; i++) {
const
columnName = columnNames[i],
columnValue = columnValues[i];

if (columnValue === undefined) {
// Pretend undefineds are not there
continue;
}

if (firstField) {
firstField = false;
} else {
result.text += ', ';
}
if (i > 0) result.text += ', ';
if (columnValue instanceof SQLFragment ||
columnValue instanceof Parameter ||
columnValue === Default) this.compileExpression(columnValue, result, parentTable, columnName);
Expand All @@ -408,36 +396,27 @@ export class SQLFragment<RunResult = pg.QueryResult['rows'], Constraint = never>

} else if (typeof expression === 'object') {
// must be a Whereable object, so put together a WHERE clause
const columnNames = <Column[]>Object.keys(expression).sort();
const columnNames = <Column[]>Object.keys(expression).filter(key => (<any>expression)[key] !== undefined).sort();

if (columnNames.length) { // if the object is not empty
let firstField = true;
result.text += '(';
for (let i = 0, len = columnNames.length; i < len; i++) {
const
columnName = columnNames[i],
columnValue = (<any>expression)[columnName];

if (columnValue === undefined) {
// Pretend undefineds are not there
continue;
}

if (firstField) {
firstField = false;
} else {
result.text += ' AND ';
}
if (i > 0) result.text += ' AND ';
if (columnValue instanceof SQLFragment) {
result.text += '(';
this.compileExpression(columnValue, result, parentTable, columnName);
result.text += ')';

} else {
const assignOperator = columnValue === null ? 'IS' : '=';
result.text += `"${columnName}" ${assignOperator} `;
this.compileExpression(columnValue instanceof ParentColumn ? columnValue : new Parameter(columnValue),
result, parentTable, columnName);
if (columnValue === null) {
result.text += `"${columnName}" IS NULL`;
} else {
result.text += `"${columnName}" = `;
this.compileExpression(columnValue instanceof ParentColumn ? columnValue : new Parameter(columnValue), result, parentTable, columnName);
}
}
}
result.text += ')';
Expand Down

0 comments on commit 66ed307

Please sign in to comment.