@Override public java.util.List<gen.model.test.Clicked> search( org.revenj.patterns.Specification<gen.model.test.Clicked> specification, Integer limit, Integer offset) { final String selectType = "SELECT it"; java.util.function.Consumer<java.sql.PreparedStatement> applyFilters = ps -> {}; java.sql.Connection connection = getConnection(); try (org.revenj.postgres.PostgresWriter pgWriter = org.revenj.postgres.PostgresWriter.create()) { String sql; if (specification == null) { sql = "SELECT r FROM \"test\".\"Clicked_event\" r"; } else if (specification instanceof gen.model.test.Clicked.BetweenNumbers) { gen.model.test.Clicked.BetweenNumbers spec = (gen.model.test.Clicked.BetweenNumbers) specification; sql = selectType + " FROM \"test\".\"Clicked.BetweenNumbers\"(?, ?, ?) it"; applyFilters = applyFilters.andThen( ps -> { try { ps.setBigDecimal(1, spec.getMin()); } catch (Exception e) { throw new RuntimeException(e); } }); applyFilters = applyFilters.andThen( ps -> { try { Object[] __arr = new Object[spec.getInSet().size()]; int __ind = 0; for (Object __it : spec.getInSet()) __arr[__ind++] = __it; ps.setArray(2, connection.createArrayOf("numeric", __arr)); } catch (Exception e) { throw new RuntimeException(e); } }); applyFilters = applyFilters.andThen( ps -> { try { if (spec.getEn() == null) ps.setNull(3, java.sql.Types.OTHER); else { org.postgresql.util.PGobject __pgo = new org.postgresql.util.PGobject(); __pgo.setType("\"test\".\"En\""); __pgo.setValue( gen.model.test.converters.EnConverter.stringValue(spec.getEn())); ps.setObject(3, __pgo); } } catch (Exception e) { throw new RuntimeException(e); } }); } else { org.revenj.patterns.Query<gen.model.test.Clicked> query = query(specification); if (offset != null) { query = query.skip(offset); } if (limit != null) { query = query.limit(limit); } try { return query.list(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } if (limit != null) { sql += " LIMIT " + Integer.toString(limit); } if (offset != null) { sql += " OFFSET " + Integer.toString(offset); } try (java.sql.PreparedStatement statement = connection.prepareStatement(sql)) { applyFilters.accept(statement); return readFromDb(statement, new java.util.ArrayList<>()); } catch (java.sql.SQLException | java.io.IOException e) { throw new RuntimeException(e); } } finally { releaseConnection(connection); } }
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"); } }
private Array createVarcharArray(Connection conn, StringArray s) throws SQLException { if (s == null) { return conn.createArrayOf(VARCHAR, new String[] {}); } else { return conn.createArrayOf(VARCHAR, s.toArray()); } }
@Override public void setFindDocsSelectStatementParameters( CollectionSchema colSchema, Integer[] requestedDocs, Projection projection, Connection c, PreparedStatement ps) throws SQLException { ps.setString(1, colSchema.getName()); ps.setArray(2, c.createArrayOf("integer", requestedDocs)); Integer[] requiredTables = requiredTables(colSchema, projection); ps.setArray(3, c.createArrayOf("integer", requiredTables)); }
public Array createArrayOf(String typeName, Object[] elements) throws SQLException { // todo: dump elements? String methodCall = "createArrayOf(" + typeName + ", " + elements + ")"; try { return (Array) reportReturn(methodCall, realConnection.createArrayOf(typeName, elements)); } catch (SQLException s) { reportException(methodCall, s); throw s; } }
@Override public Array createArrayOf(int type, Object[] elements, Connection connection) throws SQLException { if (elements == null || elements.length == 0) { return null; } String typeName; switch (type) { case Types.VARCHAR: typeName = "varchar"; break; case Types.CLOB: typeName = "text"; break; case Types.BIT: typeName = "bool"; break; case Types.BIGINT: typeName = "int8"; break; case Types.DOUBLE: typeName = "float8"; break; case Types.TIMESTAMP: typeName = "timestamp"; break; case Types.SMALLINT: typeName = "int2"; break; case Types.INTEGER: typeName = "int4"; break; case Types.OTHER: // id switch (idType) { case VARCHAR: typeName = "varchar"; break; case UUID: typeName = "uuid"; break; case SEQUENCE: typeName = "int8"; break; default: throw new AssertionError("Unknown id type: " + idType); } break; default: throw new AssertionError("Unknown type: " + type); } return connection.createArrayOf(typeName, elements); }
@Override public void mark(String[] uris) { java.sql.Connection connection = getConnection(); try (java.sql.PreparedStatement statement = connection.prepareStatement("/*NO LOAD BALANCE*/SELECT \"test\".\"mark_Clicked\"(?)")) { Object[] ids = new Object[uris.length]; for (int i = 0; i < uris.length; i++) { ids[i] = Long.parseLong(uris[i]); } statement.setArray(1, connection.createArrayOf("bigint", ids)); statement.executeUpdate(); } catch (java.sql.SQLException e) { throw new RuntimeException(e); } finally { releaseConnection(connection); } }
@Override public java.util.List<gen.model.test.Clicked> find(String[] uris) { java.sql.Connection connection = getConnection(); try (java.sql.PreparedStatement statement = connection.prepareStatement( "SELECT r FROM \"test\".\"Clicked_event\" r WHERE r._event_id = ANY(?)")) { Object[] ids = new Object[uris.length]; for (int i = 0; i < uris.length; i++) { ids[i] = Long.parseLong(uris[i]); } statement.setArray(1, connection.createArrayOf("bigint", ids)); return readFromDb(statement, new java.util.ArrayList<>(uris.length)); } catch (java.sql.SQLException | java.io.IOException e) { throw new RuntimeException(e); } finally { releaseConnection(connection); } }
@Override public Set<String> findConflicts(@Nullable String namespace, Set<String> paths) { checkNotNull(paths, "paths is null"); Set<String> matches = new HashSet<>(); namespace = namespace(namespace); final int partitionSize = 1000; final String sql = format( "SELECT path FROM %s WHERE repository = ? AND namespace = ? AND path = ANY(?)", conflictsTable); try (Connection cx = dataSource.getConnection()) { cx.setAutoCommit(true); try (PreparedStatement ps = cx.prepareStatement(sql)) { Iterable<List<String>> partitions = Iterables.partition(paths, partitionSize); for (List<String> partition : partitions) { String[] pathsArg = partition.toArray(new String[partition.size()]); Array array = cx.createArrayOf("varchar", pathsArg); ps.clearParameters(); ps.setInt(1, repositoryId); ps.setString(2, namespace); ps.setArray(3, array); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { matches.add(rs.getString(1)); } } } } catch (SQLException e) { throw e; } } catch (SQLException e) { throw propagate(e); } return matches; }
public void setDeleteDidsStatementParameters(PreparedStatement ps, Collection<Integer> dids) throws SQLException { Connection connection = ps.getConnection(); final int maxInArray = (2 << 15) - 1; // = 2^16 -1 = 65535 Integer[] didsToDelete = dids.toArray(new Integer[dids.size()]); int i = 0; while (i < didsToDelete.length) { int toIndex = Math.min(i + maxInArray, didsToDelete.length); Integer[] subDids = Arrays.copyOfRange(didsToDelete, i, toIndex); Array arr = connection.createArrayOf("integer", subDids); ps.setArray(1, arr); ps.addBatch(); i = toIndex; } }
@BeforeClass public static void setUpObjects() throws Exception { // (Note: Can't use JdbcTest's connect(...) for this test class.) connection = new Driver().connect("jdbc:drill:zk=local", JdbcAssert.getDefaultProperties()); plainStatement = connection.createStatement(); preparedStatement = connection.prepareStatement("VALUES 'PreparedStatement query'"); try { connection.prepareCall("VALUES 'CallableStatement query'"); fail("Test seems to be out of date. Was prepareCall(...) implemented?"); } catch (SQLException | UnsupportedOperationException e) { // Expected. } try { connection.createArrayOf("INTEGER", new Object[0]); fail("Test seems to be out of date. Were arrays implemented?"); } catch (SQLException | UnsupportedOperationException e) { // Expected. } resultSet = plainStatement.executeQuery("VALUES 'plain Statement query'"); resultSet.next(); resultSetMetaData = resultSet.getMetaData(); databaseMetaData = connection.getMetaData(); // Self-check that member variables are set: assertFalse("Test setup error", connection.isClosed()); assertFalse("Test setup error", plainStatement.isClosed()); assertFalse("Test setup error", preparedStatement.isClosed()); assertFalse("Test setup error", resultSet.isClosed()); // (No ResultSetMetaData.isClosed() or DatabaseMetaData.isClosed():) assertNotNull("Test setup error", resultSetMetaData); assertNotNull("Test setup error", databaseMetaData); }
@Override public void removeConflicts(final @Nullable String ns, final Iterable<String> paths) { checkNotNull(paths, "paths is null"); final String namespace = namespace(ns); final String sql = format( "DELETE FROM %s WHERE repository = ? AND namespace = ? AND path = ANY(?)", conflictsTable); try (Connection cx = dataSource.getConnection()) { cx.setAutoCommit(false); try (PreparedStatement ps = cx.prepareStatement(sql)) { final int partitionSize = 1000; Iterable<List<String>> partitions = Iterables.partition(paths, partitionSize); for (List<String> partition : partitions) { String[] pathsArg = partition.toArray(new String[partition.size()]); Array array = cx.createArrayOf("varchar", pathsArg); ps.clearParameters(); ps.setInt(1, repositoryId); ps.setString(2, namespace); ps.setArray(3, array); ps.executeUpdate(); } cx.commit(); } catch (SQLException e) { cx.rollback(); throw e; } finally { cx.setAutoCommit(true); } } catch (SQLException e) { throw propagate(e); } }
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { return _wrapped.createArrayOf(typeName, elements); }
@Override public Array createArrayOf(String s, Object[] objects) throws SQLException { return conn.createArrayOf(s, objects); }
@Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { checkState(); return conn.createArrayOf(typeName, elements); }
public Array createArrayOf(String typeName, Object[] elements) throws SQLException { return connection.createArrayOf(typeName, elements); }
@Test public void testUnsupportedOperations() throws Exception { Connection conn = getConnection(); try { conn.prepareCall(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setReadOnly(false); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setCatalog(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.getCatalog(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.prepareCall(null, 0, 0); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setTypeMap(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.getTypeMap(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setSavepoint(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setSavepoint(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.rollback(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.releaseSavepoint(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.prepareCall(null, 0, 0, 0); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.createClob(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.createBlob(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.createNClob(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.createSQLXML(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.createArrayOf(null, (Object[]) null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.createStruct(null, (Object[]) null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setSchema(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.getSchema(); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.abort(null); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.setNetworkTimeout(null, 0); fail(); } catch (UnsupportedOperationException ignore) { } try { conn.getNetworkTimeout(); fail(); } catch (UnsupportedOperationException ignore) { } }
@Override public boolean exists(org.revenj.patterns.Specification<gen.model.test.Clicked> specification) { final String selectType = "SELECT exists(SELECT *"; java.util.function.Consumer<java.sql.PreparedStatement> applyFilters = ps -> {}; java.sql.Connection connection = getConnection(); try (org.revenj.postgres.PostgresWriter pgWriter = org.revenj.postgres.PostgresWriter.create()) { String sql = null; if (specification == null) { sql = "SELECT exists(SELECT * FROM \"test\".\"Clicked_event\" r"; } else if (specification instanceof gen.model.test.Clicked.BetweenNumbers) { gen.model.test.Clicked.BetweenNumbers spec = (gen.model.test.Clicked.BetweenNumbers) specification; sql = selectType + " FROM \"test\".\"Clicked.BetweenNumbers\"(?, ?, ?) it"; applyFilters = applyFilters.andThen( ps -> { try { ps.setBigDecimal(1, spec.getMin()); } catch (Exception e) { throw new RuntimeException(e); } }); applyFilters = applyFilters.andThen( ps -> { try { Object[] __arr = new Object[spec.getInSet().size()]; int __ind = 0; for (Object __it : spec.getInSet()) __arr[__ind++] = __it; ps.setArray(2, connection.createArrayOf("numeric", __arr)); } catch (Exception e) { throw new RuntimeException(e); } }); applyFilters = applyFilters.andThen( ps -> { try { if (spec.getEn() == null) ps.setNull(3, java.sql.Types.OTHER); else { org.postgresql.util.PGobject __pgo = new org.postgresql.util.PGobject(); __pgo.setType("\"test\".\"En\""); __pgo.setValue( gen.model.test.converters.EnConverter.stringValue(spec.getEn())); ps.setObject(3, __pgo); } } catch (Exception e) { throw new RuntimeException(e); } }); } else { try { return query(specification).any(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } try (java.sql.PreparedStatement statement = connection.prepareStatement(sql + ")")) { applyFilters.accept(statement); try (java.sql.ResultSet rs = statement.executeQuery()) { rs.next(); return rs.getBoolean(1); } } catch (java.sql.SQLException e) { throw new RuntimeException(e); } } finally { releaseConnection(connection); } }