comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 441:84f6d86ed590

Optimise composing the "SET TIME ZONE ..." string by using a StringBuilder. Less String copying. Moved 2 utility methods to the end with the other utility methods.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 10 Feb 2021 19:38:22 +0100 (2021-02-10)
parents 57d46c34c649
children 3dfcd06fd8ba
comparison
equal deleted inserted replaced
440:a851e6d6a71a 441:84f6d86ed590
212 this.defaultFetchSize = fetchsize; 212 this.defaultFetchSize = fetchsize;
213 conn_props.setProperty("fetchsize", fetchsize_prop); 213 conn_props.setProperty("fetchsize", fetchsize_prop);
214 } else { 214 } else {
215 addWarning("Fetch size must either be positive or -1. Value " + fetchsize + " ignored", "M1M05"); 215 addWarning("Fetch size must either be positive or -1. Value " + fetchsize + " ignored", "M1M05");
216 } 216 }
217 } catch (java.lang.NumberFormatException e) { 217 } catch (NumberFormatException e) {
218 addWarning("Invalid fetch size. Value '" + fetchsize_prop + "' ignored", "M1M05"); 218 addWarning("Unable to parse fetch size number from: " + fetchsize_prop, "M1M05");
219 } 219 }
220 } 220 }
221 221
222 final String treatBlobAsVarBinary_prop = props.getProperty("treat_blob_as_binary"); 222 final String treatBlobAsVarBinary_prop = props.getProperty("treat_blob_as_binary");
223 if (treatBlobAsVarBinary_prop != null) { 223 if (treatBlobAsVarBinary_prop != null) {
276 server.setHash(hash); 276 server.setHash(hash);
277 if (database != null) 277 if (database != null)
278 server.setDatabase(database); 278 server.setDatabase(database);
279 server.setLanguage(language); 279 server.setLanguage(language);
280 280
281 HandshakeOptions handshakeOptions = new HandshakeOptions();
282 final Calendar cal = Calendar.getInstance(); 281 final Calendar cal = Calendar.getInstance();
283 int offsetMillis = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); 282 int offsetMillis = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
284 int offsetSeconds = offsetMillis / 1000; 283 int offsetSeconds = offsetMillis / 1000;
284 final HandshakeOptions handshakeOptions = new HandshakeOptions();
285 handshakeOptions.setTimeZone(offsetSeconds); 285 handshakeOptions.setTimeZone(offsetSeconds);
286 handshakeOptions.setReplySize(defaultFetchSize); 286 handshakeOptions.setReplySize(defaultFetchSize);
287 server.setHandshakeOptions(handshakeOptions); 287 server.setHandshakeOptions(handshakeOptions);
288 288
289 // we're debugging here... uhm, should be off in real life 289 // we're debugging here... uhm, should be off in real life
377 // enable auto commit 377 // enable auto commit
378 setAutoCommit(true); 378 setAutoCommit(true);
379 379
380 // set our time zone on the server, if we haven't already 380 // set our time zone on the server, if we haven't already
381 if (handshakeOptions.mustSendTimeZone()) { 381 if (handshakeOptions.mustSendTimeZone()) {
382 final StringBuilder tz = new StringBuilder(64);
383 tz.append("SET TIME ZONE INTERVAL '");
382 int offsetMinutes = handshakeOptions.getTimeZone() / 60; 384 int offsetMinutes = handshakeOptions.getTimeZone() / 60;
383 String tz = offsetMinutes < 0 ? "-" : "+"; 385 if (offsetMinutes < 0) {
384 tz += (Math.abs(offsetMinutes) / 60 < 10 ? "0" : "") + (Math.abs(offsetMinutes) / 60) + ":"; 386 tz.append('-');
385 offsetMinutes -= (offsetMinutes / 60) * 60; 387 offsetMinutes = -offsetMinutes; // make it positive
386 tz += (offsetMinutes < 10 ? "0" : "") + offsetMinutes; 388 } else {
387 sendIndependentCommand("SET TIME ZONE INTERVAL '" + tz + "' HOUR TO MINUTE"); 389 tz.append('+');
390 }
391 int offsetHours = offsetMinutes / 60;
392 if (offsetHours < 10)
393 tz.append('0');
394 tz.append(offsetHours).append(':');
395 offsetMinutes -= offsetHours * 60;
396 if (offsetMinutes < 10)
397 tz.append('0');
398 tz.append(offsetMinutes).append("' HOUR TO MINUTE");
399 sendIndependentCommand(tz.toString());
388 } 400 }
389 } 401 }
390 402
391 // we're absolutely not closed, since we're brand new 403 // we're absolutely not closed, since we're brand new
392 closed = false; 404 closed = false;
1463 if (conn_props.containsKey(name)) 1475 if (conn_props.containsKey(name))
1464 conn_props.remove(name); 1476 conn_props.remove(name);
1465 return; 1477 return;
1466 } 1478 }
1467 // only set value for supported property names, warn about the others 1479 // only set value for supported property names, warn about the others
1468 if (checkValidProperty(name, "setClientInfo")) 1480 if (checkValidProperty(name, "setClientInfo")) {
1469 {
1470 conn_props.setProperty(name, value); 1481 conn_props.setProperty(name, value);
1471 } 1482 }
1472 } 1483 }
1473 1484
1474 /** 1485 /**
1499 if (props != null) { 1510 if (props != null) {
1500 for (Entry<Object, Object> entry : props.entrySet()) { 1511 for (Entry<Object, Object> entry : props.entrySet()) {
1501 setClientInfo(entry.getKey().toString(), entry.getValue().toString()); 1512 setClientInfo(entry.getKey().toString(), entry.getValue().toString());
1502 } 1513 }
1503 } 1514 }
1504 }
1505
1506 private boolean checkValidProperty(String name, String context) {
1507 boolean valid = isValidProperty(name);
1508 if (!valid) {
1509 addWarning(java.lang.String.format("%s: '%s' is not a recognised property", context, name), "01M07");
1510 }
1511 return valid;
1512 }
1513
1514 private boolean isValidProperty(String name) {
1515 return name.equals("host") ||
1516 name.equals("port") ||
1517 name.equals("user") ||
1518 name.equals("password") ||
1519 name.equals("database") ||
1520 name.equals("language") ||
1521 name.equals("so_timeout") ||
1522 name.equals("debug") ||
1523 name.equals("hash") ||
1524 name.equals("treat_blob_as_binary") ||
1525 name.equals("treat_clob_as_varchar") ||
1526 name.equals("fetchsize") ||
1527 name.equals("logfile")
1528 ;
1529 } 1515 }
1530 1516
1531 //== Java 1.7 methods (JDBC 4.1) 1517 //== Java 1.7 methods (JDBC 4.1)
1532 1518
1533 /** 1519 /**
1746 .append(':').append(port) 1732 .append(':').append(port)
1747 .append('/').append(database); 1733 .append('/').append(database);
1748 if (lang == LANG_MAL) 1734 if (lang == LANG_MAL)
1749 sb.append("?language=mal"); 1735 sb.append("?language=mal");
1750 return sb.toString(); 1736 return sb.toString();
1737 }
1738
1739 private boolean checkValidProperty(String name, String context) {
1740 if (isValidProperty(name))
1741 return true;
1742 addWarning(context + ": '" + name + "' is not a recognised property", "01M07");
1743 return false;
1744 }
1745
1746 // supported MonetDB connection properties.
1747 // See also MonetDatabaseMetaData.getClientInfoProperties()
1748 private boolean isValidProperty(String name) {
1749 return name.equals("host") ||
1750 name.equals("port") ||
1751 name.equals("user") ||
1752 name.equals("password") ||
1753 name.equals("language") ||
1754 name.equals("database") ||
1755 name.equals("debug") ||
1756 name.equals("logfile") ||
1757 name.equals("hash") ||
1758 name.equals("treat_blob_as_binary") ||
1759 name.equals("treat_clob_as_varchar") ||
1760 name.equals("so_timeout") ||
1761 name.equals("fetchsize"); // only supported by servers from version 11.41.1 onwards
1751 } 1762 }
1752 1763
1753 1764
1754 // Internal caches for 3 static mserver environment values, so they aren't queried from mserver again and again 1765 // Internal caches for 3 static mserver environment values, so they aren't queried from mserver again and again
1755 private String env_current_user; 1766 private String env_current_user;