mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 04:47:29 -08:00
Add support for IN and NOT IN clauses in Postgres
This commit is contained in:
parent
54e1f62535
commit
21b5e86151
|
@ -250,6 +250,14 @@ export const whereFixedCollection: INodeProperties = {
|
|||
name: 'Not Equal',
|
||||
value: '!=',
|
||||
},
|
||||
{
|
||||
name: 'In',
|
||||
value: 'IN',
|
||||
},
|
||||
{
|
||||
name: 'Not In',
|
||||
value: 'NOT IN',
|
||||
},
|
||||
{
|
||||
name: 'Like',
|
||||
value: 'LIKE',
|
||||
|
|
|
@ -4,11 +4,15 @@ import type pg from 'pg-promise/typescript/pg-subset';
|
|||
|
||||
export type QueryMode = 'single' | 'transaction' | 'independently';
|
||||
|
||||
export type QueryValue = string | number | IDataObject | string[];
|
||||
export type QueryValue = string | number | IDataObject | string[] | number[];
|
||||
export type QueryValues = QueryValue[];
|
||||
export type QueryWithValues = { query: string; values?: QueryValues };
|
||||
|
||||
export type WhereClause = { column: string; condition: string; value: string | number };
|
||||
export type WhereClause = {
|
||||
column: string;
|
||||
condition: string;
|
||||
value: string | number | string[] | number[];
|
||||
};
|
||||
export type SortRule = { column: string; direction: string };
|
||||
export type ColumnInfo = {
|
||||
column_name: string;
|
||||
|
|
|
@ -142,20 +142,57 @@ export function addWhereClauses(
|
|||
|
||||
clause.value = value;
|
||||
}
|
||||
const columnReplacement = `$${replacementIndex}:name`;
|
||||
values.push(clause.column);
|
||||
replacementIndex = replacementIndex + 1;
|
||||
|
||||
let valueReplacement = '';
|
||||
if (clause.condition !== 'IS NULL' && clause.condition !== 'IS NOT NULL') {
|
||||
valueReplacement = ` $${replacementIndex}`;
|
||||
let formattedClause = '';
|
||||
|
||||
if (['IN', 'NOT IN'].includes(clause.condition)) {
|
||||
if (!Array.isArray(clause.value)) {
|
||||
throw new NodeOperationError(
|
||||
node,
|
||||
`Operator in entry ${index + 1} of 'Select Rows' works with arrays, but value ${
|
||||
clause.value
|
||||
} is not an array`,
|
||||
{
|
||||
itemIndex,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (clause.value.length > 0) {
|
||||
const columnReplacement = `$${replacementIndex}:name`;
|
||||
values.push(clause.column);
|
||||
replacementIndex = replacementIndex + 1;
|
||||
|
||||
const valueReplacement = "(" + clause.value
|
||||
.map((_, idx) => `$${replacementIndex + idx}`)
|
||||
.join(', ') + ")";
|
||||
values.push(...clause.value);
|
||||
replacementIndex += clause.value.length;
|
||||
|
||||
formattedClause = `${columnReplacement} ${clause.condition} ${valueReplacement}`;
|
||||
} else {
|
||||
formattedClause = '1 = 0'
|
||||
}
|
||||
} else if (['IS NULL', 'IS NOT NULL'].includes(clause.condition)) {
|
||||
const columnReplacement = `$${replacementIndex}:name`;
|
||||
values.push(clause.column);
|
||||
replacementIndex = replacementIndex++;
|
||||
|
||||
formattedClause = `${columnReplacement} ${clause.condition}`;
|
||||
} else {
|
||||
const columnReplacement = `$${replacementIndex}:name`;
|
||||
values.push(clause.column);
|
||||
replacementIndex++;
|
||||
|
||||
const valueReplacement = `$${replacementIndex}`;
|
||||
values.push(clause.value);
|
||||
replacementIndex = replacementIndex + 1;
|
||||
replacementIndex++;
|
||||
|
||||
formattedClause = ` ${columnReplacement} ${clause.condition} ${valueReplacement}`;
|
||||
}
|
||||
|
||||
const operator = index === clauses.length - 1 ? '' : ` ${combineWith}`;
|
||||
|
||||
whereQuery += ` ${columnReplacement} ${clause.condition}${valueReplacement}${operator}`;
|
||||
whereQuery += ` ${formattedClause}${operator}`;
|
||||
});
|
||||
|
||||
return [`${query}${whereQuery}`, replacements.concat(...values)];
|
||||
|
|
Loading…
Reference in a new issue