Hi,

To Rami Ojares: those additions look nice! There might be a few
smaller changes I would like to do (probably remove primaryKeyColumns,
DbObjectName; add test cases), but I'm sure we can work that out.
Would you be willing to contribute it to H2? If yes then I suggest we
work on a patch and once we both agree on a solution I will give you
the rights to commit it to H2. Or I can commit it if you like.

I think the "last modified" is quite a common problem. For H2, there
is a simpler solution that doesn't require a trigger: computed
columns. Example (the "@sleep 2" only works in the H2 Console tool):

create table test(
  id int primary key,
  name varchar(255),
  last_modfied timestamp as now()
);
insert into test(id, name) values(1, 'Hello');
select * from test;
@sleep 2;
update test set name = 'Hallo';
select * from test;
drop table test;

For details about computed columns, see
http://h2database.com/html/features.html#computed_columns

I will extend the documentation a bit:

"
A computed column is a column whose value is calculated before
storing. The formula is evaluated when the row is inserted, and
re-evaluated every time the row is updated. One use case is to
automatically update the last-modification time:

CREATE TABLE TEST(ID INT, NAME VARCHAR, LAST_MOD TIMESTAMP AS NOW());
"

However of course using a trigger also works. Please note computed
columns are not supported by most other databases (MS SQL Server
supports them, but the behavior is different there).

One case were a trigger is required is to write an audit log / a
history of changes. But in that case you usually don't replace any
values in the row but insert a row into another table. Except if you
only keep a short history in the same table, say to remember the last
few state changes or so (I saw that in a bug-tracking tool many years
ago).

> in a whole 800 page book I'm writing about persistence

Please tell me if you need someone to review.

Regards,
Thomas

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to