java.sql.Struct Java Examples

The following examples show how to use java.sql.Struct. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: micro-integrator   Author: wso2   File: SQLQuery.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Processes a SQL Array instance and transform it into a ParamValue
 * instance
 *
 * @param dataArray
 *            SQLArray instance
 * @param paramValue
 *            Container into which the SQLArray elements should be populated
 * @return ParamValue instance containing all the elements of the
 *         corresponding SQLArray instance
 * @throws SQLException
 *             When it fails to processes the result set produced by the
 *             SQLArray instance
 */
private ParamValue processSQLArray(Array dataArray, ParamValue paramValue) throws SQLException {
    ResultSet rs = null;
    try {
        rs = dataArray.getResultSet();
        while (rs.next()) {
            Object arrayEl = rs.getObject(2);
            if (arrayEl instanceof Struct) {
                paramValue.getArrayValue().add(new ParamValue((Struct) arrayEl));
            } else if (arrayEl instanceof Array) {
                paramValue.getArrayValue().add(
                        processSQLArray((Array) arrayEl, new ParamValue(
                                ParamValue.PARAM_VALUE_ARRAY)));
            } else {
                paramValue.getArrayValue().add(new ParamValue(String.valueOf(arrayEl)));
            }
        }
        return paramValue;
    } finally {
        this.releaseResources(rs, null);
    }
}
 
Example #2
Source Project: snowflake-jdbc   Author: snowflakedb   File: LogicalConnection.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException
{
  throwExceptionIfClosed();

  try
  {
    return physicalConnection.createStruct(typeName, attributes);
  }
  catch (SQLException e)
  {
    pooledConnection.fireConnectionErrorEvent(e);
    throw e;
  }
}
 
Example #3
Source Project: ClickHouse-Native-JDBC   Author: housepower   File: AbstractPreparedStatement.java    License: Apache License 2.0 6 votes vote down vote up
private boolean assembleComplexQuotedParameter(StringBuilder queryBuilder, Object parameter) throws SQLException {
    if (parameter instanceof Array) {
        queryBuilder.append("[");
        Object[] arrayData = (Object[]) ((Array) parameter).getArray();
        for (int arrayIndex = 0; arrayIndex < arrayData.length; arrayIndex++) {
            assembleParameter(arrayData[arrayIndex], queryBuilder);
            queryBuilder.append(arrayIndex == arrayData.length - 1 ? "]" : ",");
        }
        return true;
    } else if (parameter instanceof Struct) {
        queryBuilder.append("(");
        Object[] structData = ((Struct) parameter).getAttributes();
        for (int structIndex = 0; structIndex < structData.length; structIndex++) {
            assembleParameter(structData[structIndex], queryBuilder);
            queryBuilder.append(structIndex == structData.length - 1 ? ")" : ",");
        }
        return true;
    }
    return false;
}
 
Example #4
Source Project: ClickHouse-Native-JDBC   Author: housepower   File: QueryComplexTypeITest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void successfullyTuple() throws Exception {
    withNewConnection(connection -> {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("SELECT (toUInt32(1),'2')");

        Assert.assertTrue(rs.next());
        Struct struct = (Struct) rs.getObject(1);
        Assert.assertEquals(struct.getAttributes(), new Object[]{(long) 1, "2"});

        Map<String, Class<?>> attrNameWithClass = new LinkedHashMap<String, Class<?>>();
        attrNameWithClass.put("_2", String.class);
        attrNameWithClass.put("_1", Long.class);
        Assert.assertEquals(struct.getAttributes(attrNameWithClass),
                            new Object[]{"2", (long) 1});
    });
}
 
Example #5
Source Project: calcite-avatica   Author: apache   File: StructImplTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test public void testStructAccessor() throws Exception {
  // Create rows based on the inputValues data
  List<List<Object>> rows = new ArrayList<>();
  for (Object o : columnInputBundle.inputValues) {
    rows.add(Collections.singletonList(o));
  }
  try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
    List<Accessor> accessors =
        cursor.createAccessors(
            Collections.singletonList(columnInputBundle.metaData), Unsafe.localCalendar(), null);
    Accessor accessor = accessors.get(0);
    int i = 0;
    while (cursor.next()) {
      Struct s = accessor.getObject(Struct.class);
      Object[] expectedStructAttributes = columnInputBundle.expectedValues.get(i).getAttributes();
      Object[] actualStructAttributes = s.getAttributes();
      assertArrayEquals(expectedStructAttributes, actualStructAttributes);
      i++;
    }
  }
}
 
Example #6
Source Project: r-course   Author: joanby   File: JDBC4ResultSet.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
    if (type == null) {
        throw SQLError.createSQLException("Type parameter can not be null", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
    }

    if (type.equals(Struct.class)) {
        throw new SQLFeatureNotSupportedException();
    } else if (type.equals(RowId.class)) {
        return (T) getRowId(columnIndex);
    } else if (type.equals(NClob.class)) {
        return (T) getNClob(columnIndex);
    } else if (type.equals(SQLXML.class)) {
        return (T) getSQLXML(columnIndex);
    }

    return super.getObject(columnIndex, type);
}
 
Example #7
Source Project: Komondor   Author: wn-upf   File: JDBC4ResultSet.java    License: GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
    if (type == null) {
        throw SQLError.createSQLException("Type parameter can not be null", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
    }

    if (type.equals(Struct.class)) {
        throw new SQLFeatureNotSupportedException();
    } else if (type.equals(RowId.class)) {
        return (T) getRowId(columnIndex);
    } else if (type.equals(NClob.class)) {
        return (T) getNClob(columnIndex);
    } else if (type.equals(SQLXML.class)) {
        return (T) getSQLXML(columnIndex);
    }

    return super.getObject(columnIndex, type);
}
 
Example #8
Source Project: orcas   Author: opitzconsulting   File: JdbcConnectionHandler.java    License: Apache License 2.0 5 votes vote down vote up
public static Struct createStruct(String pTypeName, Object[] pAttributes, CallableStatementProvider pCallableStatementProvider) {
    try {
        CallableStatementProviderImpl lCallableStatementProviderImpl = (CallableStatementProviderImpl) pCallableStatementProvider;
        return lCallableStatementProviderImpl._connection.createStruct(lCallableStatementProviderImpl._parameters
            .getOrcasDbUser()
            .toUpperCase()
            + "."
            + pTypeName.toUpperCase(), pAttributes);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source Project: clearpool   Author: xionghuiCoder   File: PoolConnectionImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
  this.checkState();
  Struct struct = null;
  try {
    struct = this.connection.createStruct(typeName, attributes);
  } catch (SQLException ex) {
    this.handleException(ex);
  }
  return struct;
}
 
Example #10
Source Project: hottub   Author: dsrg-uoft   File: SQLOutputImplTests.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test(enabled = true)
public void test08() throws Exception {
    Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
        "Batman"};
    Struct s = new StubStruct(sqlType, attributes);
    outImpl.writeStruct(s);
    SerialStruct ss = (SerialStruct) results.get(0);
    assertTrue(Arrays.equals(attributes, (Object[]) ss.getAttributes()));
    assertTrue(sqlType.equals(ss.getSQLTypeName()));
}
 
Example #11
Source Project: gemfirexd-oss   Author: gemxd   File: LogicalConnection40.java    License: Apache License 2.0 5 votes vote down vote up
public Struct createStruct(String typeName, Object[] attributes)
      throws SQLException {
      try
      {
  		checkForNullPhysicalConnection();
          return physicalConnection_.createStruct( typeName, attributes );
} catch (SQLException sqle) {
	notifyException(sqle);
	throw sqle;
}
  }
 
Example #12
Source Project: FHIR   Author: IBM   File: PooledConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes)
                throws SQLException {
    try {
        return wrapped.createStruct(typeName, attributes);
    }
    catch (SQLException x) {
        this.reusable = !pool.checkConnectionFailure(x);
        throw x;
    }
}
 
Example #13
Source Project: importer-exporter   Author: 3dcitydb   File: GeometryConverterAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public GeometryObject getEnvelope(Object geomObj) throws SQLException {
	GeometryObject envelope = null;

	if (geomObj instanceof Struct) {
		JGeometry geometry = JGeometry.loadJS((Struct)geomObj);
		double[] ordinates = geometry.getMBR();
		double[] coordinates;

		if (geometry.getDimensions() == 3)
			coordinates = new double[]{ordinates[0], ordinates[1], ordinates[2], ordinates[3], ordinates[4], ordinates[5]};
		else 
			coordinates = new double[]{ordinates[0], ordinates[1], 0, ordinates[2], ordinates[3], 0};

		envelope = GeometryObject.createEnvelope(coordinates, 3, geometry.getSRID());
	}

	return envelope;
}
 
Example #14
Source Project: gemfirexd-oss   Author: gemxd   File: BrokeredConnection40.java    License: Apache License 2.0 5 votes vote down vote up
public Struct createStruct(String typeName, Object[] attributes)
      throws SQLException {
     try {
         return getRealConnection().createStruct (typeName, attributes);
     } catch (SQLException sqle) {
         notifyException(sqle);
         throw sqle;
     }
}
 
Example #15
Source Project: gemfirexd-oss   Author: gemxd   File: LogicalConnection40.java    License: Apache License 2.0 5 votes vote down vote up
public Struct createStruct(String typeName, Object[] attributes)
      throws SQLException {
      try
      {
  		checkForNullPhysicalConnection();
          return physicalConnection_.createStruct( typeName, attributes );
} catch (SQLException sqle) {
	notifyException(sqle);
	throw sqle;
}
  }
 
Example #16
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: SQLOutputImplTests.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test(enabled = true)
public void test08() throws Exception {
    Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
        "Batman"};
    Struct s = new StubStruct(sqlType, attributes);
    outImpl.writeStruct(s);
    SerialStruct ss = (SerialStruct) results.get(0);
    assertTrue(Arrays.equals(attributes, (Object[]) ss.getAttributes()));
    assertTrue(sqlType.equals(ss.getSQLTypeName()));
}
 
Example #17
Source Project: calcite   Author: apache   File: CalciteRemoteDriverTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Check that the "set" conversion table looks like Table B-5 in JDBC 4.1
 * specification */
@Test void testTableB5() {
  SqlType[] columns = {
      SqlType.TINYINT, SqlType.SMALLINT, SqlType.INTEGER, SqlType.BIGINT,
      SqlType.REAL, SqlType.FLOAT, SqlType.DOUBLE, SqlType.DECIMAL,
      SqlType.NUMERIC, SqlType.BIT, SqlType.BOOLEAN, SqlType.CHAR,
      SqlType.VARCHAR, SqlType.LONGVARCHAR, SqlType.BINARY, SqlType.VARBINARY,
      SqlType.LONGVARBINARY, SqlType.DATE, SqlType.TIME, SqlType.TIMESTAMP,
      SqlType.ARRAY, SqlType.BLOB, SqlType.CLOB, SqlType.STRUCT, SqlType.REF,
      SqlType.DATALINK, SqlType.JAVA_OBJECT, SqlType.ROWID, SqlType.NCHAR,
      SqlType.NVARCHAR, SqlType.LONGNVARCHAR, SqlType.NCLOB, SqlType.SQLXML
  };
  Class[] rows = {
      String.class, BigDecimal.class, Boolean.class, Byte.class, Short.class,
      Integer.class, Long.class, Float.class, Double.class, byte[].class,
      BigInteger.class, java.sql.Date.class, Time.class, Timestamp.class,
      Array.class, Blob.class, Clob.class, Struct.class, Ref.class,
      URL.class, Class.class, RowId.class, NClob.class, SQLXML.class,
      Calendar.class, java.util.Date.class
  };
  for (Class row : rows) {
    final String s = row == Date.class ? row.getName() : row.getSimpleName();
    out.print(pad(s));
    for (SqlType column : columns) {
      out.print(SqlType.canSet(row, column) ? "x " : ". ");
    }
    out.println();
  }
}
 
Example #18
Source Project: ClickHouse-Native-JDBC   Author: housepower   File: DataTypeTuple.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serializeBinaryBulk(Object[] data, BinarySerializer serializer) throws SQLException, IOException {
    for (int i = 0; i < nestedTypes.length; i++) {
        Object[] elemsData = new Object[data.length];
        for (int row = 0; row < data.length; row++) {
            elemsData[row] = ((Struct) data[row]).getAttributes()[i];
        }
        nestedTypes[i].serializeBinaryBulk(elemsData, serializer);
    }
}
 
Example #19
Source Project: ClickHouse-Native-JDBC   Author: housepower   File: DataTypeTuple.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException {
    Object[][] rowsWithElems = getRowsWithElems(rows, deserializer);

    Struct[] rowsData = new Struct[rows];
    for (int row = 0; row < rows; row++) {
        Object[] elemsData = new Object[nestedTypes.length];

        for (int elemIndex = 0; elemIndex < nestedTypes.length; elemIndex++) {
            elemsData[elemIndex] = rowsWithElems[elemIndex][row];
        }
        rowsData[row] = new ClickHouseStruct("Tuple", elemsData);
    }
    return rowsData;
}
 
Example #20
Source Project: spliceengine   Author: splicemachine   File: BrokeredConnection40.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public Struct createStruct(String typeName, Object[] attributes)
      throws SQLException {
     try {
         return getRealConnection().createStruct (typeName, attributes);
     } catch (SQLException sqle) {
         notifyException(sqle);
         throw sqle;
     }
}
 
Example #21
Source Project: lams   Author: lamsfoundation   File: JdbcTypeJavaClassMappings.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static ConcurrentHashMap<Class, Integer> buildJavaClassToJdbcTypeCodeMappings() {
	final ConcurrentHashMap<Class, Integer> workMap = new ConcurrentHashMap<>();

	// these mappings are the ones outlined specifically in the spec
	workMap.put( String.class, Types.VARCHAR );
	workMap.put( BigDecimal.class, Types.NUMERIC );
	workMap.put( BigInteger.class, Types.NUMERIC );
	workMap.put( Boolean.class, Types.BIT );
	workMap.put( Short.class, Types.SMALLINT );
	workMap.put( Integer.class, Types.INTEGER );
	workMap.put( Long.class, Types.BIGINT );
	workMap.put( Float.class, Types.REAL );
	workMap.put( Double.class, Types.DOUBLE );
	workMap.put( byte[].class, Types.LONGVARBINARY );
	workMap.put( java.sql.Date.class, Types.DATE );
	workMap.put( Time.class, Types.TIME );
	workMap.put( Timestamp.class, Types.TIMESTAMP );
	workMap.put( Blob.class, Types.BLOB );
	workMap.put( Clob.class, Types.CLOB );
	workMap.put( Array.class, Types.ARRAY );
	workMap.put( Struct.class, Types.STRUCT );
	workMap.put( Ref.class, Types.REF );
	workMap.put( Class.class, Types.JAVA_OBJECT );
	workMap.put( RowId.class, Types.ROWID );
	workMap.put( SQLXML.class, Types.SQLXML );


	// additional "common sense" registrations
	workMap.put( Character.class, Types.CHAR );
	workMap.put( char[].class, Types.VARCHAR );
	workMap.put( Character[].class, Types.VARCHAR );
	workMap.put( Byte[].class, Types.LONGVARBINARY );
	workMap.put( java.util.Date.class, Types.TIMESTAMP );
	workMap.put( Calendar.class, Types.TIMESTAMP );

	return workMap;
}
 
Example #22
Source Project: hottub   Author: dsrg-uoft   File: SQLInputImplTests.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void test11() throws Exception {
    Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
        "Batman"};
    map.put(sqlType, Class.forName("util.SuperHero"));
    Struct struct = new StubStruct(sqlType, attributes);
    Object[] values = {struct};
    SQLInputImpl sqli = new SQLInputImpl(values, map);
    Object o = sqli.readObject();

    assertTrue(hero.equals(o));

}
 
Example #23
Source Project: importer-exporter   Author: 3dcitydb   File: UtilAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected BoundingBox transformBoundingBox(BoundingBox bbox, DatabaseSrs sourceSrs, DatabaseSrs targetSrs, Connection connection) throws SQLException {
    BoundingBox result = new BoundingBox(bbox);
    int sourceSrid = get2DSrid(sourceSrs, connection);
    int targetSrid = get2DSrid(targetSrs, connection);

    try (PreparedStatement psQuery = connection.prepareStatement("select SDO_CS.TRANSFORM(MDSYS.SDO_GEOMETRY(2003, " +
            sourceSrid + ", NULL, MDSYS.SDO_ELEM_INFO_ARRAY(1, 1003, 1), " +
            "MDSYS.SDO_ORDINATE_ARRAY(?,?,?,?)), " + targetSrid + ") from dual")) {
        psQuery.setDouble(1, bbox.getLowerCorner().getX());
        psQuery.setDouble(2, bbox.getLowerCorner().getY());
        psQuery.setDouble(3, bbox.getUpperCorner().getX());
        psQuery.setDouble(4, bbox.getUpperCorner().getY());

        try (ResultSet rs = psQuery.executeQuery()) {
            if (rs.next()) {
                Struct struct = (Struct) rs.getObject(1);
                if (!rs.wasNull() && struct != null) {
                    JGeometry geom = JGeometry.loadJS(struct);
                    double[] ordinatesArray = geom.getOrdinatesArray();

                    result.getLowerCorner().setX(ordinatesArray[0]);
                    result.getLowerCorner().setY(ordinatesArray[1]);
                    result.getUpperCorner().setX(ordinatesArray[2]);
                    result.getUpperCorner().setY(ordinatesArray[3]);
                    result.setSrs(targetSrs);
                }
            }
        }

        return result;
    }
}
 
Example #24
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: SQLOutputImplTests.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test(enabled = true)
public void test08() throws Exception {
    Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
        "Batman"};
    Struct s = new StubStruct(sqlType, attributes);
    outImpl.writeStruct(s);
    SerialStruct ss = (SerialStruct) results.get(0);
    assertTrue(Arrays.equals(attributes, (Object[]) ss.getAttributes()));
    assertTrue(sqlType.equals(ss.getSQLTypeName()));
}
 
Example #25
Source Project: brooklin   Author: linkedin   File: MockJDBCConnection.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
  return null;
}
 
Example #26
Source Project: phoenix   Author: cloudera-labs   File: DelegatingConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
  return conn.createStruct(typeName, attributes);
}
 
Example #27
Source Project: carbon-device-mgt   Author: wso2   File: MockConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
    return null;
}
 
Example #28
Source Project: gemfirexd-oss   Author: gemxd   File: BrokeredConnection.java    License: Apache License 2.0 4 votes vote down vote up
public Struct createStruct(String typeName, Object[] attributes)
      throws SQLException {
  throw new AssertionError("unexpected call in JDBC 4.0");
}
 
Example #29
Source Project: cassandra-jdbc-wrapper   Author: adejanovski   File: AbstractConnection.java    License: Apache License 2.0 4 votes vote down vote up
public Struct createStruct(String arg0, Object[] arg1) throws SQLException
{
    throw new SQLFeatureNotSupportedException(NOT_SUPPORTED);
}
 
Example #30
Source Project: presto   Author: prestosql   File: PrestoConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Struct createStruct(String typeName, Object[] attributes)
        throws SQLException
{
    throw new SQLFeatureNotSupportedException("createStruct");
}