Skip to content
Snippets Groups Projects
Commit 15032057 authored by S.Listl's avatar S.Listl
Browse files

SqlBuilder insert functions

parent 7ba451b7
No related branches found
No related tags found
No related merge requests found
import("system.neon");
import("system.db");
import("system.vars"); import("system.vars");
import("Sql_lib");
var rowdata = vars.get("$local.rowdata"); var rowdata = vars.get("$local.rowdata");
var columns = [
"AB_ATTRIBUTEID",
"ATTRIBUTE_ACTIVE",
"ATTRIBUTE_NAME",
"ATTRIBUTE_PARENT_ID",
"ATTRIBUTE_TYPE",
"DROPDOWNDEFINITION",
"DROPDOWNFILTER",
"SORTING"
];
var values = [
rowdata["UID.value"],
rowdata["ATTRIBUTE_ACTIVE.value"] || "",
rowdata["ATTRIBUTE_NAME.value"] || "",
rowdata["ATTRIBUTE_PARENT_ID.value"] || "",
rowdata["ATTRIBUTE_TYPE.value"] || "",
rowdata["DROPDOWNDEFINITION.value"] || "",
rowdata["DROPDOWNFILTER.value"] || "",
rowdata["SORTING.value"] || ""
];
db.insertData("AB_ATTRIBUTE", columns, null, values); new SqlBuilder().insertFields({
\ No newline at end of file "AB_ATTRIBUTEID" : rowdata["UID.value"],
"ATTRIBUTE_PARENT_ID" : rowdata["ATTRIBUTE_PARENT_ID.value"],
"DROPDOWNDEFINITION" : rowdata["DROPDOWNDEFINITION.value"],
"ATTRIBUTE_ACTIVE" : rowdata["ATTRIBUTE_ACTIVE.value"],
"ATTRIBUTE_NAME" : rowdata["ATTRIBUTE_NAME.value"],
"ATTRIBUTE_TYPE" : rowdata["ATTRIBUTE_TYPE.value"],
"DROPDOWNFILTER" : rowdata["DROPDOWNFILTER.value"],
"SORTING" : rowdata["SORTING.value"]
}, "AB_ATTRIBUTE");
\ No newline at end of file
...@@ -385,6 +385,27 @@ newWhere("SALESORDER.SALESORDERID", "$field.SALESORDERID") ...@@ -385,6 +385,27 @@ newWhere("SALESORDER.SALESORDERID", "$field.SALESORDERID")
.updateFields({"ORDERSTATUS" : "1"}); .updateFields({"ORDERSTATUS" : "1"});
---- ----
==== insert-functions
SqlBuilder also provides functions for inserts which don't require a where-condition, they can be called on a `new SqlBuilder()` directly.
The function `insertData()` is a wrapper for `db.insertData()` and has similar parameters:
[source,js]
----
new SqlBuilder()
.insertData("AB_OBJECTRELATION", ["AB_OBJECTRELATIONID", "INFO"], null, [util.getNewUUID(), vars.get("$field.INFO")]);
----
To make the code more readable and more compact, there is another function, `insertFields()`. The syntax is similar to `updateFields()`, you have to pass the columns and values as an object. You can also set the parameter `pAutoUidField`, then the given column will be set to a random UUID:
[source,js]
----
new SqlBuilder().insertFields({
"ACTIVITY_ID" : pActivityId,
"OBJECT_ROWID" : pRowId,
"OBJECT_TYPE" : pObjectType
}, "ACTIVITYLINK", "ACTIVITYLINKID");
----
<<< <<<
== Examples and use cases == Examples and use cases
......
...@@ -789,7 +789,7 @@ function SqlBuilder (pAlias) ...@@ -789,7 +789,7 @@ function SqlBuilder (pAlias)
this.alias = pAlias; this.alias = pAlias;
this._subselectAlias = null; this._subselectAlias = null;
this._where = {}; this._where = {};
this._initWhere(); this._initWhere();
} }
...@@ -2341,6 +2341,7 @@ SqlBuilder.prototype.updateData = function(pExecuteOnlyIfConditionExists, pTable ...@@ -2341,6 +2341,7 @@ SqlBuilder.prototype.updateData = function(pExecuteOnlyIfConditionExists, pTable
* @param {Object} pFieldValues Object with the columns to update as keys mapped to their values * @param {Object} pFieldValues Object with the columns to update as keys mapped to their values
* @param {String} [pTableName] The table for updating data. If undefined, the from part of the SqlBuilder will be used (works only if it is a tablename). If no from is set, * @param {String} [pTableName] The table for updating data. If undefined, the from part of the SqlBuilder will be used (works only if it is a tablename). If no from is set,
* the table of the first where-condition is used. * the table of the first where-condition is used.
* @return {Number} the number of rows affected
* @example * @example
* newWhere("SALESORDER.SALESORDERID", "$field.SALESORDERID") * newWhere("SALESORDER.SALESORDERID", "$field.SALESORDERID")
* .updateFields({"ORDERSTATUS" : "1"}); //pTableName can be omitted here since it's clearly defined by the given condition * .updateFields({"ORDERSTATUS" : "1"}); //pTableName can be omitted here since it's clearly defined by the given condition
...@@ -2362,6 +2363,74 @@ SqlBuilder.prototype.updateFields = function (pFieldValues, pTableName) ...@@ -2362,6 +2363,74 @@ SqlBuilder.prototype.updateFields = function (pFieldValues, pTableName)
return this.updateData(true, pTableName, columns, null, values); return this.updateData(true, pTableName, columns, null, values);
} }
/**
* Inserts data in the database. This function doesn't require any where-condition, it is intended to be called right after 'new SqlBuilder()'. <br/>
*
* @param {String} [pTableName] The table for inserting data. If undefined, the from part of the SqlBuilder will be used (works only if it is a tablename). If no from is set,
* the table of the first where-condition is used.
* @param {String[]} pColumns The columns where you want to insert into.
* @param {SQLTYPES[]} [pColumnTypes=null] normally you can set this to null as the types are calculated if not provided
* @param {String[]} pValues The values to be inserted.
* @param {Number} [pTimeout=-1]
* @return {Number} the number of rows affected
* @throws {Error} if no table is defined
*/
SqlBuilder.prototype.insertData = function(pTableName, pColumns, pColumnTypes, pValues, pTimeout)
{
if (!pTableName && !this._tableName)
throw SqlBuilder._ERROR_NO_TABLE();
if (!pColumns)
pColumns = null;
return db.insertData(
(pTableName ? pTableName : this._tableName),
pColumns,
pColumnTypes,
pValues,
(this.alias ? this.alias : db.getCurrentAlias()),
(pTimeout ? pTimeout : -1));
}
/**
* Inserts data in the database. This function calls SqlBuilder.prototype.insertData, but provides a shorter syntax to
* improve the readability.
*
* @param {Object} pFieldValues Object with the columns to update as keys mapped to their values
* @param {String} [pTableName] The table for updating data. If undefined, the from part of the SqlBuilder will be used (works only if it is a tablename). If no from is set,
* the table of the first where-condition is used.
* @param {String} [pAutoUidField] UID column that should be filled with a random UUID
* @return {Number} the number of rows affected
* @example
* new SqlBuilder().insertFields({
* "ACTIVITY_ID" : pActivityId,
* "OBJECT_ROWID" : pRowId,
* "OBJECT_TYPE" : pObjectType
* }, "ACTIVITYLINK", "ACTIVITYLINKID");
*/
SqlBuilder.prototype.insertFields = function (pFieldValues, pTableName, pAutoUidField)
{
if (!pFieldValues || typeof(pFieldValues) !== "object")
throw SqlBuilder._ERROR_UPDATE_VALUES_INVALID;
if (pAutoUidField)
pFielsValues[pAutoUidField] = util.getNewUUID();
var columns = [];
var values = [];
for (let field in pFieldValues)
{
if (pFieldValues[field] !== undefined && pFieldValues[field] !== null)
{
columns.push(field);
values.push(pFieldValues[field].toString());
}
}
if (columns.length === 0)
return 0;
return this.insertData(pTableName, columns, null, values);
}
/** /**
* Deletes data from the database.<br/> * Deletes data from the database.<br/>
* Note: the default for pExecuteOnlyIfConditionExists is true to prevent updating all rows if the SqlBuilder has no condition. * Note: the default for pExecuteOnlyIfConditionExists is true to prevent updating all rows if the SqlBuilder has no condition.
......
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