@Override public T read(Connection connection, K key) throws SQLException { PreparedStatement preparedStatement = selectQueryPreparer.prepare(connection).bind(key); try { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return selectQueryMapper.map(resultSet); } } finally { safeClose(preparedStatement); } return null; }
protected <RH extends CheckedConsumer<? super K>> RH executeQueryPreparerInBatchMode(Connection connection, Collection<T> values, RH keyConsumer, QueryPreparer<T> queryPreparer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepareStatement(connection); try { FieldMapper<T, PreparedStatement> mapper = queryPreparer.mapper(); for (T value : values) { mapper.mapTo(value, preparedStatement, null); preparedStatement.addBatch(); } preparedStatement.executeBatch(); if (hasGeneratedKeys && keyConsumer != null) { handleGeneratedKeys(keyConsumer, preparedStatement); } return keyConsumer; } catch(Exception e) { ErrorHelper.rethrow(e); } finally { safeClose(preparedStatement); } return keyConsumer; }
@Override public RewrittenStatement rewrite(String s, Binding binding, StatementContext statementContext) { final String sql = queryPreparer.toRewrittenSqlQuery(o); return new SfmRewrittenStatement(sql); }
PreparedStatement ps = insertQueryPreparer.prepare(connection).bind(dbObject); try { ps.execute(); final QueryBinder<DbObject> queryBinder = selectQueryPreparer.prepare(connection); ps = selectQueryPreparer.prepareStatement(connection); try { queryBinder.bindTo(dbObject, ps);
@Override public T read(Connection connection, K key) throws SQLException { PreparedStatement preparedStatement = selectQueryPreparer.prepare(connection).bind(key); try { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return selectQueryMapper.map(resultSet); } } finally { safeClose(preparedStatement); } return null; }
protected <RH extends CheckedConsumer<? super K>> RH executeQueryPreparerInBatchMode(Connection connection, Collection<T> values, RH keyConsumer, QueryPreparer<T> queryPreparer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepareStatement(connection); try { FieldMapper<T, PreparedStatement> mapper = queryPreparer.mapper(); for (T value : values) { mapper.mapTo(value, preparedStatement, null); preparedStatement.addBatch(); } preparedStatement.executeBatch(); if (hasGeneratedKeys && keyConsumer != null) { handleGeneratedKeys(keyConsumer, preparedStatement); } return keyConsumer; } catch(Exception e) { ErrorHelper.rethrow(e); } finally { safeClose(preparedStatement); } return keyConsumer; }
protected <RH extends CheckedConsumer<? super K>, QPT> RH executeQueryPreparer(Connection connection, QPT value, RH keyConsumer, QueryPreparer<QPT> queryPreparer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(value); try { preparedStatement.executeUpdate(); if (hasGeneratedKeys && keyConsumer != null) { handleGeneratedKeys(keyConsumer, preparedStatement); } return keyConsumer; } finally { safeClose(preparedStatement); } }
protected <RH extends CheckedConsumer<? super K>, QPT> RH executeQueryPreparer(Connection connection, QPT value, RH keyConsumer, QueryPreparer<QPT> queryPreparer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(value); try { preparedStatement.executeUpdate(); if (hasGeneratedKeys && keyConsumer != null) { handleGeneratedKeys(keyConsumer, preparedStatement); } return keyConsumer; } finally { safeClose(preparedStatement); } }
@Override public T readFirst(Connection connection, P p) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(p); try { ResultSet rs = preparedStatement.executeQuery(); try { if (rs.next()) { return mapper.map(rs); } return null; } finally { safeClose(rs); } } finally { safeClose(preparedStatement); } }
@Override public T readFirst(Connection connection, P p) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(p); try { ResultSet rs = preparedStatement.executeQuery(); try { if (rs.next()) { return mapper.map(rs); } return null; } finally { safeClose(rs); } } finally { safeClose(preparedStatement); } }
@Override public <C extends CheckedConsumer<? super T>> C read(Connection connection, P p, C consumer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(p); try { ResultSet rs = preparedStatement.executeQuery(); try { mapper.forEach(rs, consumer); } finally { safeClose(rs); } } finally { safeClose(preparedStatement); } return consumer; }
@Override public <C extends CheckedConsumer<? super T>> C read(Connection connection, P p, C consumer) throws SQLException { PreparedStatement preparedStatement = queryPreparer.prepare(connection).bind(p); try { ResultSet rs = preparedStatement.executeQuery(); try { mapper.forEach(rs, consumer); } finally { safeClose(rs); } } finally { safeClose(preparedStatement); } return consumer; }
@Test public void testSelectWithInList() throws SQLException { NamedSqlQuery selectInListQuery = NamedSqlQuery.parse("select * from Table where name in (?) and id = ? "); QueryPreparer<QueryParamList> selectInListMapper = jdbcMapperFactory.from(QueryParamList.class).to(selectInListQuery); Connection conn = mock(Connection.class); PreparedStatement mps = mock(PreparedStatement.class); QueryParamList value = new QueryParamList(); value.name = Arrays.asList("name1", "name2"); value.id = 3334; when(conn.prepareStatement("select * from Table where name in (?, ?) and id = ? ")).thenReturn(mps); PreparedStatement ps = selectInListMapper.prepare(conn).bind(value); assertSame(mps, ps); verify(mps).setString(1, "name1"); verify(mps).setString(2, "name2"); verify(mps).setInt(3, 3334); }
@Test public void testSelectWithInArray() throws SQLException { NamedSqlQuery selectInListQuery = NamedSqlQuery.parse("select * from Table where name = ? and id in (?) "); QueryPreparer<QueryParamArray> selectInListMapper = jdbcMapperFactory.from(QueryParamArray.class).to(selectInListQuery); Connection conn = mock(Connection.class); PreparedStatement mps = mock(PreparedStatement.class); QueryParamArray value = new QueryParamArray(); value.name = "nannme"; value.id = new int[] { 3334, 3336 }; when(conn.prepareStatement("select * from Table where name = ? and id in (?, ?) ")).thenReturn(mps); PreparedStatement ps = selectInListMapper.prepare(conn).bind(value); assertSame(mps, ps); verify(mps).setString(1, "nannme"); verify(mps).setInt(2, 3334); verify(mps).setInt(3, 3336); }
@Test public void testQueryBinderClosePsOnException() throws SQLException { NamedSqlQuery query = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? "); QueryPreparer<DbObject> queryPreparer = jdbcMapperFactory .from(DbObject.class).to(query); Connection conn = mock(Connection.class); PreparedStatement ps = mock(PreparedStatement.class); when(conn.prepareStatement(anyString())).thenReturn(ps); doThrow(new IllegalStateException()).when(ps).setLong(anyInt(), anyLong()); try { queryPreparer.prepare(conn).bind(DbObject.newInstance()); fail(); } catch(IllegalStateException e) { // expected } verify(ps).close(); }