Commits

Anonymous committed c27f562

Fixes #20: MetaData.getExportedKeys() doesn't return all foreign keys.

Comments (0)

Files changed (2)

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

                     count++;
                 }
 
-                exportedKeysQuery.append(");");
                 fk.close();
             }
             catch (SQLException e) {
             }
         }
 
+        exportedKeysQuery.append(");");
+
         String sql = (count > 0) ? exportedKeysQuery.toString() : (String.format(
                 "select %s as PKTABLE_CAT, %s as PKTABLE_SCHEM, %s as PKTABLE_NAME, ", quote(catalog), quote(schema),
                 quote(table))

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

     }
 
     @Test
+    public void numberOfgetImportedKeysCols() 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 child2 (id1 integer, id2 integer, foreign key(id2, id1) references parent(id2, id1))");
+
+        ResultSet importedKeys = meta.getImportedKeys(null, null, "child1");
+
+        //child1: 1st fk (simple)
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child1", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("FKCOLUMN_NAME"));
+
+        //child1: 2nd fk (simple)
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child1", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("FKCOLUMN_NAME"));
+
+        assertFalse(importedKeys.next());
+
+        importedKeys = meta.getImportedKeys(null, null, "child2");
+
+        //child2: 1st fk (composite)
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child2", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("FKCOLUMN_NAME"));
+
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child2", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("FKCOLUMN_NAME"));
+
+        assertFalse(importedKeys.next());
+
+        importedKeys.close();
+    }
+
+/*    @Test
+    public void columnOrderOfgetExportedKeys() throws SQLException {
+
+    exportedKeys.close();
+
+    }*/
+
+    @Test
+    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 child2 (id1 integer, id2 integer, foreign key(id2, id1) references parent(id2, id1))");
+
+        ResultSet importedKeys = meta.getExportedKeys(null, null, "parent");
+
+        //1st fk (simple) - child1
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child1", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("FKCOLUMN_NAME"));
+
+        //2nd fk (simple) - child1
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child1", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("FKCOLUMN_NAME"));
+
+        //3rd fk (composite) - child2
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child2", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id2", importedKeys.getString("FKCOLUMN_NAME"));
+
+        assertTrue(importedKeys.next());
+        assertEquals("parent", importedKeys.getString("PKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("PKCOLUMN_NAME"));
+        assertNotNull(importedKeys.getString("PK_NAME"));
+        assertNotNull(importedKeys.getString("FK_NAME"));
+        assertEquals("child2", importedKeys.getString("FKTABLE_NAME"));
+        assertEquals("id1", importedKeys.getString("FKCOLUMN_NAME"));
+
+        assertFalse(importedKeys.next());
+
+        importedKeys.close();
+    }
+
+    @Test
     public void columnOrderOfgetTables() throws SQLException {
         ResultSet rs = meta.getTables(null, null, null, null);
         assertTrue(rs.next());