changeset 118:846cedacec75 embedded

Simplified the the beginning of the JDBC embedded connection, so only the URL is necessary to be provided to the Driver Manager to start the connection. The JDBC embedded connection URL has the format "jdbc:monetdb:embedded:<directory>" in contrast to the already existing MAPI one: "jdbc:monetdb://<host>[:port]/<database>".
author Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
date Mon, 20 Feb 2017 09:26:50 +0100 (2017-02-20)
parents 595d455b70a2
children 02f560eb3cf2
files src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in
diffstat 2 files changed, 53 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
@@ -106,8 +106,8 @@ public abstract class MonetConnection ex
         this.language = language;
         this.blobIsBinary = blobIsBinary;
         this.clobIsLongChar = clobIsLongChar;
-        String embedded = props.getProperty("embedded");
-        this.isEmbedded = embedded != null && embedded.equals("true");
+        //"instance of" should be cleanner, but this is faster.
+        this.isEmbedded = props.getProperty("embedded", "false").equals("true");
     }
 
     /**
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in
@@ -45,14 +45,15 @@ import java.util.logging.Logger;
  * where [:&lt;port&gt;] denotes that a port is optional. If not
  * given the default (@JDBC_DEF_PORT@) will be used.
  *
- * @author Fabian Groffen
+ * @author Fabian Groffen, Pedro Ferreira
  * @version @JDBC_MAJOR@.@JDBC_MINOR@ (@JDBC_VER_SUFFIX@)
  */
 final public class MonetDriver implements Driver {
-	// the url kind will be jdbc:monetdb://<host>[:<port>]/<database>
+	// the url kind will be jdbc:monetdb://<host>[:<port>]/<database> (in a MAPI connection)
+	// the url kind will be jdbc:monetdb:embedded:<directory> (in an Embedded connection)
 	// Chapter 9.2.1 from Sun JDBC 3.0 specification
 	/** The prefix of a MonetDB url */
-	private static final String MONETURL = "jdbc:monetdb://";
+	private static final String MONETURL = "jdbc:monetdb:";
 	/** Major version of this driver */
 	private static final int DRIVERMAJOR = @JDBC_MAJOR@;
 	/** Minor version of this driver */
@@ -145,17 +146,7 @@ final public class MonetDriver implement
 
 		prop = new DriverPropertyInfo("hash", "");
 		prop.required = false;
-		prop.description = "Force the use of the given hash algorithm during challenge response (one of SHA1, MD5, plain)";
-		props.add(prop);
-
-		prop = new DriverPropertyInfo("host", "localhost");
-		prop.required = false;
-		prop.description = "The MonetDB server hostname (MAPI connection only)";
-		props.add(prop);
-
-		prop = new DriverPropertyInfo("port", PORT);
-		prop.required = false;
-		prop.description = "The port to connect to the MonetDB server (MAPI connection only)";
+		prop.description = "Force the use of the given hash algorithm during challenge response (one of SHA1, MD5, plain) (MAPI connection only)";
 		props.add(prop);
 
 		prop = new DriverPropertyInfo("so_timeout", "0");
@@ -163,11 +154,6 @@ final public class MonetDriver implement
 		prop.description = "Defines the maximum time to wait in milliseconds on a blocking read socket call (MAPI connection only)"; // this corresponds to the Connection.setNetworkTimeout() method introduced in JDBC 4.1
 		props.add(prop);
 
-		prop = new DriverPropertyInfo("database", "");
-		prop.required = false;
-		prop.description = "The database name to connect (MAPI connection only)";
-		props.add(prop);
-
 		prop = new DriverPropertyInfo("follow_redirects", "true");
 		prop.required = false;
 		prop.description = "Whether redirects issued by the server should be followed (MAPI connection only)";
@@ -188,16 +174,6 @@ final public class MonetDriver implement
 		prop.description = "What language to use for MonetDB conversations (experts only)";
 		props.add(prop);
 
-		prop = new DriverPropertyInfo("embedded", "false");
-		prop.required = false;
-		prop.description = "Whether or not to use an embedded MonetDB connection";
-		props.add(prop);
-
-		prop = new DriverPropertyInfo("directory", "");
-		prop.required = false;
-		prop.description = "Sets the directory to set the database in a embedded connection (Embedded connection only)";
-		props.add(prop);
-
 		DriverPropertyInfo[] dpi = new DriverPropertyInfo[props.size()];
 		return props.toArray(dpi);
 	}
@@ -363,51 +339,59 @@ final public class MonetDriver implement
 	 */
 	public Connection connect(String url, Properties info) throws SQLException {
 		int tmp;
+		boolean isEmbedded;
 		Properties props = new Properties();
 		props.putAll(info);
 		info = props;
 
-		// url should be of style jdbc:monetdb://<host>/<database>
 		if (!acceptsURL(url))
 			throw new SQLException("Invalid URL: it does not start with: " + MONETURL, "08M26");
 
-		// remove leading "jdbc:" so the rest is a valid hierarchical URI
-		URI uri;
-		try {
-			uri = new URI(url.substring(5));
-		} catch (URISyntaxException e) {
-			throw new SQLException(e.toString(), "08M26");
+		if(!url.startsWith("jdbc:monetdb:embedded:")) {
+		    // url should be of style jdbc:monetdb://<host>/<database>
+            isEmbedded = false;
+            URI uri;
+            try {
+                uri = new URI(url.substring(5));
+            } catch (URISyntaxException e) {
+                throw new SQLException(e.toString(), "08M26");
+            }
+
+            String uri_host = uri.getHost();
+            if (uri_host == null)
+                throw new SQLException("Invalid URL: no hostname given or unparsable in '" + url + "'", "08M26");
+            info.put("host", uri_host);
+
+            int uri_port = uri.getPort();
+            if (uri_port > 0)
+                info.put("port", "" + uri_port);
+
+            // check the database
+            String uri_path = uri.getPath();
+            if (uri_path != null && uri_path.length() != 0) {
+                uri_path = uri_path.substring(1);
+                if (!uri_path.trim().isEmpty())
+                    info.put("database", uri_path);
+            }
+
+            String uri_query = uri.getQuery();
+            if (uri_query != null) {
+                // handle additional arguments
+                String args[] = uri_query.split("&");
+                for (String arg : args) {
+                    tmp = arg.indexOf('=');
+                    if (tmp > 0)
+                        info.put(arg.substring(0, tmp), arg.substring(tmp + 1));
+                }
+            }
+		} else {
+	    	// url should be of style jdbc:monetdb:embedded:<directory>
+       		isEmbedded = true;
+    		info.put("directory", url.substring(22));
 		}
 
-		String uri_host = uri.getHost();
-		if (uri_host == null)
-			throw new SQLException("Invalid URL: no hostname given or unparsable in '" + url + "'", "08M26");
-		info.put("host", uri_host);
-
-		int uri_port = uri.getPort();
-		if (uri_port > 0)
-			info.put("port", "" + uri_port);
-
-		// check the database
-		String uri_path = uri.getPath();
-		if (uri_path != null && uri_path.length() != 0) {
-			uri_path = uri_path.substring(1);
-			if (!uri_path.trim().isEmpty())
-				info.put("database", uri_path);
-		}
-
-		String uri_query = uri.getQuery();
-		if (uri_query != null) {
-			// handle additional arguments
-			String args[] = uri_query.split("&");
-			for (String arg : args) {
-				tmp = arg.indexOf('=');
-				if (tmp > 0)
-					info.put(arg.substring(0, tmp), arg.substring(tmp + 1));
-			}
-		}
-
-		// finally return the Connection as requested
+		info.put("embedded", Boolean.toString(isEmbedded));
+        // finally return the Connection as requested
 		return CreateMonetDBJDBCConnection(info);
 	}
 
@@ -416,7 +400,7 @@ final public class MonetDriver implement
 			IllegalArgumentException {
 		MonetConnection res;
 
-		boolean isEmbedded = Boolean.parseBoolean(props.getProperty("embedded", "false"));
+        boolean isEmbedded = Boolean.parseBoolean(props.getProperty("embedded", "false"));
 		String language = props.getProperty("language", "sql");
 		String username = props.getProperty("user");
 		String password = props.getProperty("password");
@@ -505,7 +489,7 @@ final public class MonetDriver implement
 			res.setSoTimeout(sockTimeout);
 		}
 
-		try { //atempt to connect and authenticate the user
+		try { //attempt to connect and authenticate the user
 			List<String> warnings = res.connect(username, password);
 			if(warnings != null) {
 				for (String warning : warnings) {