From d1df483737feb7eb4440c9db43048cd713172ed9 Mon Sep 17 00:00:00 2001 From: Johannes Hoermann <j.hoermann@adito.de> Date: Thu, 6 Dec 2018 10:10:13 +0100 Subject: [PATCH] HowToSqlConditionLib.adoc added --- others/guide/HowToSqlConditionLib.adoc | 52 ++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/others/guide/HowToSqlConditionLib.adoc b/others/guide/HowToSqlConditionLib.adoc index fb39163358..3e42bbb01e 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; +} +---- -- GitLab