# HG changeset patch # User Sascha L. Teichmann # Date 1312323376 0 # Node ID aca3b46160cbf632d90ec5551346f92b6ee1e385 # Parent d419c99048257fede0da1cf99470c1745451eae9 Added support for more than one db connection in datacage templating. flys-artifacts/trunk@2457 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r d419c9904825 -r aca3b46160cb flys-artifacts/ChangeLog --- 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 + + 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 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 * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java: diff -r d419c9904825 -r aca3b46160cb flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.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 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 connections; protected Map statements; + protected Deque connectionsStack; public BuildHelper( - Node output, - Connection connection, - Map parameters + Node output, + List connections, + Map parameters ) { - this.output = output; - this.connection = connection; - frames = new StackFrames(parameters); - statements = new HashMap(); - owner = getOwnerDocument(output); + if (connections.isEmpty()) { + throw new IllegalArgumentException("no connections given."); + } + + this.connections = connections; + connectionsStack = new ArrayDeque(); + this.output = output; + frames = new StackFrames(parameters); + owner = getOwnerDocument(output); + statements = + new HashMap(); } 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 wrap(Connection connection) { + List list = new ArrayList(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 connections, + Node output, + Map parameters + ) + throws SQLException + { + BuildHelper helper = new BuildHelper(output, connections, parameters); helper.build(); } diff -r d419c9904825 -r aca3b46160cb flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java --- 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