Search in sources :

Example 31 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class PgBackendProtocol method getContent.

private void getContent(ResultSet rs, PgColInfo col, int column) throws SQLException, TeiidSQLException, IOException {
    switch(col.type) {
        case PG_TYPE_BOOL:
            boolean b = rs.getBoolean(column);
            if (!rs.wasNull()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                writer.write(b ? "t" : "f");
            }
            break;
        case PG_TYPE_BPCHAR:
        case PG_TYPE_DATE:
        case PG_TYPE_FLOAT4:
        case PG_TYPE_FLOAT8:
        case PG_TYPE_INT2:
        case PG_TYPE_INT4:
        case PG_TYPE_INT8:
        case PG_TYPE_NUMERIC:
        case PG_TYPE_TIME:
        case PG_TYPE_TIMESTAMP_NO_TMZONE:
        case PG_TYPE_VARCHAR:
            String value = rs.getString(column);
            if (value != null) {
                writer.write(value);
            }
            break;
        case PG_TYPE_GEOMETRY:
            Object val = rs.getObject(column);
            if (val != null) {
                Blob blob = GeometryUtils.geometryToEwkb((GeometryType) rs.unwrap(ResultSetImpl.class).getRawCurrentValue());
                String hexewkb = PropertiesUtils.toHex(blob.getBytes(1, (int) blob.length()));
                writer.write(hexewkb);
            }
            break;
        case PG_TYPE_XML:
        case PG_TYPE_TEXT:
            Reader r = rs.getCharacterStream(column);
            if (r != null) {
                try {
                    ObjectConverterUtil.write(writer, r, this.maxLobSize, false);
                } finally {
                    r.close();
                }
            }
            break;
        case PG_TYPE_BYTEA:
            Blob blob = rs.getBlob(column);
            if (blob != null) {
                try {
                    String blobString = PGbytea.toPGString(ObjectConverterUtil.convertToByteArray(blob.getBinaryStream(), this.maxLobSize));
                    writer.write(blobString);
                } catch (OutOfMemoryError e) {
                    // $NON-NLS-1$
                    throw new StreamCorruptedException("data too big: " + e.getMessage());
                }
            }
            break;
        case PG_TYPE_CHARARRAY:
        case PG_TYPE_TEXTARRAY:
        case PG_TYPE_OIDARRAY:
        case PG_TYPE_BOOLARRAY:
        case PG_TYPE_INT2ARRAY:
        case PG_TYPE_INT4ARRAY:
        case PG_TYPE_INT8ARRAY:
        case PG_TYPE_FLOAT4ARRAY:
        case PG_TYPE_FLOAT8ARRAY:
        case PG_TYPE_NUMERICARRAY:
        case PG_TYPE_DATEARRAY:
        case PG_TYPE_TIMEARRAY:
        case PG_TYPE_TIMESTAMP_NO_TMZONEARRAY:
            {
                Array obj = rs.getArray(column);
                if (obj != null) {
                    writer.append("{");
                    boolean first = true;
                    Object array = obj.getArray();
                    int length = java.lang.reflect.Array.getLength(array);
                    for (int i = 0; i < length; i++) {
                        if (!first) {
                            writer.append(",");
                        } else {
                            first = false;
                        }
                        Object o = java.lang.reflect.Array.get(array, i);
                        if (o != null) {
                            if (col.type == PG_TYPE_TEXTARRAY) {
                                escapeQuote(writer, o.toString());
                            } else {
                                writer.append(o.toString());
                            }
                        }
                    }
                    writer.append("}");
                }
            }
            break;
        case PG_TYPE_INT2VECTOR:
        case PG_TYPE_OIDVECTOR:
            {
                ArrayImpl obj = (ArrayImpl) rs.getObject(column);
                if (obj != null) {
                    boolean first = true;
                    for (Object o : obj.getValues()) {
                        if (!first) {
                            writer.append(" ");
                        } else {
                            first = false;
                        }
                        if (o != null) {
                            writer.append(o.toString());
                        } else {
                            writer.append('0');
                        }
                    }
                }
            }
            break;
        default:
            Object obj = rs.getObject(column);
            if (obj != null) {
                throw new TeiidSQLException("unknown datatype " + col.type + " failed to convert");
            }
            break;
    }
}
Also used : Array(java.sql.Array) Blob(java.sql.Blob) ResultSetImpl(org.teiid.jdbc.ResultSetImpl) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ArrayImpl(org.teiid.core.types.ArrayImpl) Reader(java.io.Reader) StreamCorruptedException(java.io.StreamCorruptedException)

Aggregations

ArrayImpl (org.teiid.core.types.ArrayImpl)31 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SQLException (java.sql.SQLException)3 Array (org.teiid.query.sql.symbol.Array)3 Constant (org.teiid.query.sql.symbol.Constant)3 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Array (java.sql.Array)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TransformationException (org.teiid.core.types.TransformationException)2 XMLType (org.teiid.core.types.XMLType)2 ProcedureParameter (org.teiid.metadata.ProcedureParameter)2 LanguageObject (org.teiid.query.sql.LanguageObject)2 Command (org.teiid.query.sql.lang.Command)2 Expression (org.teiid.query.sql.symbol.Expression)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1