diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 305:f33401ea2a6c

Artifact database: Refactorized the usage of dialect independent SQL to be reusable. artifacts/trunk@2412 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 10:19:35 +0000
parents 190aa68ae7a8
children d96bcb40dbf9
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Jul 27 13:51:30 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Jul 28 10:19:35 2011 +0000
@@ -20,6 +20,9 @@
 import de.intevation.artifacts.common.utils.StringUtils;
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.artifactdatabase.db.SQLExecutor;
+import de.intevation.artifactdatabase.db.SQL;
+
 import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.sql.Types;
@@ -48,172 +51,92 @@
     /**
      * The SQL statement to create new artifact id inside the database.
      */
-    public static final String SQL_NEXT_ID =
-        SQL.get("artifacts.id.nextval");
+    public String SQL_NEXT_ID;
 
     /**
      * The SQL statement to insert an artifact into the database.
      */
-    public static final String SQL_INSERT =
-        SQL.get("artifacts.insert");
+    public String SQL_INSERT;
 
     /**
      * The SQL statement to update some columns of an existing
      * artifact in the database.
      */
-    public static final String SQL_UPDATE =
-        SQL.get("artifacts.update");
+    public String SQL_UPDATE;
 
     /**
      * The SQL statement to touch the access time of an
      * artifact inside the database.
      */
-    public static final String SQL_TOUCH =
-        SQL.get("artifacts.touch");
+    public String SQL_TOUCH;
 
     /**
      * The SQL statement to load an artifact by a given
      * identifier from the database.
      */
-    public static final String SQL_LOAD_BY_GID =
-        SQL.get("artifacts.select.gid");
+    public String SQL_LOAD_BY_GID;
 
     /**
      * The SQL statement to get the database id of an artifact
      * identified by the identifier.
      */
-    public static final String SQL_GET_ID =
-        SQL.get("artifacts.get.id");
+    public String SQL_GET_ID;
 
     /**
      * The SQL statement to replace the content of an
      * existing artifact inside the database.
      */
-    public static final String SQL_REPLACE =
-        SQL.get("artifacts.replace");
+    public String SQL_REPLACE;
 
     // USER SQL
 
-    public static final String SQL_USERS_NEXT_ID =
-        SQL.get("users.id.nextval");
-
-    public static final String SQL_USERS_INSERT =
-        SQL.get("users.insert");
-
-    public static final String SQL_USERS_SELECT_ID_BY_GID =
-        SQL.get("users.select.id.by.gid");
-
-    public static final String SQL_USERS_SELECT_GID =
-        SQL.get("users.select.gid");
-
-    public static final String SQL_USERS_DELETE_ID =
-        SQL.get("users.delete.id");
-
-    public static final String SQL_USERS_DELETE_COLLECTIONS =
-        SQL.get("users.delete.collections");
-
-    public static final String SQL_USERS_SELECT_ALL =
-        SQL.get("users.select.all");
-
-    public static final String SQL_USERS_COLLECTIONS =
-        SQL.get("users.collections");
-
-    public static final String SQL_USERS_COLLECTION_IDS =
-        SQL.get("users.collection.ids");
-
-    public static final String SQL_USERS_DELETE_ALL_COLLECTIONS =
-        SQL.get("users.delete.all.collections");
-
-    public static final String SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY =
-        SQL.get("artifacts.in.one.collection.only");
-
-    public static final String SQL_OUTDATE_ARTIFACTS_COLLECTION =
-        SQL.get("outdate.artifacts.collection");
-
-    public static final String SQL_UPDATE_COLLECTION_TTL =
-        SQL.get("collections.update.ttl");
-
-    public static final String SQL_UPDATE_COLLECTION_NAME =
-        SQL.get("collections.update.name");
-
-    public static final String SQL_OUTDATE_ARTIFACTS_USER =
-        SQL.get("outdate.artifacts.user");
-
-    public static final String SQL_DELETE_USER_COLLECTION_ITEMS =
-        SQL.get("delete.user.collection.items");
-
-    public static final String SQL_COLLECTIONS_NEXT_ID =
-        SQL.get("collections.id.nextval");
-
-    public static final String SQL_COLLECTIONS_INSERT =
-        SQL.get("collections.insert");
-
-    public static final String SQL_COLLECTIONS_SELECT_USER =
-        SQL.get("collections.select.user");
-
-    public static final String SQL_COLLECTIONS_SELECT_ALL =
-        SQL.get("collections.select.all");
-
-    public static final String SQL_COLLECTIONS_SELECT_GID =
-        SQL.get("collections.select.by.gid");
-
-    public static final String SQL_COLLECTIONS_CREATION_TIME =
-        SQL.get("collection.creation.time");
-
-    public static final String SQL_COLLECTIONS_ID_BY_GID =
-        SQL.get("collections.id.by.gid");
-
-    public static final String SQL_DELETE_COLLECTION_ITEMS =
-        SQL.get("delete.collection.items");
-
-    public static final String SQL_DELETE_COLLECTION =
-        SQL.get("delete.collection");
-
-    public static final String SQL_COLLECTION_CHECK_ARTIFACT =
-        SQL.get("collection.check.artifact");
-
-    public static final String SQL_COLLECTION_ITEMS_ID_NEXTVAL =
-        SQL.get("collection.items.id.nextval");
-
-    public static final String SQL_COLLECTION_ITEMS_INSERT =
-        SQL.get("collection.items.insert");
-
-    public static final String SQL_COLLECTION_GET_ATTRIBUTE =
-        SQL.get("collection.get.attribute");
-
-    public static final String SQL_COLLECTION_SET_ATTRIBUTE =
-        SQL.get("collection.set.attribute");
-
-    public static final String SQL_COLLECTION_ITEM_GET_ATTRIBUTE =
-        SQL.get("collection.item.get.attribute");
-
-    public static final String SQL_COLLECTION_ITEM_SET_ATTRIBUTE =
-        SQL.get("collection.item.set.attribute");
-
-    public static final String SQL_COLLECTIONS_TOUCH_BY_GID =
-        SQL.get("collections.touch.by.gid");
-
-    public static final String SQL_COLLECTION_ITEM_ID_CID_AID =
-        SQL.get("collection.item.id.cid.aid");
-
-    public static final String SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT =
-        SQL.get("collection.item.outdate.artifact");
-
-    public static final String SQL_COLLECTION_ITEM_DELETE =
-        SQL.get("collection.item.delete");
-
-    public static final String SQL_COLLECTIONS_TOUCH_BY_ID =
-        SQL.get("collections.touch.by.id");
-
-    public static final String SQL_COLLECTION_ITEMS_LIST_GID =
-        SQL.get("collection.items.list.gid");
-
-    public static final String SQL_ALL_ARTIFACTS =
-        SQL.get("all.artifacts");
+    public String SQL_USERS_NEXT_ID;
+    public String SQL_USERS_INSERT;
+    public String SQL_USERS_SELECT_ID_BY_GID;
+    public String SQL_USERS_SELECT_GID;
+    public String SQL_USERS_DELETE_ID;
+    public String SQL_USERS_DELETE_COLLECTIONS;
+    public String SQL_USERS_SELECT_ALL;
+    public String SQL_USERS_COLLECTIONS;
+    public String SQL_USERS_COLLECTION_IDS;
+    public String SQL_USERS_DELETE_ALL_COLLECTIONS;
+    public String SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY;
+    public String SQL_OUTDATE_ARTIFACTS_COLLECTION;
+    public String SQL_UPDATE_COLLECTION_TTL;
+    public String SQL_UPDATE_COLLECTION_NAME;
+    public String SQL_OUTDATE_ARTIFACTS_USER;
+    public String SQL_DELETE_USER_COLLECTION_ITEMS;
+    public String SQL_COLLECTIONS_NEXT_ID;
+    public String SQL_COLLECTIONS_INSERT;
+    public String SQL_COLLECTIONS_SELECT_USER;
+    public String SQL_COLLECTIONS_SELECT_ALL;
+    public String SQL_COLLECTIONS_SELECT_GID;
+    public String SQL_COLLECTIONS_CREATION_TIME;
+    public String SQL_COLLECTIONS_ID_BY_GID;
+    public String SQL_DELETE_COLLECTION_ITEMS;
+    public String SQL_DELETE_COLLECTION;
+    public String SQL_COLLECTION_CHECK_ARTIFACT;
+    public String SQL_COLLECTION_ITEMS_ID_NEXTVAL;
+    public String SQL_COLLECTION_ITEMS_INSERT;
+    public String SQL_COLLECTION_GET_ATTRIBUTE;
+    public String SQL_COLLECTION_SET_ATTRIBUTE;
+    public String SQL_COLLECTION_ITEM_GET_ATTRIBUTE;
+    public String SQL_COLLECTION_ITEM_SET_ATTRIBUTE;
+    public String SQL_COLLECTIONS_TOUCH_BY_GID;
+    public String SQL_COLLECTION_ITEM_ID_CID_AID;
+    public String SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT;
+    public String SQL_COLLECTION_ITEM_DELETE;
+    public String SQL_COLLECTIONS_TOUCH_BY_ID;
+    public String SQL_COLLECTION_ITEMS_LIST_GID;
+    public String SQL_ALL_ARTIFACTS;
 
     /** The singleton.*/
     protected static Backend instance;
 
+    protected SQLExecutor sqlExecutor;
+
+    protected DBConfig config;
+
     /**
      * The database cleaner. Reference is stored here because
      * the cleaner is woken up if the backend finds an outdated
@@ -336,15 +259,25 @@
     public Backend() {
     }
 
+    public Backend(DBConfig config) {
+        this.config = config;
+        sqlExecutor = new SQLExecutor(config.getDBConnection());
+        setupSQL(config.getSQL());
+    }
+
     /**
      * Constructor to create a backend with a link to the database cleaner.
      * @param cleaner The clean which periodically removes outdated
      * artifacts from the database.
      */
-    public Backend(DatabaseCleaner cleaner) {
+    public Backend(DBConfig config, DatabaseCleaner cleaner) {
+        this(config);
         this.cleaner = cleaner;
     }
 
+    public DBConfig getConfig() {
+        return config;
+    }
 
     /**
      * Returns the singleton of this Backend.
@@ -353,12 +286,69 @@
      */
     public static synchronized Backend getInstance() {
         if (instance == null) {
-            instance = new Backend();
+            instance = new Backend(DBConfig.getInstance());
         }
 
         return instance;
     }
 
+    protected void setupSQL(SQL sql) {
+        SQL_NEXT_ID = sql.get("artifacts.id.nextval");
+        SQL_INSERT = sql.get("artifacts.insert");
+        SQL_UPDATE = sql.get("artifacts.update");
+        SQL_TOUCH = sql.get("artifacts.touch");
+        SQL_LOAD_BY_GID = sql.get("artifacts.select.gid");
+        SQL_GET_ID = sql.get("artifacts.get.id");
+        SQL_REPLACE = sql.get("artifacts.replace");
+        SQL_USERS_NEXT_ID = sql.get("users.id.nextval");
+        SQL_USERS_INSERT = sql.get("users.insert");
+        SQL_USERS_SELECT_ID_BY_GID = sql.get("users.select.id.by.gid");
+        SQL_USERS_SELECT_GID = sql.get("users.select.gid");
+        SQL_USERS_DELETE_ID = sql.get("users.delete.id");
+        SQL_USERS_DELETE_COLLECTIONS = sql.get("users.delete.collections");
+        SQL_USERS_SELECT_ALL = sql.get("users.select.all");
+        SQL_USERS_COLLECTIONS = sql.get("users.collections");
+        SQL_USERS_COLLECTION_IDS = sql.get("users.collection.ids");
+        SQL_USERS_DELETE_ALL_COLLECTIONS =
+            sql.get("users.delete.all.collections");
+        SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY =
+            sql.get("artifacts.in.one.collection.only");
+        SQL_OUTDATE_ARTIFACTS_COLLECTION =
+            sql.get("outdate.artifacts.collection");
+        SQL_UPDATE_COLLECTION_TTL = sql.get("collections.update.ttl");
+        SQL_UPDATE_COLLECTION_NAME = sql.get("collections.update.name");
+        SQL_OUTDATE_ARTIFACTS_USER = sql.get("outdate.artifacts.user");
+        SQL_DELETE_USER_COLLECTION_ITEMS =
+            sql.get("delete.user.collection.items");
+        SQL_COLLECTIONS_NEXT_ID = sql.get("collections.id.nextval");
+        SQL_COLLECTIONS_INSERT = sql.get("collections.insert");
+        SQL_COLLECTIONS_SELECT_USER = sql.get("collections.select.user");
+        SQL_COLLECTIONS_SELECT_ALL = sql.get("collections.select.all");
+        SQL_COLLECTIONS_SELECT_GID = sql.get("collections.select.by.gid");
+        SQL_COLLECTIONS_CREATION_TIME = sql.get("collection.creation.time");
+        SQL_COLLECTIONS_ID_BY_GID = sql.get("collections.id.by.gid");
+        SQL_DELETE_COLLECTION_ITEMS = sql.get("delete.collection.items");
+        SQL_DELETE_COLLECTION = sql.get("delete.collection");
+        SQL_COLLECTION_CHECK_ARTIFACT = sql.get("collection.check.artifact");
+        SQL_COLLECTION_ITEMS_ID_NEXTVAL =
+            sql.get("collection.items.id.nextval");
+        SQL_COLLECTION_ITEMS_INSERT = sql.get("collection.items.insert");
+        SQL_COLLECTION_GET_ATTRIBUTE = sql.get("collection.get.attribute");
+        SQL_COLLECTION_SET_ATTRIBUTE = sql.get("collection.set.attribute");
+        SQL_COLLECTION_ITEM_GET_ATTRIBUTE =
+            sql.get("collection.item.get.attribute");
+        SQL_COLLECTION_ITEM_SET_ATTRIBUTE =
+            sql.get("collection.item.set.attribute");
+        SQL_COLLECTIONS_TOUCH_BY_GID = sql.get("collections.touch.by.gid");
+        SQL_COLLECTION_ITEM_ID_CID_AID = sql.get("collection.item.id.cid.aid");
+        SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT =
+            sql.get("collection.item.outdate.artifact");
+        SQL_COLLECTION_ITEM_DELETE = sql.get("collection.item.delete");
+        SQL_COLLECTIONS_TOUCH_BY_ID = sql.get("collections.touch.by.id");
+        SQL_COLLECTION_ITEMS_LIST_GID = sql.get("collection.items.list.gid");
+        SQL_ALL_ARTIFACTS = sql.get("all.artifacts");
+    }
+
     /**
      * Sets the factory lookup mechanism to decouple ArtifactDatabase
      * and Backend.
@@ -511,7 +501,7 @@
 
         final Object [] loaded = new Object[1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_LOAD_BY_GID);
                 stmnt.setString(1, identifer);
@@ -601,7 +591,7 @@
 
         final int [] id = new int[1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 prepareStatement(SQL_GET_ID);
@@ -687,7 +677,7 @@
     ) {
         final int [] id = new int[1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_NEXT_ID);
                 result = stmnt.executeQuery();
@@ -738,7 +728,7 @@
      * @param artifact The persistent wrapper around the living artifact.
      */
     public void touch(final PersistentArtifact artifact) {
-        new SQLExecutor() {
+        sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_TOUCH);
                 stmnt.setInt(1, artifact.getId());
@@ -755,7 +745,7 @@
      * artifact.
      */
     public void store(final PersistentArtifact artifact) {
-        new SQLExecutor() {
+        sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_UPDATE);
                 stmnt.setInt(2, artifact.getId());
@@ -782,7 +772,7 @@
 
         final byte [] roleData = XMLUtils.toByteArray(role, true);
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 prepareStatement(SQL_USERS_NEXT_ID);
@@ -829,7 +819,7 @@
             return false;
         }
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
 
@@ -895,7 +885,7 @@
 
         final User [] user = new User[1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_USERS_SELECT_GID);
                 stmnt.setString(1, identifier);
@@ -924,7 +914,7 @@
     ) {
         final ArrayList<User> users = new ArrayList<User>();
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_USERS_SELECT_ALL);
                 result = stmnt.executeQuery();
@@ -970,7 +960,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 // fetch owner id
                 prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
@@ -1065,7 +1055,7 @@
 
         final ArtifactCollection[] ac = new ArtifactCollection[1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 prepareStatement(SQL_COLLECTIONS_SELECT_GID);
@@ -1126,7 +1116,7 @@
         final ArrayList<ArtifactCollection> collections =
             new ArrayList<ArtifactCollection>();
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
 
             public boolean doIt() throws SQLException {
 
@@ -1188,7 +1178,7 @@
             logger.debug("Invalid collection id: '" + collectionId + "'");
             return false;
         }
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 // fetch collection id
                 prepareStatement(SQL_COLLECTIONS_ID_BY_GID);
@@ -1232,7 +1222,7 @@
 
         final byte[][] data = new byte[1][1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_COLLECTION_GET_ATTRIBUTE);
                 stmnt.setString(1, collectionId);
@@ -1263,7 +1253,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        return new SQLExecutor() {
+        return sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // set the column in collection items
@@ -1304,7 +1294,7 @@
 
         final byte [][] data = new byte[1][1];
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_COLLECTION_ITEM_GET_ATTRIBUTE);
                 stmnt.setString(1, collectionId);
@@ -1340,7 +1330,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        return new SQLExecutor() {
+        return sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // set the column in collection items
@@ -1384,7 +1374,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 // fetch artifact id
                 prepareStatement(SQL_GET_ID);
@@ -1457,7 +1447,7 @@
             logger.debug("Invalid collection id: '" + collectionId + "'");
             return false;
         }
-        return new SQLExecutor() {
+        return sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // fetch id, collection id and artitfact id
@@ -1510,7 +1500,7 @@
         final ArrayList<CollectionItem> collectionItems =
             new ArrayList<CollectionItem>();
 
-        SQLExecutor exec = new SQLExecutor() {
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_COLLECTION_ITEMS_LIST_GID);
                 stmnt.setString(1, collectionId);
@@ -1538,7 +1528,7 @@
             return false;
         }
 
-        return new SQLExecutor() {
+        return sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_UPDATE_COLLECTION_TTL);
                 if (ttl == null) {
@@ -1563,7 +1553,7 @@
             return false;
         }
 
-        return new SQLExecutor() {
+        return sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_UPDATE_COLLECTION_NAME);
                 stmnt.setString(1, name);
@@ -1583,7 +1573,7 @@
             return false;
         }
 
-        return new SQLExecutor() {
+        return sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // a little cache to avoid too much deserializations.

http://dive4elements.wald.intevation.org