use of org.sqlite.parser.ast.QualifiedName in project sqlite-jna by gwenn.
the class DbMeta method getBestRowIdentifier.
@Override
public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException {
checkOpen();
final StringBuilder sql = new StringBuilder();
catalog = schemaProvider.getDbName(catalog, table);
sql.append("select ").append(scope).append(" as SCOPE, ").append("cn as COLUMN_NAME, ").append("ct as DATA_TYPE, ").append("tn as TYPE_NAME, ").append(// FIXME precision (19 for LONG, 15 for REAL) or display size (20 for LONG, 25 for REAL)
"10 as COLUMN_SIZE, ").append("0 as BUFFER_LENGTH, ").append(// FIXME scale (0 for LONG, 15 for REAL)
"0 as DECIMAL_DIGITS, ").append("pc as PSEUDO_COLUMN from (");
// Pragma cannot be used as subquery...
int count = -1;
String colName = null;
String colType = null;
Pragma pragma = new Pragma(new QualifiedName(catalog, "table_info"), new IdExpr(table));
try (PreparedStatement table_info = c.prepareStatement(pragma.toSql());
ResultSet rs = table_info.executeQuery()) {
// 1:cid|2:name|3:type|4:notnull|5:dflt_value|6:pk
while (count < 2 && rs.next()) {
if (count < 0) {
// table exists
count = 0;
}
if (rs.getBoolean(6) && (nullable || rs.getBoolean(4))) {
colName = rs.getString(2);
colType = getSQLiteType(rs.getString(3));
count++;
}
}
} catch (StmtException e) {
// query does not return ResultSet
assert e.getErrorCode() == ErrCodes.WRAPPER_SPECIFIC;
count = -1;
}
if (count == 1) {
sql.append("SELECT ").append(quote(colName)).append(" as cn, ").append(getJavaType(colType)).append(" as ct, ").append("'").append(colType).append("' as tn, ").append(bestRowNotPseudo).append(" as pc) order by SCOPE");
} else {
sql.append("SELECT ").append("'ROWID' AS cn, ").append(Types.INTEGER).append(" AS ct, ").append("'INTEGER' AS tn, ").append(bestRowPseudo).append(" AS pc) order by SCOPE");
if (count < 0) {
sql.append(" limit 0");
}
}
final PreparedStatement columns = c.prepareStatement(sql.toString());
columns.closeOnCompletion();
return columns.executeQuery();
}
Aggregations