comparison src/main/java/org/monetdb/jdbc/MonetDriver.java @ 793:5bfe3357fb1c monetdbs

Use the new url parser
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Wed, 06 Dec 2023 16:17:13 +0100 (17 months ago)
parents 2f36ac68ac35
children 04a27386789f
comparison
equal deleted inserted replaced
792:9dea0795a926 793:5bfe3357fb1c
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2023 MonetDB B.V. 6 * Copyright 1997 - July 2008 CWI, August 2008 - 2023 MonetDB B.V.
7 */ 7 */
8 8
9 package org.monetdb.jdbc; 9 package org.monetdb.jdbc;
10 10
11 import java.net.URI; 11 import org.monetdb.mcl.net.MonetUrlParser;
12 import org.monetdb.mcl.net.Parameter;
13 import org.monetdb.mcl.net.Target;
14 import org.monetdb.mcl.net.ValidationError;
15
16 import java.net.URISyntaxException;
12 import java.sql.Connection; 17 import java.sql.Connection;
13 import java.sql.Driver; 18 import java.sql.Driver;
14 import java.sql.DriverManager; 19 import java.sql.DriverManager;
15 import java.sql.DriverPropertyInfo; 20 import java.sql.DriverPropertyInfo;
16 import java.sql.SQLException; 21 import java.sql.SQLException;
95 { 100 {
96 // url should be of style jdbc:monetdb://<host>/<database> 101 // url should be of style jdbc:monetdb://<host>/<database>
97 if (!acceptsURL(url)) 102 if (!acceptsURL(url))
98 return null; 103 return null;
99 104
100 final Properties props = new Properties(); 105 Target target = new Target();
101 // set the optional properties and their defaults here 106
102 props.put("port", "50000");
103 props.put("debug", "false");
104 props.put("language", "sql"); // mal, sql, <future>
105 props.put("so_timeout", "0");
106
107 if (info != null)
108 props.putAll(info);
109 info = props;
110
111 // remove leading "jdbc:" so the rest is a valid hierarchical URI
112 final URI uri;
113 try { 107 try {
114 uri = new URI(url.substring(5)); 108 if (info != null) {
115 } catch (java.net.URISyntaxException e) { 109 for (String key : info.stringPropertyNames()) {
116 return null; 110 String value = info.getProperty(key);
111 if (key.equals(Parameter.HOST.name))
112 value = Target.unpackHost(value);
113 target.setString(key, value);
114 }
115 }
116 MonetUrlParser.parse(target, url.substring(5));
117 } catch (ValidationError | URISyntaxException e) {
118 throw new SQLException(e.getMessage());
117 } 119 }
118 120
119 final String uri_host = uri.getHost(); 121 // finally return the Connection object as requested
120 if (uri_host == null) 122 return new MonetConnection(target);
121 return null;
122 info.put("host", uri_host);
123
124 int uri_port = uri.getPort();
125 if (uri_port > 0)
126 info.put("port", Integer.toString(uri_port));
127
128 // check the database
129 String uri_path = uri.getPath();
130 if (uri_path != null && !uri_path.isEmpty()) {
131 uri_path = uri_path.substring(1).trim();
132 if (!uri_path.isEmpty())
133 info.put("database", uri_path);
134 }
135
136 final String uri_query = uri.getQuery();
137 if (uri_query != null) {
138 int pos;
139 // handle additional connection properties separated by the & character
140 final String args[] = uri_query.split("&");
141 for (int i = 0; i < args.length; i++) {
142 pos = args[i].indexOf('=');
143 if (pos > 0)
144 info.put(args[i].substring(0, pos), args[i].substring(pos + 1));
145 }
146 }
147
148 // finally return the Connection object as requested
149 return new MonetConnection(info);
150 } 123 }
151 124
152 /** 125 /**
153 * Retrieves the driver's major version number. Initially this should be 1. 126 * Retrieves the driver's major version number. Initially this should be 1.
154 * 127 *