diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 167f57951b6f055b236d6a91b51a959041addfd6..6d270c9c0d5faf915789220e5a78ede0dd80b2f1 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -12,6 +12,9 @@ import("Util_lib"); * With this object you do not have to check if the string is empty or not; * you don't need to append a "1=1" condition or similar; * this objects gains most benefit if you have a lot of conditions that are added (or not) depending on tons of JDito-conditions + * + * You can also use SqlCondition.begin(alias) for simpler object creation without new and without the need for an extra variable to save the object. + * * @class * @param {String} [alias=the current alias] the database alias where the condition shall be executed later (important for column types of preparedStatements) * @example //TODO: add missing example @@ -21,8 +24,38 @@ function SqlCondition(alias) { this.preparedValues = null; this._init();//the properties are initalized in an extra function because init is nearly the same as resetting (clearing) the SqlConditions this.alias = alias; - this.cachedTypes = {}; - this.lastWasOr = false; + this._cachedTypes = {}; + this._lastWasOr = false; +} + +/** + * Alternative possibility to crate a new condition. + * With this you don't need new SqlCondition and you can use the object directly after it's creation + * --> cleaner code + * + * It is very usefull for the orSqlCondition() and andSqlCondition() because now you can create a new condition inline. + * You can also use it for simple selects without the need to save the conditionObject in an extra variable. + * See Examples! + * + * @param {String} [alias=the current alias] the database alias where the condition shall be executed later (important for column types of preparedStatements) + * @return {Object} the new SqlCondition-object + * + * @example + * vars mySelect = SqlCondition.begin(alias) + * .and("MYID = '123'") + * .and(SqlCondition.begin() + * .or("NAME = 'Max'") + * .or("NAME = 'Bob'") + * ) + * .buildSelect("select * from MYTABLE"); + * + * // Or use it for simple selects: + * var sum = db.cell(SqlCondition.begin() + * .andPrepared("STOCK.PRODUCT_ID", pid) + * .buildSelect("select sum(QUANTITY * IN_OUT) from STOCK")); + */ +SqlCondition.begin = function(alias) { + return new SqlCondition(alias); } /** @@ -67,13 +100,13 @@ SqlCondition.prototype.or = function(cond) { if (!cond) return this; - if (this._sqlStorage && !this.lastWasOr) { + if (this._sqlStorage && !this._lastWasOr) { this._sqlStorage = "(" + this._sqlStorage + ") or (" + cond + ")"; - this.lastWasOr = true; + this._lastWasOr = true; - } else if (this._sqlStorage && this.lastWasOr) { + } else if (this._sqlStorage && this._lastWasOr) { this._sqlStorage = this._sqlStorage + " or (" + cond + ")"; - this.lastWasOr = true; + this._lastWasOr = true; } else { this._sqlStorage = cond; @@ -81,6 +114,13 @@ SqlCondition.prototype.or = function(cond) { return this; } +/** + * append another condition with SQL-or; Also paranthesize the existing conditions + * + * @param {Object} cond the condition which shall be appended + * @param {String} [alternativeCond=""] condition if the given SqlCondition has none + * @return {Object} current SqlCondition-object + */ SqlCondition.prototype.orSqlCondition = function(cond, alternativeCond) { this.or(" ( " + cond.toString(alternativeCond) + " ) "); if (cond.preparedValues) { @@ -89,6 +129,13 @@ SqlCondition.prototype.orSqlCondition = function(cond, alternativeCond) { return this; } +/** + * append another condition with SQL-and + * + * @param {Object} cond the condition which shall be appended + * @param {String} [alternativeCond=""] condition if the given SqlCondition has none + * @return {Object} current SqlCondition-object + */ SqlCondition.prototype.andSqlCondition = function(cond, alternativeCond) { this.and(" ( " + cond.toString(alternativeCond) + " ) "); if (cond.preparedValues) { @@ -273,11 +320,11 @@ SqlCondition.prototype._prepare = function(field, value, cond, fieldType) { // only if fieldType is undefined, cache the column-Type for next use on same Column if (fieldType == undefined) { - if (this.cachedTypes[field] != undefined) { - fieldType = this.cachedTypes[field]; + if (this._cachedTypes[field] != undefined) { + fieldType = this._cachedTypes[field]; } else { fieldType = SqlUtils.getSingleColumnType(field, undefined, this.alias); - this.cachedTypes[field] = type; + this._cachedTypes[field] = type; } }