From ed397248dd74e3dcd4a57f0eeca2816b3aa627b2 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 10 Aug 2012 13:15:20 +0200 Subject: [PATCH] [#1692] Replace Factory.executeInsert(), Factory.executeUpdate() and similar methods with more succinct variants --- .../org/jooq/test/_/testcases/CRUDTests.java | 14 +++-- .../main/java/org/jooq/FactoryOperations.java | 53 +++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/Factory.java | 37 +++++++++++++ .../main/java/org/jooq/impl/FactoryProxy.java | 21 ++++++++ .../java/org/jooq/impl/TableRecordImpl.java | 28 +++------- jOOQ/src/main/java/org/jooq/impl/Util.java | 17 ++++++ 6 files changed, 145 insertions(+), 25 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java index fe41ff3dc5..e539812c21 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java @@ -206,19 +206,27 @@ extends BaseTest> int executeInsert(Table table, R record) throws DataAccessException; + /** + * Insert one record + *

+ * This executes something like the following statement: + *

INSERT INTO [table] ... VALUES [record] 
+ *

+ * Unlike {@link TableRecord#storeUsing(TableField...)} or + * {@link UpdatableRecord#store()}, this does not change any of the argument + * record's internal "changed" flags, such that a subsequent + * call to {@link TableRecord#storeUsing(TableField...)} or + * {@link UpdatableRecord#store()} might lead to another INSERT + * statement being executed. + * + * @return The number of inserted records + * @throws DataAccessException if something went wrong executing the query + */ + @Support + > int executeInsert(R record) throws DataAccessException; + /** * Update a table *

UPDATE [table] SET [modified values in record] 
* * @return The number of updated records * @throws DataAccessException if something went wrong executing the query + * @deprecated - 2.5.0 [#1692] - This "mass" update is no longer supported */ + @Deprecated @Support > int executeUpdate(Table table, R record) throws DataAccessException; @@ -1079,7 +1103,10 @@ public interface FactoryOperations extends Configuration { * * @return The number of updated records * @throws DataAccessException if something went wrong executing the query + * @deprecated - 2.5.0 [#1692] - Use + * {@link #executeUpdate(TableRecord, Condition)} instead */ + @Deprecated @Support , T> int executeUpdate(Table table, R record, Condition condition) throws DataAccessException; @@ -1090,7 +1117,9 @@ public interface FactoryOperations extends Configuration { * * @return The number of updated records * @throws DataAccessException if something went wrong executing the query + * @deprecated - 2.5.0 [#1692] - This "mass" update is no longer supported */ + @Deprecated @Support > int executeUpdateOne(Table table, R record) throws DataAccessException; @@ -1100,11 +1129,35 @@ public interface FactoryOperations extends Configuration { * * @return The number of updated records * @throws DataAccessException if something went wrong executing the query + * @deprecated - 2.5.0 [#1692] - The semantics of + * {@link #executeUpdate(TableRecord, Condition)} has changed. + * This method here is no longer necessary. */ + @Deprecated @Support , T> int executeUpdateOne(Table table, R record, Condition condition) throws DataAccessException; + /** + * Update a table + *
UPDATE [table] SET [modified values in record] 
+ * + * @return The number of updated records + * @throws DataAccessException if something went wrong executing the query + */ + @Support + > int executeUpdate(R record) throws DataAccessException; + + /** + * Update a table + *
UPDATE [table] SET [modified values in record] WHERE [condition]
+ * + * @return The number of updated records + * @throws DataAccessException if something went wrong executing the query + */ + @Support + , T> int executeUpdate(R record, Condition condition) throws DataAccessException; + /** * Delete records from a table
DELETE FROM [table]
* diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index de6d92ca87..6b558741d4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -6062,6 +6062,7 @@ public class Factory implements FactoryOperations { * {@inheritDoc} */ @Override + @Deprecated public final > int executeInsert(Table table, R record) { InsertQuery insert = insertQuery(table); insert.setRecord(record); @@ -6072,6 +6073,17 @@ public class Factory implements FactoryOperations { * {@inheritDoc} */ @Override + public final > int executeInsert(R record) { + InsertQuery insert = insertQuery(record.getTable()); + insert.setRecord(record); + return insert.execute(); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated public final > int executeUpdate(Table table, R record) { return executeUpdate(table, record, trueCondition()); } @@ -6080,6 +6092,7 @@ public class Factory implements FactoryOperations { * {@inheritDoc} */ @Override + @Deprecated public final , T> int executeUpdate(Table table, R record, Condition condition) { UpdateQuery update = updateQuery(table); update.addConditions(condition); @@ -6091,6 +6104,7 @@ public class Factory implements FactoryOperations { * {@inheritDoc} */ @Override + @Deprecated public final > int executeUpdateOne(Table table, R record) { return filterUpdateOne(executeUpdate(table, record)); } @@ -6099,10 +6113,33 @@ public class Factory implements FactoryOperations { * {@inheritDoc} */ @Override + @Deprecated public final , T> int executeUpdateOne(Table table, R record, Condition condition) { return filterUpdateOne(executeUpdate(table, record, condition)); } + /** + * {@inheritDoc} + */ + @Override + public final > int executeUpdate(R record) { + UpdateQuery update = updateQuery(record.getTable()); + Util.addConditions(update, record, record.getTable().getMainKey().getFieldsArray()); + update.setRecord(record); + return update.execute(); + } + + /** + * {@inheritDoc} + */ + @Override + public final , T> int executeUpdate(R record, Condition condition) { + UpdateQuery update = updateQuery(record.getTable()); + update.addConditions(condition); + update.setRecord(record); + return update.execute(); + } + /** * {@inheritDoc} */ diff --git a/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java b/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java index cbb0fc1696..4583608c07 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java +++ b/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java @@ -548,30 +548,51 @@ public final class FactoryProxy implements FactoryOperations { } @Override + @Deprecated public final > int executeInsert(Table table, R record) { return getDelegate().executeInsert(table, record); } @Override + public final > int executeInsert(R record) throws DataAccessException { + return getDelegate().executeInsert(record); + } + + @Override + @Deprecated public final > int executeUpdate(Table table, R record) { return getDelegate().executeUpdate(table, record); } @Override + @Deprecated public final , T> int executeUpdate(Table table, R record, Condition condition) { return getDelegate().executeUpdate(table, record, condition); } @Override + @Deprecated public final > int executeUpdateOne(Table table, R record) { return getDelegate().executeUpdateOne(table, record); } @Override + @Deprecated public final , T> int executeUpdateOne(Table table, R record, Condition condition) { return getDelegate().executeUpdateOne(table, record, condition); } + @Override + public final > int executeUpdate(R record) throws DataAccessException { + return getDelegate().executeUpdate(record); + } + + @Override + public final , T> int executeUpdate(R record, Condition condition) + throws DataAccessException { + return getDelegate().executeUpdate(record, condition); + } + @Override public final > int executeDelete(Table table) { return getDelegate().executeDelete(table); diff --git a/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java b/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java index 9e28e7130e..3615b8217e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java @@ -204,7 +204,7 @@ public class TableRecordImpl> extends AbstractRecord im private final int storeUpdate(TableField[] keys) { UpdateQuery update = create().updateQuery(getTable()); addChangedValues(update); - addConditions(update, keys); + Util.addConditions(update, this, keys); // Don't store records if no value was set by client code if (!update.isExecutable()) return 0; @@ -237,8 +237,8 @@ public class TableRecordImpl> extends AbstractRecord im TableField v = getUpdatableTable().getRecordVersion(); TableField t = getUpdatableTable().getRecordTimestamp(); - if (v != null) addCondition(query, v); - if (t != null) addCondition(query, t); + if (v != null) Util.addCondition(query, this, v); + if (t != null) Util.addCondition(query, this, t); } private final boolean isTimestampOrVersionAvailable() { @@ -255,7 +255,7 @@ public class TableRecordImpl> extends AbstractRecord im public final int deleteUsing(TableField... keys) { try { DeleteQuery delete = create().deleteQuery(getTable()); - addConditions(delete, keys); + Util.addConditions(delete, this, keys); if (isExecuteWithOptimisticLocking()) { @@ -288,7 +288,7 @@ public class TableRecordImpl> extends AbstractRecord im @Override public final void refreshUsing(TableField... keys) { SimpleSelectQuery select = create().selectQuery(getTable()); - addConditions(select, keys); + Util.addConditions(select, this, keys); if (select.execute() == 1) { AbstractRecord record = (AbstractRecord) select.getResult().get(0); @@ -308,7 +308,7 @@ public class TableRecordImpl> extends AbstractRecord im */ private final void checkIfChanged(TableField[] keys) { SimpleSelectQuery select = create().selectQuery(getTable()); - addConditions(select, keys); + Util.addConditions(select, this, keys); // [#1547] SQLite doesn't support FOR UPDATE. CUBRID and SQL Server // can simulate it, though! @@ -361,22 +361,6 @@ public class TableRecordImpl> extends AbstractRecord im setValue(field, (Value) value); } - /** - * Add primary key conditions to a query - */ - private final void addConditions(ConditionProvider query, TableField[] keys) { - for (Field field : keys) { - addCondition(query, field); - } - } - - /** - * Add a field condition to a query - */ - private final void addCondition(ConditionProvider provider, Field field) { - provider.addConditions(field.equal(getValue(field))); - } - /** * Set all changed values of this record to a store query */ diff --git a/jOOQ/src/main/java/org/jooq/impl/Util.java b/jOOQ/src/main/java/org/jooq/impl/Util.java index 4915eccaa7..b5c159231e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Util.java +++ b/jOOQ/src/main/java/org/jooq/impl/Util.java @@ -63,6 +63,7 @@ import javax.persistence.Column; import javax.persistence.Entity; import org.jooq.ArrayRecord; +import org.jooq.ConditionProvider; import org.jooq.Configuration; import org.jooq.Cursor; import org.jooq.DataType; @@ -1084,4 +1085,20 @@ final class Util { return null; } } + + /** + * Add primary key conditions to a query + */ + static final void addConditions(ConditionProvider query, Record record, Field... keys) { + for (Field field : keys) { + addCondition(query, record, field); + } + } + + /** + * Add a field condition to a query + */ + static final void addCondition(ConditionProvider provider, Record record, Field field) { + provider.addConditions(field.equal(record.getValue(field))); + } } \ No newline at end of file