[Dive4elements-commits] [PATCH] Generate valid DB connection strings

Wald Commits scm-commit at wald.intevation.org
Thu Feb 28 18:55:47 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1362074142 -3600
# Node ID 48b231a02d3aaddbd760c439eb5590330c4464fc
# Parent  d4fdd98a04f76d560bc013e7e504ce91421fc81b
Generate valid DB connection strings.

diff -r d4fdd98a04f7 -r 48b231a02d3a flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java	Thu Feb 28 18:04:19 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java	Thu Feb 28 18:55:42 2013 +0100
@@ -14,10 +14,10 @@
     private static final Logger logger = Logger.getLogger(MapUtils.class);
 
     public static final Pattern DB_URL_PATTERN =
-        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z_-]+)");
 
     public static final Pattern DB_PSQL_URL_PATTERN =
-        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)");
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9_-]+)");
 
     /**
      * This method returns a connection string for databases used by
@@ -46,8 +46,11 @@
         logger.debug("Groups for connection string: " + m.groupCount());
         int groups = m.groupCount();
 
-        for (int i = 0; i <= groups; i++) {
-            logger.debug("Group " + i + ": " + m.group(i));
+
+        if (logger.isDebugEnabled()) {
+            for (int i = 0; i <= groups; i++) {
+                logger.debug("Group " + i + ": " + m.group(i));
+            }
         }
 
         String connection = null;
@@ -73,19 +76,30 @@
             String port = m.group(3);
             String db   = m.group(4);
 
-            StringBuilder sb = new StringBuilder();
-            sb.append("dbname=" + db);
-            sb.append("host='" + host + "'");
-            sb.append("port=" + port);
-            sb.append("password='" + pass + "'");
-            sb.append("sslmode=disable");
-
-            connection = sb.toString();
+            connection = createConnectionString(user, pass, host, db, port);
         }
 
         return connection;
     }
 
+    public static String createConnectionString(
+        String user,
+        String pass,
+        String host,
+        String db,
+        String port
+    ) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("dbname=").append(db);
+        sb.append(" host='").append(host).append("'");
+        sb.append(" user=").append(user);
+        sb.append(" port=").append(port);
+        // XXX: We need to escape this somehow.
+        sb.append(" password='").append(pass).append("'");
+        sb.append(" sslmode=disable");
+        return sb.toString();
+    }
+
     protected static String getPostgreSQLConnection() {
         SessionFactoryImpl sf = (SessionFactoryImpl)
         SessionFactoryProvider.getSessionFactory();
@@ -120,15 +134,7 @@
         String port = m.group(3);
         String db   = m.group(4);
 
-        StringBuilder sb = new StringBuilder();
-        sb.append("dbname=" + db);
-        sb.append(" host='" + host + "'");
-        sb.append(" port=" + port);
-        sb.append(" user=" + user);
-        sb.append(" password='" + pass + "'");
-        sb.append(" sslmode=disable");
-
-        connection = sb.toString();
+        connection = createConnectionString(user, pass, host, db, port);
 
         logger.debug("Created connection: '" + connection + "'");
 


More information about the Dive4elements-commits mailing list