From 56d1dd1250a54ff33d13cf32d13c8bd0c34b8838 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 15 Dec 2012 16:00:38 +0100 Subject: [PATCH] [#2021] Add UpdatableRecord.refresh(Field...) to allow for refreshing a subset of the Record's values --- .../org/jooq/test/_/testcases/CRUDTests.java | 7 ++++++- .../main/java/org/jooq/UpdatableRecord.java | 18 +++++++++++++++++- .../org/jooq/impl/UpdatableRecordImpl.java | 12 ++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java index 67db214dcd..c26461e4b2 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/CRUDTests.java @@ -394,11 +394,16 @@ extends BaseTest> extends Updatable * key or main unique key. *

* This is in fact the same as calling - * refresh(getTable().getMainKey().getFieldsArray()) + * refresh(getFields().toArray(new Field[0]) *

* The executed statement is

      * SELECT * FROM [table]
@@ -290,6 +290,22 @@ public interface UpdatableRecord> extends Updatable
      */
     void refresh() throws DataAccessException;
 
+    /**
+     * Refresh this record from the database, based on the value of the primary
+     * key or main unique key.
+     * 

+ * The executed statement is

+     * SELECT [fields] FROM [table]
+     * WHERE [main key fields = main key values]
+ * + * @throws DataAccessException This exception is thrown if + *
    + *
  • something went wrong executing the query
  • the + * record does not exist anymore in the database
  • + *
+ */ + void refresh(Field... fields); + /** * Duplicate this record (in memory) and reset all fields from the primary * key or main unique key, such that a subsequent call to {@link #store()} diff --git a/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java b/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java index 03d0627b0e..ffaf503461 100644 --- a/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java @@ -51,6 +51,7 @@ import org.jooq.Identity; import org.jooq.InsertQuery; import org.jooq.Record; import org.jooq.SQLDialect; +import org.jooq.SelectQuery; import org.jooq.SimpleSelectQuery; import org.jooq.StoreQuery; import org.jooq.TableField; @@ -317,13 +318,20 @@ public class UpdatableRecordImpl> extends TableReco @Override public final void refresh() { - SimpleSelectQuery select = create().selectQuery(getTable()); + refresh(getFields().toArray(new Field[0])); + } + + @Override + public final void refresh(Field... fields) { + SelectQuery select = create().selectQuery(); + select.addSelect(fields); + select.addFrom(getTable()); Utils.addConditions(select, this, getMainKey().getFieldsArray()); if (select.execute() == 1) { AbstractRecord record = (AbstractRecord) select.getResult().get(0); - for (Field field : getFields()) { + for (Field field : fields) { setValue(field, record.getValue0(field)); } }