Detecting if a record has changed

29 views
Skip to first unread message

Ryan How

unread,
Sep 28, 2012, 5:21:45 AM9/28/12
to jooq...@googlegroups.com
Hi, I notice internally that JOOQ stores whether there have been changes to a record. Is there a way to access this. I want to use it to detect if a record is dirty and needs committing. I can't find any public methods that expose the functionality. eg record.hasChanges(), or record.isDirty()

Additionally it would be great to have the original values accessible also, but I can just fetch again from the database, so that's not a biggy :). Would also mean a lot more memory overhead I guess if you are updating a large recordset.

Thanks! Ryan

Lukas Eder

unread,
Sep 28, 2012, 5:45:18 AM9/28/12
to jooq...@googlegroups.com
Hello,

> Hi, I notice internally that JOOQ stores whether there have been changes to
> a record. Is there a way to access this. I want to use it to detect if a
> record is dirty and needs committing. I can't find any public methods that
> expose the functionality. eg record.hasChanges(), or record.isDirty()

So far, I tried to avoid exposing these internals. But there's no
general reason why this shouldn't be exposed (I think). I'll evaluate
this as feature request #1848:
https://github.com/jOOQ/jOOQ/issues/1848

In the mean time, note that you can just store() any record. If there
are no changes, no SQL statement will be executed, and the result
should be 0:

record.setValue(VALUE, "changed value");
assertEquals(1, record.store());
assertEquals(0, record.store());

> Additionally it would be great to have the original values accessible also,
> but I can just fetch again from the database, so that's not a biggy :).
> Would also mean a lot more memory overhead I guess if you are updating a
> large recordset.

Original values are referenced in UpdatableRecords already, as can be seen here:
https://github.com/jOOQ/jOOQ/blob/master/jOOQ/src/main/java/org/jooq/impl/Value.java

This was necessary to implement the optimistic locking feature. I'll
consider exposing them as well.

Cheers
Lukas

Ryan How

unread,
Sep 28, 2012, 7:51:22 AM9/28/12
to jooq...@googlegroups.com
Thanks,

I wanted to use it to detect if the record is dirty, so then it can
prompt the user to save or not before actually saving.
I'll use a bit of reflection and hope the internals don't change to much :)

Thanks, Ryan

Lukas Eder

unread,
Sep 28, 2012, 8:28:40 AM9/28/12
to jooq...@googlegroups.com
> I wanted to use it to detect if the record is dirty, so then it can prompt
> the user to save or not before actually saving.

OK, I'll see what I can do. It shouldn't be hard to add.

> I'll use a bit of reflection and hope the internals don't change to much :)

Chances are, that the next version will have this feature implemented.
So don't worry about changing internals.
Or, you could hook an ExecuteListener into the Factory, detecting if a
store() *would* execute an UPDATE or not, and then abort the UPDATE
;-)

Cheers
Lukas

Lukas Eder

unread,
Sep 28, 2012, 11:19:19 AM9/28/12
to jooq...@googlegroups.com
I have implemented #1849:
https://github.com/jOOQ/jOOQ/issues/1849

// This has been added to Record
Record original()

// ... and it is overridden for increased type-safety in TableRecord<R
extends Record> as
R original()

I'll see what can be done about the isChanged flags in a bit...

Lukas Eder

unread,
Sep 28, 2012, 11:52:05 AM9/28/12
to jooq...@googlegroups.com
#1848 is implemented, too
https://github.com/jOOQ/jOOQ/issues/1848

// This has been added to Record
boolean changed()

If you want to discover the concrete changed value(s), you can compare
Record with Record.original()

Cheers
Lukas

Ryan How

unread,
Sep 28, 2012, 2:01:24 PM9/28/12
to jooq...@googlegroups.com
Wow, you are efficient!

Thanks!

I couldn't pay for support that good :)


Lukas Eder

unread,
Sep 28, 2012, 2:32:19 PM9/28/12
to jooq...@googlegroups.com
> I couldn't pay for support that good :)

Yes you can! Here:
http://sourceforge.net/project/project_donations.php?group_id=283484

;-)

Ryan How

unread,
Sep 30, 2012, 8:27:27 PM9/30/12
to jooq...@googlegroups.com
;)

Lukas Eder

unread,
Oct 1, 2012, 2:37:18 AM10/1/12
to jooq...@googlegroups.com
Thank you very much, Ryan. I really appreciate your donation!

Am Montag, 1. Oktober 2012 schrieb Ryan How <rh...@exemail.com.au>:
> ;)
>
>
Reply all
Reply to author
Forward
0 new messages