Skip to content

Commit

Permalink
Fix getImportedKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Jan 5, 2017
1 parent c7c1a34 commit bf86da4
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java
Expand Up @@ -1097,12 +1097,12 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
// and return the columname and type from:
// "PRAGMA table_info(tablename)"
// which returns data like this:
// sqlite> PRAGMA lastyear.table_info(gross_sales);
// cid|name|type|notnull|dflt_value|pk
// 0|year|INTEGER|0|'2006'|0
// 1|month|TEXT|0||0
// 2|monthlygross|REAL|0||0
// 3|sortcol|INTEGER|0||0
// sqlite> PRAGMA lastyear.table_info(gross_sales);
// cid|name|type|notnull|dflt_value|pk
// 0|year|INTEGER|0|'2006'|0
// 1|month|TEXT|0||0
// 2|monthlygross|REAL|0||0
// 3|sortcol|INTEGER|0||0
// sqlite>

// and then make the cursor have these columns
Expand Down Expand Up @@ -1379,15 +1379,15 @@ public ResultSet getExportedKeys(String catalog, String schema, String table) th
}

rs.close();

ResultSet fk = null;
String target = table.toLowerCase();
// find imported keys for each table
for (String tbl : tableList) {
try {
fk = stat.executeQuery("pragma foreign_key_list('" + escape(tbl) + "')");
} catch (SQLException e) {
if (e.getErrorCode() == Codes.SQLITE_DONE)
if (e.getErrorCode() == Codes.SQLITE_DONE)
continue; // expected if table has no foreign keys

throw e;
Expand Down Expand Up @@ -1478,6 +1478,13 @@ public ResultSet getExportedKeys(String catalog, String schema, String table) th
return ((CoreStatement)stat).executeQuery(sql.toString(), true);
}

private StringBuilder appendDummyForeignKeyList(StringBuilder sql) {
sql.append("select -1 as ks, '' as ptn, '' as fcn, '' as pcn, ")
.append(DatabaseMetaData.importedKeyNoAction).append(" as ur, ")
.append(DatabaseMetaData.importedKeyNoAction).append(" as dr) limit 0;");
return sql;
}

/**
* @see java.sql.DatabaseMetaData#getImportedKeys(java.lang.String, java.lang.String,
* java.lang.String)
Expand All @@ -1492,7 +1499,7 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
.append("ptn as PKTABLE_NAME, pcn as PKCOLUMN_NAME, ")
.append(quote(catalog)).append(" as FKTABLE_CAT, ")
.append(quote(schema)).append(" as FKTABLE_SCHEM, ")
.append(quote(table)).append(" as FKTABLE_NAME, ")
.append(quote(table)).append(" as FKTABLE_NAME, ")
.append("fcn as FKCOLUMN_NAME, ks as KEY_SEQ, ur as UPDATE_RULE, dr as DELETE_RULE, '' as FK_NAME, '' as PK_NAME, ")
.append(Integer.toString(DatabaseMetaData.importedKeyInitiallyDeferred)).append(" as DEFERRABILITY from (");

Expand All @@ -1501,14 +1508,12 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
rs = stat.executeQuery("pragma foreign_key_list('" + escape(table) + "');");
}
catch (SQLException e) {
sql.append("select -1 as ks, '' as ptn, '' as fcn, '' as pcn, ")
.append(DatabaseMetaData.importedKeyNoAction).append(" as ur, ")
.append(DatabaseMetaData.importedKeyNoAction).append(" as dr) limit 0;");

sql = appendDummyForeignKeyList(sql);
return ((CoreStatement)stat).executeQuery(sql.toString(), true);
}

for (int i = 0; rs.next(); i++) {
int i = 0;
for (; rs.next(); i++) {
int keySeq = rs.getInt(2) + 1;
String PKTabName = rs.getString(3);
String FKColName = rs.getString(4);
Expand Down Expand Up @@ -1544,6 +1549,11 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
}
rs.close();

if(i == 0) {
sql = appendDummyForeignKeyList(sql);
}

System.out.println("----------\n" + sql);
return ((CoreStatement)stat).executeQuery(sql.append(");").toString(), true);
}

Expand Down

0 comments on commit bf86da4

Please sign in to comment.