@Override public final SQLMergeClause merge(RelationalPath<?> path) { return new SQLMergeClause(connection, configuration, path); }
/** * Execute the clause and return the generated key cast to the given type. * If no rows were created, null is returned, otherwise the key of the first row is returned. * * @param <T> * @param type type of key * @return generated key */ public <T> T executeWithKey(Class<T> type) { return executeWithKey(type, null); }
protected <T> List<T> executeWithKeys(Class<T> type, @Nullable Path<T> path) { ResultSet rs = null; try { rs = executeWithKeys(); List<T> rv = new ArrayList<T>(); while (rs.next()) { rv.add(configuration.get(rs, path, 1, type)); } return rv; } catch (SQLException e) { throw configuration.translate(e); } finally { if (rs != null) { close(rs); } reset(); } }
@SuppressWarnings("unchecked") protected long executeCompositeMerge() { if (hasRow()) { // update SQLUpdateClause update = new SQLUpdateClause(connection(), configuration, entity); populate(update); addListeners(update); addKeyConditions(update); return update.execute(); } else { // insert SQLInsertClause insert = new SQLInsertClause(connection(), configuration, entity); addListeners(insert); populate(insert); return insert.execute(); } }
protected <T> T executeWithKey(Class<T> type, @Nullable Path<T> path) { ResultSet rs = executeWithKeys(); try { if (rs.next()) { return configuration.get(rs, path, 1, type); } else { return null; } } catch (SQLException e) { throw configuration.translate(e); } finally { close(rs); } }
context = startContext(connection(), metadata, entity); try { if (configuration.getTemplates().isNativeMerge()) { PreparedStatement stmt = null; if (batches.isEmpty()) { stmt = createStatement(true); listeners.notifyMerge(entity, metadata, keys, columns, values, subQuery); listeners.executed(context); } else { Collection<PreparedStatement> stmts = createStatements(true); if (stmts != null && stmts.size() > 1) { throw new IllegalStateException("executeWithKeys called with batch statement and multiple SQL strings"); if (hasRow()) { SQLUpdateClause update = new SQLUpdateClause(connection(), configuration, entity); update.addListener(listeners); populate(update); addKeyConditions(update); reset(); endContext(context); return EmptyResultSet.DEFAULT; } else { SQLInsertClause insert = new SQLInsertClause(connection(), configuration, entity); insert.addListener(listeners); populate(insert); return insert.executeWithKeys();
protected long executeNativeMerge() { context = startContext(connection(), metadata, entity); PreparedStatement stmt = null; Collection<PreparedStatement> stmts = null; try { if (batches.isEmpty()) { stmt = createStatement(false); listeners.notifyMerge(entity, metadata, keys, columns, values, subQuery); return rc; } else { stmts = createStatements(false); listeners.notifyMerges(entity, metadata, batches); long rc = executeBatch(stmts); listeners.executed(context); return rc; onException(context,e); throw configuration.translate(queryString, constants, e); } finally { if (stmt != null) { close(stmt); close(stmts); reset(); endContext(context);
@Test public void clear() { KeyAccessorsTest.QEmployee emp1 = new KeyAccessorsTest.QEmployee("emp1"); SQLMergeClause merge = new SQLMergeClause(null, new H2Templates(), emp1); merge.set(emp1.id, 1); merge.addBatch(); assertEquals(1, merge.getBatchCount()); merge.clear(); assertEquals(0, merge.getBatchCount()); } }
@Test @IncludeIn(H2) public void mergeBatch() { SQLMergeClause merge = merge(survey) .keys(survey.id) .set(survey.id, 5) .set(survey.name, "5") .addBatch(); assertEquals(1, merge.getBatchCount()); assertFalse(merge.isEmpty()); merge .keys(survey.id) .set(survey.id, 6) .set(survey.name, "6") .addBatch(); assertEquals(2, merge.getBatchCount()); assertEquals(2, merge.execute()); assertEquals(1L, query().from(survey).where(survey.name.eq("5")).fetchCount()); assertEquals(1L, query().from(survey).where(survey.name.eq("6")).fetchCount()); }
@Test @ExcludeIn({H2, CUBRID, SQLSERVER}) public void merge_with_keys_Projected() throws SQLException { assertNotNull(merge(survey).keys(survey.id) .set(survey.id, 8) .set(survey.name, "Hello you").executeWithKey(survey.id)); }
@Test @ExcludeIn({CUBRID, DB2, DERBY, POSTGRESQL, SQLSERVER, TERADATA}) public void merge_with_keys_Null_Id() throws SQLException { ResultSet rs = merge(survey).keys(survey.id) .setNull(survey.id) .set(survey.name, "Hello World").executeWithKeys(); assertTrue(rs.next()); assertTrue(rs.getObject(1) != null); rs.close(); }
@Test @IncludeIn(H2) public void mergeBatch_with_subquery() { SQLMergeClause merge = merge(survey) .keys(survey.id) .columns(survey.id, survey.name) .select(query().from(survey2).select(survey2.id.add(20), survey2.name)) .addBatch(); merge(survey) .keys(survey.id) .columns(survey.id, survey.name) .select(query().from(survey2).select(survey2.id.add(40), survey2.name)) .addBatch(); assertEquals(1, merge.execute()); }
@Test @ExcludeIn({H2, CUBRID, SQLSERVER}) public void merge_with_keys_listener() throws SQLException { final AtomicBoolean result = new AtomicBoolean(); SQLListener listener = new SQLBaseListener() { @Override public void end(SQLListenerContext context) { result.set(true); } }; SQLMergeClause clause = merge(survey).keys(survey.id) .set(survey.id, 7) .set(survey.name, "Hello World"); clause.addListener(listener); ResultSet rs = clause.executeWithKeys(); assertTrue(rs.next()); assertTrue(rs.getObject(1) != null); rs.close(); assertTrue(result.get()); }
@Test @ExcludeIn({H2, CUBRID, SQLSERVER}) public void merge_with_keys() throws SQLException { ResultSet rs = merge(survey).keys(survey.id) .set(survey.id, 7) .set(survey.name, "Hello World").executeWithKeys(); assertTrue(rs.next()); assertTrue(rs.getObject(1) != null); rs.close(); }
@Test public void merge_listener() { final AtomicInteger calls = new AtomicInteger(0); SQLListener listener = new SQLBaseListener() { @Override public void end(SQLListenerContext context) { if (context.getData(AbstractSQLQuery.PARENT_CONTEXT) == null) { calls.incrementAndGet(); } } }; SQLMergeClause clause = merge(survey).keys(survey.id) .set(survey.id, 5) .set(survey.name, "Hello World"); clause.addListener(listener); assertEquals(1, clause.execute()); assertEquals(1, calls.intValue()); }
protected SQLMergeClause merge(RelationalPath<?> e) { SQLMergeClause sqlMergeClause = new SQLMergeClause(connection, configuration, e); sqlMergeClause.addListener(new TestLoggingListener()); return sqlMergeClause; }
public <T> List<T> executeWithKeys(Class<T> type) { return executeWithKeys(type, null); }
protected boolean hasRow() { SQLQuery<?> query = new SQLQuery<Void>(connection(), configuration).from(entity); for (SQLListener listener : listeners.getListeners()) { query.addListener(listener); } query.addListener(SQLNoCloseListener.DEFAULT); addKeyConditions(query); return query.select(Expressions.ONE).fetchFirst() != null; }
@Test @ExcludeIn({H2, CUBRID, SQLSERVER}) public void merge_with_keys_Projected2() throws SQLException { Path<Object> idPath = ExpressionUtils.path(Object.class, "id"); Object id = merge(survey).keys(survey.id) .set(survey.id, 9) .set(survey.name, "Hello you").executeWithKey(idPath); assertNotNull(id); }