diff --git a/process/Campaign_lib/process.js b/process/Campaign_lib/process.js index a5bc33f35db385f78334c42b552933c1aea9dd05..598b0411b20cc51c62966a8a2be3269428bbf285 100644 --- a/process/Campaign_lib/process.js +++ b/process/Campaign_lib/process.js @@ -341,12 +341,10 @@ CampaignUtils.GetParticipantsAlreadyAddedCountByCondition = function(pWhereCondi } CampaignUtils.GetContactIdsNotInCampaignByRowIds = function(pCampaignId, pParticipantRowIds) -{ - let rowIdsAsRay = _CampaignUtils._convertToSqlValuesList(pParticipantRowIds); - +{ return newSelect("CONTACT.CONTACTID") .from("CONTACT") - .where("CONTACT.CONTACTID", rowIdsAsRay, SqlBuilder.IN()) + .where("CONTACT.CONTACTID", pParticipantRowIds, SqlBuilder.IN()) .and("CONTACT.CONTACTID", newSelect("CAMPAIGNPARTICIPANT.CONTACT_ID") .from("CAMPAIGNPARTICIPANT") @@ -507,17 +505,4 @@ _CampaignUtils._openAddParticipantContext = function(pContext, pTargetDataExpres params["dataSourceTableName_param"] = pSourceTableName; neon.openContext(pContext, null, null, neon.OPERATINGSTATE_NEW, params); -} - -_CampaignUtils._convertToSqlValuesList = function(pValuesArray) -{ - let rowIds = "("; - for (i = 0; i < pValuesArray.length; i++) - { - rowIds += "'" + pValuesArray[i] + "'"; - if(i < pValuesArray.length-1) - rowIds += ","; - } - rowIds += ")"; - return rowIds; -} +} \ No newline at end of file diff --git a/process/Communication_lib/process.js b/process/Communication_lib/process.js index 05aa83f61f043ca118d03bd8cddc935b6a057170..74aad0b75f22a1eaa22fe662e95dd798a0109475 100644 --- a/process/Communication_lib/process.js +++ b/process/Communication_lib/process.js @@ -67,10 +67,11 @@ CommUtil.setStandardForCategory = function(pAffectedRowId, pNewStandardCommId, p if (pNewStandardCommId != "") { //set the new standard comm-record - cond = newWhere("COMMUNICATION.COMMUNICATIONID", pNewStandardCommId); - //check communicationid, contactId and medium to prevent data-inconsistency when bad function params are passed by (e.g communicationid of a different category) - cond.andPrepare().andPrepare("COMMUNICATION.CONTACT_ID", pAffectedRowId); - cond.and("MEDIUM_ID in ('" + mediumIds.join("', '") + "')"); + cond = newWhere("COMMUNICATION.COMMUNICATIONID", pNewStandardCommId) + //check communicationid, contactId and medium to prevent data-inconsistency when bad function params are passed by (e.g communicationid of a different category) + .and("COMMUNICATION.CONTACT_ID", pAffectedRowId) + .and("COMMUNICATION.MEDIUM_ID", mediumIds, SqlBuilder.IN()); + statements.push(["COMMUNICATION", cols, types, ["1"], cond.build()]); } diff --git a/process/Context_lib/process.js b/process/Context_lib/process.js index 793ff376e29af696557d3c99408415745398b23c..f9693dc4738035fd967d2d449c8d985535532253 100644 --- a/process/Context_lib/process.js +++ b/process/Context_lib/process.js @@ -534,6 +534,8 @@ ContextUtils.getSelectMap = function() */ ContextUtils.getNameSubselectSql = function(pContextIdDbField, pRowIdDbField) { + // TODO: prepared? + var select = "(case " + pContextIdDbField + " "; var selectMap = ContextUtils.getSelectMap () diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js index 35d5c636af49928b03d82c7981e0f350a9e0be61..1ec852b8ea81e336ae2bf28beca2f22b94889d5b 100644 --- a/process/DuplicateScanner_lib/process.js +++ b/process/DuplicateScanner_lib/process.js @@ -32,11 +32,13 @@ function DuplicateScannerUtils() {} */ DuplicateScannerUtils.loadFilters = function(pFilterName, pTargetEntity) { - let query = "select FILTER_CONDITION, COUNT_CHARACTERS_TO_USE, MAX_RESULTS_THRESHOLD from DUPLICATESCANNERPREFILTERCONFIG" - + " join DUPLICATESCANNER on DUPLICATESCANNER.ID = DUPLICATESCANNERPREFILTERCONFIG.DUPLICATESCANNER_ID" - + " where FILTER_NAME = '" + pFilterName + "'" - + " and ENTITY_TO_SCAN_NAME = '" + pTargetEntity + "'"; - return db.table(query); + let query = newSelect("FILTER_CONDITION, COUNT_CHARACTERS_TO_USE, MAX_RESULTS_THRESHOLD") + .from("DUPLICATESCANNERPREFILTERCONFIG") + .join("DUPLICATESCANNER", "DUPLICATESCANNER.ID = DUPLICATESCANNERPREFILTERCONFIG.DUPLICATESCANNER_ID") + .where("DUPLICATESCANNER.FILTER_NAME", pFilterName) + .and("DUPLICATESCANNER.ENTITY_TO_SCAN_NAME", pFilterName); + + return query.table(); } /* @@ -48,10 +50,14 @@ DuplicateScannerUtils.loadFilters = function(pFilterName, pTargetEntity) */ DuplicateScannerUtils.DeleteCachedDuplicate = function(pDuplicateId) { - let query = "select count(ID), CLUSTERID from DUPLICATECLUSTERS" - + " where CLUSTERID in (select CLUSTERID from DUPLICATECLUSTERS where DUPLICATEID = '"+ pDuplicateId +"')" - + " and DUPLICATEID != '"+ pDuplicateId +"'" - + " group by CLUSTERID"; + let query = newSelect("count(ID), CLUSTERID from DUPLICATECLUSTERS") + .from("DUPLICATECLUSTERS") + .where("DUPLICATECLUSTERS.CLUSTERID", newSelect("CLUSTERID") + .from("DUPLICATECLUSTERS") + .where("DUPLICATECLUSTERS.DUPLICATEID", pDuplicateId), + SqlBuilder.IN()) + .and("DUPLICATECLUSTERS.DUPLICATEID", pDuplicateId, "# != ?") + .groupBy("CLUSTERID"); let coundAndClusterId = db.array(db.ROW, query); let countDuplicatesInClusterWithoutParameterId = coundAndClusterId[0]; @@ -64,14 +70,16 @@ DuplicateScannerUtils.DeleteCachedDuplicate = function(pDuplicateId) if(countDuplicatesInClusterWithoutParameterId <= 1) { let deleteStatements = []; - deleteStatements.push(["DUPLICATECLUSTERS", "DUPLICATECLUSTERS.CLUSTERID = '"+ clusterId +"'"]); - deleteStatements.push(["UNRELATEDDUPLICATES", "UNRELATEDDUPLICATES.CLUSTERID = '"+ clusterId +"'"]); + deleteStatements.push(["DUPLICATECLUSTERS", newWhere("DUPLICATECLUSTERS.CLUSTERID", clusterId).build()]); + deleteStatements.push(["UNRELATEDDUPLICATES", newWhere("UNRELATEDDUPLICATES.CLUSTERID", clusterId).build()]); db.deletes(deleteStatements); } else { - db.deleteData("DUPLICATECLUSTERS", "DUPLICATECLUSTERS.DUPLICATEID = '"+ pDuplicateId +"'"); + newWhereIfSet("DUPLICATECLUSTERS.DUPLICATEID", pDuplicateId) + .deleteData(true, "DUPLICATECLUSTERS"); + //Delete all records where this duplicateId is mentioned DuplicateScannerUtils.DeleteAllUnrelatedDuplicateRelations(pDuplicateId); } @@ -86,7 +94,8 @@ DuplicateScannerUtils.DeleteCachedDuplicate = function(pDuplicateId) */ DuplicateScannerUtils.DeleteDuplicateClustersByTargetEntity = function(pTargetEntity) { - return db.deleteData("DUPLICATECLUSTERS", "DUPLICATECLUSTERS.TARGET_ENTITY = '"+ pTargetEntity +"'") + return newWhereIfSet("DUPLICATECLUSTERS.TARGET_ENTITY", pTargetEntity) + .deleteData(true, "DUPLICATECLUSTERS"); } /* @@ -106,12 +115,13 @@ DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations = function(pTargetEntit let INDEX_NEW_CLUSTERID = 0; let INDEX_OLD_CLUSTERID = 1; - let query = "select dc1.CLUSTERID, ud.CLUSTERID from UNRELATEDDUPLICATES ud" - + " join DUPLICATECLUSTERS dc1 on dc1.DUPLICATEID = ud.SOURCEDUPLICATEID" - + " join DUPLICATECLUSTERS dc2 on dc2.DUPLICATEID = ud.UNRELATEDDUPLICATEID" - + " where dc1.TARGET_ENTITY = '" + pTargetEntity + "'"; + let query = newSelect("dc1.CLUSTERID, ud.CLUSTERID") + .from("UNRELATEDDUPLICATES", "ud") + .join("DUPLICATECLUSTERS", "dc1.DUPLICATEID = ud.SOURCEDUPLICATEID", "dc1") + .join("DUPLICATECLUSTERS", "dc2.DUPLICATEID = ud.UNRELATEDDUPLICATEID", "dc2") + .where(["DUPLICATECLUSTERS", "TARGET_ENTITY", "dc1"], pTargetEntity); - let newIdOldIdRay = db.table(query); + let newIdOldIdRay = query.table(); let updateStatements = []; //Build update statements to set new clusterIds @@ -120,9 +130,8 @@ DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations = function(pTargetEntit let newClusterId = newIdOldIdRay[i][INDEX_NEW_CLUSTERID]; let oldClusterId = newIdOldIdRay[i][INDEX_OLD_CLUSTERID]; let updateColumns = ["CLUSTERID"]; - let condition = "UNRELATEDDUPLICATES.CLUSTERID = '" + oldClusterId + "'"; - let updateStatement = ["UNRELATEDDUPLICATES", updateColumns, null, [newClusterId], condition]; + let updateStatement = ["UNRELATEDDUPLICATES", updateColumns, null, [newClusterId], newWhere("UNRELATEDDUPLICATES.CLUSTERID", oldClusterId).build()]; updateStatements.push(updateStatement); } @@ -132,8 +141,8 @@ DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations = function(pTargetEntit * All unrelated duplicate ids that still exist in a cluster, have been updated with the new cluster id. * All records with a nonexistend clusterid can now be deleted because they haven't been detected as a duplicate any more. */ - let deleteCondition = "CLUSTERID not in (select dc1.CLUSTERID from DUPLICATECLUSTERS dc1)"; - db.deleteData("UNRELATEDDUPLICATES", deleteCondition); + newWhere("UNRELATEDDUPLICATES.CLUSTERID", newSelect("dc1.CLUSTERID").from("DUPLICATECLUSTERS", "dc1"), SqlBuilder.NOT_IN()) + .deleteData(true, "UNRELATEDDUPLICATES"); } /* @@ -232,7 +241,7 @@ DuplicateScannerUtils.GetCachedDuplicatesForClusterId = function(pClusterId) .from("DUPLICATECLUSTERS") .where("DUPLICATECLUSTERS.DUPLICATEID", newSelect("UNRELATEDDUPLICATEID").from("UNRELATEDDUPLICATES"), SqlBuilder.NOT_IN()) - .and("DUPLICATECLUSTERS.CLUSTERID", clusterIdRay) + .and("DUPLICATECLUSTERS.CLUSTERID", pClusterId) .arrayColumn(); } @@ -330,7 +339,7 @@ DuplicateScannerUtils.ScanRecords = function(pTargetEntity, pTargetRecordsData, //If the contact id loading query has no results, stop. //No ids should be deleted if an error has been made in this query. if(pTargetRecordsData.length <= 0) - return; + return undefined; //First it gets checked if the current id has already been identified. If that's the case it'll continue with the next. //Otherwise an object gets build in the form of ["FilterFieldName" = "FilterFieldValueFromQuery"] with which a scan for possible duplicates get's started @@ -631,7 +640,10 @@ DuplicateScannerUtils.MergePerson = function(pSourceContactId, pTargetContactId) let updateStatementsSystemAlias = []; let deleteStatements = []; - var sourcePersonId = db.cell("select PERSON_ID from CONTACT where CONTACTID = '" + pSourceContactId + "'"); + var sourcePersonId = newSelect("PERSON_ID") + .from("CONTACT") + .where("CONTACT.CONTACTID", pSourceContactId) + .cell(); var tableInfosCurrentAlias = _DuplicateScannerUtils._getMergeUpdateTableInfosCurrentAlias(); var tableInfosSystemAlias = _DuplicateScannerUtils._getMergeUpdateTableInfosSystemAlias(); @@ -757,7 +769,7 @@ var INDEX_CONDITION = 2; _DuplicateScannerUtils._buildUpdateResetStandardCommunications = function(pSourceContactId) { - return [["COMMUNICATION", ["ISSTANDARD"], null, ["0"], "CONTACT_ID = '" + pSourceContactId + "'"]]; + return [["COMMUNICATION", ["ISSTANDARD"], null, ["0"], newWhere("COMMUNICATION.CONTACT_ID", pSourceContactId).build()]]; } /* @@ -988,16 +1000,14 @@ _DuplicateScannerUtils._deleteDuplicateClusters = function () */ _DuplicateScannerUtils._buildUpdateAttachParticipantsToNewContactQuery = function (pTableName, pContactIdColumn, pAssignableIdColumn, pSourceContactId, pTargetContactId, updateStatements) { - var selectAssignableIdsOfTargetContactQuery = "select " + pAssignableIdColumn - + " from " + pTableName - + " where " + pContactIdColumn + " = '" + pTargetContactId + "'"; + var selectAssignableIdsOfTargetContactQuery = newSelect(pAssignableIdColumn) + .from(pTableName) + .where([pTableName, pContactIdColumn], pTargetContactId); - let updateCondition = pAssignableIdColumn - + " not in" - + " (" + selectAssignableIdsOfTargetContactQuery + ")" - + " and " + pContactIdColumn + " = '" + pSourceContactId + "'"; + let updateCondition = newWhere([pTableName, pAssignableIdColumn], selectAssignableIdsOfTargetContactQuery, SqlBuilder.NOT_IN()) + .and([pTableName, pContactIdColumn], pSourceContactId) - return [[pTableName, [pContactIdColumn], null, [pTargetContactId], updateCondition]]; + return [[pTableName, [pContactIdColumn], null, [pTargetContactId], updateCondition.build()]]; } @@ -1007,15 +1017,15 @@ _DuplicateScannerUtils._buildDeleteRemoveObsoleteParticipantsRecordsQuery = func //DELETE FROM CAMPAIGNPARTICIPANT // WHERE ( CAMPAIGN_ID in (select ab.CAMPAIGN_ID from (select CAMPAIGN_ID, CONTACT_ID from CAMPAIGNPARTICIPANT) ab where ab.CONTACT_ID = '64a51ec3-e75d-4415-8aa2-a00a1e9be0b0') and CAMPAIGN_ID = '51960918-3b24-4bac-8f1c-3892bf210f6d') - var selectAssignableIdsOfTargetContactQuery = "select " + pAssignableIdColumn - + " from " + pTableName - + " where " + pContactIdColumn + " = '" + pTargetContactId + "'"; - - let deleteCondition = pAssignableIdColumn + " in" - + " (" + selectAssignableIdsOfTargetContactQuery + ")" - + " and " + pAssignableIdColumn + " = '" + pSourceContactId + "'"; + var selectAssignableIdsOfTargetContactQuery = newSelect(pAssignableIdColumn) + .from(pTableName) + .where([pTableName, pContactIdColumn], pTargetContactId); + + let deleteCondition = newWhere([pTableName, pAssignableIdColumn], selectAssignableIdsOfTargetContactQuery, SqlBuilder.IN()) + .and([pTableName, pAssignableIdColumn], pSourceContactId) + let recordsToDelete = []; - recordsToDelete.push([pTableName, deleteCondition]); + recordsToDelete.push([pTableName, deleteCondition.build()]); return recordsToDelete; } @@ -1028,24 +1038,24 @@ _DuplicateScannerUtils._buildDeleteRemoveObsoleteParticipantsRecordsQuery = func _DuplicateScannerUtils._buildDeletePersonAndContactQuery = function(pSourcePersonId, pSourceContactId) { let recordsToDelete = [] - recordsToDelete.push(["PERSON", "PERSONID = '" + pSourcePersonId + "'"]); - recordsToDelete.push(["CONTACT", "CONTACTID = '" + pSourceContactId + "'"]); + recordsToDelete.push(["PERSON", newWhere("PERSON.PERSONID", pSourcePersonId).build()]); + recordsToDelete.push(["CONTACT", newWhere("CONTACT.CONTACTID", pSourcePersonId).build()]); return recordsToDelete; } _DuplicateScannerUtils._buildDeleteOrganisationAndContactQuery = function(pSourceOrganisationId, pSourceContactId) { let recordsToDelete = [] - recordsToDelete.push(["ORGANISATION", "ORGANISATIONID = '" + pSourceOrganisationId + "'"]); - recordsToDelete.push(["CONTACT", "CONTACTID = '" + pSourceContactId + "'"]); + recordsToDelete.push(["ORGANISATION", newWhere("ORGANISATION.ORGANISATIONID", pSourceOrganisationId).build()]); + recordsToDelete.push(["CONTACT", newWhere("CONTACT.CONTACTID", pSourceContactId).build()]); return recordsToDelete; } _DuplicateScannerUtils._buildDeleteCachedUnrelatedDuplicateQuery = function(pSourceContactId) { let recordsToDelete = [] - recordsToDelete.push(["UNRELATEDDUPLICATES", "SOURCEDUPLICATEID = '" + pSourceContactId + "'"]); - recordsToDelete.push(["UNRELATEDDUPLICATES", "UNRELATEDDUPLICATEID = '" + pSourceContactId + "'"]); + recordsToDelete.push(["UNRELATEDDUPLICATES", newWhere("UNRELATEDDUPLICATES.SOURCEDUPLICATEID", pSourceContactId).build()]); + recordsToDelete.push(["UNRELATEDDUPLICATES", newWhere("UNRELATEDDUPLICATES.UNRELATEDDUPLICATEID", pSourceContactId).build()]); return recordsToDelete; } @@ -1080,12 +1090,12 @@ _DuplicateScannerUtils._buildStatement = function(pTableinfos, pSourceContactId, let columnName = pTableinfos[INDEX_COLUMN_NAME]; let additionalCondition = pTableinfos[INDEX_CONDITION]; - let condition = columnName + " = '" + pSourceContactId + "'"; + let condition = newWhere([tableName, columnName], pSourceContactId); if(additionalCondition != "") - condition += " and ( " + additionalCondition + ") "; + condition.and(additionalCondition); - return [tableName, [columnName], null, [pTargetContactId], condition]; + return [tableName, [columnName], null, [pTargetContactId], condition.build()]; } /* diff --git a/process/ImporterMappingFunctions_lib/process.js b/process/ImporterMappingFunctions_lib/process.js index f1e01b6faf78079bf497745c1ae3b8d1e4597ea9..bf69b6bd91fb449803730837d0c918e1eece1410 100644 --- a/process/ImporterMappingFunctions_lib/process.js +++ b/process/ImporterMappingFunctions_lib/process.js @@ -11,6 +11,8 @@ import("Attribute_lib"); import("Sql_lib"); import("Importer_lib"); +// TODO: use SqlBuilder + ///////////////////////////////////////////////////////////////////// /// toolkit methods for the import handler /// /// DO NOT TOUCH - use lib_importerCustomMappingFunctions /// diff --git a/process/Importer_lib/process.js b/process/Importer_lib/process.js index 776241153b46f4ebda53a69fee545123be9df6da..45203c0f3f61d9a75acb0eb76684a6e467a4e1f7 100644 --- a/process/Importer_lib/process.js +++ b/process/Importer_lib/process.js @@ -10,6 +10,8 @@ import("system.text"); import("ImporterCustomMappingFunctions_lib"); import("ImporterMappingFunctions_lib"); +// TODO: use SqlBuilder + /* ┌─────────────────────────────────────────────────────────────┠│ importer module constructor function │ diff --git a/process/OfferOrder_lib/process.js b/process/OfferOrder_lib/process.js index 9e02ea00da00ce3008a1799650eac7c70b1462f4..b89d167903fb1214cb039289ac601dba86dcdf04 100644 --- a/process/OfferOrder_lib/process.js +++ b/process/OfferOrder_lib/process.js @@ -83,7 +83,7 @@ function ItemUtils(pOfferOrderId, pTableName) { //check if itemsort/pos has been changed if (oiTree[oiid].itemsort != compTree[oiid].itemsort || oiTree[oiid].pos != compTree[oiid].pos) { var vals = [compTree[oiid].itemsort, compTree[oiid].pos]; - statements.push([this.tableName + "ITEM", cols, colTypes, vals, this.tableName + "ITEM" + "ID = '" + oiid + "'"]); + statements.push([this.tableName + "ITEM", cols, colTypes, vals, newWhere([this.tableName + "ITEM", this.tableName + "ITEMID"], oiid).build()]); } } } diff --git a/process/Offer_lib/process.js b/process/Offer_lib/process.js index 91ca23abe7a4bc2a80ae7a6d2fb1539ce1cdf7cd..8940287ff658d189e7d3fbc0628fcc75bb9911e9 100644 --- a/process/Offer_lib/process.js +++ b/process/Offer_lib/process.js @@ -331,7 +331,7 @@ OfferUtils.copyOfferItems = function (pSourceOfferId, pTargetOfferId) { var InputMapping = { "OFFERITEM": { - condition: "OFFER_ID = '" + pSourceOfferId + "' order by ITEMSORT", + condition: newWhere("OFFERITEM.OFFER_ID", pSourceOfferId).orderBy("ITEMSORT").toString(), ValueMapping: { "OFFER_ID" : pTargetOfferId } diff --git a/process/Order_lib/process.js b/process/Order_lib/process.js index 4ea803b9071b0b826aa26301d55622f43d8ec0eb..e75ed7697edc7bcf0075dc5b7a81fc3e4918af38 100644 --- a/process/Order_lib/process.js +++ b/process/Order_lib/process.js @@ -145,7 +145,7 @@ OrderUtils.copyOfferItemsToOrder = function (pOfferId, pOrderId) "INFO" : "INFO", "VAT" : "VAT" }, - condition: "OFFER_ID = '" + pOfferId + "' order by ITEMSORT", + condition: newWhere("OFFERITEM.OFFER_ID", pSourceOfferId).orderBy("ITEMSORT").toString(), ValueMapping: { "OFFER_ID" : pOrderId } diff --git a/process/blobHandler/process.js b/process/blobHandler/process.js index 80fd6b0323da731d3d3304665bf91902cbc7ae5e..91929e8a2143bc9c369a33a8c5dc06fb184ca165 100644 --- a/process/blobHandler/process.js +++ b/process/blobHandler/process.js @@ -49,7 +49,7 @@ function updateBlob (path, filename) function readBlob (path, filename) { var fullPath = path + filename; - var fromWhereCond = " from ASYS_BINARIES where ID = '" + filename + "'"; + var fromWhereCond = new SqlBuilder(SqlUtils.getBinariesAlias()).from("ASYS_BINARIES").where("ASYS_BINARIES.ASYS_BINARIES", filename); sqlHelper = new SqlMaskingUtils(); @@ -58,9 +58,10 @@ function readBlob (path, filename) var blob = fileIO.getData(fullPath, util.DATA_BINARY); result.string(blob); } - else if (db.cell("select " + sqlHelper.bindataLength("BINDATA") + fromWhereCond, SqlUtils.getBinariesAlias()) > 0) + // copy to reuse it inside of the if with other select field + else if (fromWhereCond.copy().select(sqlHelper.bindataLength("BINDATA")).cell() > 0) { - blob = db.cell("select BINDATA " + fromWhereCond, SqlUtils.getBinariesAlias() ); + blob = fromWhereCond.select("BINDATA").cell(); result.string(blob); } }