Search in sources :

Example 6 with QualifiedName

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();
}
Also used : IdExpr(org.sqlite.parser.ast.IdExpr) StmtException(org.sqlite.StmtException) QualifiedName(org.sqlite.parser.ast.QualifiedName) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) EnhancedPragma(org.sqlite.parser.EnhancedPragma) Pragma(org.sqlite.parser.ast.Pragma)

Aggregations

Pragma (org.sqlite.parser.ast.Pragma)6 QualifiedName (org.sqlite.parser.ast.QualifiedName)6 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 StmtException (org.sqlite.StmtException)4 EnhancedPragma (org.sqlite.parser.EnhancedPragma)4 IdExpr (org.sqlite.parser.ast.IdExpr)4 SQLWarning (java.sql.SQLWarning)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1