Mercurial > hg > monetdb-java
comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 480:849af4b76b28
Optimise code by reducing local variables which are used only once, replacing complex string concatenation by using StringBuilder, replacing some ternairy operators.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 10 Jun 2021 20:43:16 +0200 (2021-06-10) |
parents | d414e5c4470c |
children | c8f3950eec93 |
comparison
equal
deleted
inserted
replaced
479:1c8b9aec43a6 | 480:849af4b76b28 |
---|---|
1198 * | 1198 * |
1199 * @return a String representing this Object | 1199 * @return a String representing this Object |
1200 */ | 1200 */ |
1201 @Override | 1201 @Override |
1202 public String toString() { | 1202 public String toString() { |
1203 return "MonetDB Connection (" + getJDBCURL() + ") " + | 1203 return "MonetDB Connection (" + getJDBCURL() |
1204 (closed ? "disconnected" : "connected"); | 1204 + (closed ? ") disconnected" : ") connected"); |
1205 } | 1205 } |
1206 | 1206 |
1207 //== Java 1.6 methods (JDBC 4.0) | 1207 //== Java 1.6 methods (JDBC 4.0) |
1208 | 1208 |
1209 /** | 1209 /** |
1692 throw new SQLException("query timeout milliseconds is less than zero", "M1M05"); | 1692 throw new SQLException("query timeout milliseconds is less than zero", "M1M05"); |
1693 | 1693 |
1694 checkNotClosed(); | 1694 checkNotClosed(); |
1695 Statement st = null; | 1695 Statement st = null; |
1696 try { | 1696 try { |
1697 final String callstmt; | |
1697 // as of release Jun2020 (11.37.7) the function sys.settimeout(bigint) is deprecated and replaced by new sys.setquerytimeout(int) | 1698 // as of release Jun2020 (11.37.7) the function sys.settimeout(bigint) is deprecated and replaced by new sys.setquerytimeout(int) |
1698 final boolean postJun2020 = (getDatabaseMajorVersion() >=11) && (getDatabaseMinorVersion() >= 37); | 1699 if ((getDatabaseMajorVersion() == 11) && (getDatabaseMinorVersion() < 37)) |
1699 final String callstmt = postJun2020 ? "CALL sys.\"setquerytimeout\"(" + millis + ")" | 1700 callstmt = "CALL sys.\"settimeout\"(" + millis + ")"; |
1700 : "CALL sys.\"settimeout\"(" + millis + ")"; | 1701 else |
1702 callstmt = "CALL sys.\"setquerytimeout\"(" + millis + ")"; | |
1701 // for debug: System.out.println("Before: " + callstmt); | 1703 // for debug: System.out.println("Before: " + callstmt); |
1702 st = createStatement(); | 1704 st = createStatement(); |
1703 st.execute(callstmt); | 1705 st.execute(callstmt); |
1704 // for debug: System.out.println("After : " + callstmt); | 1706 // for debug: System.out.println("After : " + callstmt); |
1705 | 1707 |
1865 if (env_monet_version == null) | 1867 if (env_monet_version == null) |
1866 getEnvValues(); | 1868 getEnvValues(); |
1867 if (env_monet_version != null) { | 1869 if (env_monet_version != null) { |
1868 try { | 1870 try { |
1869 // from version string such as 11.33.9 extract number: 11 | 1871 // from version string such as 11.33.9 extract number: 11 |
1870 final int start = env_monet_version.indexOf('.'); | 1872 final int end = env_monet_version.indexOf('.'); |
1871 databaseMajorVersion = Integer.parseInt((start >= 0) ? env_monet_version.substring(0, start) : env_monet_version); | 1873 databaseMajorVersion = Integer.parseInt((end >= 0) ? env_monet_version.substring(0, end) : env_monet_version); |
1872 } catch (NumberFormatException nfe) { | 1874 } catch (NumberFormatException nfe) { |
1873 // ignore | 1875 // ignore |
1874 } | 1876 } |
1875 } | 1877 } |
1876 } | 1878 } |
2041 * @throws SQLException if an IO exception or a database error occurs | 2043 * @throws SQLException if an IO exception or a database error occurs |
2042 */ | 2044 */ |
2043 private void sendCommand(final String command, final boolean usequeryTempl) throws SQLException { | 2045 private void sendCommand(final String command, final boolean usequeryTempl) throws SQLException { |
2044 synchronized (server) { | 2046 synchronized (server) { |
2045 try { | 2047 try { |
2046 out.writeLine(usequeryTempl ? (queryTempl[0] + command + queryTempl[1]) | 2048 if (usequeryTempl) |
2047 : (commandTempl[0] + command + commandTempl[1]) ); | 2049 out.writeLine(queryTempl[0] + command + queryTempl[1]); |
2050 else | |
2051 out.writeLine(commandTempl[0] + command + commandTempl[1]); | |
2048 final String error = in.waitForPrompt(); | 2052 final String error = in.waitForPrompt(); |
2049 if (error != null) | 2053 if (error != null) |
2050 throw new SQLException(error.substring(6), error.substring(0, 5)); | 2054 throw new SQLException(error.substring(6), error.substring(0, 5)); |
2051 } catch (SocketTimeoutException e) { | 2055 } catch (SocketTimeoutException e) { |
2052 close(); // JDBC 4.1 semantics, abort() | 2056 close(); // JDBC 4.1 semantics, abort() |
2964 * Change the reply size of the server. If the given | 2968 * Change the reply size of the server. If the given |
2965 * value is the same as the current value known to use, | 2969 * value is the same as the current value known to use, |
2966 * then ignore this call. If it is set to 0 we get a | 2970 * then ignore this call. If it is set to 0 we get a |
2967 * prompt after the server sent it's header. | 2971 * prompt after the server sent it's header. |
2968 */ | 2972 */ |
2969 int size = (cachesize == 0 ? defaultFetchSize : cachesize); | 2973 int size = cachesize; |
2974 if (size == 0) | |
2975 size = defaultFetchSize; | |
2970 if (maxrows > 0 && maxrows < size) | 2976 if (maxrows > 0 && maxrows < size) |
2971 size = (int)maxrows; | 2977 size = (int)maxrows; |
2972 // don't do work if it's not needed | 2978 // don't do work if it's not needed |
2973 if (lang == LANG_SQL && size != curReplySize && templ != commandTempl) { | 2979 if (lang == LANG_SQL && size != curReplySize && templ != commandTempl) { |
2974 sendControlCommand("reply_size " + size); | 2980 sendControlCommand("reply_size " + size); |
2977 curReplySize = size; | 2983 curReplySize = size; |
2978 } | 2984 } |
2979 // }}} set reply size | 2985 // }}} set reply size |
2980 | 2986 |
2981 // send query to the server | 2987 // send query to the server |
2982 out.writeLine( (templ[0] == null ? "" : templ[0]) + query + (templ[1] == null ? "" : templ[1]) ); | 2988 out.writeLine(templ[0] + query + templ[1]); |
2983 | 2989 |
2984 // go for new results | 2990 // go for new results |
2985 String tmpLine = in.readLine(); | 2991 String tmpLine = in.readLine(); |
2986 int linetype = in.getLineType(); | 2992 int linetype = in.getLineType(); |
2987 Response res = null; | 2993 Response res = null; |
3018 break; | 3024 break; |
3019 case StartOfHeaderParser.Q_SCHEMA: | 3025 case StartOfHeaderParser.Q_SCHEMA: |
3020 res = new SchemaResponse(); | 3026 res = new SchemaResponse(); |
3021 break; | 3027 break; |
3022 case StartOfHeaderParser.Q_TRANS: | 3028 case StartOfHeaderParser.Q_TRANS: |
3023 final boolean ac = sohp.getNextAsString().equals("t") ? true : false; | 3029 final boolean ac = sohp.getNextAsString().equals("t"); |
3024 if (autoCommit && ac) { | 3030 if (autoCommit && ac) { |
3025 addWarning("Server enabled auto commit mode " + | 3031 addWarning("Server enabled auto commit mode " + |
3026 "while local state already was auto commit.", "01M11"); | 3032 "while local state already was auto commit.", "01M11"); |
3027 } | 3033 } |
3028 autoCommit = ac; | 3034 autoCommit = ac; |
3032 // a new block of results for a response... | 3038 // a new block of results for a response... |
3033 final int id = sohp.getNextAsInt(); | 3039 final int id = sohp.getNextAsInt(); |
3034 sohp.getNextAsInt(); // columncount | 3040 sohp.getNextAsInt(); // columncount |
3035 final int rowcount = sohp.getNextAsInt(); | 3041 final int rowcount = sohp.getNextAsInt(); |
3036 final int offset = sohp.getNextAsInt(); | 3042 final int offset = sohp.getNextAsInt(); |
3037 final ResultSetResponse t = (rsresponses != null) ? rsresponses.get(Integer.valueOf(id)) : null; | 3043 final ResultSetResponse t; |
3044 if (rsresponses != null) | |
3045 t = rsresponses.get(Integer.valueOf(id)); | |
3046 else | |
3047 t = null; | |
3038 if (t == null) { | 3048 if (t == null) { |
3039 error = "M0M12!no ResultSetResponse with id " + id + " found"; | 3049 error = "M0M12!no ResultSetResponse with id " + id + " found"; |
3040 break; | 3050 break; |
3041 } | 3051 } |
3042 final DataBlockResponse r = new DataBlockResponse(rowcount, t.getRSType() == ResultSet.TYPE_FORWARD_ONLY); | 3052 final DataBlockResponse r = new DataBlockResponse(rowcount, t.getRSType() == ResultSet.TYPE_FORWARD_ONLY); |
3043 t.addDataBlockResponse(offset, r); | 3053 t.addDataBlockResponse(offset, r); |
3044 res = r; | 3054 res = r; |
3045 } break; | 3055 } break; |
3046 } // end of switch (sohp.parse(tmpLine)) | 3056 } // end of switch (sohp.parse(tmpLine)) |
3047 } catch (MCLParseException e) { | 3057 } catch (MCLParseException e) { |
3058 final int offset = e.getErrorOffset(); | |
3048 error = "M0M10!error while parsing start of header:\n" + | 3059 error = "M0M10!error while parsing start of header:\n" + |
3049 e.getMessage() + | 3060 e.getMessage() + |
3050 " found: '" + tmpLine.charAt(e.getErrorOffset()) + "'" + | 3061 " found: '" + tmpLine.charAt(offset) + |
3051 " in: \"" + tmpLine + "\"" + | 3062 "' in: \"" + tmpLine + |
3052 " at pos: " + e.getErrorOffset(); | 3063 "\" at pos: " + offset; |
3053 // flush all the rest | 3064 // flush all the rest |
3054 in.waitForPrompt(); | 3065 in.waitForPrompt(); |
3055 linetype = in.getLineType(); | 3066 linetype = in.getLineType(); |
3056 break; | 3067 break; |
3057 } | 3068 } |