changeset 199:e87d89d371f1

Make some public non-JDBC methods accessable only to classes of the same package. Improve robustness of the connect() method by checking on null of input argument.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 14 Dec 2017 18:07:59 +0100 (2017-12-14)
parents 376a15ce49e3
children c38d4eaf5479
files src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in
diffstat 2 files changed, 22 insertions(+), 21 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
@@ -1645,11 +1645,11 @@ public class MonetConnection
 	 * @return the MonetDB JDBC Connection URL (without user name and password).
 	 * Defined as public because it is called from: MonetDatabaseMetaData.java getURL()
 	 */
-	public String getJDBCURL() {
+	String getJDBCURL() {
 		String language = "";
 		if (lang == LANG_MAL)
 			language = "?language=mal";
-		return "jdbc:monetdb://" + hostname + ":" + port + "/" + database + language;
+		return MonetDriver.MONETURL + hostname + ":" + port + "/" + database + language;
 	}
 
 	/**
@@ -1657,7 +1657,7 @@ public class MonetConnection
 	 * This allows generic JDBC programs to fetch Blob data via getBytes()
 	 * instead of getBlob() and Blob.getBinaryStream() to reduce overhead.
 	 */
-	public boolean mapBlobAsVarBinary() {
+	boolean mapBlobAsVarBinary() {
 		return treatBlobAsVarBinary;
 	}
 
@@ -1666,7 +1666,7 @@ public class MonetConnection
 	 * This allows generic JDBC programs to fetch Clob data via getString()
 	 * instead of getClob() and Clob.getCharacterStream() to reduce overhead.
 	 */
-	public boolean mapClobAsVarChar() {
+	boolean mapClobAsVarChar() {
 		return treatClobAsVarChar;
 	}
 
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in
@@ -48,7 +48,7 @@ final public class MonetDriver implement
 	// the url kind will be jdbc:monetdb://<host>[:<port>]/<database>
 	// Chapter 9.2.1 from Sun JDBC 3.0 specification
 	/** The prefix of a MonetDB url */
-	private static final String MONETURL = "jdbc:monetdb://";
+	static final String MONETURL = "jdbc:monetdb://";
 	/** Major version of this driver */
 	private static final int DRIVERMAJOR = @JDBC_MAJOR@;
 	/** Minor version of this driver */
@@ -114,7 +114,10 @@ final public class MonetDriver implement
 	public Connection connect(String url, Properties info)
 		throws SQLException
 	{
-		int tmp;
+		// url should be of style jdbc:monetdb://<host>/<database>
+		if (!acceptsURL(url))
+			throw new SQLNonTransientConnectionException("Invalid URL: it does not start with: " + MONETURL, "08M26");
+
 		Properties props = new Properties();
 		// set the optional properties and their defaults here
 		props.put("port", PORT);
@@ -122,13 +125,10 @@ final public class MonetDriver implement
 		props.put("language", "sql");	// mal, sql, <future>
 		props.put("so_timeout", "0");
 
-		props.putAll(info);
+		if (info != null)
+			props.putAll(info);
 		info = props;
 
-		// url should be of style jdbc:monetdb://<host>/<database>
-		if (!acceptsURL(url))
-			throw new SQLNonTransientConnectionException("Invalid URL: it does not start with: " + MONETURL, "08M26");
-
 		// remove leading "jdbc:" so the rest is a valid hierarchical URI
 		URI uri;
 		try {
@@ -148,24 +148,25 @@ final public class MonetDriver implement
 
 		// 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())
+		if (uri_path != null && !uri_path.isEmpty()) {
+			uri_path = uri_path.substring(1).trim();
+			if (!uri_path.isEmpty())
 				info.put("database", uri_path);
 		}
 
 		String uri_query = uri.getQuery();
 		if (uri_query != null) {
-			// handle additional arguments
+			int pos;
+			// handle additional connection properties separated by the & character
 			String args[] = uri_query.split("&");
 			for (int i = 0; i < args.length; i++) {
-				tmp = args[i].indexOf('=');
-				if (tmp > 0)
-					info.put(args[i].substring(0, tmp), args[i].substring(tmp + 1));
+				pos = args[i].indexOf('=');
+				if (pos > 0)
+					info.put(args[i].substring(0, pos), args[i].substring(pos + 1));
 			}
 		}
 
-		// finally return the Connection as requested
+		// finally return the Connection object as requested
 		return new MonetConnection(info);
 	}
 
@@ -407,11 +408,11 @@ final public class MonetDriver implement
 		return DRIVERMAJOR + "." + DRIVERMINOR + " (" + DRIVERVERSIONSUFFIX + ")";
 	}
 
-	public static int getDriverMajorVersion() {
+	static int getDriverMajorVersion() {
 		return DRIVERMAJOR;
 	}
 
-	public static int getDriverMinorVersion() {
+	static int getDriverMinorVersion() {
 		return DRIVERMINOR;
 	}
 }