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

add methods wrapping db. functions to sqlbuilder

parent 994b2cb0
No related branches found
No related tags found
No related merge requests found
......@@ -330,13 +330,23 @@ var ifSetTests = [
pTester.assert(0, actual._where.preparedValues.length, "no params should be added");
}],
["empty subqueries", function(pTester)
["empty simple conditions", function(pTester)
{
var actual = new SqlBuilder()
.orIfSet("")
.andIfSet(["", []])
.andIfSet(new SqlBuilder())
pTester.assert("", actual._where._sqlStorage, "no sql should be added");
pTester.assert(0, actual._where.preparedValues.length, "no params should be added");
}],
["empty subqueries", function(pTester)
{
var actual = new SqlBuilder()
.orIfSet("PERSON.FIRSTNAME", ["", []])
.andIfSet("PERSON.LASTNAME", new SqlBuilder())
pTester.assert("", actual._where._sqlStorage, "no sql should be added");
pTester.assert(0, actual._where.preparedValues.length, "no params should be added");
}]
......
......@@ -754,10 +754,15 @@ SqlBuilder.begin = function(pAlias)
* @return {String} the sql as string
*/
SqlBuilder.prototype.toString = function()
{
if (this.build()[0] !== "")
{
var built = this.build()
if (built[0] !== "")
{
return SqlUtils.translateStatementWithQuotes(this.build());
if (!this.isFullSelect() && this.hasCondition())
return SqlUtils.translateConditionWithQuotes(built, this.alias);
else
return SqlUtils.translateStatementWithQuotes(built, this.alias);
}
return "";
......@@ -840,7 +845,7 @@ 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
* @deprecated use .and()
*/
SqlBuilder.prototype.where = function(pCondition)
{
......@@ -895,13 +900,20 @@ SqlBuilder.prototype._whereCondition = function(pCondition, pMandatory, pAddPrep
{
// add only brackets if needed
var sqlString = sql._where._sqlStorage;
if (pBrackets)
sqlString = " ( " + sqlString + " ) ";
var condString = sqlString;
if (condString.trim() != "")
{
if (pBrackets)
condString = " ( " + condString + " ) ";
pAddPreparedConditionCallback([condString, sql._where.preparedValues], pBrackets);
return this;
}
else if (pMandatory)
{
throw SqlBuilder.ERROR_INVALID_CONDITION_VALUE_TYPE();
}
return this;
}
......@@ -925,7 +937,15 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
// Both can be handled by _prepare
if (Array.isArray(sql) || typeofSql == "string")
{
pAddPreparedConditionCallback(this._prepare(undefined, sql, pCondition));
if (sql[0])
{
pAddPreparedConditionCallback(this._prepare(undefined, sql, pCondition));
}
else if (pMandatory)
{
throw SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE();
}
return this;
}
......@@ -939,7 +959,8 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
{
throw SqlBuilder.ERROR_NO_CONDITION();
}
logging.log(JSON.stringify([subQuery], null, "\t"))
if (subQuery.isFullSelect())
{
var preparedObj = subQuery.build();
......@@ -1087,7 +1108,7 @@ SqlBuilder.prototype._or = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
}
else if (that.hasCondition() && !that._where._lastWasOr)
{
var cond = pPreparedCondition[0];
let cond = pPreparedCondition[0];
if (!pAlreadySurroundedByBrackets)
cond = "(" + pPreparedCondition[0] + ")";
......@@ -1098,7 +1119,7 @@ SqlBuilder.prototype._or = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
}
else if (that.hasCondition() && that._where._lastWasOr)
{
var cond = pPreparedCondition[0];
let cond = pPreparedCondition[0];
if (!pAlreadySurroundedByBrackets)
cond = "(" + pPreparedCondition[0] + ")";
......@@ -1397,12 +1418,142 @@ SqlBuilder.prototype.build = function()
* It resolves all prepared values.
* @param {String} [pAlias=undefined] the alias to use for db.translateStatement
* @return {String} plain SQL statement
*
* @deprecated use .toString()
*/
SqlBuilder.prototype.translate = function(pAlias)
SqlBuilder.prototype.translate = function()
{
return this.toString();
}
SqlBuilder.prototype.updateData = function(pExecuteOnlyIfConditionExists, pTableName, pColumns, pColumnTypes, pValues, pTimeout)
{
if (this._checkForDelete(pExecuteOnlyIfConditionExists))
{
db.updateData(
(pTableName ? pTableName : this._from),
pColumns,
pColumnTypes,
pValues,
this.buildCondition(),
(this.alias ? this.alias : db.getCurrentAlias()),
(pTimeout ? pTimeout : -1));
}
}
SqlBuilder.prototype.deleteData = function(pExecuteOnlyIfConditionExists, pTableName, pTimeout)
{
if (this._checkForUpdate(pExecuteOnlyIfConditionExists))
{
return db.deleteData(
(pTableName ? pTableName : this._from),
this.buildCondition(),
(this.alias ? this.alias : db.getCurrentAlias()),
(pTimeout ? pTimeout : -1));
}
else
{
return 0;
}
}
SqlBuilder.prototype.cell = function(pExecuteOnlyIfConditionExists)
{
if (this._checkForSelect(pExecuteOnlyIfConditionExists))
{
return db.cell(this.build(),
(this.alias ? this.alias : db.getCurrentAlias()));
}
else
{
return "";
}
}
SqlBuilder.prototype.array = function(pType, pExecuteOnlyIfConditionExists, pMaxRows, pTimeout)
{
return SqlUtils.translateStatementWithQuotes(this.build(), pAlias);
if (this._checkForSelect(pExecuteOnlyIfConditionExists))
{
return db.array(pType, this.build(),
(this.alias ? this.alias : db.getCurrentAlias()),
(pMaxRows ? pMaxRows : 0),
(pTimeout ? pTimeout : -1));
}
else
{
return [];
}
}
SqlBuilder.prototype.arrayPage = function(pStartIndex, pRowCount, pExecuteOnlyIfConditionExists, pTimeout)
{
if (this._checkForSelect(pExecuteOnlyIfConditionExists))
{
return db.arrayPage(pType, this.build(),
(this.alias ? this.alias : db.getCurrentAlias()),
pStartIndex,
pRowCount,
(pTimeout ? pTimeout : -1));
}
else
{
return [];
}
}
SqlBuilder.prototype.table = function(pExecuteOnlyIfConditionExists, pMaxRows, pTimeout)
{
if (this._checkForSelect(pExecuteOnlyIfConditionExists))
{
return db.table(this.build(),
(this.alias ? this.alias : db.getCurrentAlias()),
(pMaxRows ? pMaxRows : 0),
(pTimeout ? pTimeout : -1));
}
else
{
return [];
}
}
SqlBuilder.prototype.tablePage = function(pStartIndex, pRowCount, pExecuteOnlyIfConditionExists, pTimeout)
{
if (this._checkForSelect(pExecuteOnlyIfConditionExists))
{
return db.tablePage(this.build(),
(this.alias ? this.alias : db.getCurrentAlias()),
pStartIndex,
pRowCount,
(pTimeout ? pTimeout : -1));
}
else
{
return [];
}
}
SqlBuilder.prototype._checkForUpdate = function(pExecuteOnlyIfConditionExists) {
if (pExecuteOnlyIfConditionExists == undefined)
pExecuteOnlyIfConditionExists = true;
return !pExecuteOnlyIfConditionExists || this.hasCondition();
}
SqlBuilder.prototype._checkForSelect = function(pExecuteOnlyIfConditionExists) {
if (pExecuteOnlyIfConditionExists == undefined)
pExecuteOnlyIfConditionExists = false;
if (this.isFullSelect())
{
return !pExecuteOnlyIfConditionExists || this.hasCondition();
}
else
{
// TODO: error
throw new Error()
}
}
/**
*provides functions for masking sql functions
......@@ -2362,8 +2513,14 @@ SqlUtils.translateWithQuotes = function(pStatement, pExecutionCallback) {
* @param {[String, String[]]} pStatement Same as the first parameter of db.translateStatement.
* @returns {String} The SQL, same as the result of db.translateStatement.
*/
SqlUtils.translateStatementWithQuotes = function(pStatement) {
return SqlUtils.translateWithQuotes(pStatement, function(pValue) {return db.translateStatement(pValue)});
SqlUtils.translateStatementWithQuotes = function(pStatement, pAlias) {
return SqlUtils.translateWithQuotes(pStatement, function(pValue)
{
if (pAlias)
return db.translateStatement(pValue, pAlias)
else
return db.translateStatement(pValue)
});
}
/**
......@@ -2371,8 +2528,14 @@ SqlUtils.translateStatementWithQuotes = function(pStatement) {
* @param {[String, String[]]} pStatement Same as the first parameter of db.translateCondition.
* @returns {String} The SQL, same as the result of db.translateCondition.
*/
SqlUtils.translateConditionWithQuotes = function(pStatement) {
return SqlUtils.translateWithQuotes(pStatement, function(pValue) {return db.translateCondition(pValue)});
SqlUtils.translateConditionWithQuotes = function(pStatement, pAlias) {
return SqlUtils.translateWithQuotes(pStatement, function(pValue)
{
if (pAlias)
return db.translateCondition(pValue, pAlias)
else
return db.translateCondition(pValue)
});
}
SqlUtils.parseField = function(pField)
......
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