pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["and with a builder as value and condition (field is null|undefined) should add the whole builder as subquery",function(pTester)
{
varactual=newSqlBuilder()
.and(null,newSqlBuilder()
.select("FIRSTNAME")
.from("PERSON")
.and("PERSON.FIRSTNAME","Tim")
.and("PERSON.LASTNAME","Admin"),
"exists ?")
pTester.assert(expected,actual);
pTester.assert("exists ( select FIRSTNAME from PERSON where PERSON.FIRSTNAME = ? and PERSON.LASTNAME = ? ) ",actual._where._sqlStorage,"prepared sql");
pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["SqlBuilder as first param. It should use only the condition from the Builder.",function(pTester){
vartestCond=newSqlBuilder()
.select("FIRSTNAME")
.from("PERSON")
.andCondition("PERSON.FIRSTNAME = 'Admin'");
varexpected=" ( PERSON.FIRSTNAME = 'Admin' ) ";
["and with a builder as value and field should add the whole builder as subquery with field = (subquery)",function(pTester)
{
varactual=newSqlBuilder()
.and("PERSON.FIRSTNAME",newSqlBuilder()
.select("FIRSTNAME")
.from("PERSON")
.and("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");
pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["and with a prepared statement-array as value and field is null|undefined should add the whole statement as subquery",function(pTester)
{
varactual=newSqlBuilder()
.andCondition(testCond)
.toString()
.and(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(expected,actual);
pTester.assert("exists ( select FIRSTNAME from PERSON.FIRSTNAME = ? ) and ( exists (select FIRSTNAME from PERSON.FIRSTNAME = ?) ) ",actual._where._sqlStorage,"prepared sql");
pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["wrong type as first parameter throws error",function(pTester){
newSqlBuilder()
.andCondition(9)
.toString()
["and with a prepared statement-array as value and field should add the whole statement as subquery with field = (subquery)",function(pTester)
{
varactual=newSqlBuilder()
.and("PERSON.FIRSTNAME",["select FIRSTNAME from PERSON.FIRSTNAME = ?",[["Peter",12]]])
pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["or with a builder as value and condition (field is null|undefined) should add the whole builder as subquery",function(pTester)
{
varactual=newSqlBuilder()
.or(null,newSqlBuilder()
.select("FIRSTNAME")
.from("PERSON")
.or("PERSON.FIRSTNAME","Tim")
.or("PERSON.LASTNAME","Admin"),
"exists ?")
pTester.assert(expected,actual);
pTester.assert("exists ( select FIRSTNAME from PERSON where PERSON.FIRSTNAME = ? or PERSON.LASTNAME = ? ) ",actual._where._sqlStorage,"prepared sql");
pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["SqlBuilder as subcondition with cond = 'exists ?' and field = null",function(pTester){
varexpected="select * from CONTACT where ( exists ( select PERSONID from PERSON where ( PERSON.FIRSTNAME = 'Admin' ) ) ) ";
["or with a builder as value and field should add the whole builder as subquery with field = (subquery)",function(pTester)
{
varactual=newSqlBuilder()
.or("PERSON.FIRSTNAME",newSqlBuilder()
.select("FIRSTNAME")
.from("PERSON")
.or("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");
pTester.assert(2,actual._where.preparedValues.length,"number of params");
}],
["or with a prepared statement-array as value and field is null|undefined should add the whole statement as subquery",function(pTester)
{
varactual=newSqlBuilder()
.select("*")
.from("CONTACT")
.and(undefined,
newSqlBuilder()
.select("PERSONID")
.from("PERSON")
.and("PERSON.FIRSTNAME","Admin")
,false,"exists ?")
.toString();
.or(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(expected,actual);
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");
}],
]
varmandatoryAndTests=[
["default of pMandatory should be true -> error on pValue = null",function(pTester){
newSqlBuilder().and("PERSON.USER_NEW","");
},SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["if pMandatory = true error on pValue = null",function(pTester){
newSqlBuilder().and("PERSON.USER_NEW",null,true);
},SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["if pMandatory = false: NO error on pValue = null and no condition added",function(pTester){
// TODO: maybe call add(null, sql, pMandatory, pCond, ...) if sql.isFullSelect() and pCond is proviede. Else use only select and throw error -> better: only one possible way and a clear solution if someone does it wrong less implict