diff --git a/others/guide/HowToSqlConditionLib.adoc b/others/guide/HowToSqlConditionLib.adoc index fb3916335850d78f07e686f9f9eb823112282870..3e42bbb01ebb94b5415901ef146f7007fc7ba47c 100644 --- a/others/guide/HowToSqlConditionLib.adoc +++ b/others/guide/HowToSqlConditionLib.adoc @@ -1,5 +1,5 @@ -How to use the SqlCondition -=========================== +How to use the SqlCondition (state: 06.12.2018) +=============================================== :toc2: left :numbered: @@ -72,6 +72,9 @@ The field name needs to be given with Or if you use only COLUMNNAME, you have to provide the fieldType. +But keep in mind: Other than the original adito db. functions this lib caches the field types automatically (for the livetime of an SqlCondition Object). +So most times it is save to use the first method. Even in loops or if you use the same Column several times. + [source,javascript] ---- myDescriptiveNameOfTheCondition.orPrepared("PERS.FIRSTNAME", 'Bob', "#<?"); @@ -136,4 +139,47 @@ Same as build and adds a string before and after the condition. [source,javascript] ---- var myPreparedStatementArray = myDescriptiveNameOfTheCondition.buildSelect("select * from PERS", "1=0", "order by FIRSTNAME"); ----- \ No newline at end of file +---- + +== real world example == +Some examples, how this lib can be used. + +[source,javascript] +---- +var productPriceData = (db.ROW, db.array(SqlCondition.begin() + .andPrepare("PRODUCTPRICE.BUYSELL", buySell) + .andPrepare("PRODUCTPRICE.PRODUCT_ID", pid) + .andPrepare("PRODUCTPRICE.VALID_FROM", today, "# <= ?") + .andSqlCondition(SqlCondition.begin() + .orPrepare("PRODUCTPRICE.VALID_TO", today, "# >= ?") + .or("PRODUCTPRICE.VALID_TO is null"), "1 = 2"); + .buildSelect("select PRICE, CURRENCY from PRODUCTPRICE", "1 = 2", "order by VALID_FROM desc"))); +---- + +With a loop: +[source,javascript] +---- +function _getClassificationCondition(include) +{ + var resultingCondition = new SqlCondition(); + resultingCondition.andPrepare("SALESPROJECT_CLASSIFICATION.SALESPROJECT_ID", salesprojectId) + .andPrepare("SALESPROJECT_CLASSIFICATION.CLASS", classId); + + var typeCondition = new SqlCondition(); + + entryKeywords.forEach(function(entry) + { + if (include) + { + typeCondition.orPrepare("SALESPROJECT_CLASSIFICATION.TYPE", entry[0], "# = ?"); + } + else + { + typeCondition.andPrepare("SALESPROJECT_CLASSIFICATION.TYPE", entry[0], "# <> ?"); + } + }); + + resultingCondition.andSqlCondition(typeCondition, "1 = 0"); + return resultingCondition; +} +----