[#1692] Replace Factory.executeInsert(), Factory.executeUpdate() and

similar methods with more succinct variants
This commit is contained in:
Lukas Eder 2012-08-10 13:15:20 +02:00
parent a9b61b1a2f
commit ed397248dd
6 changed files with 145 additions and 25 deletions

View File

@ -206,19 +206,27 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
author.setValue(TAuthor_ID(), 15);
author.setValue(TAuthor_LAST_NAME(), "Kästner");
assertEquals(1, create().executeInsert(TAuthor(), author));
assertEquals(1, create().executeInsert(author));
author.refresh();
assertEquals(Integer.valueOf(15), author.getValue(TAuthor_ID()));
assertEquals("Kästner", author.getValue(TAuthor_LAST_NAME()));
assertEquals(0, create().executeUpdate(TAuthor(), author, TAuthor_ID().equal(15)));
assertEquals(0, create().executeUpdate(author, TAuthor_ID().equal(15)));
author.setValue(TAuthor_FIRST_NAME(), "Erich");
assertEquals(1, create().executeUpdate(TAuthor(), author, TAuthor_ID().equal(15)));
assertEquals(1, create().executeUpdate(author, TAuthor_ID().equal(15)));
author = create().fetchOne(TAuthor(), TAuthor_FIRST_NAME().equal("Erich"));
assertEquals(Integer.valueOf(15), author.getValue(TAuthor_ID()));
assertEquals("Erich", author.getValue(TAuthor_FIRST_NAME()));
assertEquals("Kästner", author.getValue(TAuthor_LAST_NAME()));
// [#1692] Check for new simplified update method
author.setValue(TAuthor_FIRST_NAME(), "Fritz");
assertEquals(1, create().executeUpdate(author));
author = create().fetchOne(TAuthor(), TAuthor_FIRST_NAME().equal("Fritz"));
assertEquals(Integer.valueOf(15), author.getValue(TAuthor_ID()));
assertEquals("Fritz", author.getValue(TAuthor_FIRST_NAME()));
assertEquals("Kästner", author.getValue(TAuthor_LAST_NAME()));
create().executeDelete(TAuthor(), TAuthor_LAST_NAME().equal("Kästner"));
assertEquals(null, create().fetchOne(TAuthor(), TAuthor_FIRST_NAME().equal("Erich")));
}

View File

@ -1059,17 +1059,41 @@ public interface FactoryOperations extends Configuration {
*
* @return The number of inserted records
* @throws DataAccessException if something went wrong executing the query
* @deprecated - 2.5.0 [#1692] - Use {@link #executeInsert(TableRecord)}
* instead
*/
@Deprecated
@Support
<R extends TableRecord<R>> int executeInsert(Table<R> table, R record) throws DataAccessException;
/**
* Insert one record
* <p>
* This executes something like the following statement:
* <code><pre>INSERT INTO [table] ... VALUES [record] </pre></code>
* <p>
* Unlike {@link TableRecord#storeUsing(TableField...)} or
* {@link UpdatableRecord#store()}, this does not change any of the argument
* <code>record</code>'s internal "changed" flags, such that a subsequent
* call to {@link TableRecord#storeUsing(TableField...)} or
* {@link UpdatableRecord#store()} might lead to another <code>INSERT</code>
* statement being executed.
*
* @return The number of inserted records
* @throws DataAccessException if something went wrong executing the query
*/
@Support
<R extends TableRecord<R>> int executeInsert(R record) throws DataAccessException;
/**
* Update a table
* <code><pre>UPDATE [table] SET [modified values in record] </pre></code>
*
* @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
<R extends TableRecord<R>> int executeUpdate(Table<R> 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
<R extends TableRecord<R>, T> int executeUpdate(Table<R> 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
<R extends TableRecord<R>> int executeUpdateOne(Table<R> 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
<R extends TableRecord<R>, T> int executeUpdateOne(Table<R> table, R record, Condition condition)
throws DataAccessException;
/**
* Update a table
* <code><pre>UPDATE [table] SET [modified values in record] </pre></code>
*
* @return The number of updated records
* @throws DataAccessException if something went wrong executing the query
*/
@Support
<R extends UpdatableRecord<R>> int executeUpdate(R record) throws DataAccessException;
/**
* Update a table
* <code><pre>UPDATE [table] SET [modified values in record] WHERE [condition]</pre></code>
*
* @return The number of updated records
* @throws DataAccessException if something went wrong executing the query
*/
@Support
<R extends TableRecord<R>, T> int executeUpdate(R record, Condition condition) throws DataAccessException;
/**
* Delete records from a table <code><pre>DELETE FROM [table]</pre></code>
*

View File

@ -6062,6 +6062,7 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
@Deprecated
public final <R extends TableRecord<R>> int executeInsert(Table<R> table, R record) {
InsertQuery<R> insert = insertQuery(table);
insert.setRecord(record);
@ -6072,6 +6073,17 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
public final <R extends TableRecord<R>> int executeInsert(R record) {
InsertQuery<R> insert = insertQuery(record.getTable());
insert.setRecord(record);
return insert.execute();
}
/**
* {@inheritDoc}
*/
@Override
@Deprecated
public final <R extends TableRecord<R>> int executeUpdate(Table<R> table, R record) {
return executeUpdate(table, record, trueCondition());
}
@ -6080,6 +6092,7 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
@Deprecated
public final <R extends TableRecord<R>, T> int executeUpdate(Table<R> table, R record, Condition condition) {
UpdateQuery<R> update = updateQuery(table);
update.addConditions(condition);
@ -6091,6 +6104,7 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
@Deprecated
public final <R extends TableRecord<R>> int executeUpdateOne(Table<R> table, R record) {
return filterUpdateOne(executeUpdate(table, record));
}
@ -6099,10 +6113,33 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
@Deprecated
public final <R extends TableRecord<R>, T> int executeUpdateOne(Table<R> table, R record, Condition condition) {
return filterUpdateOne(executeUpdate(table, record, condition));
}
/**
* {@inheritDoc}
*/
@Override
public final <R extends UpdatableRecord<R>> int executeUpdate(R record) {
UpdateQuery<R> update = updateQuery(record.getTable());
Util.addConditions(update, record, record.getTable().getMainKey().getFieldsArray());
update.setRecord(record);
return update.execute();
}
/**
* {@inheritDoc}
*/
@Override
public final <R extends TableRecord<R>, T> int executeUpdate(R record, Condition condition) {
UpdateQuery<R> update = updateQuery(record.getTable());
update.addConditions(condition);
update.setRecord(record);
return update.execute();
}
/**
* {@inheritDoc}
*/

View File

@ -548,30 +548,51 @@ public final class FactoryProxy implements FactoryOperations {
}
@Override
@Deprecated
public final <R extends TableRecord<R>> int executeInsert(Table<R> table, R record) {
return getDelegate().executeInsert(table, record);
}
@Override
public final <R extends TableRecord<R>> int executeInsert(R record) throws DataAccessException {
return getDelegate().executeInsert(record);
}
@Override
@Deprecated
public final <R extends TableRecord<R>> int executeUpdate(Table<R> table, R record) {
return getDelegate().executeUpdate(table, record);
}
@Override
@Deprecated
public final <R extends TableRecord<R>, T> int executeUpdate(Table<R> table, R record, Condition condition) {
return getDelegate().executeUpdate(table, record, condition);
}
@Override
@Deprecated
public final <R extends TableRecord<R>> int executeUpdateOne(Table<R> table, R record) {
return getDelegate().executeUpdateOne(table, record);
}
@Override
@Deprecated
public final <R extends TableRecord<R>, T> int executeUpdateOne(Table<R> table, R record, Condition condition) {
return getDelegate().executeUpdateOne(table, record, condition);
}
@Override
public final <R extends UpdatableRecord<R>> int executeUpdate(R record) throws DataAccessException {
return getDelegate().executeUpdate(record);
}
@Override
public final <R extends TableRecord<R>, T> int executeUpdate(R record, Condition condition)
throws DataAccessException {
return getDelegate().executeUpdate(record, condition);
}
@Override
public final <R extends TableRecord<R>> int executeDelete(Table<R> table) {
return getDelegate().executeDelete(table);

View File

@ -204,7 +204,7 @@ public class TableRecordImpl<R extends TableRecord<R>> extends AbstractRecord im
private final int storeUpdate(TableField<R, ?>[] keys) {
UpdateQuery<R> 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<R extends TableRecord<R>> extends AbstractRecord im
TableField<R, ?> v = getUpdatableTable().getRecordVersion();
TableField<R, ?> 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<R extends TableRecord<R>> extends AbstractRecord im
public final int deleteUsing(TableField<R, ?>... keys) {
try {
DeleteQuery<R> delete = create().deleteQuery(getTable());
addConditions(delete, keys);
Util.addConditions(delete, this, keys);
if (isExecuteWithOptimisticLocking()) {
@ -288,7 +288,7 @@ public class TableRecordImpl<R extends TableRecord<R>> extends AbstractRecord im
@Override
public final void refreshUsing(TableField<R, ?>... keys) {
SimpleSelectQuery<R> 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<R extends TableRecord<R>> extends AbstractRecord im
*/
private final void checkIfChanged(TableField<R, ?>[] keys) {
SimpleSelectQuery<R> 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<R extends TableRecord<R>> extends AbstractRecord im
setValue(field, (Value<T>) value);
}
/**
* Add primary key conditions to a query
*/
private final void addConditions(ConditionProvider query, TableField<R, ?>[] keys) {
for (Field<?> field : keys) {
addCondition(query, field);
}
}
/**
* Add a field condition to a query
*/
private final <T> void addCondition(ConditionProvider provider, Field<T> field) {
provider.addConditions(field.equal(getValue(field)));
}
/**
* Set all changed values of this record to a store query
*/

View File

@ -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 <T> void addCondition(ConditionProvider provider, Record record, Field<T> field) {
provider.addConditions(field.equal(record.getValue(field)));
}
}