Skip to content
Snippets Groups Projects
Commit d1df9174 authored by Johannes Hörmann's avatar Johannes Hörmann
Browse files

fix some sql generation of sql builder, add "or"

parent 98441ff0
No related branches found
No related tags found
No related merge requests found
......@@ -661,7 +661,8 @@ function SqlBuilder (pAlias)
_sqlStorage: "",
// save, if the last condition was an OR. For better bracket-placement
_lastWasOr: false
_lastWasOr: false,
_previouslyOnlyOr: false
}
}
......@@ -844,8 +845,7 @@ SqlBuilder.prototype.where = function(pCondition)
return this;
}
SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
SqlBuilder.prototype._whereCondition = function(pCondition, pMandatory, pAddPreparedConditionCallback, pBrackets)
{
if (pCondition === undefined)
return this;
......@@ -859,10 +859,8 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
// the field is a simple string -> just add the string, no prepared statement
if (typeofSql == "string")
{
if (this.hasCondition())
this._where._sqlStorage += " and ";
this._where._sqlStorage += sql;
pAddPreparedConditionCallback([sql, []])
return this;
}
......@@ -870,8 +868,12 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
if (Array.isArray(sql))
{
this._where.preparedValues = this._where.preparedValues.concat(sql[1]);
return this.andCondition(" ( " + sql[0] + " ) ");
// add only brackets if needed
if (pBrackets)
sql[0] = " ( " + sql[0] + " ) ";
pAddPreparedConditionCallback([sql[0], []], pBrackets)
return this;
}
......@@ -879,14 +881,15 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
// 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
if (sql instanceof SqlBuilder)
{
var condString = " ( " + sql._where._sqlStorage + " ) ";
// add only brackets if needed
var sqlString = sql._where._sqlStorage;
if (pBrackets)
sqlString = " ( " + sqlString + " ) ";
var condString = sqlString;
if (condString.trim() != "")
{
this.andCondition(condString);
if (sql._where.preparedValues)
{
this._where.preparedValues = this._where.preparedValues.concat(sql._where.preparedValues);
}
pAddPreparedConditionCallback([condString, sql._where.preparedValues], pBrackets);
}
return this;
}
......@@ -894,7 +897,7 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
throw SqlBuilder.ERROR_INVALID_CONDITION_VALUE_TYPE();
}
SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition, pAddPreparedConditionCallback)
{
if (pSubquery === undefined)
return this;
......@@ -906,11 +909,11 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
var typeofSql = typeof sql;
// the field is a simple string -> just add the string, no prepared statement
// the field is an array -> it is a prepared condition
// the field is an array -> it is a prepared statement which already SHOULD contain exists or another condition
// Both can be handled by _prepare
if (Array.isArray(sql) || typeofSql == "string")
{
this.andCondition(this._prepare(undefined, sql, pCond));
pAddPreparedConditionCallback(this._prepare(undefined, sql, pCondition));
return this;
}
......@@ -919,8 +922,8 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
{
var subQuery = pSubquery;
// Without condition this function cannot be used
if (!pCond)
// Without condition this function cannot be used with SqlBuilder object as it cannot contain a condition
if (!pCondition)
{
throw SqlBuilder.ERROR_NO_CONDITION();
}
......@@ -928,7 +931,7 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
if (subQuery.isFullSelect())
{
var preparedObj = subQuery.build();
this.andCondition(this._prepare(undefined, preparedObj, pCond));
pAddPreparedConditionCallback(this._prepare(undefined, preparedObj, pCondition));
}
else if (pMandatory)
{
......@@ -955,7 +958,7 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
* }
* @return {SqlBuilder} current SqlBuilder object
*/
SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFieldType)
SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCond, pFieldType, pAddPreparedConditionCallback)
{
// For now: treat "" as null just like db.insert, db.table etc. Maybe otion for later: more pMandatory-types
if (pValue === "")
......@@ -964,34 +967,48 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
if (pMandatory === undefined)
pMandatory = true;
// // just call the andCondition function if it is only a Condition
// if (!pFieldOrCond !== undefined && (pValue === undefined) && pCond === undefined && pFieldType === undefined)
// {
// return this.andCondition(pFieldOrCond, pMandatory);
// }
// just call the andCondition function if it is only a Condition
if (!pFieldOrCond !== undefined && (pValue === undefined) && pCond === undefined && pFieldType === undefined)
{
return this._whereCondition(pFieldOrCond, pMandatory, pAddPreparedConditionCallback, true);
}
// first check the default-mandatory-cases: null or undefined. everything else such as ch3ecking $-variables is done later
// first check the default-mandatory-cases: null or undefined. everything else such as checking $-variables is done later
if (pMandatory && !(pValue === false || pValue))
throw SqlBuilder.ERROR_VALUE_IS_MANDATORY();
// a field and a value is given OR pFieldOrSQL == null and pValue instanceof SqlBuilder and pCond is given -> preparedSQL
if((typeof pFieldOrCond == "string" || Array.isArray(pFieldOrCond)) || (!pFieldOrCond && pCond))
// a field is string or array -> normal case
// OR !pFieldOrCond and pValue and pCond is given -> preparedSQL/SqlBuilder can be used without field if pCond is set
if((typeof pFieldOrCond == "string" || Array.isArray(pFieldOrCond)) || (!pFieldOrCond && (pCond && pValue instanceof SqlBuilder || !(pValue instanceof SqlBuilder))))
{
var field = pFieldOrCond;
var typeofValue = typeof pValue;
// pValue can be...
// ... a SqlBuilder -> it is a SqlBuilder containing a complete subquery
if (pValue instanceof SqlBuilder || Array.isArray(pValue) || (typeofValue == "string" && (pFieldOrCond == undefined || pFieldOrCond == null)))
{
if (pFieldOrCond !== null && pFieldOrCond !== undefined)
{
if (!pCond)
pCond = "# = ?"
pCond = SqlUtils.replaceConditionTemplate(pCond, '#', SqlUtils.parseField(pFieldOrCond)[0])
}
return this.andSubquery(pValue, pMandatory, pCond)
else
{
if (!pCond)
pCond = "?"
}
return this._whereSubquery(pValue, pMandatory, pCond, pAddPreparedConditionCallback);
}
if (!pCond)
pCond = "# = ?"
// ... a string starting with $ -> jdito varable which has to be resolved
if (typeofValue == "string" && pValue[0] == "$")
{
......@@ -1004,7 +1021,7 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
if (pValue)
{
this.andCondition(this._prepare(field, pValue, pCond, pFieldType));
this._whereCondition(this._prepare(field, pValue, pCond, pFieldType), undefined, pAddPreparedConditionCallback);
}
return this;
}
......@@ -1017,7 +1034,7 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
// ... everything else -> just pass it
if (pValue === false || pValue)
this.andCondition(this._prepare(field, pValue, pCond, pFieldType));
this._whereCondition(this._prepare(field, pValue, pCond, pFieldType), undefined, pAddPreparedConditionCallback);
return this;
}
......@@ -1025,6 +1042,93 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
throw SqlBuilder.ERROR_UNSUPPORTED_PARAMETER_COMBINATION();
}
SqlBuilder.prototype._and = function(pFieldOrCond, pValue, pMandatory, pCond, pFieldType)
{
var that = this;
return this._addWhere(pFieldOrCond, pValue, pMandatory, pCond, pFieldType, function(pPreparedCondition) {
that._where._previouslyOnlyOr = false;
if (pPreparedCondition.length == 2 && typeof pPreparedCondition[0] == "string" && pPreparedCondition[0] != "" && Array.isArray(pPreparedCondition[1]))
{
if (that.hasCondition())
that._where._sqlStorage += " and ";
that._where._sqlStorage += pPreparedCondition[0];
that._where.preparedValues = that._where.preparedValues.concat(pPreparedCondition[1]);
}
})
}
SqlBuilder.prototype._or = function(pFieldOrCond, pValue, pMandatory, pCond, pFieldType)
{
var that = this;
return this._addWhere(pFieldOrCond, pValue, pMandatory, pCond, pFieldType, function(pPreparedCondition, pAlreadySurroundedByBrackets) {
if (pPreparedCondition.length == 2 && typeof pPreparedCondition[0] == "string" && pPreparedCondition[0] != "" && Array.isArray(pPreparedCondition[1]))
{
if (that._where._previouslyOnlyOr)
{
that._where._sqlStorage = that._where._sqlStorage + " or " + pPreparedCondition[0];
that._where._lastWasOr = true;
}
else if (that.hasCondition() && !that._where._lastWasOr)
{
var cond = pPreparedCondition[0];
if (!pAlreadySurroundedByBrackets)
cond = "(" + pPreparedCondition[0] + ")";
that._where._sqlStorage = "(" + that._where._sqlStorage + ") or " + cond;
that._where._lastWasOr = true;
}
else if (that.hasCondition() && that._where._lastWasOr)
{
var cond = pPreparedCondition[0];
if (!pAlreadySurroundedByBrackets)
cond = "(" + pPreparedCondition[0] + ")";
that._where._sqlStorage = that._where._sqlStorage + " or " + cond;
that._where._lastWasOr = true;
}
else
{
if (!that.hasCondition())
that._where._previouslyOnlyOr = true;
that._where._sqlStorage = pPreparedCondition[0];
}
that._where.preparedValues = that._where.preparedValues.concat(pPreparedCondition[1]);
}
})
}
SqlBuilder.prototype.or = function(pFieldOrCond, pValue, pCond, pFieldType)
{
return this._or(pFieldOrCond, pValue, true, pCond, pFieldType);
}
SqlBuilder.prototype.orIfSet = function(pFieldOrCond, pValue, pCond, pFieldType)
{
return this._or(pFieldOrCond, pValue, false, pCond, pFieldType);
}
SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pCond, pFieldType)
{
return this._and(pFieldOrCond, pValue, true, pCond, pFieldType);
}
SqlBuilder.prototype.andIfSet = function(pFieldOrCond, pValue, pCond, pFieldType)
{
return this._and(pFieldOrCond, pValue, false, pCond, pFieldType);
}
/**
* Sets the order by clause of the sql.
* @param {String} pOrderBy
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment