Search in sources :

Example 6 with PreparedStatementImpl

use of org.teiid.jdbc.PreparedStatementImpl in project teiid by teiid.

the class ODBCServerRemoteImpl method getPgColInfo.

/**
 * @see PgCatalogMetadataStore add_pg_attribute for mod calculation
 */
private List<PgColInfo> getPgColInfo(ResultSetMetaData meta) throws SQLException {
    if (meta == null) {
        return null;
    }
    int columns = meta.getColumnCount();
    final ArrayList<PgColInfo> result = new ArrayList<PgColInfo>(columns);
    for (int i = 1; i <= columns; i++) {
        final PgColInfo info = new PgColInfo();
        info.name = meta.getColumnLabel(i);
        info.type = meta.getColumnType(i);
        String typeName = meta.getColumnTypeName(i);
        info.type = convertType(info.type, typeName);
        info.precision = meta.getColumnDisplaySize(i);
        if (info.type == PG_TYPE_NUMERIC) {
            info.mod = 4 + 65536 * Math.min(32767, meta.getPrecision(i)) + Math.min(32767, meta.getScale(i));
        } else if (info.type == PG_TYPE_BPCHAR || info.type == PG_TYPE_VARCHAR) {
            info.mod = (int) Math.min(Integer.MAX_VALUE, 4 + (long) meta.getColumnDisplaySize(i));
        } else {
            info.mod = -1;
        }
        String name = meta.getColumnName(i);
        String table = meta.getTableName(i);
        String schema = meta.getSchemaName(i);
        if (schema != null) {
            final PreparedStatementImpl ps = this.connection.prepareStatement(// $NON-NLS-1$
            "select " + // $NON-NLS-1$
            "pg_catalog.getOid(SYS.Columns.TableUID), " + // $NON-NLS-1$
            "cast(SYS.Columns.Position as short), " + // $NON-NLS-1$
            "cast((select p.value from SYS.Properties p where p.name = 'pg_type:oid' and p.uid = SYS.Columns.uid) as integer) " + // $NON-NLS-1$
            "from SYS.Columns where Name = ? and TableName = ? and SchemaName = ?");
            try {
                ps.setString(1, name);
                ps.setString(2, table);
                ps.setString(3, schema);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    info.reloid = rs.getInt(1);
                    info.attnum = rs.getShort(2);
                    int specificType = rs.getInt(3);
                    if (!rs.wasNull()) {
                        info.type = specificType;
                    }
                }
            } finally {
                ps.close();
            }
        }
        result.add(info);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl) PgColInfo(org.teiid.odbc.PGUtil.PgColInfo)

Aggregations

PreparedStatementImpl (org.teiid.jdbc.PreparedStatementImpl)6 SQLException (java.sql.SQLException)4 TeiidSQLException (org.teiid.jdbc.TeiidSQLException)4 ArrayList (java.util.ArrayList)2 ResultsFuture (org.teiid.client.util.ResultsFuture)2 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 UpdateResponse (org.teiid.odata.api.UpdateResponse)1 PgColInfo (org.teiid.odbc.PGUtil.PgColInfo)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1