Mercurial > hg > monetdb-java
comparison src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in @ 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 | 1296dbcc4958 |
children | c38d4eaf5479 |
comparison
equal
deleted
inserted
replaced
198:376a15ce49e3 | 199:e87d89d371f1 |
---|---|
46 */ | 46 */ |
47 final public class MonetDriver implements Driver { | 47 final public class MonetDriver implements Driver { |
48 // the url kind will be jdbc:monetdb://<host>[:<port>]/<database> | 48 // the url kind will be jdbc:monetdb://<host>[:<port>]/<database> |
49 // Chapter 9.2.1 from Sun JDBC 3.0 specification | 49 // Chapter 9.2.1 from Sun JDBC 3.0 specification |
50 /** The prefix of a MonetDB url */ | 50 /** The prefix of a MonetDB url */ |
51 private static final String MONETURL = "jdbc:monetdb://"; | 51 static final String MONETURL = "jdbc:monetdb://"; |
52 /** Major version of this driver */ | 52 /** Major version of this driver */ |
53 private static final int DRIVERMAJOR = @JDBC_MAJOR@; | 53 private static final int DRIVERMAJOR = @JDBC_MAJOR@; |
54 /** Minor version of this driver */ | 54 /** Minor version of this driver */ |
55 private static final int DRIVERMINOR = @JDBC_MINOR@; | 55 private static final int DRIVERMINOR = @JDBC_MINOR@; |
56 /** Version suffix string */ | 56 /** Version suffix string */ |
112 */ | 112 */ |
113 @Override | 113 @Override |
114 public Connection connect(String url, Properties info) | 114 public Connection connect(String url, Properties info) |
115 throws SQLException | 115 throws SQLException |
116 { | 116 { |
117 int tmp; | 117 // url should be of style jdbc:monetdb://<host>/<database> |
118 if (!acceptsURL(url)) | |
119 throw new SQLNonTransientConnectionException("Invalid URL: it does not start with: " + MONETURL, "08M26"); | |
120 | |
118 Properties props = new Properties(); | 121 Properties props = new Properties(); |
119 // set the optional properties and their defaults here | 122 // set the optional properties and their defaults here |
120 props.put("port", PORT); | 123 props.put("port", PORT); |
121 props.put("debug", "false"); | 124 props.put("debug", "false"); |
122 props.put("language", "sql"); // mal, sql, <future> | 125 props.put("language", "sql"); // mal, sql, <future> |
123 props.put("so_timeout", "0"); | 126 props.put("so_timeout", "0"); |
124 | 127 |
125 props.putAll(info); | 128 if (info != null) |
129 props.putAll(info); | |
126 info = props; | 130 info = props; |
127 | |
128 // url should be of style jdbc:monetdb://<host>/<database> | |
129 if (!acceptsURL(url)) | |
130 throw new SQLNonTransientConnectionException("Invalid URL: it does not start with: " + MONETURL, "08M26"); | |
131 | 131 |
132 // remove leading "jdbc:" so the rest is a valid hierarchical URI | 132 // remove leading "jdbc:" so the rest is a valid hierarchical URI |
133 URI uri; | 133 URI uri; |
134 try { | 134 try { |
135 uri = new URI(url.substring(5)); | 135 uri = new URI(url.substring(5)); |
146 if (uri_port > 0) | 146 if (uri_port > 0) |
147 info.put("port", Integer.toString(uri_port)); | 147 info.put("port", Integer.toString(uri_port)); |
148 | 148 |
149 // check the database | 149 // check the database |
150 String uri_path = uri.getPath(); | 150 String uri_path = uri.getPath(); |
151 if (uri_path != null && uri_path.length() != 0) { | 151 if (uri_path != null && !uri_path.isEmpty()) { |
152 uri_path = uri_path.substring(1); | 152 uri_path = uri_path.substring(1).trim(); |
153 if (!uri_path.trim().isEmpty()) | 153 if (!uri_path.isEmpty()) |
154 info.put("database", uri_path); | 154 info.put("database", uri_path); |
155 } | 155 } |
156 | 156 |
157 String uri_query = uri.getQuery(); | 157 String uri_query = uri.getQuery(); |
158 if (uri_query != null) { | 158 if (uri_query != null) { |
159 // handle additional arguments | 159 int pos; |
160 // handle additional connection properties separated by the & character | |
160 String args[] = uri_query.split("&"); | 161 String args[] = uri_query.split("&"); |
161 for (int i = 0; i < args.length; i++) { | 162 for (int i = 0; i < args.length; i++) { |
162 tmp = args[i].indexOf('='); | 163 pos = args[i].indexOf('='); |
163 if (tmp > 0) | 164 if (pos > 0) |
164 info.put(args[i].substring(0, tmp), args[i].substring(tmp + 1)); | 165 info.put(args[i].substring(0, pos), args[i].substring(pos + 1)); |
165 } | 166 } |
166 } | 167 } |
167 | 168 |
168 // finally return the Connection as requested | 169 // finally return the Connection object as requested |
169 return new MonetConnection(info); | 170 return new MonetConnection(info); |
170 } | 171 } |
171 | 172 |
172 /** | 173 /** |
173 * Retrieves the driver's major version number. Initially this should be 1. | 174 * Retrieves the driver's major version number. Initially this should be 1. |
405 */ | 406 */ |
406 public static String getDriverVersion() { | 407 public static String getDriverVersion() { |
407 return DRIVERMAJOR + "." + DRIVERMINOR + " (" + DRIVERVERSIONSUFFIX + ")"; | 408 return DRIVERMAJOR + "." + DRIVERMINOR + " (" + DRIVERVERSIONSUFFIX + ")"; |
408 } | 409 } |
409 | 410 |
410 public static int getDriverMajorVersion() { | 411 static int getDriverMajorVersion() { |
411 return DRIVERMAJOR; | 412 return DRIVERMAJOR; |
412 } | 413 } |
413 | 414 |
414 public static int getDriverMinorVersion() { | 415 static int getDriverMinorVersion() { |
415 return DRIVERMINOR; | 416 return DRIVERMINOR; |
416 } | 417 } |
417 } | 418 } |