From b59784354fab57740c86697b11af540a01fc628e Mon Sep 17 00:00:00 2001 From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local> Date: Wed, 18 Dec 2019 13:44:45 +0100 Subject: [PATCH] Sql_lib: condition constants added, error constants renamed --- process/Sql_lib/process.js | 172 +++++++++++++++++++++++++++---------- 1 file changed, 126 insertions(+), 46 deletions(-) diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index cff23b03d0..8d2f7d9e69 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -777,7 +777,7 @@ function newWhereIfSet(pFieldOrCond, pValue, pCondition, pFieldType, pAlias) function SqlBuilder (pAlias) { if(!(this instanceof SqlBuilder)) - throw SqlBuilder.ERROR_INSTANCIATE_WITH_NEW(); + throw SqlBuilder._ERROR_INSTANCIATE_WITH_NEW(); this._select = null; this._from = null; this._tableName = null; //for update/delete @@ -822,88 +822,88 @@ SqlBuilder.prototype.copy = function() } } -// errors which are throwed by the SqlBuilder -SqlBuilder.ERROR_INSTANCIATE_WITH_NEW = function() +// errors which are thrown by the SqlBuilder +SqlBuilder._ERROR_INSTANCIATE_WITH_NEW = function() { return new Error(translate.text("SqlBuilder must be instanciated with 'new' or one of the factory methods (newSelect, newWhere, newWhereIfSet)")); } -SqlBuilder.ERROR_INVALID_CONDITION_VALUE_TYPE = function() +SqlBuilder._ERROR_INVALID_CONDITION_VALUE_TYPE = function() { return new Error(translate.text("SqlBuilder: invalid value-type for pCondition")); } -SqlBuilder.ERROR_NO_CONDITION = function() +SqlBuilder._ERROR_NO_CONDITION = function() { return new Error(translate.text("SqlBuilder: if you use a subQuery (e.g. SqlBuilder) you have to provide pCondition (e.g. \"exists ?\")")); } -SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE = function() +SqlBuilder._ERROR_INVALID_SUBQUERY_TYPE = function() { return new Error(translate.text("SqlBuilder: invalid value-type for pFieldOrCond. It can be a fully qualified SqlBuilder (e.g. select, from, ... have to be set) or an jdito-prepared-statement array")); } -SqlBuilder.ERROR_VALUE_IS_MANDATORY = function() +SqlBuilder._ERROR_VALUE_IS_MANDATORY = function() { return new Error(translate.text("SqlBuilder: pValue (or pFieldOrCond if only one param) is not allowed to be null, undefined or []. (use *IfSet functions if you need optional conditions which are just ignored if value is null or undefined)")); } -SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR = function() +SqlBuilder._ERROR_VALUE_IS_MANDATORY_JDITO_VAR = function() { return new Error(translate.text("SqlBuilder: pValue has to be a jdito variable which returns something different than null. (use *IfSet functions if you need optional conditions which are just ignored if value is null or undefined)")); } -SqlBuilder.ERROR_UNSUPPORTED_PARAMETER_COMBINATION = function() +SqlBuilder._ERROR_UNSUPPORTED_PARAMETER_COMBINATION = function() { return new Error(translate.text("SqlBuilder: unsupportet parameter combination")); } -SqlBuilder.ERROR_NO_TABLE = function() +SqlBuilder._ERROR_NO_TABLE = function() { return new Error(translate.text("SqlBuilder.deleteDat/updateData: You have to specify a tablename")); } -SqlBuilder.ERROR_NO_PARAMETER_PROVIDED = function() +SqlBuilder._ERROR_NO_PARAMETER_PROVIDED = function() { return new Error(translate.text("SqlBuilder: You have to specify at least one parameter")); } -SqlBuilder.ERROR_WHERE_NOT_FIRST = function() +SqlBuilder._ERROR_WHERE_NOT_FIRST = function() { return new Error(translate.text("SqlBuilder: .where has to be called before following and/or.")); } -SqlBuilder.ERROR_ONLY_ONE_WHERE = function() +SqlBuilder._ERROR_ONLY_ONE_WHERE = function() { return new Error(translate.text("SqlBuilder: .where has to be called only one time. Use and/or for further conditions.")); } -SqlBuilder.ERROR_INCOMPLETE_SELECT = function () +SqlBuilder._ERROR_INCOMPLETE_SELECT = function () { return new Error(translate.text("SqlBuilder: select and from were expected, but not provided.")); } -SqlBuilder.ERROR_CONDITION_IS_MANDATORY = function () +SqlBuilder._ERROR_CONDITION_IS_MANDATORY = function () { return new Error(translate.text("SqlBuilder: You have to provide a subquery as SqlBuilder, prepared-array or string")); } -SqlBuilder.ERROR_SUBSELECT_AS_FIELD_NOT_COMPLETE = function () +SqlBuilder._ERROR_SUBSELECT_AS_FIELD_NOT_COMPLETE = function () { return new Error(translate.text("SqlBuilder: If pFieldOrCond is a SqlBuilder & pValue is provided, pFieldOrCond has to be a full SqlBuilder which will be used as subselect")); } -SqlBuilder.ERROR_SUBSELECT_AS_FIELD_NO_FIELD_TYPE = function () +SqlBuilder._ERROR_SUBSELECT_AS_FIELD_NO_FIELD_TYPE = function () { return new Error(translate.text("SqlBuilder: If pFieldOrCond is a SqlBuilder & pValue is provided, you have to provide also pFieldType, as the type cannot be calculated from pFieldOrCond because it is a subselect")); } -SqlBuilder.ERROR_CONDITION_WRONG_FORMAT = function () +SqlBuilder._ERROR_CONDITION_WRONG_FORMAT = function () { return new Error(translate.text("SqlBuilder: The '#' in pCondition has to occur before the '?' and '?' has to occur 1 time, '#' has to occur 1 or 0 times.")); } -SqlBuilder.ERROR_NOT_BOOLEAN = function () +SqlBuilder._ERROR_NOT_BOOLEAN = function () { return new Error(translate.text("pExecuteOnlyIfConditionExists has to be of type boolean. This parameter controls what happens if the condition is empty (select / delete all or nothing)")); } @@ -1255,11 +1255,11 @@ SqlBuilder.prototype._setWhere = function (pFieldOrCond, pValue, pCondition, pFi // where has to be called before all other and / or if (this.hasCondition()) - throw SqlBuilder.ERROR_WHERE_NOT_FIRST(); + throw SqlBuilder._ERROR_WHERE_NOT_FIRST(); // only one where call is allowed if (this._where._whereWasCalled) - throw SqlBuilder.ERROR_ONLY_ONE_WHERE(); + throw SqlBuilder._ERROR_ONLY_ONE_WHERE(); this._where._whereWasCalled = true; return pAddCondFn.call(this, pFieldOrCond, pValue, pCondition, pFieldType); @@ -1309,7 +1309,7 @@ SqlBuilder.prototype._whereCondition = function(pCondition, pMandatory, pAddPrep return this; } else if (pMandatory) - throw SqlBuilder.ERROR_CONDITION_IS_MANDATORY(); + throw SqlBuilder._ERROR_CONDITION_IS_MANDATORY(); return this; } @@ -1331,12 +1331,12 @@ SqlBuilder.prototype._whereCondition = function(pCondition, pMandatory, pAddPrep return this; } else if (pMandatory) - throw SqlBuilder.ERROR_CONDITION_IS_MANDATORY(); + throw SqlBuilder._ERROR_CONDITION_IS_MANDATORY(); return this; } - throw SqlBuilder.ERROR_INVALID_CONDITION_VALUE_TYPE(); + throw SqlBuilder._ERROR_INVALID_CONDITION_VALUE_TYPE(); } /** @@ -1367,7 +1367,7 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition if (sql[0]) pAddPreparedConditionCallback(this, this._prepare(undefined, sql, pCondition)); else if (pMandatory) - throw SqlBuilder.ERROR_VALUE_IS_MANDATORY(); + throw SqlBuilder._ERROR_VALUE_IS_MANDATORY(); return this; } @@ -1379,7 +1379,7 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition // Without condition this function cannot be used with SqlBuilder object as it cannot contain a condition if (!pCondition) - throw SqlBuilder.ERROR_NO_CONDITION(); + throw SqlBuilder._ERROR_NO_CONDITION(); if (subQuery.isFullSelect()) { @@ -1387,12 +1387,12 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition pAddPreparedConditionCallback(this, this._prepare(undefined, preparedObj, pCondition)); } else if (pMandatory) - throw SqlBuilder.ERROR_VALUE_IS_MANDATORY(); + throw SqlBuilder._ERROR_VALUE_IS_MANDATORY(); return this; } - throw SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE(); + throw SqlBuilder._ERROR_INVALID_SUBQUERY_TYPE(); } /** @@ -1411,19 +1411,19 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCondition, pFieldType, pAddPreparedConditionCallback) { if (pCondition && !SqlUtils.checkConditionFormat(pCondition)) - throw SqlBuilder.ERROR_CONDITION_WRONG_FORMAT(); + throw SqlBuilder._ERROR_CONDITION_WRONG_FORMAT(); if (pMandatory === undefined) pMandatory = true; if (!this._where._whereWasCalled) - throw SqlBuilder.ERROR_WHERE_NOT_FIRST(); + throw SqlBuilder._ERROR_WHERE_NOT_FIRST(); if (!pMandatory && pFieldOrCond === undefined && pValue === undefined && pCondition === undefined && pFieldType === undefined) return this; if (pFieldOrCond === undefined && pValue === undefined && pCondition === undefined && pFieldType === undefined) - throw SqlBuilder.ERROR_NO_PARAMETER_PROVIDED(); + throw SqlBuilder._ERROR_NO_PARAMETER_PROVIDED(); // Special case: if only pFieldOrCond is set and we can identify it as a valid field-string (e.g. "Table.Field") we assume that it is not just a condition string. // --> we can check pValue for undefined and also allow simple string-conditions @@ -1433,7 +1433,7 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon if (pValue === undefined && pCondition === undefined && pFieldType === undefined && typeof pFieldOrCond == "string" && SqlUtils.isFullFieldQualifier(pFieldOrCond)) { if (pMandatory) - throw SqlBuilder.ERROR_VALUE_IS_MANDATORY(); + throw SqlBuilder._ERROR_VALUE_IS_MANDATORY(); else return this; } @@ -1447,11 +1447,11 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon { if (!pFieldOrCond.isFullSelect()) { - throw SqlBuilder.ERROR_SUBSELECT_AS_FIELD_NOT_COMPLETE(); + throw SqlBuilder._ERROR_SUBSELECT_AS_FIELD_NOT_COMPLETE(); } if (!pFieldType) - throw SqlBuilder.ERROR_SUBSELECT_AS_FIELD_NO_FIELD_TYPE(); + throw SqlBuilder._ERROR_SUBSELECT_AS_FIELD_NO_FIELD_TYPE(); var tmpCond = newWhere(this.alias) ._addWhere("SQL_LIB_DUMMY_TABLE.SQL_LIB_DUMMY_COLUMN", pValue, pMandatory, pCondition, pFieldType, pAddPreparedConditionCallback); @@ -1467,7 +1467,7 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon // first check the default-mandatory-cases: null or undefined. everything else such as checking $-variables is done later if (pMandatory && (pValue === null || pValue === undefined)) - throw SqlBuilder.ERROR_VALUE_IS_MANDATORY(); + throw SqlBuilder._ERROR_VALUE_IS_MANDATORY(); // a field is string or array -> normal case // OR !pFieldOrCond and pValue and pCondition is given -> preparedSQL/SqlBuilder can be used without field if pCondition is set (e.g. with "exists ?") @@ -1486,7 +1486,7 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon if (pValue.length == 0) { if (pMandatory) - throw SqlBuilder.ERROR_VALUE_IS_MANDATORY(); + throw SqlBuilder._ERROR_VALUE_IS_MANDATORY(); return this; } @@ -1507,7 +1507,7 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon if (pFieldOrCond !== null && pFieldOrCond !== undefined) { if (!pCondition) - pCondition = "# = ?" + pCondition = SqlBuilder.EQUAL(); pCondition = SqlUtils.replaceConditionTemplate(pCondition, '#', SqlUtils.parseField(pFieldOrCond)[0]) } @@ -1522,14 +1522,14 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon } if (!pCondition) - pCondition = "# = ?" + pCondition = SqlBuilder.EQUAL(); // ... a string starting with $ -> jdito varable which has to be resolved if (typeofValue == "string" && pValue.length >= 2 && pValue[0] == "$" && pValue[1] != "$") // escape $ if using two $ { pValue = vars.getString(pValue) if (pMandatory && pValue === null) - throw SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR(); + throw SqlBuilder._ERROR_VALUE_IS_MANDATORY_JDITO_VAR(); if (pValue == "" || pValue) this._whereCondition(this._prepare(field, pValue, pCondition, pFieldType), undefined, pAddPreparedConditionCallback); @@ -1552,7 +1552,7 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon } - throw SqlBuilder.ERROR_UNSUPPORTED_PARAMETER_COMBINATION(); + throw SqlBuilder._ERROR_UNSUPPORTED_PARAMETER_COMBINATION(); } /** @@ -1697,6 +1697,86 @@ SqlBuilder.YEAR_EQUALS = function () return (new SqlMaskingUtils().yearFromDate("#")) + " = ?"; } +/** + * Constant-like function which provides a value for pCondition if you need a "# = ?" statement. + * This is the default for the pCondition parameter, so it can be omitted. + * + * @return {String} + * + * @example + * var cond = newWhere("PERSON.FIRSTNAME", "Fritz", SqlBuilder.EQUAL()) + */ +SqlBuilder.EQUAL = function () +{ + return "# = ?"; +} + +/** + * Constant-like function which provides a value for pCondition if you need a "# <> ?" statement. + * + * @return {String} + * + * @example + * var cond = newWhere("PERSON.FIRSTNAME", "Fritz", SqlBuilder.NOT_EQUALS()) + */ +SqlBuilder.NOT_EQUAL = function () +{ + return "# <> ?"; +} + +/** + * Constant-like function which provides a value for pCondition if you need a "# like ?" statement. + * + * @return {String} + * + * @example + * var cond = newWhere("PERSON.FIRSTNAME", "F%", SqlBuilder.LIKE()) + */ +SqlBuilder.LIKE = function () +{ + return "# like ?"; +} + +/** + * Constant-like function which provides a value for pCondition if you need a "# > ?" statement. + * + * @return {String} + */ +SqlBuilder.GREATER_THAN = function () +{ + return "# > ?"; +} + +/** + * Constant-like function which provides a value for pCondition if you need a "# < ?" statement. + * + * @return {String} + */ +SqlBuilder.LESS_THAN = function () +{ + return "# < ?"; +} + +/** + * Constant-like function which provides a value for pCondition if you need a "# >= ?" statement. + * + * @return {String} + */ +SqlBuilder.GREATER_OR_EQUAL = function () +{ + return "# >= ?"; +} + +/** + * Constant-like function which provides a value for pCondition if you need a "# <= ?" statement. + * + * @return {String} + */ +SqlBuilder.LESS_OR_EQUAL = function () +{ + return "# <= ?"; +} + /** * Throws an error if pValue is null, undefined or a SqlBuilder without condition (or if pValue is a $-variable: error if the result of it is null or undefined)<br/> * Also throws an error if pFieldOrCond is the only parameter and it is null<br/> @@ -2034,7 +2114,7 @@ SqlBuilder.prototype._prepare = function(pField, pValue, pCondition, pFieldType, throw new Error(translate.withArguments("${SQL_LIB_UNDEFINED_VALUE} field: %0", [pField])); if (pCondition == undefined) - pCondition = "# = ?"; + pCondition = SqlBuilder.EQUAL(); var alias, field; if (pField != null) @@ -2214,7 +2294,7 @@ SqlBuilder.prototype.updateData = function(pExecuteOnlyIfConditionExists, pTable if (this._checkForUpdate(pExecuteOnlyIfConditionExists)) { if (!pTableName && !this._tableName) - throw SqlBuilder.ERROR_NO_TABLE(); + throw SqlBuilder._ERROR_NO_TABLE(); if (!pColumns) pColumns = null; @@ -2248,7 +2328,7 @@ SqlBuilder.prototype.deleteData = function(pExecuteOnlyIfConditionExists, pTable if (this._checkForUpdate(pExecuteOnlyIfConditionExists)) { if (!pTableName && !this._tableName) - throw SqlBuilder.ERROR_NO_TABLE(); + throw SqlBuilder._ERROR_NO_TABLE(); return db.deleteData( (pTableName ? pTableName : this._tableName), @@ -2425,7 +2505,7 @@ SqlBuilder.prototype._checkForUpdate = function(pExecuteOnlyIfConditionExists) pExecuteOnlyIfConditionExists = true; if (typeof pExecuteOnlyIfConditionExists !== "boolean") - throw SqlBuilder.ERROR_NOT_BOOLEAN(); + throw SqlBuilder._ERROR_NOT_BOOLEAN(); return !pExecuteOnlyIfConditionExists || this.hasCondition(); } @@ -2441,7 +2521,7 @@ SqlBuilder.prototype._checkForSelect = function(pExecuteOnlyIfConditionExists) pExecuteOnlyIfConditionExists = false; if (typeof pExecuteOnlyIfConditionExists !== "boolean") - throw SqlBuilder.ERROR_NOT_BOOLEAN(); + throw SqlBuilder._ERROR_NOT_BOOLEAN(); if (this.isFullSelect()) { @@ -2449,7 +2529,7 @@ SqlBuilder.prototype._checkForSelect = function(pExecuteOnlyIfConditionExists) } else { - throw SqlBuilder.ERROR_INCOMPLETE_SELECT(); + throw SqlBuilder._ERROR_INCOMPLETE_SELECT(); } } -- GitLab