comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 764:052c23fbfab2

Corrected implementation of Connection methods getClientInfo() and setClientInfo(). They used to get/set Connection properties instead of Client Info properties which was a incorrect. MonetDB does not support Client Info properties.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 05 Jul 2023 21:34:11 +0200 (22 months ago)
parents 1566dbfaa13b
children c85c2d90d31f
comparison
equal deleted inserted replaced
763:e8afd7de2538 764:052c23fbfab2
72 */ 72 */
73 public class MonetConnection 73 public class MonetConnection
74 extends MonetWrapper 74 extends MonetWrapper
75 implements Connection, AutoCloseable 75 implements Connection, AutoCloseable
76 { 76 {
77 /** the successful processed input properties */
78 private final Properties conn_props = new Properties();
79
80 /** The hostname to connect to */ 77 /** The hostname to connect to */
81 private final String hostname; 78 private final String hostname;
82 /** The port to connect on the host to */ 79 /** The port to connect on the host to */
83 private int port; 80 private int port;
84 /** The database to use (currently not used) */ 81 /** The database to use (currently not used) */
170 HandshakeOption.SizeHeader sizeHeaderSetting = new HandshakeOption.SizeHeader(true); 167 HandshakeOption.SizeHeader sizeHeaderSetting = new HandshakeOption.SizeHeader(true);
171 HandshakeOption.TimeZone timeZoneSetting = new HandshakeOption.TimeZone(0); 168 HandshakeOption.TimeZone timeZoneSetting = new HandshakeOption.TimeZone(0);
172 169
173 // for debug: System.out.println("New connection object. Received properties are: " + props.toString()); 170 // for debug: System.out.println("New connection object. Received properties are: " + props.toString());
174 // get supported property values from the props argument. 171 // get supported property values from the props argument.
175 // When a value is found add it to the internal conn_props list for use by getClientInfo().
176 this.hostname = props.getProperty("host"); 172 this.hostname = props.getProperty("host");
177 if (this.hostname != null)
178 conn_props.setProperty("host", this.hostname);
179 173
180 final String port_prop = props.getProperty("port"); 174 final String port_prop = props.getProperty("port");
181 if (port_prop != null) { 175 if (port_prop != null) {
182 try { 176 try {
183 this.port = Integer.parseInt(port_prop); 177 this.port = Integer.parseInt(port_prop);
184 } catch (NumberFormatException e) { 178 } catch (NumberFormatException e) {
185 addWarning("Unable to parse port number from: " + port_prop, "M1M05"); 179 addWarning("Unable to parse port number from: " + port_prop, "M1M05");
186 } 180 }
187 conn_props.setProperty("port", Integer.toString(this.port));
188 } 181 }
189 182
190 this.database = props.getProperty("database"); 183 this.database = props.getProperty("database");
191 if (this.database != null)
192 conn_props.setProperty("database", this.database);
193
194 this.username = props.getProperty("user"); 184 this.username = props.getProperty("user");
195 if (this.username != null)
196 conn_props.setProperty("user", this.username);
197
198 this.password = props.getProperty("password"); 185 this.password = props.getProperty("password");
199 if (this.password != null)
200 conn_props.setProperty("password", this.password);
201
202 String language = props.getProperty("language"); 186 String language = props.getProperty("language");
203 if (language != null)
204 conn_props.setProperty("language", language);
205 187
206 boolean debug = false; 188 boolean debug = false;
207 String debug_prop = props.getProperty("debug"); 189 String debug_prop = props.getProperty("debug");
208 if (debug_prop != null) { 190 if (debug_prop != null) {
209 debug = Boolean.parseBoolean(debug_prop); 191 debug = Boolean.parseBoolean(debug_prop);
210 conn_props.setProperty("debug", Boolean.toString(debug));
211 } 192 }
212 193
213 final String hash = props.getProperty("hash"); 194 final String hash = props.getProperty("hash");
214 if (hash != null)
215 conn_props.setProperty("hash", hash);
216 195
217 String autocommit_prop = props.getProperty("autocommit"); 196 String autocommit_prop = props.getProperty("autocommit");
218 if (autocommit_prop != null) { 197 if (autocommit_prop != null) {
219 boolean ac = Boolean.parseBoolean(autocommit_prop); 198 boolean ac = Boolean.parseBoolean(autocommit_prop);
220 autoCommitSetting.set(ac); 199 autoCommitSetting.set(ac);
221 conn_props.setProperty("autocommit", Boolean.toString(ac));
222 } 200 }
223 201
224 final String fetchsize_prop = props.getProperty("fetchsize"); 202 final String fetchsize_prop = props.getProperty("fetchsize");
225 if (fetchsize_prop != null) { 203 if (fetchsize_prop != null) {
226 try { 204 try {
227 int fetchsize = Integer.parseInt(fetchsize_prop); 205 int fetchsize = Integer.parseInt(fetchsize_prop);
228 if (fetchsize > 0 || fetchsize == -1) { 206 if (fetchsize > 0 || fetchsize == -1) {
229 replySizeSetting.set(fetchsize); 207 replySizeSetting.set(fetchsize);
230 conn_props.setProperty("fetchsize", fetchsize_prop);
231 } else { 208 } else {
232 addWarning("Fetch size must either be positive or -1. Value " + fetchsize + " ignored", "M1M05"); 209 addWarning("Fetch size must either be positive or -1. Value " + fetchsize + " ignored", "M1M05");
233 } 210 }
234 } catch (NumberFormatException e) { 211 } catch (NumberFormatException e) {
235 addWarning("Unable to parse fetch size number from: " + fetchsize_prop, "M1M05"); 212 addWarning("Unable to parse fetch size number from: " + fetchsize_prop, "M1M05");
237 } 214 }
238 215
239 final String treatBlobAsVarBinary_prop = props.getProperty("treat_blob_as_binary"); 216 final String treatBlobAsVarBinary_prop = props.getProperty("treat_blob_as_binary");
240 if (treatBlobAsVarBinary_prop != null) { 217 if (treatBlobAsVarBinary_prop != null) {
241 treatBlobAsVarBinary = Boolean.parseBoolean(treatBlobAsVarBinary_prop); 218 treatBlobAsVarBinary = Boolean.parseBoolean(treatBlobAsVarBinary_prop);
242 conn_props.setProperty("treat_blob_as_binary", Boolean.toString(treatBlobAsVarBinary));
243 if (treatBlobAsVarBinary) 219 if (treatBlobAsVarBinary)
244 typeMap.put("blob", Byte[].class); 220 typeMap.put("blob", Byte[].class);
245 } 221 }
246 222
247 final String treatClobAsVarChar_prop = props.getProperty("treat_clob_as_varchar"); 223 final String treatClobAsVarChar_prop = props.getProperty("treat_clob_as_varchar");
248 if (treatClobAsVarChar_prop != null) { 224 if (treatClobAsVarChar_prop != null) {
249 treatClobAsVarChar = Boolean.parseBoolean(treatClobAsVarChar_prop); 225 treatClobAsVarChar = Boolean.parseBoolean(treatClobAsVarChar_prop);
250 conn_props.setProperty("treat_clob_as_varchar", Boolean.toString(treatClobAsVarChar));
251 if (treatClobAsVarChar) 226 if (treatClobAsVarChar)
252 typeMap.put("clob", String.class); 227 typeMap.put("clob", String.class);
253 } 228 }
254 229
255 int sockTimeout = 0; 230 int sockTimeout = 0;
262 sockTimeout = 0; 237 sockTimeout = 0;
263 } 238 }
264 } catch (NumberFormatException e) { 239 } catch (NumberFormatException e) {
265 addWarning("Unable to parse socket timeout number from: " + so_timeout_prop, "M1M05"); 240 addWarning("Unable to parse socket timeout number from: " + so_timeout_prop, "M1M05");
266 } 241 }
267 conn_props.setProperty("so_timeout", Integer.toString(sockTimeout));
268 } 242 }
269 243
270 // check mandatory input arguments 244 // check mandatory input arguments
271 if (hostname == null || hostname.isEmpty()) 245 if (hostname == null || hostname.isEmpty())
272 throw new IllegalArgumentException("Missing or empty host name"); 246 throw new IllegalArgumentException("Missing or empty host name");
281 // fallback to default language: sql 255 // fallback to default language: sql
282 language = "sql"; 256 language = "sql";
283 addWarning("No language specified, defaulting to 'sql'", "M1M05"); 257 addWarning("No language specified, defaulting to 'sql'", "M1M05");
284 } 258 }
285 259
286 // warn about unrecognized settings 260 // warn about unrecognized property names
287 for (Entry<Object,Object> e: props.entrySet()) { 261 for (Entry<Object,Object> e: props.entrySet()) {
288 checkValidProperty(e.getKey().toString(), "MonetConnection"); 262 checkValidProperty(e.getKey().toString(), "MonetConnection");
289 } 263 }
290 264
291 server = new MapiSocket(); 265 server = new MapiSocket();
1429 * or this method is called on a closed connection 1403 * or this method is called on a closed connection
1430 * @since 1.6 1404 * @since 1.6
1431 */ 1405 */
1432 @Override 1406 @Override
1433 public String getClientInfo(final String name) throws SQLException { 1407 public String getClientInfo(final String name) throws SQLException {
1434 if (name == null || name.isEmpty()) 1408 // MonetDB doesn't support any Client Info Properties yet
1435 return null; 1409 return null;
1436 checkNotClosed();
1437 return conn_props.getProperty(name);
1438 } 1410 }
1439 1411
1440 /** 1412 /**
1441 * Returns a list containing the name and current value of each client info 1413 * Returns a list containing the name and current value of each client info
1442 * property supported by the driver. The value of a client info property may 1414 * property supported by the driver. The value of a client info property may
1449 * or this method is called on a closed connection 1421 * or this method is called on a closed connection
1450 * @since 1.6 1422 * @since 1.6
1451 */ 1423 */
1452 @Override 1424 @Override
1453 public Properties getClientInfo() throws SQLException { 1425 public Properties getClientInfo() throws SQLException {
1454 checkNotClosed(); 1426 // MonetDB doesn't support any Client Info Properties yet
1455 // return a clone of the connection properties object 1427 return new Properties();
1456 return new Properties(conn_props);
1457 } 1428 }
1458 1429
1459 /** 1430 /**
1460 * Sets the value of the client info property specified by name to the value specified by value. 1431 * Sets the value of the client info property specified by name to the value specified by value.
1461 * Applications may use the DatabaseMetaData.getClientInfoProperties method to determine 1432 * Applications may use the DatabaseMetaData.getClientInfoProperties method to determine
1492 * or this method is called on a closed connection 1463 * or this method is called on a closed connection
1493 * @since 1.6 1464 * @since 1.6
1494 */ 1465 */
1495 @Override 1466 @Override
1496 public void setClientInfo(final String name, final String value) throws SQLClientInfoException { 1467 public void setClientInfo(final String name, final String value) throws SQLClientInfoException {
1497 if (name == null || name.isEmpty()) { 1468 // MonetDB doesn't support any Client Info Properties yet
1498 addWarning("setClientInfo: missing property name", "01M07"); 1469 addWarning("setClientInfo: client info property name not recognized", "01M07");
1499 return;
1500 }
1501 // If the value is null, the current value of the specified property is cleared.
1502 if (value == null) {
1503 if (conn_props.containsKey(name))
1504 conn_props.remove(name);
1505 return;
1506 }
1507 // only set value for supported property names, warn about the others
1508 if (checkValidProperty(name, "setClientInfo")) {
1509 conn_props.setProperty(name, value);
1510 }
1511 } 1470 }
1512 1471
1513 /** 1472 /**
1514 * Sets the value of the connection's client info properties. 1473 * Sets the value of the connection's client info properties.
1515 * The Properties object contains the names and values of the client info 1474 * The Properties object contains the names and values of the client info
1852 * @param context the method name from where this is called 1811 * @param context the method name from where this is called
1853 * @return valid true or false 1812 * @return valid true or false
1854 */ 1813 */
1855 private boolean checkValidProperty(String name, String context) { 1814 private boolean checkValidProperty(String name, String context) {
1856 // supported MonetDB connection properties. 1815 // supported MonetDB connection properties.
1857 // See also MonetDatabaseMetaData.getClientInfoProperties() 1816 // See also MonetDriver.connect()
1858 if (name.equals("host") 1817 if (name.equals("host")
1859 || name.equals("port") 1818 || name.equals("port")
1860 || name.equals("user") 1819 || name.equals("user")
1861 || name.equals("password") 1820 || name.equals("password")
1862 || name.equals("language") 1821 || name.equals("language")
1869 || name.equals("autocommit") 1828 || name.equals("autocommit")
1870 || name.equals("so_timeout") 1829 || name.equals("so_timeout")
1871 || name.equals("fetchsize")) // only supported by servers from version 11.41.1 onwards 1830 || name.equals("fetchsize")) // only supported by servers from version 11.41.1 onwards
1872 return true; 1831 return true;
1873 1832
1874 addWarning(context + ": '" + name + "' is not a recognised property", "01M07"); 1833 addWarning(context + " property name '" + name + "' is not recognized", "01M07");
1875 return false; 1834 return false;
1876 } 1835 }
1877 1836
1878 // Internal caches for 4 static mserver5 environment values 1837 // Internal caches for 4 static mserver5 environment values
1879 private String env_current_user; 1838 private String env_current_user;