changeset 809:aa4108a5bc34 monetdbs

Move more URL and properties parsing responsibilities to Target
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Tue, 12 Dec 2023 12:03:24 +0100 (16 months ago)
parents 6b7778153d23
children 425592a53fcd
files src/main/java/org/monetdb/jdbc/MonetDriver.java src/main/java/org/monetdb/mcl/net/Target.java
diffstat 2 files changed, 32 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/monetdb/jdbc/MonetDriver.java
+++ b/src/main/java/org/monetdb/jdbc/MonetDriver.java
@@ -106,31 +106,12 @@ public final class MonetDriver implement
 		if (!acceptsURL(url))
 			return null;
 
-		Target target = new Target();
-
 		try {
-			// If properties are given, add those first
-			if (info != null) {
-				for (String key : info.stringPropertyNames()) {
-					String value = info.getProperty(key);
-					if (key.equals(Parameter.HOST.name))
-						value = Target.unpackHost(value);
-					target.setString(key, value);
-				}
-			}
-
-			// If url is exactly "jdbc:monetdb:", use just the properties.
-			// This is different from, say, jdbc:monetdb://, because the
-			// latter will clear preexisting host, port, TLS and database settings.
-			// Useful in combination with Target.toProperties().
-			if (!url.equals("jdbc:monetdb:"))
-				MonetUrlParser.parse(target, url.substring(5));
+			Target target = new Target(url, info);
+			return new MonetConnection(target);
 		} catch (ValidationError | URISyntaxException e) {
 			throw new SQLException(e.getMessage());
 		}
-
-        // finally return the Connection object as requested
-		return new MonetConnection(target);
 	}
 
 	/**
--- a/src/main/java/org/monetdb/mcl/net/Target.java
+++ b/src/main/java/org/monetdb/mcl/net/Target.java
@@ -1,5 +1,7 @@
 package org.monetdb.mcl.net;
 
+import java.net.URISyntaxException;
+import java.util.Properties;
 import java.util.regex.Pattern;
 
 public class Target {
@@ -42,6 +44,12 @@ public class Target {
         this.timezone = (int)Parameter.TIMEZONE.getDefault();
     }
 
+    public Target(String url, Properties props) throws URISyntaxException, ValidationError {
+        this();
+        setProperties(props);
+        parseUrl(url);
+    }
+
     public void barrier() {
         if (userWasSet && !passwordWasSet)
             password = "";
@@ -89,6 +97,28 @@ public class Target {
         assign(parm, parm.getDefault());
     }
 
+    public void setProperties(Properties props) throws ValidationError {
+        if (props != null) {
+            for (String key : props.stringPropertyNames()) {
+                String value = props.getProperty(key);
+                if (key.equals(Parameter.HOST.name))
+                    value = Target.unpackHost(value);
+                setString(key, value);
+            }
+        }
+    }
+
+    public void parseUrl(String url) throws URISyntaxException, ValidationError {
+        if (url == null)
+            return;
+        if (url.startsWith("jdbc:"))
+            url = url.substring(5);
+        if (url.equals("monetdb:")) {
+            return;
+        }
+        MonetUrlParser.parse(this, url);
+    }
+
     private void assign(Parameter parm, Object value) {
         switch (parm) {
             case TLS: setTls((boolean)value); break;