下面列出了java.sql.SQLData#java.sql.Struct 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
}
@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;
}
}
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;
}
@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});
});
}
@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++;
}
}
}
@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);
}
@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);
}
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);
}
}
@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;
}
@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()));
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
try
{
checkForNullPhysicalConnection();
return physicalConnection_.createStruct( typeName, attributes );
} catch (SQLException sqle) {
notifyException(sqle);
throw sqle;
}
}
@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;
}
}
@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;
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
try {
return getRealConnection().createStruct (typeName, attributes);
} catch (SQLException sqle) {
notifyException(sqle);
throw sqle;
}
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
try
{
checkForNullPhysicalConnection();
return physicalConnection_.createStruct( typeName, attributes );
} catch (SQLException sqle) {
notifyException(sqle);
throw sqle;
}
}
@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()));
}
/** 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();
}
}
@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);
}
}
@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;
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
try {
return getRealConnection().createStruct (typeName, attributes);
} catch (SQLException sqle) {
notifyException(sqle);
throw sqle;
}
}
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;
}
@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));
}
@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;
}
}
@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()));
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return null;
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return conn.createStruct(typeName, attributes);
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return null;
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
throw new AssertionError("unexpected call in JDBC 4.0");
}
public Struct createStruct(String arg0, Object[] arg1) throws SQLException
{
throw new SQLFeatureNotSupportedException(NOT_SUPPORTED);
}
@Override
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException
{
throw new SQLFeatureNotSupportedException("createStruct");
}