Commits

Grace Batumbya committed dbf89ec

Fixes issue #107 DatabaseMetaData.getPrimaryKeys does not always return all primary keys for multi-line table definitions.

Comments (0)

Files changed (3)

 Work in progress:
 
+* Issue #107 - DatabaseMetaData.getPrimaryKeys does not always return all primary keys for multi-line table definitions.
 * Issue #65 - Calling Statement.setMaxRows(N) has the effect of returning N+1 rows
 * Issue #64 - ArrayIndexOutOfBoundsException when calling ResultSetMetaData.getScale() for VARCHAR column types
 * Issue #56 - Implement getBinaryStream() in RS.java

src/main/java/org/sqlite/MetaData.java

      * Pattern used to extract a named primary key.
      */
      protected final static Pattern FK_NAMED_PATTERN =
-         Pattern.compile(".* constraint +(.*?) +foreign +key *\\((.*?)\\).*", Pattern.CASE_INSENSITIVE);
+         Pattern.compile(".*\\sCONSTRAINT\\s+(.*?)\\s*FOREIGN\\s+KEY\\s*\\((.*?)\\).*",
+             Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 
      /**
      * @see java.sql.DatabaseMetaData#getExportedKeys(java.lang.String, java.lang.String,
      * Pattern used to extract column order for an unnamed primary key.
      */
     protected final static Pattern PK_UNNAMED_PATTERN =
-            Pattern.compile(".* primary +key *\\((.*?,+.*?)\\).*", Pattern.CASE_INSENSITIVE);
+        Pattern.compile(".*\\sPRIMARY\\s+KEY\\s+\\((.*?,+.*?)\\).*",
+            Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 
     /**
      * Pattern used to extract a named primary key.
      */
      protected final static Pattern PK_NAMED_PATTERN =
-         Pattern.compile(".* constraint +(.*?) +primary +key *\\((.*?)\\).*", Pattern.CASE_INSENSITIVE);
+         Pattern.compile(".*\\sCONSTRAINT\\s+(.*?)\\s+PRIMARY\\s+KEY\\s+\\((.*?)\\).*",
+             Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 
     /**
      * Parses the sqlite_master table for a table's primary key

src/test/java/org/sqlite/DBMetaDataTest.java

     public void numberOfgetExportedKeysCols() throws SQLException {
 
         stat.executeUpdate("create table parent (id1 integer, id2 integer, primary key(id1, id2))");
-        stat.executeUpdate("create table child1 (id1 integer, id2 integer, foreign key(id1) references parent(id1), foreign key(id2) references parent(id2))");
+        stat.executeUpdate("create table child1 (id1 integer, id2 integer,\r\n foreign\tkey(id1) references parent(id1), foreign key(id2) references parent(id2))");
         stat.executeUpdate("create table child2 (id1 integer, id2 integer, foreign key(id2, id1) references parent(id2, id1))");
 
         ResultSet exportedKeys = meta.getExportedKeys(null, null, "parent");
         stat.executeUpdate("create table pk3 (col1, col2, col3, col4, primary key (col3, col2  ));");
         // extra spaces and mixed case are intentional, do not remove!
         stat.executeUpdate("create table pk4 (col1, col2, col3, col4, " +
-                "CONSTraint named  primary key   (col3, col2  ));");
+                "\r\nCONSTraint\r\nnamed  primary\r\n\t\t key   (col3, col2  ));");
 
         rs = meta.getPrimaryKeys(null, null, "nopk");
         assertFalse(rs.next());
 
         // With explicit primary column defined.
         stat.executeUpdate("create table REFERRED (ID integer primary key not null)");
-        stat.executeUpdate("create table REFERRING (ID integer, RID integer, constraint fk foreign key (RID) references REFERRED(id))");
+        stat.executeUpdate("create table REFERRING (ID integer, RID integer, constraint fk\r\n foreign\tkey\r\n(RID) references REFERRED(id))");
 
         exportedKeys = meta.getExportedKeys(null, null, "referred");
         assertEquals("referred", exportedKeys.getString("PKTABLE_NAME"));