diff --git a/entity/Person_entity/entityfields/campaignactiongroup/children/sqltests/onActionProcess.js b/entity/Person_entity/entityfields/campaignactiongroup/children/sqltests/onActionProcess.js index 98ea89310f744206c5cb423567aa15657286f50e..1e272ce9591930a7cdf2658b77c8bf7aea7729b1 100644 --- a/entity/Person_entity/entityfields/campaignactiongroup/children/sqltests/onActionProcess.js +++ b/entity/Person_entity/entityfields/campaignactiongroup/children/sqltests/onActionProcess.js @@ -10,7 +10,7 @@ var validAndUsageTests = new TestSuite([ ["and should just add simple strings as condition just as it is", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME = 'Tim'") // NOTE: you should not do this as this does not add a real prepared statement with "?" + .where("PERSON.FIRSTNAME = 'Tim'") // NOTE: you should not do this as this does not add a real prepared statement with "?" .and("PERSON.LASTNAME = 'Admin'") pTester.assert("PERSON.FIRSTNAME = 'Tim' and PERSON.LASTNAME = 'Admin'", actual._where._sqlStorage, "prepared sql"); @@ -20,7 +20,7 @@ var validAndUsageTests = new TestSuite([ ["and should add a condition if field and value are passed", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .and("PERSON.LASTNAME", "Admin") pTester.assert("PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ?", actual._where._sqlStorage, "prepared sql"); @@ -31,7 +31,7 @@ var validAndUsageTests = new TestSuite([ { vars.set("$global.TestUnitValueName", "Tim"); var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", "$global.TestUnitValueName") + .where("PERSON.FIRSTNAME", "$global.TestUnitValueName") pTester.assert("( PERSON.FIRSTNAME = 'Tim' ) ", actual.toString()); }], @@ -39,7 +39,7 @@ var validAndUsageTests = new TestSuite([ ["and should use the given condition pattern", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", "Tim", "# <> ?") + .where("PERSON.FIRSTNAME", "Tim", "# <> ?") .and("PERSON.LASTNAME", "Admin") pTester.assert("PERSON.FIRSTNAME <> ? and PERSON.LASTNAME = ?", actual._where._sqlStorage, "prepared sql"); @@ -49,7 +49,7 @@ var validAndUsageTests = new TestSuite([ ["and should use the given SQLTYPE if provided", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", 6, null, SQLTYPES.INTEGER) + .where("PERSON.FIRSTNAME", 6, null, SQLTYPES.INTEGER) .and("PERSON.LASTNAME", 7, undefined, SQLTYPES.INTEGER) .and("PERSON.LASTNAME", 8, "# <> ?", SQLTYPES.INTEGER) @@ -63,7 +63,7 @@ var validAndUsageTests = new TestSuite([ ["and only with a prepared statement-array should just use it as it is", function(pTester) { var actual = new SqlBuilder() - .and([ + .where([ "PERSON.FIRSTNAME = ?", [["Peter", 12]] ]) .and([ @@ -77,8 +77,8 @@ var validAndUsageTests = new TestSuite([ ["and only with a SqlBulder object should just use the condition from it", function(pTester) { var actual = new SqlBuilder() - .and(new SqlBuilder() - .and("PERSON.FIRSTNAME", "Tim") + .where(new SqlBuilder() + .where("PERSON.FIRSTNAME", "Tim") .and("PERSON.LASTNAME", "Admin")) pTester.assert(" ( PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); @@ -88,10 +88,10 @@ var validAndUsageTests = new TestSuite([ ["and with a builder as value and condition (field is null|undefined) should add the whole builder as subquery", function(pTester) { var actual = new SqlBuilder() - .and(null, new SqlBuilder() + .where(null, new SqlBuilder() .select("FIRSTNAME") .from("PERSON") - .and("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .and("PERSON.LASTNAME", "Admin"), "exists ?") @@ -102,10 +102,10 @@ var validAndUsageTests = new TestSuite([ ["and with a builder as value and field should add the whole builder as subquery with field = (subquery)", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", new SqlBuilder() + .where("PERSON.FIRSTNAME", new SqlBuilder() .select("FIRSTNAME") .from("PERSON") - .and("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .and("PERSON.LASTNAME", "Admin")) pTester.assert("PERSON.FIRSTNAME = ( select FIRSTNAME from PERSON where PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); @@ -115,7 +115,7 @@ var validAndUsageTests = new TestSuite([ ["and with a prepared statement-array as value and field is null|undefined should add the whole statement as subquery", function(pTester) { var actual = new SqlBuilder() - .and(null, ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]], "exists ?") + .where(null, ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]], "exists ?") .and(null, ["exists (select FIRSTNAME from PERSON.FIRSTNAME = ?)", [["Peter", 12]]]) // also without pCond it should work as the condition could be included in the prep statement pTester.assert("exists ( select FIRSTNAME from PERSON.FIRSTNAME = ? ) and ( exists (select FIRSTNAME from PERSON.FIRSTNAME = ?) ) ", actual._where._sqlStorage, "prepared sql"); @@ -125,7 +125,7 @@ var validAndUsageTests = new TestSuite([ ["and with a prepared statement-array as value and field should add the whole statement as subquery with field = (subquery)", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]]) + .where("PERSON.FIRSTNAME", ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]]) pTester.assert("PERSON.FIRSTNAME = ( select FIRSTNAME from PERSON.FIRSTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); pTester.assert(1, actual._where.preparedValues.length, "number of params"); @@ -136,7 +136,7 @@ var validOrUsageTests = new TestSuite([ ["or should just add simple strings as condition just as it is", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME = 'Tim'") // NOTE: you should not do this as this does not add a real prepared statement with "?" + .where("PERSON.FIRSTNAME = 'Tim'") // NOTE: you should not do this as this does not add a real prepared statement with "?" .or("PERSON.LASTNAME = 'Admin'") pTester.assert("PERSON.FIRSTNAME = 'Tim' or PERSON.LASTNAME = 'Admin'", actual._where._sqlStorage, "prepared sql"); @@ -146,26 +146,17 @@ var validOrUsageTests = new TestSuite([ ["or should add a condition if field and value are passed", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .or("PERSON.LASTNAME", "Admin") pTester.assert("PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ?", actual._where._sqlStorage, "prepared sql"); pTester.assert(2, actual._where.preparedValues.length, "number of params"); }], - ["or should add a condition if field and value as jdito-var are passed", function(pTester) - { - vars.set("$global.TestUnitValueName", "Tim"); - var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", "$global.TestUnitValueName") - - pTester.assert("( PERSON.FIRSTNAME = 'Tim' ) ", actual.toString()); - }], - ["or should use the given condition pattern", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", "Tim", "# <> ?") + .where("PERSON.FIRSTNAME", "Tim", "# <> ?") .or("PERSON.LASTNAME", "Admin") pTester.assert("PERSON.FIRSTNAME <> ? or PERSON.LASTNAME = ?", actual._where._sqlStorage, "prepared sql"); @@ -175,7 +166,7 @@ var validOrUsageTests = new TestSuite([ ["or should use the given SQLTYPE if provided", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", 6, null, SQLTYPES.INTEGER) + .where("PERSON.FIRSTNAME", 6, null, SQLTYPES.INTEGER) .or("PERSON.LASTNAME", 7, undefined, SQLTYPES.INTEGER) .or("PERSON.LASTNAME", 8, "# <> ?", SQLTYPES.INTEGER) @@ -186,7 +177,7 @@ var validOrUsageTests = new TestSuite([ ["or only with a prepared statement-array should just use it as it is", function(pTester) { var actual = new SqlBuilder() - .or([ + .where([ "PERSON.FIRSTNAME = ?", [["Peter", 12]] ]) .or([ @@ -200,8 +191,8 @@ var validOrUsageTests = new TestSuite([ ["or only with a SqlBulder object should just use the condition from it", function(pTester) { var actual = new SqlBuilder() - .or(new SqlBuilder() - .or("PERSON.FIRSTNAME", "Tim") + .where(new SqlBuilder() + .where("PERSON.FIRSTNAME", "Tim") .or("PERSON.LASTNAME", "Admin")) pTester.assert(" ( PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); @@ -211,10 +202,10 @@ var validOrUsageTests = new TestSuite([ ["or with a builder as value and condition (field is null|undefined) should add the whole builder as subquery", function(pTester) { var actual = new SqlBuilder() - .or(null, new SqlBuilder() + .where(null, new SqlBuilder() .select("FIRSTNAME") .from("PERSON") - .or("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .or("PERSON.LASTNAME", "Admin"), "exists ?") @@ -225,10 +216,10 @@ var validOrUsageTests = new TestSuite([ ["or with a builder as value and field should add the whole builder as subquery with field = (subquery)", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", new SqlBuilder() + .where("PERSON.FIRSTNAME", new SqlBuilder() .select("FIRSTNAME") .from("PERSON") - .or("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .or("PERSON.LASTNAME", "Admin")) pTester.assert("PERSON.FIRSTNAME = ( select FIRSTNAME from PERSON where PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); @@ -238,20 +229,11 @@ var validOrUsageTests = new TestSuite([ ["or with a prepared statement-array as value and field is null|undefined should add the whole statement as subquery", function(pTester) { var actual = new SqlBuilder() - .or(null, ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]], "exists ?") + .where(null, ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]], "exists ?") .or(null, ["exists (select FIRSTNAME from PERSON.FIRSTNAME = ?)", [["Peter", 12]]]) // also without pCond it should work as the condition could be included in the prep statement pTester.assert("exists ( select FIRSTNAME from PERSON.FIRSTNAME = ? ) or ( exists (select FIRSTNAME from PERSON.FIRSTNAME = ?) ) ", actual._where._sqlStorage, "prepared sql"); pTester.assert(2, actual._where.preparedValues.length, "number of params"); - }], - - ["or with a prepared statement-array as value and field should add the whole statement as subquery with field = (subquery)", function(pTester) - { - var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", ["select FIRSTNAME from PERSON.FIRSTNAME = ?", [["Peter", 12]]]) - - pTester.assert("PERSON.FIRSTNAME = ( select FIRSTNAME from PERSON.FIRSTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); - pTester.assert(1, actual._where.preparedValues.length, "number of params"); }] ]); @@ -259,10 +241,10 @@ var combinedAndOrTests = new TestSuite([ ["or combining two and", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .and("PERSON.LASTNAME", "Admin") .or(new SqlBuilder() - .and("PERSON.FIRSTNAME", "Peter") + .where("PERSON.FIRSTNAME", "Peter") .and("PERSON.LASTNAME", "Müller")) pTester.assert("(PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ?) or ( PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); @@ -272,11 +254,11 @@ var combinedAndOrTests = new TestSuite([ ["and combining two or", function(pTester) { var actual = new SqlBuilder() - .and(new SqlBuilder() - .or("PERSON.FIRSTNAME", "Tim") + .where(new SqlBuilder() + .where("PERSON.FIRSTNAME", "Tim") .or("PERSON.LASTNAME", "Admin")) .and(new SqlBuilder() - .or("PERSON.FIRSTNAME", "Peter") + .where("PERSON.FIRSTNAME", "Peter") .or("PERSON.LASTNAME", "Müller")) pTester.assert(" ( PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) and ( PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) ", actual._where._sqlStorage, "prepared sql"); @@ -286,19 +268,19 @@ var combinedAndOrTests = new TestSuite([ ["some and/or combinations in one select", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.FIRSTNAME", "Tim") + .where("PERSON.FIRSTNAME", "Tim") .or("PERSON.FIRSTNAME", "Franz") .and("PERSON.LASTNAME", "Admin") .and(new SqlBuilder() - .or("PERSON.FIRSTNAME", "Peter") + .where("PERSON.FIRSTNAME", "Peter") .or("PERSON.LASTNAME", "Müller")) .or("PERSON.FIRSTNAME", "Franz") .and("PERSON.FIRSTNAME", "Franz") .or(new SqlBuilder() - .and("PERSON.FIRSTNAME", "Peter") + .where("PERSON.FIRSTNAME", "Peter") .and("PERSON.LASTNAME", "Müller") .and(new SqlBuilder() - .or("PERSON.FIRSTNAME", "Peter") + .where("PERSON.FIRSTNAME", "Peter") .or("PERSON.LASTNAME", "Müller"))) pTester.assert("PERSON.FIRSTNAME = ? or PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ? and ( PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) or (PERSON.FIRSTNAME = ?) and PERSON.FIRSTNAME = ? or ( PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ? and ( PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) ) ", actual._where._sqlStorage, "prepared sql"); @@ -310,7 +292,7 @@ var ifSetTests = new TestSuite([ ["simple or if set with all types of empty values. Note that undefined leads to using the first parameter as subquery", function(pTester) { var actual = new SqlBuilder() - .orIfSet("PERSON.FIRSTNAME", "") + .whereIfSet("PERSON.FIRSTNAME", "") .andIfSet("PERSON.LASTNAME", null) pTester.assert("", actual._where._sqlStorage, "no sql should be added"); @@ -323,7 +305,7 @@ var ifSetTests = new TestSuite([ vars.set("$global.TestingVarEmptyString", ""); var actual = new SqlBuilder() - .orIfSet("PERSON.FIRSTNAME", "$global.TestingVarNull") + .whereIfSet("PERSON.FIRSTNAME", "$global.TestingVarNull") .andIfSet("PERSON.FIRSTNAME", "$global.TestingVarEmptyString") pTester.assert("", actual._where._sqlStorage, "no sql should be added"); @@ -333,7 +315,7 @@ var ifSetTests = new TestSuite([ ["empty simple conditions", function(pTester) { var actual = new SqlBuilder() - .orIfSet("") + .whereIfSet("") .andIfSet(["", []]) .andIfSet(new SqlBuilder()) @@ -344,7 +326,7 @@ var ifSetTests = new TestSuite([ ["empty subqueries", function(pTester) { var actual = new SqlBuilder() - .orIfSet("PERSON.FIRSTNAME", ["", []]) + .whereIfSet("PERSON.FIRSTNAME", ["", []]) .andIfSet("PERSON.LASTNAME", new SqlBuilder()) pTester.assert("", actual._where._sqlStorage, "no sql should be added"); @@ -370,7 +352,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .select("FIRSTNAME") .from("PERSON") - .and("PERSON.PERSONID", "TEST-5") + .where("PERSON.PERSONID", "TEST-5") pTester.assert("Franz", builder.cell()); }], @@ -397,7 +379,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .select("FIRSTNAME, LASTNAME") .from("PERSON") - .and("PERSON.PERSONID", "TEST-5"); + .where("PERSON.PERSONID", "TEST-5"); var actual = builder.array(db.ROW); pTester.assert("Franz", actual[0], "firstname should be 'Franz'"); @@ -429,7 +411,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .select("FIRSTNAME, LASTNAME") .from("PERSON") - .or("PERSON.PERSONID", "TEST-5") + .where("PERSON.PERSONID", "TEST-5") .or("PERSON.PERSONID", "TEST-6") .orderBy("PERSONID asc"); @@ -468,7 +450,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .from("SQL_LIB_TEST_TABLE") - .and("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); + .where("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); var deletedRows = builder.deleteData(); pTester.assert(1, deletedRows); @@ -481,7 +463,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .from("PERSON") - .and("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); + .where("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); var deletedRows = builder.deleteData(false, "SQL_LIB_TEST_TABLE"); pTester.assert(1, deletedRows); @@ -520,7 +502,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .from("SQL_LIB_TEST_TABLE") - .and("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); + .where("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); builder.updateData(false, ["FIRSTNAME"], null, ["Fritz"]); @@ -536,7 +518,7 @@ var dbWrapperTests = new TestSuite([ var builder = new SqlBuilder() .from("PERSON") - .and("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); + .where("SQL_LIB_TEST_TABLE.TESTID", "TEST-7"); builder.updateData(false, ["FIRSTNAME"], null, ["Fritz"], "SQL_LIB_TEST_TABLE"); @@ -600,96 +582,96 @@ var mandatoryErrorTests = new TestSuite([ // and ["and without parameter should error", function(pTester) { - new SqlBuilder().and(); + new SqlBuilder().where(); }, SqlBuilder.ERROR_NO_PARAMETER_PROVIDED()], ["and with '' as value should error", function(pTester) { - new SqlBuilder().and("PERSON.FIRSTNAME", ""); + new SqlBuilder().where("PERSON.FIRSTNAME", ""); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ["and with null as value should error", function(pTester) { - new SqlBuilder().and("PERSON.FIRSTNAME", null); + new SqlBuilder().where("PERSON.FIRSTNAME", null); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ["and with a jdito-var containing null should error", function(pTester) { vars.set("$global.TestingVarNull", null); - new SqlBuilder().and("PERSON.FIRSTNAME", "$global.TestingVarNull"); + new SqlBuilder().where("PERSON.FIRSTNAME", "$global.TestingVarNull"); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()], ["and with a jdito-var containing '' should error", function(pTester) { vars.set("$global.TestingVarEmptyString", ""); - new SqlBuilder().and("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); + new SqlBuilder().where("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()], ["and with a jdito-var containing '' should error", function(pTester) { vars.set("$global.TestingVarEmptyString", ""); - new SqlBuilder().and("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); + new SqlBuilder().where("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()], ["and with an empty sql-builder as subquery should error", function(pTester) { - new SqlBuilder().and("PERSON.FIRSTNAME", new SqlBuilder()); + new SqlBuilder().where("PERSON.FIRSTNAME", new SqlBuilder()); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ["and with an empty prepared statement as subquery should error", function(pTester) { - new SqlBuilder().and("PERSON.FIRSTNAME", ["", []]); + new SqlBuilder().where("PERSON.FIRSTNAME", ["", []]); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], // or ["or without parameter should error", function(pTester) { - new SqlBuilder().or(); + new SqlBuilder().where(); }, SqlBuilder.ERROR_NO_PARAMETER_PROVIDED()], ["or with '' as value should error", function(pTester) { - new SqlBuilder().or("PERSON.FIRSTNAME", ""); + new SqlBuilder().where("PERSON.FIRSTNAME", ""); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ["or with null as value should error", function(pTester) { - new SqlBuilder().or("PERSON.FIRSTNAME", null); + new SqlBuilder().where("PERSON.FIRSTNAME", null); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ["or with a jdito-var containing null should error", function(pTester) { vars.set("$global.TestingVarNull", null); - new SqlBuilder().or("PERSON.FIRSTNAME", "$global.TestingVarNull"); + new SqlBuilder().where("PERSON.FIRSTNAME", "$global.TestingVarNull"); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()], ["or with a jdito-var containing '' should error", function(pTester) { vars.set("$global.TestingVarEmptyString", ""); - new SqlBuilder().or("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); + new SqlBuilder().where("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()], ["or with a jdito-var containing '' should error", function(pTester) { vars.set("$global.TestingVarEmptyString", ""); - new SqlBuilder().or("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); + new SqlBuilder().where("PERSON.FIRSTNAME", "$global.TestingVarEmptyString"); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()], ["or with an empty sql-builder as subquery should error", function(pTester) { - new SqlBuilder().or("PERSON.FIRSTNAME", new SqlBuilder()); + new SqlBuilder().where("PERSON.FIRSTNAME", new SqlBuilder()); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ["or with an empty prepared statement as subquery should error", function(pTester) { - new SqlBuilder().or("PERSON.FIRSTNAME", ["", []]); + new SqlBuilder().where("PERSON.FIRSTNAME", ["", []]); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], ]) @@ -699,7 +681,7 @@ var inStatementTests = new TestSuite([ ["simple and in", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.LASTNAME", ["Franz", "Fritz"]); + .where("PERSON.LASTNAME", ["Franz", "Fritz"]); pTester.assert(" ( PERSON.LASTNAME in (?, ?) ) ", actual._where._sqlStorage, "prepared sql"); pTester.assert(2, actual._where.preparedValues.length, "number of params"); @@ -708,7 +690,7 @@ var inStatementTests = new TestSuite([ ["simple and not in", function(pTester) { var actual = new SqlBuilder() - .and("PERSON.LASTNAME", ["Franz", "Fritz"], "# not in ?"); + .where("PERSON.LASTNAME", ["Franz", "Fritz"], "# not in ?"); pTester.assert(" ( PERSON.LASTNAME not in (?, ?) ) ", actual._where._sqlStorage, "prepared sql"); pTester.assert(2, actual._where.preparedValues.length, "number of params"); @@ -717,7 +699,7 @@ var inStatementTests = new TestSuite([ ["andIfSet should ignore empty array", function(pTester) { var actual = new SqlBuilder() - .andIfSet("PERSON.LASTNAME", []); + .whereIfSet("PERSON.LASTNAME", []); pTester.assert("", actual._where._sqlStorage, "prepared sql should be empty"); pTester.assert(0, actual._where.preparedValues.length, "number of params should be 0"); @@ -726,14 +708,14 @@ var inStatementTests = new TestSuite([ ["and should error on an empty array", function(pTester) { new SqlBuilder() - .and("PERSON.LASTNAME", []); + .where("PERSON.LASTNAME", []); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()], // or ["simple or in", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.LASTNAME", ["Franz", "Fritz"]); + .where("PERSON.LASTNAME", ["Franz", "Fritz"]); pTester.assert(" ( PERSON.LASTNAME in (?, ?) ) ", actual._where._sqlStorage, "prepared sql"); pTester.assert(2, actual._where.preparedValues.length, "number of params"); @@ -742,7 +724,7 @@ var inStatementTests = new TestSuite([ ["simple or not in", function(pTester) { var actual = new SqlBuilder() - .or("PERSON.LASTNAME", ["Franz", "Fritz"], "# not in ?"); + .where("PERSON.LASTNAME", ["Franz", "Fritz"], "# not in ?"); pTester.assert(" ( PERSON.LASTNAME not in (?, ?) ) ", actual._where._sqlStorage, "prepared sql"); pTester.assert(2, actual._where.preparedValues.length, "number of params"); @@ -751,7 +733,7 @@ var inStatementTests = new TestSuite([ ["orIfSet should ignore empty array", function(pTester) { var actual = new SqlBuilder() - .orIfSet("PERSON.LASTNAME", []); + .whereIfSet("PERSON.LASTNAME", []); pTester.assert("", actual._where._sqlStorage, "prepared sql should be empty"); pTester.assert(0, actual._where.preparedValues.length, "number of params should be 0"); @@ -760,7 +742,7 @@ var inStatementTests = new TestSuite([ ["or should error on an empty array", function(pTester) { new SqlBuilder() - .or("PERSON.LASTNAME", []); + .where("PERSON.LASTNAME", []); }, SqlBuilder.ERROR_VALUE_IS_MANDATORY()] ]); diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 7ef8ed97b62a7658b9774fb3d23eaf7cd146e3fe..01e8064e4db77546fa9851d5a42075fb61c24240 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -689,7 +689,12 @@ SqlCondition.equalsNot = function(pField, pValue, pAlternativeCond, pAlias) { function NewSelect(pFields, pAlias) { - return new SqlBuilder(pAlias).select(pFields) + return new SqlBuilder(pAlias).select(pFields); +} + +function NewWhere(pFieldOrCond, pValue, pCond, pFieldType, pAlias) +{ + return new SqlBuilder(pAlias).where(pFieldOrCond, pValue, pCond, pFieldType); } /** @@ -721,7 +726,8 @@ function SqlBuilder (pAlias) // save, if the last condition was an OR. For better bracket-placement _lastWasOr: false, - _previouslyOnlyOr: false + _previouslyOnlyOr: false, + _whereWasCalled: false } } @@ -755,11 +761,6 @@ SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR = function() return new Error(translate.text("SqlBuilder.and/or: pValue has to be a jdito variable which returns not null, \"\"")); } -SqlBuilder.ERROR_NO_ARRAY_AS_VALUE = function() -{ - return new Error(translate.text("SqlBuilder.and/or: Arrays for pValue are not allowed. You may need andIn, orIn, andNotIn, orNotIn which accepts an array.")); -} - SqlBuilder.ERROR_UNSUPPORTED_PARAMETER_COMBINATION = function() { return new Error(translate.text("SqlBuilder.and/or: unsupportet parameter combination")); @@ -775,6 +776,16 @@ SqlBuilder.ERROR_NO_PARAMETER_PROVIDED = function() return new Error(translate.text("SqlBuilder.and/or: You have to specify at least one parameter")); } +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() +{ + return new Error(translate.text("SqlBuilder: .where has to be called only one time. Use and/or for further conditions.")); +} + /** * Alternative way of creating a new SqlBuilder object that allows to use * methods on it directly without having to put brackets around it @@ -904,13 +915,49 @@ SqlBuilder.prototype.leftJoin = function(pTable, pCondition, pAlias) * a string (without the where keyword), a SqlCondition or an array (for prepared queries). * * @return {SqlBuilder} current SqlBuilder object - * @deprecated use .and() */ -SqlBuilder.prototype.where = function(pCondition) +SqlBuilder.prototype.where = function(pFieldOrCond, pValue, pCond, pFieldType) { - this._where.preparedValues = pCondition.preparedValues; - this._where._sqlStorage = pCondition._sqlStorage - return this; + // for deprecated use (setting to a SqlCondition) + if (pFieldOrCond instanceof SqlCondition && pValue === undefined && pCond === undefined && pFieldType === undefined) + { + this._where.preparedValues = pFieldOrCond.preparedValues; + this._where._sqlStorage = pFieldOrCond._sqlStorage + return this; + } + + // where has to be called before all other and / or + if (this.hasCondition()) + { + throw SqlBuilder.ERROR_WHERE_NOT_FIRST() + } + + // only one where call is allowed + if (this._where._whereWasCalled) + { + throw SqlBuilder.ERROR_ONLY_ONE_WHERE() + } + + this._where._whereWasCalled = true; + return this.or(pFieldOrCond, pValue, pCond, pFieldType); +} + +SqlBuilder.prototype.whereIfSet = function(pFieldOrCond, pValue, pCond, pFieldType) +{ + // where has to be called before all other and / or + if (this.hasCondition()) + { + throw SqlBuilder.ERROR_WHERE_NOT_FIRST() + } + + // only one where call is allowed + if (this._where._whereWasCalled) + { + throw SqlBuilder.ERROR_ONLY_ONE_WHERE() + } + + this._where._whereWasCalled = true; + return this.orIfSet(pFieldOrCond, pValue, pCond, pFieldType); } /** @@ -1056,6 +1103,11 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition */ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCond, pFieldType, pAddPreparedConditionCallback) { + if (!this._where._whereWasCalled) + { + throw SqlBuilder.ERROR_WHERE_NOT_FIRST() + } + if (pFieldOrCond === undefined && pValue === undefined && pCond === undefined && pFieldType === undefined) { throw SqlBuilder.ERROR_NO_PARAMETER_PROVIDED();