Juan Hernandez has uploaded a new change for review. Change subject: core: Sort table names before building SQL ......................................................................
core: Sort table names before building SQL Currently when the search backend builds the SQL staement corresponding a query that uses a column not supported by the corresponding auto completer it uses all the possible columns. To do so it iterates a map that contains the types and names of the columns. But the order of this map depends on the implementation of the map class. This implementation has changed from Java 7 and Java 8 (and may change as well in minor versions, the order isn't part of the contract of the map). As a result the SyntaxCheckerTest.testVm test fails when executed with Java 8, because it expects the order generated by Java 7. To avoid that issue this patch changes the auto completer base class class so that column names are alphabetically sorted before generating the SQL statement. Change-Id: I29f9f192e194f0079dae7f130d502530f0bdd0ae Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- M backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/BaseConditionFieldAutoCompleter.java M backend/manager/modules/searchbackend/src/test/java/org/ovirt/engine/core/searchbackend/SyntaxCheckerTest.java 2 files changed, 23 insertions(+), 12 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/77/36177/1 diff --git a/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/BaseConditionFieldAutoCompleter.java b/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/BaseConditionFieldAutoCompleter.java index 62bb486..a98fe02 100644 --- a/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/BaseConditionFieldAutoCompleter.java +++ b/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/BaseConditionFieldAutoCompleter.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.text.DateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -154,7 +155,6 @@ String value, boolean caseSensitive) { StringBuilder sb = new StringBuilder(" ( "); - boolean firstTime = true; if (!StringHelper.isNullOrEmpty(value) && !"''".equals(value)) { value = StringFormat.format(getI18NPrefix() + "'%%%1$s%%'", StringHelper.trim(value, '\'')); @@ -164,20 +164,31 @@ } else if ("!=".equals(relations)) { relations = "NOT " + getLikeSyntax(caseSensitive); } + + // The names of the columns need to be sorted in order to make sure that the generated SQL statement will + // always be the same regardless of the internal details of the map implementation: + List<String> columnNames = new ArrayList<String>(); for (Map.Entry<String, String> columnNameEntry : columnNameDict.entrySet()) { if (typeDict.get(columnNameEntry.getKey()) == String.class && !notFreeTextSearchableFieldsList.contains(columnNameEntry.getKey())) { - if (firstTime) { - firstTime = false; - } else { - sb.append(" OR "); - } - sb.append(StringFormat.format(" %1$s.%2$s %3$s %4$s", - tableName, - columnNameEntry.getValue(), - relations, - value)); + columnNames.add(columnNameEntry.getValue()); } } + Collections.sort(columnNames); + + boolean firstTime = true; + for (String columnName : columnNames) { + if (firstTime) { + firstTime = false; + } else { + sb.append(" OR "); + } + sb.append(StringFormat.format(" %1$s.%2$s %3$s %4$s", + tableName, + columnName, + relations, + value)); + } + sb.append(" ) "); return sb.toString(); } diff --git a/backend/manager/modules/searchbackend/src/test/java/org/ovirt/engine/core/searchbackend/SyntaxCheckerTest.java b/backend/manager/modules/searchbackend/src/test/java/org/ovirt/engine/core/searchbackend/SyntaxCheckerTest.java index 55e7994..9d46354 100644 --- a/backend/manager/modules/searchbackend/src/test/java/org/ovirt/engine/core/searchbackend/SyntaxCheckerTest.java +++ b/backend/manager/modules/searchbackend/src/test/java/org/ovirt/engine/core/searchbackend/SyntaxCheckerTest.java @@ -184,7 +184,7 @@ // Used to validate that searching values not in fields search all fields testValidSql("Vm: mac=00:1a:4a:d4:53:94", - "SELECT * FROM (SELECT * FROM vms WHERE ( vm_guid IN (SELECT distinct vms_with_tags.vm_guid FROM vms_with_tags WHERE ( vms_with_tags.vm_pool_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.run_on_vds_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_fqdn LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.tag_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.guest_cur_user_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.description LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.quota_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_host LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_ip LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.storage_pool_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vds_group_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.free_text_comment LIKE '%mac=00:1a:4a:d4:53:94%' ) )) ORDER BY vm_name ASC ) as T1 OFFSET (1 -1) LIMI! T 0"); + "SELECT * FROM (SELECT * FROM vms WHERE ( vm_guid IN (SELECT distinct vms_with_tags.vm_guid FROM vms_with_tags WHERE ( vms_with_tags.description LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.free_text_comment LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.guest_cur_user_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.quota_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.run_on_vds_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.storage_pool_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.tag_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vds_group_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_fqdn LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_host LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_ip LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_name LIKE '%mac=00:1a:4a:d4:53:94%' OR vms_with_tags.vm_pool_name LIKE '%mac=00:1a:4a:d4:53:94%' ) )) ORDER BY vm_name ASC ) as T1 OFFSET (1 -1) LIMI! T 0"); } @Test -- To view, visit http://gerrit.ovirt.org/36177 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I29f9f192e194f0079dae7f130d502530f0bdd0ae Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches