changeset 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 1c8b9aec43a6
children 076abe23e8a3
files src/main/java/org/monetdb/jdbc/MonetConnection.java src/main/java/org/monetdb/jdbc/MonetResultSet.java src/main/java/org/monetdb/jdbc/MonetStatement.java
diffstat 3 files changed, 65 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/monetdb/jdbc/MonetConnection.java
+++ b/src/main/java/org/monetdb/jdbc/MonetConnection.java
@@ -1200,8 +1200,8 @@ public class MonetConnection
 	 */
 	@Override
 	public String toString() {
-		return "MonetDB Connection (" + getJDBCURL() + ") " +
-				(closed ? "disconnected" : "connected");
+		return "MonetDB Connection (" + getJDBCURL()
+			+ (closed ? ") disconnected" : ") connected");
 	}
 
 	//== Java 1.6 methods (JDBC 4.0)
@@ -1694,10 +1694,12 @@ public class MonetConnection
 		checkNotClosed();
 		Statement st = null;
 		try {
+			final String callstmt;
 			// as of release Jun2020 (11.37.7) the function sys.settimeout(bigint) is deprecated and replaced by new sys.setquerytimeout(int)
-			final boolean postJun2020 = (getDatabaseMajorVersion() >=11) && (getDatabaseMinorVersion() >= 37);
-			final String callstmt = postJun2020 ? "CALL sys.\"setquerytimeout\"(" + millis + ")"
-							    : "CALL sys.\"settimeout\"(" + millis + ")";
+			if ((getDatabaseMajorVersion() == 11) && (getDatabaseMinorVersion() < 37))
+				callstmt = "CALL sys.\"settimeout\"(" + millis + ")";
+			else
+				callstmt = "CALL sys.\"setquerytimeout\"(" + millis + ")";
 			// for debug: System.out.println("Before: " + callstmt);
 			st = createStatement();
 			st.execute(callstmt);
@@ -1867,8 +1869,8 @@ public class MonetConnection
 			if (env_monet_version != null) {
 				try {
 					// from version string such as 11.33.9 extract number: 11
-					final int start = env_monet_version.indexOf('.');
-					databaseMajorVersion = Integer.parseInt((start >= 0) ? env_monet_version.substring(0, start) : env_monet_version);
+					final int end = env_monet_version.indexOf('.');
+					databaseMajorVersion = Integer.parseInt((end >= 0) ? env_monet_version.substring(0, end) : env_monet_version);
 				} catch (NumberFormatException nfe) {
 					// ignore
 				}
@@ -2043,8 +2045,10 @@ public class MonetConnection
 	private void sendCommand(final String command, final boolean usequeryTempl) throws SQLException {
 		synchronized (server) {
 			try {
-				out.writeLine(usequeryTempl ? (queryTempl[0] + command + queryTempl[1])
-							: (commandTempl[0] + command + commandTempl[1]) );
+				if (usequeryTempl)
+					out.writeLine(queryTempl[0] + command + queryTempl[1]);
+				else
+					out.writeLine(commandTempl[0] + command + commandTempl[1]);
 				final String error = in.waitForPrompt();
 				if (error != null)
 					throw new SQLException(error.substring(6), error.substring(0, 5));
@@ -2966,7 +2970,9 @@ public class MonetConnection
 					 * then ignore this call.  If it is set to 0 we get a
 					 * prompt after the server sent it's header.
 					 */
-					int size = (cachesize == 0 ? defaultFetchSize : cachesize);
+					int size = cachesize;
+					if (size == 0)
+						size = defaultFetchSize;
 					if (maxrows > 0 && maxrows < size)
 						size = (int)maxrows;
 					// don't do work if it's not needed
@@ -2979,7 +2985,7 @@ public class MonetConnection
 					// }}} set reply size
 
 					// send query to the server
-					out.writeLine( (templ[0] == null ? "" : templ[0]) + query + (templ[1] == null ? "" : templ[1]) );
+					out.writeLine(templ[0] + query + templ[1]);
 
 					// go for new results
 					String tmpLine = in.readLine();
@@ -3020,7 +3026,7 @@ public class MonetConnection
 									res = new SchemaResponse();
 									break;
 								case StartOfHeaderParser.Q_TRANS:
-									final boolean ac = sohp.getNextAsString().equals("t") ? true : false;
+									final boolean ac = sohp.getNextAsString().equals("t");
 									if (autoCommit && ac) {
 										addWarning("Server enabled auto commit mode " +
 											"while local state already was auto commit.", "01M11");
@@ -3034,7 +3040,11 @@ public class MonetConnection
 									sohp.getNextAsInt();	// columncount
 									final int rowcount = sohp.getNextAsInt();
 									final int offset = sohp.getNextAsInt();
-									final ResultSetResponse t = (rsresponses != null) ? rsresponses.get(Integer.valueOf(id)) : null;
+									final ResultSetResponse t;
+									if (rsresponses != null)
+										t = rsresponses.get(Integer.valueOf(id));
+									else
+										t = null;
 									if (t == null) {
 										error = "M0M12!no ResultSetResponse with id " + id + " found";
 										break;
@@ -3045,11 +3055,12 @@ public class MonetConnection
 								} break;
 								} // end of switch (sohp.parse(tmpLine))
 							} catch (MCLParseException e) {
+								final int offset = e.getErrorOffset();
 								error = "M0M10!error while parsing start of header:\n" +
 									e.getMessage() +
-									" found: '" + tmpLine.charAt(e.getErrorOffset()) + "'" +
-									" in: \"" + tmpLine + "\"" +
-									" at pos: " + e.getErrorOffset();
+									" found: '" + tmpLine.charAt(offset) +
+									"' in: \"" + tmpLine +
+									"\" at pos: " + offset;
 								// flush all the rest
 								in.waitForPrompt();
 								linetype = in.getLineType();
--- a/src/main/java/org/monetdb/jdbc/MonetResultSet.java
+++ b/src/main/java/org/monetdb/jdbc/MonetResultSet.java
@@ -263,7 +263,10 @@ public class MonetResultSet
 		// store it
 		curRow = row;
 
-		final String tmpLine = (header != null) ? header.getLine(row - 1) : null;
+		if (header == null)
+			return false;
+
+		final String tmpLine = header.getLine(row - 1);
 		if (tmpLine == null)
 			return false;
 
@@ -434,7 +437,7 @@ public class MonetResultSet
 						return null;
 					return new java.io.ByteArrayInputStream(bte);
 			}
-			throw new SQLException("Cannot operate on " + types[columnIndex - 1] + " type", "M1M05");
+			throw new SQLException("Cannot operate on type: " + types[columnIndex - 1], "M1M05");
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -904,7 +907,7 @@ public class MonetResultSet
 				case Types.LONGVARBINARY:
 					return MonetBlob.hexStrToByteArray(val);
 				default:
-					throw new SQLException("Cannot operate on " + types[columnIndex - 1] + " type", "M1M05");
+					throw new SQLException("Cannot operate on type: " + types[columnIndex - 1], "M1M05");
 			}
 		} catch (NumberFormatException e) {
 			throw newSQLNumberFormatException(e);
@@ -972,7 +975,7 @@ public class MonetResultSet
 	@Override
 	public String getCursorName() throws SQLException {
 		throw new SQLException("Positioned updates not supported for this cursor ("
-				+ (header != null ? header.id : "") + ")", "0AM21");
+				+ (header != null ? header.id + ")" : ")"), "0AM21");
 	}
 
 	/**
@@ -1052,7 +1055,7 @@ public class MonetResultSet
 			break;
 		case ResultSet.FETCH_REVERSE:
 		case ResultSet.FETCH_UNKNOWN:
-			throw new SQLException("Not supported direction " + direction, "0A000");
+			throw new SQLException("Not supported direction: " + direction, "0A000");
 		default:
 			throw new SQLException("Illegal direction: " + direction, "M1M05");
 		}
@@ -2818,19 +2821,18 @@ public class MonetResultSet
 		}
 		if (pdate == null) {
 			// parsing failed
-			final String errMsg;
+			final StringBuilder errMsg = new StringBuilder(128);
 			final int epos = ppos.getErrorIndex();
 			if (epos == -1) {
-				errMsg = "parsing '" + monetDateStr + "' failed";
+				errMsg.append("parsing '").append(monetDateStr).append("' failed");
 			} else if (epos < monetDate.length()) {
-				errMsg = "parsing failed," +
-					 " found: '" + monetDate.charAt(epos) + "'" +
-					 " in: \"" + monetDateStr + "\"" +
-					 " at pos: " + (epos + (negativeYear ? 2 : 1));
+				errMsg.append("parsing failed at pos ").append(epos + (negativeYear ? 2 : 1))
+					.append(" found: '").append(monetDate.charAt(epos))
+					.append("' in '").append(monetDateStr).append("'");
 			} else {
-				errMsg = "parsing failed, expected more data after '" +	monetDateStr + "'";
+				errMsg.append("parsing failed, expected more data after '").append(monetDateStr).append("'");
 			}
-			throw new SQLException(errMsg, "01M10");
+			throw new SQLException(errMsg.toString(), "01M10");
 		}
 
 		cal.setTime(pdate);
@@ -2871,10 +2873,11 @@ public class MonetResultSet
 					while (ctr++ < 9)
 						nanos *= 10;
 				} catch (MCLParseException e) {
+					final int offset = e.getErrorOffset();
 					addWarning(e.getMessage() +
-							" found: '" + monDate[e.getErrorOffset()] + "'" +
-							" in: \"" + monetDate + "\"" +
-							" at pos: " + e.getErrorOffset(), "01M10");
+							" found: '" + monDate[offset] +
+							"' in: \"" + monetDate +
+							"\" at pos: " + offset, "01M10");
 					// default value
 					nanos = 0;
 				}
@@ -2957,8 +2960,9 @@ public class MonetResultSet
 				}
 				cal = Calendar.getInstance();
 			}
-			final int ret = getJavaDate(cal, columnIndex, Types.DATE);
-			return ret == -1 ? null : new java.sql.Date(cal.getTimeInMillis());
+			if (getJavaDate(cal, columnIndex, Types.DATE) == -1)
+				return null;
+			return new java.sql.Date(cal.getTimeInMillis());
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -3046,8 +3050,9 @@ public class MonetResultSet
 				}
 				cal = Calendar.getInstance();
 			}
-			final int ret = getJavaDate(cal, columnIndex, Types.TIME);
-			return ret == -1 ? null : new Time(cal.getTimeInMillis());
+			if (getJavaDate(cal, columnIndex, Types.TIME) == -1)
+				return null;
+			return new Time(cal.getTimeInMillis());
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
--- a/src/main/java/org/monetdb/jdbc/MonetStatement.java
+++ b/src/main/java/org/monetdb/jdbc/MonetStatement.java
@@ -206,7 +206,10 @@ public class MonetStatement
 		// copy contents of long[] into new int[]
 		final int[] counts = new int[ret.length];
 		for (int i = 0; i < ret.length; i++) {
-			counts[i] = (ret[i] >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)ret[i];
+			if (ret[i] >= Integer.MAX_VALUE)
+				counts[i] = Integer.MAX_VALUE;
+			else
+				counts[i] = (int)ret[i];
 		}
 		return counts;
 	}
@@ -776,10 +779,9 @@ public class MonetStatement
 	 */
 	@Override
 	public ResultSet getResultSet() throws SQLException {
-		return (header != null && header instanceof MonetConnection.ResultSetResponse)
-			? new MonetResultSet(this,
-					(MonetConnection.ResultSetResponse)header)
-			: null;
+		if (header != null && header instanceof MonetConnection.ResultSetResponse)
+			return new MonetResultSet(this, (MonetConnection.ResultSetResponse)header);
+		return null;
 	}
 
 	/**
@@ -1111,17 +1113,16 @@ public class MonetStatement
 	 * @throws SQLException if a database access error occurs or this
 	 *	method is called on a closed Statement
 	 */
+	@Override
 	public long getLargeUpdateCount() throws SQLException {
-		long ret = -1;
 		if (header != null) {
 			if (header instanceof MonetConnection.UpdateResponse) {
-				ret = ((MonetConnection.UpdateResponse)header).count;
+				return ((MonetConnection.UpdateResponse)header).count;
 			} else if (header instanceof MonetConnection.SchemaResponse) {
-				ret = ((MonetConnection.SchemaResponse)header).state;
+				return ((MonetConnection.SchemaResponse)header).state;
 			}
 		}
-
-		return ret;
+		return -1;
 	}
 
 	/**
@@ -1570,9 +1571,7 @@ final class MonetVirtualResultSet extend
 	 */
 	@Override
 	public void close() {
-		if (!closed) {
-			closed = true;
-			// types and columns are MonetResultSets private parts
-		}
+		closed = true;
+		// types and columns are MonetResultSets private parts
 	}
 }