Search in sources :

Example 1 with PgColInfo

use of org.teiid.odbc.PGUtil.PgColInfo 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)

Example 2 with PgColInfo

use of org.teiid.odbc.PGUtil.PgColInfo in project teiid by teiid.

the class PgBackendProtocol method sendRowDescription.

private void sendRowDescription(List<PgColInfo> cols, short[] resultColumnFormat) {
    if (cols == null) {
        // send NoData
        startMessage('n');
        sendMessage();
        return;
    }
    startMessage('T');
    writeShort(cols.size());
    for (int i = 0; i < cols.size(); i++) {
        PgColInfo info = cols.get(i);
        writeString(info.name);
        // rel ID
        writeInt(info.reloid);
        // attribute number of the column
        writeShort(info.attnum);
        // data type
        writeInt(info.type);
        // pg_type.typlen
        writeShort(getTypeSize(info.type, info.precision));
        // pg_attribute.atttypmod
        writeInt(info.mod);
        if (!isBinary(info.type) || resultColumnFormat == null) {
            // text
            writeShort(0);
        } else {
            writeShort(resultColumnFormat[resultColumnFormat.length == 1 ? 0 : i]);
        }
    }
    sendMessage();
}
Also used : PgColInfo(org.teiid.odbc.PGUtil.PgColInfo)

Aggregations

PgColInfo (org.teiid.odbc.PGUtil.PgColInfo)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 PreparedStatementImpl (org.teiid.jdbc.PreparedStatementImpl)1