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

use SqlBuilder

parent 8270458e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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()]);
}
......
......@@ -534,6 +534,8 @@ ContextUtils.getSelectMap = function()
*/
ContextUtils.getNameSubselectSql = function(pContextIdDbField, pRowIdDbField)
{
// TODO: prepared?
var select = "(case " + pContextIdDbField + " ";
var selectMap = ContextUtils.getSelectMap ()
......
......@@ -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()];
}
/*
......
......@@ -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 ///
......
......@@ -10,6 +10,8 @@ import("system.text");
import("ImporterCustomMappingFunctions_lib");
import("ImporterMappingFunctions_lib");
// TODO: use SqlBuilder
/*
┌─────────────────────────────────────────────────────────────┐
│ importer module constructor function │
......
......@@ -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()]);
}
}
}
......
......@@ -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
}
......
......@@ -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
}
......
......@@ -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);
}
}
......
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