From d50e283ba96fdbe75b0e5b7a6491f7f04f3c8871 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 6 Jun 2014 11:09:29 +0200 Subject: [PATCH] [#3322] Add UpdatableRecord.store(Field...), insert(Field...), update(Field...) --- .../jooq/test/all/testcases/CRUDTests.java | 65 +++++++++++++++++++ .../java/org/jooq/test/jOOQAbstractTest.java | 5 ++ jOOQ/src/main/java/org/jooq/TableRecord.java | 11 ++++ .../main/java/org/jooq/UpdatableRecord.java | 45 +++++++++++++ .../java/org/jooq/impl/TableRecordImpl.java | 25 ++++--- .../org/jooq/impl/UpdatableRecordImpl.java | 38 +++++++---- 6 files changed, 168 insertions(+), 21 deletions(-) diff --git a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/CRUDTests.java b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/CRUDTests.java index c4108bf002..4c28a00a52 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/CRUDTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/CRUDTests.java @@ -500,6 +500,71 @@ extends BaseTest> extends Record { */ int insert() throws DataAccessException; + /** + * Store parts of this record to the database using an INSERT + * statement. + * + * @return 1 if the record was stored to the database. 0 + * if storing was not necessary. + * @throws DataAccessException if something went wrong executing the query + * @see #insert() + */ + int insert(Field... fields) throws DataAccessException; + /** * Fetch a parent record of this record, given a foreign key *

diff --git a/jOOQ/src/main/java/org/jooq/UpdatableRecord.java b/jOOQ/src/main/java/org/jooq/UpdatableRecord.java index 24b3c6c27b..11b39d6f6b 100644 --- a/jOOQ/src/main/java/org/jooq/UpdatableRecord.java +++ b/jOOQ/src/main/java/org/jooq/UpdatableRecord.java @@ -211,6 +211,8 @@ public interface UpdatableRecord> extends TableReco * this record were changed, you can explicitly set the changed flags for * all values with {@link #changed(boolean)} or for single values with * {@link #changed(Field, boolean)}, prior to storing. + *

+ * This is the same as calling record.store(record.fields()) * * @return 1 if the record was stored to the database. 0 * if storing was not necessary. @@ -222,6 +224,20 @@ public interface UpdatableRecord> extends TableReco */ int store() throws DataAccessException, DataChangedException; + /** + * Store parts of this record to the database. + * + * @return 1 if the record was stored to the database. 0 + * if storing was not necessary. + * @throws DataAccessException if something went wrong executing the query + * @throws DataChangedException If optimistic locking is enabled and the + * record has already been changed/deleted in the database + * @see #store() + * @see #insert(Field...) + * @see #update(Field...) + */ + int store(Field... fields) throws DataAccessException, DataChangedException; + /** * Store this record back to the database using an INSERT * statement. @@ -233,6 +249,8 @@ public interface UpdatableRecord> extends TableReco * this record were changed, you can explicitly set the changed flags for * all values with {@link #changed(boolean)} or for single values with * {@link #changed(Field, boolean)}, prior to insertion. + *

+ * This is the same as calling record.insert(record.fields()) * * @return 1 if the record was stored to the database. 0 * if storing was not necessary. @@ -242,6 +260,18 @@ public interface UpdatableRecord> extends TableReco @Override int insert() throws DataAccessException; + /** + * Store parts of this record to the database using an INSERT + * statement. + * + * @return 1 if the record was stored to the database. 0 + * if storing was not necessary. + * @throws DataAccessException if something went wrong executing the query + * @see #insert() + */ + @Override + int insert(Field... fields) throws DataAccessException; + /** * Store this record back to the database using an UPDATE * statement. @@ -253,6 +283,8 @@ public interface UpdatableRecord> extends TableReco * this record were changed, you can explicitly set the changed flags for * all values with {@link #changed(boolean)} or for single values with * {@link #changed(Field, boolean)}, prior to updating. + *

+ * This is the same as calling record.update(record.fields()) * * @return 1 if the record was stored to the database. 0 * if storing was not necessary. @@ -263,6 +295,19 @@ public interface UpdatableRecord> extends TableReco */ int update() throws DataAccessException, DataChangedException; + /** + * Store parts of this record to the database using an UPDATE + * statement. + * + * @return 1 if the record was stored to the database. 0 + * if storing was not necessary. + * @throws DataAccessException if something went wrong executing the query + * @throws DataChangedException If optimistic locking is enabled and the + * record has already been changed/deleted in the database + * @see #update() + */ + int update(Field... fields) throws DataAccessException, DataChangedException; + /** * Deletes this record from the database, based on the value of the primary * key or main unique key. diff --git a/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java b/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java index 67c1d365cf..311d5f0a60 100644 --- a/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/TableRecordImpl.java @@ -120,10 +120,15 @@ public class TableRecordImpl> extends AbstractRecord im @Override public final int insert() { - return storeInsert(); + return insert(fields.fields.fields); } - final int storeInsert() { + @Override + public final int insert(Field... storeFields) { + return storeInsert(storeFields); + } + + final int storeInsert(final Field[] storeFields) { final int[] result = new int[1]; delegate(configuration(), (Record) this, INSERT) @@ -131,7 +136,7 @@ public class TableRecordImpl> extends AbstractRecord im @Override public Record operate(Record record) throws RuntimeException { - result[0] = storeInsert0(); + result[0] = storeInsert0(storeFields); return record; } }); @@ -139,10 +144,10 @@ public class TableRecordImpl> extends AbstractRecord im return result[0]; } - final int storeInsert0() { + final int storeInsert0(Field[] storeFields) { DSLContext create = create(); InsertQuery insert = create.insertQuery(getTable()); - addChangedValues(insert); + addChangedValues(storeFields, insert); // Don't store records if no value was set by client code if (!insert.isExecutable()) return 0; @@ -180,7 +185,9 @@ public class TableRecordImpl> extends AbstractRecord im } } - changed(false); + for (Field storeField : storeFields) + changed(storeField, false); + fetched = true; } @@ -215,9 +222,11 @@ public class TableRecordImpl> extends AbstractRecord im /** * Set all changed values of this record to a store query */ - final void addChangedValues(StoreQuery query) { + final void addChangedValues(Field[] storeFields, StoreQuery query) { + Fields f = new Fields(storeFields); + for (Field field : fields.fields.fields) { - if (changed(field)) { + if (changed(field) && f.field(field) != null) { addValue(query, field); } } diff --git a/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java b/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java index 479f0df761..5f6861a9f4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java @@ -69,6 +69,7 @@ import org.jooq.TableRecord; import org.jooq.UniqueKey; import org.jooq.UpdatableRecord; import org.jooq.UpdateQuery; +import org.jooq.exception.DataAccessException; import org.jooq.exception.DataChangedException; import org.jooq.exception.InvalidResultException; import org.jooq.tools.StringUtils; @@ -117,6 +118,11 @@ public class UpdatableRecordImpl> extends TableReco @Override public final int store() { + return store(fields.fields.fields); + } + + @Override + public final int store(final Field... storeFields) throws DataAccessException, DataChangedException { final int[] result = new int[1]; delegate(configuration(), (Record) this, STORE) @@ -124,7 +130,7 @@ public class UpdatableRecordImpl> extends TableReco @Override public Record operate(Record record) throws RuntimeException { - result[0] = store0(); + result[0] = store0(storeFields); return record; } }); @@ -134,10 +140,15 @@ public class UpdatableRecordImpl> extends TableReco @Override public final int update() { - return storeUpdate(getPrimaryKey().getFieldsArray()); + return update(fields.fields.fields); } - private final int store0() { + @Override + public int update(Field... storeFields) throws DataAccessException, DataChangedException { + return storeUpdate(storeFields, getPrimaryKey().getFieldsArray()); + } + + private final int store0(Field[] storeFields) { TableField[] keys = getPrimaryKey().getFieldsArray(); boolean executeUpdate = false; @@ -166,16 +177,16 @@ public class UpdatableRecordImpl> extends TableReco int result = 0; if (executeUpdate) { - result = storeUpdate(keys); + result = storeUpdate(storeFields, keys); } else { - result = storeInsert(); + result = storeInsert(storeFields); } return result; } - private final int storeUpdate(final TableField[] keys) { + private final int storeUpdate(final Field[] storeFields, final TableField[] keys) { final int[] result = new int[1]; delegate(configuration(), (Record) this, UPDATE) @@ -183,7 +194,7 @@ public class UpdatableRecordImpl> extends TableReco @Override public Record operate(Record record) throws RuntimeException { - result[0] = storeUpdate0(keys); + result[0] = storeUpdate0(storeFields, keys); return record; } }); @@ -192,9 +203,9 @@ public class UpdatableRecordImpl> extends TableReco } - private final int storeUpdate0(TableField[] keys) { + private final int storeUpdate0(Field[] storeFields, TableField[] keys) { UpdateQuery update = create().updateQuery(getTable()); - addChangedValues(update); + addChangedValues(storeFields, update); Utils.addConditions(update, this, keys); // Don't store records if no value was set by client code @@ -223,7 +234,8 @@ public class UpdatableRecordImpl> extends TableReco checkIfChanged(result, version, timestamp); if (result > 0) { - changed(false); + for (Field storeField : storeFields) + changed(storeField, false); } return result; @@ -285,9 +297,9 @@ public class UpdatableRecordImpl> extends TableReco } @Override - public final void refresh(final Field... f) { + public final void refresh(final Field... refreshFields) { SelectQuery select = create().selectQuery(); - select.addSelect(f); + select.addSelect(refreshFields); select.addFrom(getTable()); Utils.addConditions(select, this, getPrimaryKey().getFieldsArray()); @@ -298,7 +310,7 @@ public class UpdatableRecordImpl> extends TableReco .operate(new RecordOperation() { @Override public Record operate(Record record) throws RuntimeException { - setValues(f, source); + setValues(refreshFields, source); return record; } });