fixed bug 35543: incorrect export of indexes including CASE expressions
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 16 Mar 2017 15:03:46 +0100
changeset 931 839d66eb2879
parent 930 2686a1fd45b9
child 932 f82d75a04d88
fixed bug 35543: incorrect export of indexes including CASE expressions

Indexes with CASE expressions, were incorrectly and differently exported in
PG and ORA. PG transforms the expression in multi line and in both ORA and
PG extra blankspaces are added.

Fixed in by removing new lines in PG (the same as done in views) and removing
extra blankspaces in both cases.
src-test/model/indexes/FUNCTION_INDEX_CASE.xml
src-test/src/org/openbravo/dbsm/test/model/FunctionBasedIndexes.java
src/org/apache/ddlutils/platform/oracle/OracleModelLoader.java
src/org/apache/ddlutils/platform/postgresql/PostgreSqlModelLoader.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/model/indexes/FUNCTION_INDEX_CASE.xml	Thu Mar 16 15:03:46 2017 +0100
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+  <database name="TABLE TEST">
+    <table name="TEST" primaryKey="TEST_ID">
+      <column name="TEST_ID" primaryKey="true" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <column name="COL1" primaryKey="false" required="true" type="VARCHAR" size="10" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <column name="COL2" primaryKey="false" required="true" type="VARCHAR" size="10" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <index name="C_INDEX" unique="false">
+        <index-column name="functionBasedColumn" functionExpression="CASE COL1 WHEN COL2 THEN &apos;Y&apos; ELSE &apos;N&apos; END"/>
+      </index>
+    </table>
+  </database>
--- a/src-test/src/org/openbravo/dbsm/test/model/FunctionBasedIndexes.java	Wed Mar 15 13:35:00 2017 +0100
+++ b/src-test/src/org/openbravo/dbsm/test/model/FunctionBasedIndexes.java	Thu Mar 16 15:03:46 2017 +0100
@@ -232,6 +232,19 @@
     assertExport("indexes/FUNCTION_INDEX_MULTI_COL.xml", "tables/TEST.xml");
   }
 
+  @Test
+  public void indexWithCase() throws IOException {
+    resetDB();
+    updateDatabase("indexes/FUNCTION_INDEX_CASE.xml", false);
+    assertExport("indexes/FUNCTION_INDEX_CASE.xml", "tables/TEST.xml");
+
+    // 2nd update should perform model check, but it doesn't check correctly index type...
+    updateDatabase("indexes/FUNCTION_INDEX_CASE.xml");
+
+    // ...that's why we compare models now
+    assertExport("indexes/FUNCTION_INDEX_CASE.xml", "tables/TEST.xml");
+  }
+
   // Given the name of an index, return a string representation of its column, along with the
   // function applied to them
   private String getColumnsFromIndex(String indexName) {
--- a/src/org/apache/ddlutils/platform/oracle/OracleModelLoader.java	Wed Mar 15 13:35:00 2017 +0100
+++ b/src/org/apache/ddlutils/platform/oracle/OracleModelLoader.java	Thu Mar 16 15:03:46 2017 +0100
@@ -373,8 +373,10 @@
   private IndexColumn getFunctionBasedIndexColumn(String indexExpression, String databaseOwner) {
     IndexColumn indexColumn = new IndexColumn();
     indexColumn.setName("functionBasedColumn");
-    indexColumn.setFunctionExpression(removeDatabaseOwnerFromIndexExpression(
-        removeDoubleQuotes(indexExpression), databaseOwner));
+    String transformedExpression = removeDoubleQuotes(indexExpression);
+    transformedExpression = removeDatabaseOwnerFromIndexExpression(transformedExpression,
+        databaseOwner);
+    indexColumn.setFunctionExpression(transformedExpression.trim());
     return indexColumn;
   }
 
--- a/src/org/apache/ddlutils/platform/postgresql/PostgreSqlModelLoader.java	Wed Mar 15 13:35:00 2017 +0100
+++ b/src/org/apache/ddlutils/platform/postgresql/PostgreSqlModelLoader.java	Thu Mar 16 15:03:46 2017 +0100
@@ -1009,12 +1009,22 @@
    * @return the transformed index expression
    */
   private String transformIndexExpression(String indexExpression) {
-    String transformedIndexExpression = removeCastExpressions(indexExpression);
+    String transformedIndexExpression = removeNewLines(indexExpression);
+    transformedIndexExpression = removeCastExpressions(transformedIndexExpression);
     transformedIndexExpression = transformUnquotedPartOfExpression(transformedIndexExpression);
     transformedIndexExpression = removeParentheses(transformedIndexExpression);
     return transformedIndexExpression;
   }
 
+  private String removeNewLines(String indexExpression) {
+    String transformedExpression = "";
+    for (String line : indexExpression.split("\n")) {
+      transformedExpression += line.trim() + " ";
+    }
+
+    return transformedExpression.trim();
+  }
+
   private String removeCastExpressions(String indexExpression) {
     // casts will start with :: and end either with ')', ',' or ' '
     // we replace all characters between '::' and one of these ending characters