public Array toSqlArray ( final Connection connection, final Event event ) throws SQLException { final DateFormat isoDateFormat = new SimpleDateFormat ( isoDatePatterrn ); final String[] fields; // array must be large enough to hold all attributes plus id and both time stamps fields = new String[ ( event.getAttributes ().size () + 3 ) * 2]; // now populate values fields[0] = "id"; fields[1] = event.getId ().toString (); fields[2] = "sourceTimestamp"; fields[3] = isoDateFormat.format ( event.getSourceTimestamp () ); fields[4] = "entryTimestamp"; fields[5] = isoDateFormat.format ( event.getEntryTimestamp () ); int i = 6; for ( final Entry<String, Variant> entry : event.getAttributes ().entrySet () ) { fields[i] = entry.getKey (); fields[i + 1] = entry.getValue ().toString (); i += 2; } return connection.createArrayOf ( "text", fields ); }
public void testArrayA() { try { String ddl0 = "DROP TABLE ARRAYTEST IF EXISTS"; String ddl1 = "CREATE TABLE ARRAYTEST(A INTEGER ARRAY)"; String dml1 = "INSERT INTO ARRAYTEST VALUES(ARRAY[0,0])"; String dml2 = "INSERT INTO ARRAYTEST VALUES ?"; statement.execute(ddl0); statement.execute(ddl1); statement.execute(dml1); PreparedStatement ps = connection.prepareStatement(dml2); Object[] objects = new Object[] { "1", 3, 9 }; Array array = connection.createArrayOf("INTEGER", objects); ps.setArray(1, array); ps.execute(); } catch (SQLException e) { e.printStackTrace(); fail("array failure"); } }
@Test public void shortArraysWithNull() throws Exception { final Random r = new Random(); try (Connection conn = DriverManager.getConnection(url)) { ScalarType component = ColumnMetaData.scalar(Types.SMALLINT, "SMALLINT", Rep.SHORT); List<Array> arrays = new ArrayList<>(); // Construct the data for (int i = 0; i < 5; i++) { List<Short> elements = new ArrayList<>(); for (int j = 0; j < 4; j++) { short value = (short) r.nextInt(Short.MAX_VALUE); // 50% of the time, negate the value if (0 == r.nextInt(2)) { value *= -1; } elements.add(Short.valueOf(value)); } elements.add(null); arrays.add(createArray("SMALLINT", component, elements)); } // Verify read/write writeAndReadArrays(conn, "short_arrays", "SMALLINT", component, arrays, PRIMITIVE_LIST_VALIDATOR); } }
/** * Stores the Array in static for username + connectionId * * @param array * the Array to store */ public void put(Array array) { debug("Creating an array for user: " + connectionKey); if (array == null) { throw new IllegalArgumentException("array is null!"); } Set<Array> arraySet = arrayMap.get(connectionKey); if (arraySet == null) { arraySet = new LinkedHashSet<Array>(); } arraySet.add(array); arrayMap.put(connectionKey, arraySet); }
@Test public void test() { BindParameterMapperManager parameterMapperManager = new BindParameterMapperManager(); Array jdbcArray = newProxy(Array.class); Double[] array = { Double.valueOf(111.11d), Double.valueOf(222.22d) }; Connection conn = newProxy(Connection.class, (proxy, method, args) -> { if (method.getName().equals("createArrayOf")) { assertThat(args[0], is("FLOAT")); assertThat(args[1], is(array)); return jdbcArray; } return method.invoke(proxy, args); }); assertThat(parameterMapperManager.toJdbc(array, conn), is(jdbcArray)); Object[] objArray = { Double.valueOf(333.33d), "A" }; assertThat(parameterMapperManager.toJdbc(objArray, conn), is(objArray)); }
protected Array convertToArray(Connection con, Set<Access> accessTypes) throws SQLException { if (accessTypes != null) { Integer[] accessIds = new Integer[accessTypes.size()]; int j = 0; for (Access access : accessTypes) { accessIds[j++] = access.getId(); } return con.createArrayOf("smallint", accessIds); } else { return null; } }
/** * Factory method for creating Array objects. * <p> * <b>Note: </b>When <code>createArrayOf</code> is used to create an array object * that maps to a primitive data type, then it is implementation-defined * whether the <code>Array</code> object is an array of that primitive * data type or an array of <code>Object</code>. * <p> * <b>Note: </b>The JDBC driver is responsible for mapping the elements * <code>Object</code> array to the default JDBC SQL type defined in * java.sql.Types for the given class of <code>Object</code>. The default * mapping is specified in Appendix B of the JDBC specification. If the * resulting JDBC type is not the appropriate type for the given typeName then * it is implementation defined whether an <code>SQLException</code> is * thrown or the driver supports the resulting conversion. * * @param typeName the SQL name of the type the elements of the array map to. The typeName is a * database-specific name which may be the name of a built-in type, a user-defined type or a standard SQL type supported by this database. This * is the value returned by <code>Array.getBaseTypeName</code> * @param elements the elements that populate the returned object * @return an Array object whose elements map to the specified SQL type * @throws SQLException if a database error occurs, the JDBC type is not * appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection * @throws SQLFeatureNotSupportedException if the JDBC driver does not support this data type * @since 1.6 */ //#ifdef JAVA6 public Array createArrayOf(String typeName, Object[] elements) throws SQLException { checkClosed(); if (typeName == null) { throw JDBCUtil.nullArgument(); } typeName = typeName.toUpperCase(); int typeCode = Type.getTypeNr(typeName); if (typeCode < 0) { throw JDBCUtil.invalidArgument(typeName); } Type type = Type.getDefaultType(typeCode); if (type.isArrayType() || type.isLobType() || type.isRowType()) { throw JDBCUtil.invalidArgument(typeName); } Object[] newData = new Object[elements.length]; try { for (int i = 0; i < elements.length; i++) { Object o = type.convertJavaToSQL(sessionProxy, elements[i]); newData[i] = type.convertToTypeLimits(sessionProxy, o); } } catch (HsqlException e) { throw JDBCUtil.sqlException(e); } return new JDBCArray(newData, type, this); }
public Array createArrayOf(String typeName, Object[] elements) throws SQLException { try { return realConnection.createArrayOf(typeName, elements); } catch(SQLException s) { String methodCall = "createArrayOf(" + typeName + ", " + elements + ")"; reportException(methodCall, s, null); throw s; } }
protected void createDataTypesRows(RowSet crs) throws SQLException { Integer aInteger = 100; String aChar = "Oswald Cobblepot"; Long aLong = Long.MAX_VALUE; Short aShort = Short.MAX_VALUE; Double aDouble = Double.MAX_VALUE; BigDecimal aBigDecimal = BigDecimal.ONE; Boolean aBoolean = false; Float aFloat = Float.MAX_VALUE; Byte aByte = Byte.MAX_VALUE; Date aDate = Date.valueOf(LocalDate.now()); Time aTime = Time.valueOf(LocalTime.now()); Timestamp aTimeStamp = Timestamp.valueOf(LocalDateTime.now()); Array aArray = new StubArray("INTEGER", new Object[1]); Ref aRef = new SerialRef(new StubRef("INTEGER", query)); byte[] bytes = new byte[10]; crs.moveToInsertRow(); crs.updateInt(1, aInteger); crs.updateString(2, aChar); crs.updateString(3, aChar); crs.updateLong(4, aLong); crs.updateBoolean(5, aBoolean); crs.updateShort(6, aShort); crs.updateDouble(7, aDouble); crs.updateBigDecimal(8, aBigDecimal); crs.updateFloat(9, aFloat); crs.updateByte(10, aByte); crs.updateDate(11, aDate); crs.updateTime(12, aTime); crs.updateTimestamp(13, aTimeStamp); crs.updateBytes(14, bytes); crs.updateArray(15, aArray); crs.updateRef(16, aRef); crs.updateDouble(17, aDouble); crs.insertRow(); crs.moveToCurrentRow(); }
public Array getArray(int parameterIndex) throws SQLException { try { if (this.wrappedStmt != null) { return ((CallableStatement) this.wrappedStmt).getArray(parameterIndex); } throw SQLError.createSQLException("No operations allowed after statement closed", SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor); } catch (SQLException sqlEx) { checkAndFireConnectionError(sqlEx); } return null; }
@Override public Object getNullableResult(ResultSet rs, String columnName) throws SQLException { Array array = rs.getArray(columnName); return array == null ? null : array.getArray(); }
public Event fromSqlArray ( final Array array ) throws SQLException, ParseException { final DateFormat isoDateFormat = new SimpleDateFormat ( isoDatePatterrn ); final EventBuilder eb = Event.create (); final String[] fields = (String[])array.getArray (); for ( int i = 0; i < fields.length; i += 2 ) { final String key = fields[i]; final String value = fields[i + 1]; if ( key.equals ( "id" ) ) { eb.id ( UUID.fromString ( value ) ); } else if ( key.equals ( "sourceTimestamp" ) ) { eb.sourceTimestamp ( isoDateFormat.parse ( value ) ); } else if ( key.equals ( "entryTimestamp" ) ) { eb.entryTimestamp ( isoDateFormat.parse ( value ) ); } else { eb.attribute ( key, VariantEditor.toVariant ( value ) ); } } return eb.build (); }
@SuppressWarnings("unchecked") @Override public Array getArray() throws SQLException { final Object o = getObject(); if (o == null) { return null; } if (o instanceof ArrayImpl) { return (ArrayImpl) o; } // If it's not an Array already, assume it is a List. return new ArrayImpl((List<Object>) o, this); }
@Test(enabled = true) public void test06() throws Exception { Object[] coffees = new Object[]{"Espresso", "Colombian", "French Roast", "Cappuccino"}; Array a = new StubArray("VARCHAR", coffees); Object[] values = {a}; SQLInputImpl sqli = new SQLInputImpl(values, map); Array a2 = sqli.readArray(); assertTrue(Arrays.equals((Object[]) a2.getArray(), (Object[]) a.getArray())); assertTrue(a.getBaseTypeName().equals(a2.getBaseTypeName())); }
@Test(enabled = true) public void test04() throws Exception { Object[] coffees = new Object[]{"Espresso", "Colombian", "French Roast", "Cappuccino"}; Array a = new StubArray("VARCHAR", coffees); outImpl.writeArray(a); SerialArray sa = (SerialArray) results.get(0); assertTrue(Arrays.equals(coffees, (Object[]) sa.getArray())); assertTrue(a.getBaseTypeName().equals(sa.getBaseTypeName())); }
@Override public <T> T[] getArray(String columnName, Class<T> arrayElement) throws WrapperAccessException { try { Array arr = rs.getArray(columnName); if (arr != null) { @SuppressWarnings("unchecked") T[] ret = (T[]) arr.getArray(); return ret; } return null; } catch (SQLException ex) { throw new WrapperAccessException(ex.getLocalizedMessage(), ex); } }
public java.sql.Array createArrayOf(String typeName, Object[] elements) throws SQLException { checkClosed(); try { return ((java.sql.Connection) this.mc).createArrayOf(typeName, elements); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // never reached, but compiler can't tell }
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { throwIfClosed(); try { return super.createArrayOf(typeName, elements); } catch (UnsupportedOperationException e) { throw new SQLFeatureNotSupportedException(e.getMessage(), e); } }
@Test public void testCreateArrayOf() throws Exception { try (Connection conn = DriverManager.getConnection(url)) { final String componentName = SqlType.INTEGER.name(); Array a1 = conn.createArrayOf(componentName, new Object[] {1, 2, 3, 4, 5}); Array a2 = conn.createArrayOf(componentName, new Object[] {2, 3, 4, 5, 6}); Array a3 = conn.createArrayOf(componentName, new Object[] {3, 4, 5, 6, 7}); AvaticaType arrayType = ColumnMetaData.array( ColumnMetaData.scalar(Types.INTEGER, componentName, Rep.INTEGER), "NUMBERS", Rep.ARRAY); writeAndReadArrays(conn, "CREATE_ARRAY_OF_INTEGERS", componentName, arrayType, Arrays.asList(a1, a2, a3), PRIMITIVE_LIST_VALIDATOR); } }
@Override public Array createArrayOf(String arg0, Object[] arg1) throws SQLException { throw new SQLException("Method not supported"); }
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public Array getArray() throws SQLException { throw new SQLFeatureNotSupportedException(); }
@Override public void updateArray(int columnIndex, Array x) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void setArray(int parameterIndex, Array x) throws SQLException { parameters.setParameter(parameterIndex, x); }
public java.sql.Array createArrayOf(String typeName, Object[] elements) throws SQLException { return getActiveConnection().createArrayOf(typeName, elements); }
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { checkClosed(); throw SQLError.noSupport(); }
public void setArray(int parameterIndex, Array x) throws SQLException { throw new SQLException("Not Implemented: setArray(int parameterIndex, Array x)"); }
@Override public Array getArray(int columnIndex) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public Array getArray(String parameterName) throws SQLException { checkClosed(); throw SQLError.noSupport(); }
/** * <!-- start generic documentation --> * Sets the designated parameter to the given <code>java.sql.Array</code> object. * The driver converts this to an SQL <code>ARRAY</code> value when it * sends it to the database. * <!-- end generic documentation --> * * <!-- start release-specific documentation --> * <div class="ReleaseSpecificDocumentation"> * <h3>HSQLDB-Specific Information:</h3> <p> * * From version 2.0, HSQLDB supports the SQL ARRAY type. * * </div> * <!-- end release-specific documentation --> * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x an <code>Array</code> object that maps an SQL <code>ARRAY</code> value * @exception SQLException if a database access error occurs or * this method is called on a closed <code>PreparedStatement</code> * @throws SQLFeatureNotSupportedException if the JDBC driver does not support this method * @since JDK 1.2 (JDK 1.1.x developers: read the overview for * JDBCParameterMetaData) */ public synchronized void setArray(int parameterIndex, Array x) throws SQLException { checkParameterIndex(parameterIndex); Type type = this.parameterMetaData.columnTypes[parameterIndex - 1]; if (!type.isArrayType()) { throw JDBCUtil.sqlException(ErrorCode.X_42561); } if (x == null) { setParameter(parameterIndex, null); return; } Object[] data = null; if (x instanceof JDBCArray) { data = ((JDBCArray) x).getArrayInternal(); } else { Object object = x.getArray(); if (object instanceof Object[]) { Type baseType = type.collectionBaseType(); Object[] array = (Object[]) object; data = new Object[array.length]; for (int i = 0; i < data.length; i++) { data[i] = baseType.convertJavaToSQL(session, array[i]); } } else { // if foreign data is not Object[] throw JDBCUtil.notSupported(); } } parameterValues[parameterIndex - 1] = data; parameterSet[parameterIndex - 1] = Boolean.TRUE; }
@Override public Array getArray(String columnLabel) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { ps.setArray(i, (Array) parameter); }
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { lazyEnlistmentCheck(); return wrappedConnection.createArrayOf( typeName, elements ); }
@Override public Array getArray(int columnIndex) throws SQLException { return null; }
@Override public void updateArray(String columnLabel, Array x) throws SQLException { throw SQLError.noSupport(); }
@Override public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Array array = rs.getArray(columnIndex); return array == null ? null : array.getArray(); }
@Override public Array getArray(int parameterIndex) throws SQLException { return wrappedStatement.getArray( parameterIndex ); }