diff --git a/process/Sql_lib/documentation.adoc b/process/Sql_lib/documentation.adoc index 4330b3af2166a38f050685d472b18d5b0a8daab3..8b903c2155e9d54f160b06ef6fa80a586a8335e6 100644 --- a/process/Sql_lib/documentation.adoc +++ b/process/Sql_lib/documentation.adoc @@ -170,7 +170,12 @@ If used as pField, you can provide the table and column in different ways. * *pValue* This is the value of the condition: If, e.g., you need the SQL condition "FIRSTNAME = 'John'" then this parameter is "John") * *pCondition* -This parameter defines what condition is used. You can use any SQL. # will be replaced by the pFieldOrCond and ? is used for the pValue (default is "# = ?"). If, e.g., you need the SQL condition "FIRSTNAME <> 'John'" then this parameter is "# <> ?" (or SqlBuilder.NOT_EQUAL()). You can use SqlBuilder constants here (for example SqlBuilder.NOT_EQUAL(), SqlBuilder.EXISTS()) +This parameter defines what condition is used. You can use any SQL. `#` will be replaced by the pFieldOrCond and `?` is used for the pValue (default is `# = ?`). +If, e.g., you need the SQL condition `FIRSTNAME <> 'John'` then this parameter is `# <> ?` (or `SqlBuilder.NOT_EQUAL()`). +You can use SqlBuilder constants here (for example `SqlBuilder.NOT_EQUAL()`, `SqlBuilder.EXISTS()`) +For better useability, it is also possible to give the value without the bracets like this: `SqlBuilder.NOT_EQUAL` +It is possible to escape the # symbol with a backslash, note that to write a backslash in a string it needs to be escaped itself. +Example: `cond.and("MYTABLE.MYCOLUMN", "XYZ", "\\## = ?");` results in _( #MYTABLE.MYCOLUMN = 'XYZ' )_ * *pFieldType* This parameter is for specifying the SQLTYPE explicitly. In most cases, you won't need it, as the type is loaded from the db by the table name and the column name you specified in pFieldOrCond. @@ -552,7 +557,7 @@ var cond = newWhere("TABLE.FIELD", aSqlBuilderContainingFullSelect); === IN statement with array of values pValue can also be an array of values which is automatically transfered into a SQL prepared statement SQL like this: - ["(?, ?, ?)", [["Peter", SQLTYPES.VARCHAR], ["Paul", SQLTYPES.VARCHAR], ["Mary", SQLTYPES.VARCHAR]]] + `["(?, ?, ?)", [["Peter", SQLTYPES.VARCHAR], ["Paul", SQLTYPES.VARCHAR], ["Mary", SQLTYPES.VARCHAR]]]` With this it is very easy to create a in-statement: [source,js] @@ -560,7 +565,10 @@ With this it is very easy to create a in-statement: var cond = newWhere("PERSON.FIRSTNAME", ["Peter", "Paul", "Mary"], SqlBuilder.IN()) // SqlBuilder.IN() == "in ?" // Note that also SqlBuilder.NOT_IN() is possible here ---- -Note that an empty array is threated like null or undefined: using .where throws an error, using .whereIfSet ignores the whole condition. +Note that an empty array is treated like null or undefined: using .where throws an error, using .whereIfSet ignores the whole condition. + +TIP: Passing nothing as condition will result in an SqlBuilder.IN when the values are an array. +For example `myCondition.and("PERSON.FIRSTNAME", ["Peter", "Paul", "Mary"])` === IN statement with subquery diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 247df44a33d0d71ff477a74fef2edc179c275313..c202ee13879140048c214ff3161c9fc88515139c 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -1549,7 +1549,9 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon if (pFieldType === undefined || pFieldType === null) pFieldType = SqlUtils.getSingleColumnType(parsedField, undefined, this.alias); } - + //overwrite condition to set a default behaviour + if (pCondition == undefined) + pCondition = SqlBuilder.IN(); // value-array -> convert it to a prepared statement ["(?, ?, ?)", [[val1, type1], [val2, type2], [val3, type3]]] this._whereCondition(this._prepare(field, SqlUtils.getSqlInStatement(undefined, pValue, undefined, true, pFieldType), pCondition, pFieldType, false), undefined, pAddPreparedConditionCallback, true); return this;