[Dive4elements-commits] [PATCH] Added after login SQL statements for database connection. Can be used to alter the current schema to avoid naming clashes

Wald Commits scm-commit at wald.intevation.org
Sat Jan 5 13:31:04 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1357389057 -3600
# Node ID f0b3ad10f67f19c41ac99c5f5d51956b97937317
# Parent  f939e1e6cfa464c1c2c2370208444009fbe23669
Added after login SQL statements for database connection. Can be used to alter the current schema to avoid naming clashes.

diff -r f939e1e6cfa4 -r f0b3ad10f67f flys-aft/doc/conf-oracle.xml
--- a/flys-aft/doc/conf-oracle.xml	Sat Jan 05 12:20:28 2013 +0100
+++ b/flys-aft/doc/conf-oracle.xml	Sat Jan 05 13:30:57 2013 +0100
@@ -21,6 +21,9 @@
       <user>aft</user>
       <password>aft</password>
       <url>jdbc:oracle:thin:@//localhost:1521/XE</url>
+      <execute-login>
+        <statement>ALTER SESSION SET CURRENT_SCHEMA=AFT</statement>
+      </execute-login>
     </db>
   </side>
 </sync>
diff -r f939e1e6cfa4 -r f0b3ad10f67f flys-aft/doc/conf.xml
--- a/flys-aft/doc/conf.xml	Sat Jan 05 12:20:28 2013 +0100
+++ b/flys-aft/doc/conf.xml	Sat Jan 05 13:30:57 2013 +0100
@@ -29,6 +29,11 @@
       <user/>
       <password/>
       <url>jdbc:sqlite:/path/to/aft.db</url>
+      <!--
+      <execute-login>
+        <statement>ALTER SESSION SET CURRENT_SCHEMA=AFT</statement>
+      </execute-login>
+      -->
     </db>
   </side>
 </sync>
diff -r f939e1e6cfa4 -r f0b3ad10f67f flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java
--- a/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java	Sat Jan 05 12:20:28 2013 +0100
+++ b/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java	Sat Jan 05 13:30:57 2013 +0100
@@ -6,35 +6,51 @@
 import java.sql.DatabaseMetaData;
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.sql.Statement;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 public class ConnectionBuilder
 {
     private static Logger log = Logger.getLogger(ConnectionBuilder.class);
 
-    public static final String XPATH_DRIVER   = "/sync/side[@name=$type]/db/driver/text()";
-    public static final String XPATH_USER     = "/sync/side[@name=$type]/db/user/text()";
-    public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()";
-    public static final String XPATH_URL      = "/sync/side[@name=$type]/db/url/text()";
+    public static final String XPATH_DRIVER     = "/sync/side[@name=$type]/db/driver/text()";
+    public static final String XPATH_USER       = "/sync/side[@name=$type]/db/user/text()";
+    public static final String XPATH_PASSWORD   = "/sync/side[@name=$type]/db/password/text()";
+    public static final String XPATH_URL        = "/sync/side[@name=$type]/db/url/text()";
+    public static final String XPATH_EXEC_LOGIN = "/sync/side[@name=$type]/db/execute-login/statement";
 
-    protected String type;
-    protected String driver;
-    protected String user;
-    protected String password;
-    protected String url;
+    protected String       type;
+    protected String       driver;
+    protected String       user;
+    protected String       password;
+    protected String       url;
+    protected List<String> loginStatements;
 
     public ConnectionBuilder(String type, Document document) {
         this.type = type;
         extractCredentials(document);
     }
 
+    protected static List<String> extractStrings(NodeList nodes) {
+        int N = nodes.getLength();
+        List<String> result = new ArrayList<String>(N);
+        for (int i = 0; i < N; ++i) {
+            result.add(nodes.item(i).getTextContent());
+        }
+        return result;
+    }
+
     protected void extractCredentials(Document document) {
         HashMap<String, String> map = new HashMap<String, String>();
         map.put("type", type);
@@ -47,12 +63,15 @@
             document, XPATH_PASSWORD, XPathConstants.STRING, null, map);
         url = (String)XML.xpath(
             document, XPATH_URL, XPathConstants.STRING, null, map);
+        loginStatements = extractStrings((NodeList)XML.xpath(
+            document, XPATH_EXEC_LOGIN, XPathConstants.NODESET, null, map));
 
         if (log.isDebugEnabled()) {
             log.debug("driver: " + driver);
             log.debug("user: " + user);
             log.debug("password: *******");
             log.debug("url: " + url);
+            log.debug("number of login statements: " + loginStatements.size());
         }
     }
 
@@ -80,6 +99,16 @@
                 Connection.TRANSACTION_READ_UNCOMMITTED);
         }
 
+        for (String sql: loginStatements) {
+            Statement stmnt = connection.createStatement();
+            try {
+                stmnt.execute(sql);
+            }
+            finally {
+                stmnt.close();
+            }
+        }
+
         return connection;
     }
 


More information about the Dive4elements-commits mailing list