changeset 1011:aca3b46160cb

Added support for more than one db connection in datacage templating. flys-artifacts/trunk@2457 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 02 Aug 2011 22:16:16 +0000
parents d419c9904825
children 388e709224ec
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java
diffstat 3 files changed, 134 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Aug 02 16:36:22 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue Aug 02 22:16:16 2011 +0000
@@ -1,3 +1,27 @@
+2011-08-03  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	Added support for more than one db connection in datacage templating.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java:
+	  Now you can pass a list of named db connections to the build process.
+	  The purpose is to mix more then one database (e.g. the backend db and
+	  the user specific one).
+
+	  To use this feature you can add an "connection" attribute
+	  to <dc:context> with the name of the connection to use.
+	  If no connection name is given the last used is used again.
+	  Initially the first connection in the given list is used. 
+	  If the context is left the connection that was active before 
+	  will be active again in a stacking manner.
+
+	  When creating NamedConnection objects you can set a boolean flag
+	  if the results coming from the connection should be cached. This
+	  is useful e.g. for the user specific database which runs in-memory
+	  so caching would introduce some superfluous overhead.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java:
+	  When executing the queries explicitly pass if caching should be used.
+
 2011-08-02  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Tue Aug 02 16:36:22 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Tue Aug 02 22:16:16 2011 +0000
@@ -7,6 +7,8 @@
 import java.util.List;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Deque;
+import java.util.ArrayDeque;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.NodeList;
@@ -45,25 +47,59 @@
 
     protected Map<String, CompiledStatement> compiledStatements;
 
+    public static class NamedConnection {
+
+        protected String     name;
+        protected Connection connection;
+        protected boolean    cached;
+
+        public NamedConnection() {
+        }
+
+        public NamedConnection(
+            String     name, 
+            Connection connection
+        ) {
+            this(name, connection, true);
+        }
+
+        public NamedConnection(
+            String     name, 
+            Connection connection,
+            boolean    cached
+        ) {
+            this.name       = name;
+            this.connection = connection;
+            this.cached     = cached;
+        }
+    } // class NamedConnection
+
 
     public class BuildHelper
     {
         protected Node                                    output;
         protected Document                                owner;
         protected StackFrames                             frames;
-        protected Connection                              connection;
+        protected List<NamedConnection>                   connections;
         protected Map<String, CompiledStatement.Instance> statements;
+        protected Deque<NamedConnection>                  connectionsStack;
 
         public BuildHelper(
-            Node                output,
-            Connection          connection,
-            Map<String, Object> parameters
+            Node                  output,
+            List<NamedConnection> connections,
+            Map<String, Object>   parameters
         ) {
-            this.output     = output;
-            this.connection = connection;
-            frames          = new StackFrames(parameters);
-            statements      = new HashMap<String, CompiledStatement.Instance>();
-            owner           = getOwnerDocument(output);
+            if (connections.isEmpty()) {
+                throw new IllegalArgumentException("no connections given.");
+            }
+
+            this.connections = connections;
+            connectionsStack = new ArrayDeque<NamedConnection>();
+            this.output      = output;
+            frames           = new StackFrames(parameters);
+            owner            = getOwnerDocument(output);
+            statements =
+                new HashMap<String, CompiledStatement.Instance>();
         }
 
         public void build() throws SQLException {
@@ -113,33 +149,58 @@
                 return;
             }
 
+            String con = current.getAttribute("connection");
+
             String stmntText = stmntNode.item(0).getTextContent();
 
-            CompiledStatement.Instance csi = statements.get(stmntText);
+            String key = con + "-" + stmntText;
+
+            CompiledStatement.Instance csi = statements.get(key);
 
             if (csi == null) {
                 CompiledStatement cs = compiledStatements.get(stmntText);
                 csi = cs.new Instance();
-                statements.put(stmntText, csi);
+                statements.put(key, csi);
             }
 
-            ResultData rd = csi.execute(connection, frames);
-
-            String [] columns = rd.getColumnLabels();
-
+            NamedConnection connection = connectionsStack.isEmpty()
+                ? connections.get(0)
+                : connectionsStack.peek();
 
-            for (Object [] row: rd.getRows()) {
-                frames.enter();
-                try {
-                    frames.put(columns, row);
-                    for (int i = 0; i < S; ++i) {
-                        build(parent, subs.item(i));
+            if (con.length() > 0) {
+                for (NamedConnection nc: connections) {
+                    if (con.equals(nc.name)) {
+                        connection = nc;
+                        break;
                     }
                 }
-                finally {
-                    frames.leave();
+            }
+
+            connectionsStack.push(connection);
+            try {
+                ResultData rd = csi.execute(
+                    connection.connection,
+                    frames,
+                    connection.cached);
+
+                String [] columns = rd.getColumnLabels();
+
+                for (Object [] row: rd.getRows()) {
+                    frames.enter();
+                    try {
+                        frames.put(columns, row);
+                        for (int i = 0; i < S; ++i) {
+                            build(parent, subs.item(i));
+                        }
+                    }
+                    finally {
+                        frames.leave();
+                    }
                 }
             }
+            finally {
+                connectionsStack.pop();
+            }
         }
 
         protected void element(Node parent, Element current)
@@ -454,6 +515,12 @@
         return document != null ? document : (Document)node;
     }
 
+    private static final List<NamedConnection> wrap(Connection connection) {
+        List<NamedConnection> list = new ArrayList<NamedConnection>(1);
+        list.add(new NamedConnection("", connection));
+        return list;
+    }
+
     public void build(
         Connection          connection,
         Node                output,
@@ -461,7 +528,17 @@
     )
     throws SQLException
     {
-        BuildHelper helper = new BuildHelper(output, connection, parameters);
+        build(wrap(connection), output, parameters);
+    }
+
+    public void build(
+        List<NamedConnection> connections,
+        Node                  output,
+        Map<String, Object>   parameters
+    )
+    throws SQLException
+    {
+        BuildHelper helper = new BuildHelper(output, connections, parameters);
 
         helper.build();
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java	Tue Aug 02 16:36:22 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java	Tue Aug 02 22:16:16 2011 +0000
@@ -122,9 +122,17 @@
             }
         }
 
-        public ResultData execute(Connection connection, StackFrames frames)
+        public ResultData execute(
+            Connection  connection,
+            StackFrames frames,
+            boolean     cached
+        )
         throws SQLException
         {
+            if (!cached) {
+                return executeUncached(connection, frames);
+            }
+
             Cache cache = CacheFactory.getCache(DATACAGE_DB_CACHE);
 
             return cache != null

http://dive4elements.wald.intevation.org