changeset 229:328ef982d768

When an artifact is touched the collections which contain the artifact are touched, too. artifacts/trunk@1631 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 31 Mar 2011 08:58:42 +0000
parents dd977fb7552e
children fbd57d2eeaef
files ChangeLog artifact-database/doc/schema-h2.sql artifact-database/doc/schema-pg.sql artifact-database/src/main/java/de/intevation/artifactdatabase/h2/CollectionAccessUpdateTrigger.java artifact-database/src/main/resources/sql/org-h2-driver.properties
diffstat 5 files changed, 108 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Mar 31 08:42:53 2011 +0000
+++ b/ChangeLog	Thu Mar 31 08:58:42 2011 +0000
@@ -1,3 +1,22 @@
+2011-03-31	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/doc/schema-h2.sql, artifact-database/doc/schema-pg.sql:
+	  Added a trigger on artifacts. If an artifact is changed the last_access
+	  timestamp of the collections in which the artifact is in are updated, too.
+	  Needs testing!
+
+	  - In PostgreSQL it is done by a trigger written in plpgsql. So don't
+	    forget to the language to the database!
+
+	  - In H2 it is done by a trigger written in Java, because H2 does not
+	    offer a script level trigger support.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/h2/CollectionAccessUpdateTrigger.java:
+	  The H2 trigger.
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties:
+	  Added the statement which is executed if the trigger fires.
+
 2011-03-31  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
--- a/artifact-database/doc/schema-h2.sql	Thu Mar 31 08:42:53 2011 +0000
+++ b/artifact-database/doc/schema-h2.sql	Thu Mar 31 08:58:42 2011 +0000
@@ -50,4 +50,8 @@
     UNIQUE (collection_id, artifact_id)
 );
 
+CREATE TRIGGER collections_access_update_trigger AFTER UPDATE
+    ON TABLE artifacts FOR EACH ROW 
+    CALL "de.intevation.artifactdatabase.h2.CollectionAccessUpdateTrigger";
+
 COMMIT;
--- a/artifact-database/doc/schema-pg.sql	Thu Mar 31 08:42:53 2011 +0000
+++ b/artifact-database/doc/schema-pg.sql	Thu Mar 31 08:58:42 2011 +0000
@@ -50,4 +50,23 @@
     UNIQUE (collection_id, artifact_id)
 );
 
+CREATE FUNCTION collections_access_update() RETURNS trigger AS
+$$
+BEGIN
+    UPDATE collections SET last_access = current_timestamp 
+    WHERE id IN 
+        (SELECT c.id FROM collections c 
+         INNER JOIN collection_items ci ON c.id = ci.collection_id  
+         INNER JOIN artifacts a         ON a.id = ci.artifact_id 
+         WHERE a.id = NEW.id);
+    RETURN NEW;
+END;
+$$
+LANGUAGE 'plpgsql';
+
+
+CREATE TRIGGER collections_access_update_trigger AFTER UPDATE
+    ON artifacts FOR EACH ROW 
+    EXECUTE PROCEDURE collections_access_update();
+
 COMMIT;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/h2/CollectionAccessUpdateTrigger.java	Thu Mar 31 08:58:42 2011 +0000
@@ -0,0 +1,56 @@
+package de.intevation.artifactdatabase.h2;
+
+import org.h2.api.Trigger;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.PreparedStatement;
+
+import de.intevation.artifactdatabase.SQL;
+
+import org.apache.log4j.Logger;
+
+public class CollectionAccessUpdateTrigger
+implements   Trigger
+{
+    private static Logger logger =
+        Logger.getLogger(CollectionAccessUpdateTrigger.class);
+
+    public static final String COLLECTIONS_TOUCH_TRIGGER_FUNCTION =
+        SQL.get("collections.touch.trigger.function");
+
+    public void init(
+        Connection conn,
+        String     schemaName,
+        String     triggerName,
+        String     tableName,
+        boolean    before,
+        int        type
+    ) 
+    throws SQLException {
+        logger.debug("CollectionAccessUpdateTrigger.init");
+    }
+
+    public void fire(
+        Connection conn, 
+        Object []  oldRow,
+        Object []  newRow
+    )
+    throws SQLException {
+        logger.debug("CollectionAccessUpdateTrigger.fire");
+        PreparedStatement stmnt = conn.prepareStatement(
+            COLLECTIONS_TOUCH_TRIGGER_FUNCTION);
+        stmnt.setObject(1, newRow[0]);
+        stmnt.execute();
+        stmnt.close();
+    }
+
+    public void close() throws SQLException {
+        logger.debug("CollectionAccessUpdateTrigger.close");
+    }
+
+    public void remove() throws SQLException {
+        logger.debug("CollectionAccessUpdateTrigger.remove");
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Mar 31 08:42:53 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Mar 31 08:58:42 2011 +0000
@@ -84,6 +84,16 @@
     WHERE ci.collection_id IN (SELECT id FROM collections WHERE gid = ?)
 
 # COLLECTIONS
+
+collections.touch.trigger.function = \
+   UPDATE collections SET last_access = current_timestamp \
+   WHERE id IN \
+        (SELECT c.id FROM collections c \
+         INNER JOIN collection_items ci ON c.id = ci.collection_id  \
+         INNER JOIN artifacts a         ON a.id = ci.artifact_id \
+         WHERE a.id = ?)
+
+
 collections.touch.by.gid =\
     UPDATE collections SET last_access = CURRENT_TIMESTAMP \
         WHERE gid = ?

http://dive4elements.wald.intevation.org