And here's the second half of the MetaData.java file:
public ResultSet getCrossReference(String primaryCatalog,
String primarySchema, String primaryTable, String foreignCatalog,
String foreignSchema, String foreignTable)
throws SQLException {
StringBuilder sql = new StringBuilder();
ResultSet rs;
Statement stat = conn.createStatement();
try {
rs = stat.executeQuery("pragma foreign_key_list('"+escape(foreignTable)+"');");
}
catch(SQLException ex) {
rs = null;
}
sql.append("select "
+ "null as PKTABLE_CAT, "
+ "null as PKTABLE_SCHEM, "
+ "PKTABLE_NAME, "
+ "PKCOLUMN_NAME, "
+ "null as FKTABLE_CAT, "
+ "null as FKTABLE_SCHEM, "
+ "'" + escape(foreignTable) + "' as FKTABLE_NAME, "
+ "FKCOLUMN_NAME, "
+ "KEY_SEQ, "
+ "UPDATE_RULE, "
+ "DELETE_RULE, "
+ "null as FK_NAME, "
+ "null as PK_NAME, "
+ "7 as DEFERRABILITY ");
if (primaryTable == null)
primaryTable = "";
int i = 0;
sql.append(" from ( ");
if ( rs == null || !rs.next()) {
sql.append(" select ");
sql.append(" null AS PKTABLE_NAME,");
sql.append(" null AS PKCOLUMN_NAME,");
sql.append(" null AS FKCOLUMN_NAME,");
sql.append(" null AS KEY_SEQ,");
sql.append(" null AS UPDATE_RULE,");
sql.append(" null AS DELETE_RULE ");
sql.append(" ) limit 0");
}
else {
do {
String pktable = rs.getString("table");
if (primaryTable.equalsIgnoreCase(pktable)) {
continue;
}
String pkcolumn = rs.getString("to");
String fkcolumn = rs.getString("from");
String updateRule = "RESTRICT";
String deleteRule = "RESTRICT";
String seq = rs.getString("seq");
updateRule = getDbRuleCode(updateRule);
deleteRule = getDbRuleCode(deleteRule);
if (fkcolumn == null || fkcolumn.length() == 0)
fkcolumn = pkcolumn;
if (i > 0)
sql.append(" union all ");
sql.append("select ");
sql.append(sqlQuote(pktable)+" AS PKTABLE_NAME,");
sql.append(sqlQuote(pkcolumn)+" AS PKCOLUMN_NAME,");
sql.append(sqlQuote(fkcolumn)+" AS FKCOLUMN_NAME,");
sql.append(((Integer.valueOf(seq)).intValue() + 1)+" AS KEY_SEQ,");
sql.append(updateRule+" AS UPDATE_RULE,");
sql.append(deleteRule+" AS DELETE_RULE ");
i++;
} while( rs.next());
sql.append(")");
}
System.out.println(sql.toString());
return stat.executeQuery(sql.toString());
}
private String getDbRuleCode(String dbRule) {
if (dbRule.equals("CASCADE"))
dbRule = Integer.toString(DatabaseMetaData.importedKeyCascade);
else
if (dbRule.equals("SETNULL"))
dbRule = Integer.toString(DatabaseMetaData.importedKeySetNull);
else
dbRule =Integer.toString(DatabaseMetaData.importedKeyRestrict);
return dbRule;
}
public ResultSet getSchemas() throws SQLException {
if (getSchemas == null) getSchemas = conn.prepareStatement("select "
+ "null as TABLE_SCHEM, "
+ "null as TABLE_CATALOG "
+ "limit 0;");
getSchemas.clearParameters();
return getSchemas.executeQuery();
}
public ResultSet getCatalogs() throws SQLException {
if (getCatalogs == null) getCatalogs = conn.prepareStatement(
"select null as TABLE_CAT limit 0;");
getCatalogs.clearParameters();
return getCatalogs.executeQuery();
}
public ResultSet getPrimaryKeys(String c, String s, String table)
throws SQLException {
String sql;
ResultSet rs;
Statement stat = conn.createStatement();
rs = stat.executeQuery("pragma table_info('"+escape(table)+"');");
sql = "select "
+ "null as TABLE_CAT, "
+ "null as TABLE_SCHEM, "
+ "'" + escape(table) + "' as TABLE_NAME, "
+ "cn as COLUMN_NAME, "
+ "0 as KEY_SEQ, "
+ "null as PK_NAME from (";
int i;
for (i=0; rs.next(); i++) {
String colName = rs.getString(2);
if (!rs.getBoolean(6)) { i--; continue; }
if (i > 0) sql += " union all ";
sql += "select '" + escape(colName) + "' as cn";
}
sql += i == 0 ? "select null as cn) limit 0;" : ");";
rs.close();
return stat.executeQuery(sql);
}
public ResultSet getExportedKeys(String c, String s, String t)
throws SQLException {
if (getExportedKeys == null) getExportedKeys = conn.prepareStatement(
"select "
+ "null as PKTABLE_CAT, "
+ "null as PKTABLE_SCHEM, "
+ "null as PKTABLE_NAME, "
+ "null as PKCOLUMN_NAME, "
+ "null as FKTABLE_CAT, "
+ "null as FKTABLE_SCHEM, "
+ "null as FKTABLE_NAME, "
+ "null as FKCOLUMN_NAME, "
+ "null as KEY_SEQ, "
+ "null as UPDATE_RULE, "
+ "null as DELETE_RULE, "
+ "null as FK_NAME, "
+ "null as PK_NAME, "
+ "null as DEFERRABILITY limit 0;");
return getExportedKeys.executeQuery();
}
public ResultSet getImportedKeys(String c, String s, String t)
throws SQLException {
return getCrossReference(null,null,null,c,s,t);
}
public ResultSet getIndexInfo(String c, String s, String t,
boolean u, boolean approximate)
throws SQLException {
if (getIndexInfo == null) getIndexInfo =
conn.prepareStatement(
"select "
+ "null as TABLE_CAT,"
+ "null as TABLE_SCHEM,"
+ "null as TABLE_NAME,"
+ "null as NON_UNIQUE,"
+ "null as INDEX_QUALIFIER,"
+ "null as INDEX_NAME,"
+ "null as TYPE,"
+ "null as ORDINAL_POSITION,"
+ "null as COLUMN_NAME,"
+ "null as ASC_OR_DESC,"
+ "null as CARDINALITY,"
+ "null as PAGES,"
+ "null as FILTER_CONDITION limit 0;");
return getIndexInfo.executeQuery();
}
public ResultSet getProcedureColumns(String c, String s, String p,
String colPat)
throws SQLException {
if (getProcedures == null) getProcedureColumns = conn.prepareStatement(
"select "
+ "null as PROCEDURE_CAT, "
+ "null as PROCEDURE_SCHEM, "
+ "null as PROCEDURE_NAME, "
+ "null as COLUMN_NAME, "
+ "null as COLUMN_TYPE, "
+ "null as DATA_TYPE, "
+ "null as TYPE_NAME, "
+ "null as PRECISION, "
+ "null as LENGTH, "
+ "null as SCALE, "
+ "null as RADIX, "
+ "null as NULLABLE, "
+ "null as REMARKS limit 0;");
return getProcedureColumns.executeQuery();
}
public ResultSet getProcedures(String c, String s, String p)
throws SQLException {
if (getProcedures == null) getProcedures = conn.prepareStatement(
"select "
+ "null as PROCEDURE_CAT, "
+ "null as PROCEDURE_SCHEM, "
+ "null as PROCEDURE_NAME, "
+ "null as UNDEF1, "
+ "null as UNDEF2, "
+ "null as UNDEF3, "
+ "null as REMARKS, "
+ "null as PROCEDURE_TYPE limit 0;");
return getProcedures.executeQuery();
}
public ResultSet getSuperTables(String c, String s, String t)
throws SQLException {
if (getSuperTables == null) getSuperTables = conn.prepareStatement(
"select "
+ "null as TABLE_CAT, "
+ "null as TABLE_SCHEM, "
+ "null as TABLE_NAME, "
+ "null as SUPERTABLE_NAME limit 0;");
return getSuperTables.executeQuery();
}
public ResultSet getSuperTypes(String c, String s, String t)
throws SQLException {
if (getSuperTypes == null) getSuperTypes = conn.prepareStatement(
"select "
+ "null as TYPE_CAT, "
+ "null as TYPE_SCHEM, "
+ "null as TYPE_NAME, "
+ "null as SUPERTYPE_CAT, "
+ "null as SUPERTYPE_SCHEM, "
+ "null as SUPERTYPE_NAME limit 0;");
return getSuperTypes.executeQuery();
}
public ResultSet getTablePrivileges(String c, String s, String t)
throws SQLException {
if (getTablePrivileges == null)
getTablePrivileges = conn.prepareStatement(
"select "
+ "null as TABLE_CAT, "
+ "null as TABLE_SCHEM, "
+ "null as TABLE_NAME, "
+ "null as GRANTOR, "
+ "null as GRANTEE, "
+ "null as PRIVILEGE, "
+ "null as IS_GRANTABLE limit 0;");
return getTablePrivileges.executeQuery();
}
public synchronized ResultSet getTables(String c, String s,
String t, String[] types) throws SQLException {
checkOpen();
t = (t == null || "".equals(t)) ? "%" : t.toUpperCase();
String sql = "select"
+ " null as TABLE_CAT,"
+ " null as TABLE_SCHEM,"
+ " upper(name) as TABLE_NAME,"
+ " upper(type) as TABLE_TYPE,"
+ " null as REMARKS,"
+ " null as TYPE_CAT,"
+ " null as TYPE_SCHEM,"
+ " null as TYPE_NAME,"
+ " null as SELF_REFERENCING_COL_NAME,"
+ " null as REF_GENERATION"
+ " from (select name, type from sqlite_master union all"
+ " select name, type from sqlite_temp_master)"
+ " where TABLE_NAME like '" + escape(t) + "'";
if (types != null) {
sql += " and TABLE_TYPE in (";
for (int i=0; i < types.length; i++) {
if (i > 0) sql += ", ";
sql += "'" + types[i].toUpperCase() + "'";
}
sql += ")";
}
sql += ";";
return conn.createStatement().executeQuery(sql);
}
public ResultSet getTableTypes() throws SQLException {
checkOpen();
if (getTableTypes == null) getTableTypes = conn.prepareStatement(
"select 'TABLE' as TABLE_TYPE"
+ " union select 'VIEW' as TABLE_TYPE;");
getTableTypes.clearParameters();
return getTableTypes.executeQuery();
}
public ResultSet getTypeInfo() throws SQLException {
if (getTypeInfo == null) {
getTypeInfo = conn.prepareStatement(
"select "
+ "tn as TYPE_NAME, "
+ "dt as DATA_TYPE, "
+ "0 as PRECISION, "
+ "null as LITERAL_PREFIX, "
+ "null as LITERAL_SUFFIX, "
+ "null as CREATE_PARAMS, "
+ typeNullable + " as NULLABLE, "
+ "1 as CASE_SENSITIVE, "
+ typeSearchable + " as SEARCHABLE, "
+ "0 as UNSIGNED_ATTRIBUTE, "
+ "0 as FIXED_PREC_SCALE, "
+ "0 as AUTO_INCREMENT, "
+ "null as LOCAL_TYPE_NAME, "
+ "0 as MINIMUM_SCALE, "
+ "0 as MAXIMUM_SCALE, "
+ "0 as SQL_DATA_TYPE, "
+ "0 as SQL_DATETIME_SUB, "
+ "10 as NUM_PREC_RADIX from ("
+ " select 'BLOB' as tn, " + Types.BLOB + " as dt union"
+ " select 'NULL' as tn, " + Types.NULL + " as dt union"
+ " select 'REAL' as tn, " + Types.REAL+ " as dt union"
+ " select 'TEXT' as tn, " + Types.VARCHAR + " as dt union"
+ " select 'INTEGER' as tn, "+ Types.INTEGER +" as dt"
+ ") order by TYPE_NAME;"
);
}
getTypeInfo.clearParameters();
return getTypeInfo.executeQuery();
}
public ResultSet getUDTs(String c, String s, String t, int[] types)
throws SQLException {
if (getUDTs == null) getUDTs = conn.prepareStatement("select "
+ "null as TYPE_CAT, "
+ "null as TYPE_SCHEM, "
+ "null as TYPE_NAME, "
+ "null as CLASS_NAME, "
+ "null as DATA_TYPE, "
+ "null as REMARKS, "
+ "null as BASE_TYPE "
+ "limit 0;");
getUDTs.clearParameters();
return getUDTs.executeQuery();
}
public ResultSet getVersionColumns(String c, String s, String t)
throws SQLException {
if (getVersionColumns == null)
getVersionColumns = conn.prepareStatement(
"select "
+ "null as SCOPE, "
+ "null as COLUMN_NAME, "
+ "null as DATA_TYPE, "
+ "null as TYPE_NAME, "
+ "null as COLUMN_SIZE, "
+ "null as BUFFER_LENGTH, "
+ "null as DECIMAL_DIGITS, "
+ "null as PSEUDO_COLUMN limit 0;");
return getVersionColumns.executeQuery();
}
ResultSet getGeneratedKeys() throws SQLException {
if (getGeneratedKeys == null) getGeneratedKeys = conn.prepareStatement(
"select last_insert_rowid();");
return getGeneratedKeys.executeQuery();
}
/** Replace all instances of ' with '' */
private String escape(final String val) {
// TODO: this function is ugly, pass this work off to SQLite, then we
// don't have to worry about Unicode 4, other characters needing
// escaping, etc.
int len = val.length();
StringBuffer buf = new StringBuffer(len);
for (int i=0; i < len; i++) {
if (val.charAt(i) == '\'') buf.append('\'');
buf.append(val.charAt(i));
}
return buf.toString();
}
public Struct createStruct(String t, Object[] attr) throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public ResultSet getFunctionColumns(String a, String b, String c,
String d) throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public ResultSet getFunctions(String a, String b, String c) throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public ResultSet getClientInfoProperties() throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public boolean autoCommitFailureClosesAllResultSets() throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public ResultSet getSchemas(String a, String b) throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public RowIdLifetime getRowIdLifetime()throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLException("Not yet implemented by SQLite JDBC driver"); }
}