METAMODEL-156: Fixed selection of default schema
authorRatish Bansal <ratish1992@gmail.com>
Mon, 4 Feb 2019 19:41:24 +0000 (11:41 -0800)
committerKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Mon, 4 Feb 2019 19:42:06 +0000 (11:42 -0800)
CHANGES.md
jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcDataContext.java

index 4c12dbf..badb3bf 100644 (file)
@@ -1,6 +1,7 @@
 ### Apache MetaModel 5.2.1 (WIP)
 
  * [METAMODEL-1208] - Fixed closing of JDBC ResultSets when getting certain metadata.
+ * [METAMODEL-156] - Fixed issue in selection of default schema when multiple schema names match substring of connection string.
 
 ### Apache MetaModel 5.2.0
 
index 1668438..a6b285c 100644 (file)
@@ -25,6 +25,8 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -669,7 +671,17 @@ public class JdbcDataContext extends AbstractDataContext implements UpdateableDa
                                     st.nextToken();
                                 }
                                 String lastToken = st.nextToken();
+                                if (lastToken.indexOf("?") != -1) {
+                                    lastToken = lastToken.substring(0, lastToken.indexOf("?"));
+                                }
+                                // Sorting schema names in order of decreasing length
 
+                                Collections.sort(schemaNames, new Comparator<String>() {
+                                    @Override
+                                    public int compare(String s1, String s2) {
+                                        return s2.length() - s1.length();
+                                    }
+                                });
                                 for (int i = 0; i < schemaNames.size() && !found; i++) {
                                     String schemaName = schemaNames.get(i);
                                     if (lastToken.indexOf(schemaName) != -1) {