diff src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java @ 295:003ae6d881db

Add "final" keyword to method arguments and local variables where possible. It discovered some bugs in the MonetStatement constructor (changed the argument instead of object variable) which are fixed now. See also https://en.wikipedia.org/wiki/Final_(Java)
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 01 Aug 2019 20:18:43 +0200 (2019-08-01)
parents b127164342c4
children d479475888e3
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
@@ -17,7 +17,6 @@ import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
 import java.sql.SQLWarning;
 import java.util.ArrayList;
-import java.util.List;
 import java.util.concurrent.locks.ReentrantLock;
 
 /**
@@ -95,10 +94,10 @@ public class MonetStatement
 	 * @throws IllegalArgumentException is one of the arguments null or empty
 	 */
 	MonetStatement(
-		MonetConnection connection,
-		int resultSetType,
-		int resultSetConcurrency,
-		int resultSetHoldability)
+		final MonetConnection connection,
+		final int resultSetType,
+		final int resultSetConcurrency,
+		final int resultSetHoldability)
 		throws SQLException, IllegalArgumentException
 	{
 		if (connection == null)
@@ -111,15 +110,15 @@ public class MonetStatement
 		this.resultSetConcurrency = resultSetConcurrency;
 
 		// check our limits, and generate warnings as appropriate
-		if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY) {
+		if (this.resultSetConcurrency != ResultSet.CONCUR_READ_ONLY) {
 			addWarning("No concurrency mode other then read only is supported, continuing with concurrency level READ_ONLY", "01M13");
-			resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+			this.resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
 		}
 
 		// check type for supported mode
-		if (resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE) {
+		if (this.resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE) {
 			addWarning("Change sensitive scrolling ResultSet objects are not supported, continuing with a change non-sensitive scrollable cursor.", "01M14");
-			resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
+			this.resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
 		}
 
 		// check type for supported holdability
@@ -139,7 +138,7 @@ public class MonetStatement
 	 * @throws SQLException so the PreparedStatement can throw this exception
 	 */
 	@Override
-	public void addBatch(String sql) throws SQLException {
+	public void addBatch(final String sql) throws SQLException {
 		if (batch == null) {
 			// create the ArrayList at first time use
 			batch = new ArrayList<String>();
@@ -208,14 +207,14 @@ public class MonetStatement
 		}
 		batchLock.lock();
 		try {
-			int[] counts = new int[batch.size()];
+			final int[] counts = new int[batch.size()];
+			final String sep = connection.queryTempl[2];
+			final int sepLen = sep.length();
+			final BatchUpdateException e = new BatchUpdateException("Error(s) occurred while executing the batch, see next SQLExceptions for details", "22000", counts);
+			final StringBuilder tmpBatch = new StringBuilder(MapiSocket.BLOCK);
 			int offset = 0;
 			boolean first = true;
 			boolean error = false;
-			String sep = connection.queryTempl[2];
-			int sepLen = sep.length();
-			BatchUpdateException e = new BatchUpdateException("Error(s) occurred while executing the batch, see next SQLExceptions for details", "22000", counts);
-			StringBuilder tmpBatch = new StringBuilder(MapiSocket.BLOCK);
 
 			for (int i = 0; i < batch.size(); i++) {
 				String tmp = batch.get(i);
@@ -260,18 +259,20 @@ public class MonetStatement
 	}
 
 	private boolean internalBatch(
-			StringBuilder batch,
-			int[] counts,
+			final StringBuilder batch,
+			final int[] counts,
 			int offset,
-			int max,
-			BatchUpdateException e)
+			final int max,
+			final BatchUpdateException e)
 		throws BatchUpdateException
 	{
 		try {
 			boolean type = internalExecute(batch.toString());
 			int count = -1;
+
 			if (!type)
 				count = getUpdateCount();
+
 			do {
 				if (offset >= max)
 					throw new SQLException("Overflow: don't use multi statements when batching (" + max + ")", "M1M16");
@@ -362,7 +363,7 @@ public class MonetStatement
 	 * @throws SQLException if a database access error occurs
 	 */
 	@Override
-	public boolean execute(String sql) throws SQLException {
+	public boolean execute(final String sql) throws SQLException {
 		return internalExecute(sql);
 	}
 
@@ -397,7 +398,7 @@ public class MonetStatement
 	 *         Statement.NO_GENERATED_KEYS.
 	 */
 	@Override
-	public boolean execute(String sql, int autoGeneratedKeys)
+	public boolean execute(final String sql, final int autoGeneratedKeys)
 		throws SQLException
 	{
 		if (autoGeneratedKeys != Statement.RETURN_GENERATED_KEYS &&
@@ -445,7 +446,7 @@ public class MonetStatement
 	 *         valid column indexes
 	 */
 	@Override
-	public boolean execute(String sql, int[] columnIndexes)
+	public boolean execute(final String sql, final int[] columnIndexes)
 		throws SQLException
 	{
 		addWarning("execute: generated keys for fixed set of columns not supported", "01M18");
@@ -489,7 +490,7 @@ public class MonetStatement
 	 *         not valid column names
 	 */
 	@Override
-	public boolean execute(String sql, String[] columnNames)
+	public boolean execute(final String sql, final String[] columnNames)
 		throws SQLException
 	{
 		addWarning("execute: generated keys for fixed set of columns not supported", "01M18");
@@ -509,7 +510,7 @@ public class MonetStatement
 	 *         it is an update count or there are no results
 	 * @throws SQLException if a database access error occurs
 	 */
-	private boolean internalExecute(String sql) throws SQLException {
+	private boolean internalExecute(final String sql) throws SQLException {
 		// close previous query, if not closed already
 		if (lastResponseList != null) {
 			lastResponseList.close();
@@ -521,7 +522,7 @@ public class MonetStatement
 			Statement st = null;
 			try {
 				st = connection.createStatement();
-				String callstmt = "CALL \"sys\".\"settimeout\"(" + queryTimeout + ")";
+				final String callstmt = "CALL \"sys\".\"settimeout\"(" + queryTimeout + ")";
 				// for debug: System.out.println("Before: " + callstmt);
 				st.execute(callstmt);
 				// for debug: System.out.println("After : " + callstmt);
@@ -562,7 +563,7 @@ public class MonetStatement
 	 *         statement produces anything other than a single ResultSet object
 	 */
 	@Override
-	public ResultSet executeQuery(String sql) throws SQLException {
+	public ResultSet executeQuery(final String sql) throws SQLException {
 		if (execute(sql) != true)
 			throw new SQLException("Query did not produce a result set", "M1M19");
 
@@ -582,7 +583,7 @@ public class MonetStatement
 	 *         statement produces a ResultSet object
 	 */
 	@Override
-	public int executeUpdate(String sql) throws SQLException {
+	public int executeUpdate(final String sql) throws SQLException {
 		if (execute(sql) != false)
 			throw new SQLException("Query produced a result set", "M1M17");
 
@@ -608,7 +609,7 @@ public class MonetStatement
 	 *         given constant is not one of those allowed
 	 */
 	@Override
-	public int executeUpdate(String sql, int autoGeneratedKeys)
+	public int executeUpdate(final String sql, final int autoGeneratedKeys)
 		throws SQLException
 	{
 		if (autoGeneratedKeys != Statement.RETURN_GENERATED_KEYS &&
@@ -646,7 +647,7 @@ public class MonetStatement
 	 *         whose elements are valid column indexes
 	 */
 	@Override
-	public int executeUpdate(String sql, int[] columnIndexes)
+	public int executeUpdate(final String sql, final int[] columnIndexes)
 		throws SQLException
 	{
 		addWarning("executeUpdate: generated keys for fixed set of columns not supported", "01M18");
@@ -677,7 +678,7 @@ public class MonetStatement
 	 *         whose elements are valid column names
 	 */
 	@Override
-	public int executeUpdate(String sql, String[] columnNames)
+	public int executeUpdate(final String sql, final String[] columnNames)
 		throws SQLException
 	{
 		addWarning("executeUpdate: generated keys for fixed set of columns not supported", "01M18");
@@ -734,8 +735,8 @@ public class MonetStatement
 	 */
 	@Override
 	public ResultSet getGeneratedKeys() throws SQLException {
-		String[] columns, types;
-		String[][] results;
+		final String[] columns, types;
+		final String[][] results;
 
 		columns = new String[1];
 		types = new String[1];
@@ -746,7 +747,7 @@ public class MonetStatement
 		types[0] = "BIGINT";
 
 		if (header != null && header instanceof MonetConnection.UpdateResponse) {
-			String lastid = ((MonetConnection.UpdateResponse)header).lastid;
+			final String lastid = ((MonetConnection.UpdateResponse)header).lastid;
 			if (lastid.equals("-1")) {
 				results = new String[0][1];
 			} else {
@@ -833,7 +834,7 @@ public class MonetStatement
 	 * @throws SQLException if a database access error occurs
 	 */
 	@Override
-	public boolean getMoreResults(int current) throws SQLException {
+	public boolean getMoreResults(final int current) throws SQLException {
 		// protect against people calling this on an unitialised state
 		if (lastResponseList == null) {
 			header = null;
@@ -990,7 +991,7 @@ public class MonetStatement
 	 * @throws SQLException if a database access error occurs
 	 */
 	@Override
-	public void setCursorName(String name) throws SQLException {
+	public void setCursorName(final String name) throws SQLException {
 		addWarning("setCursorName: positioned updates/deletes not supported", "01M21");
 	}
 
@@ -1012,7 +1013,7 @@ public class MonetStatement
 	 * @throws SQLException if a database access error occurs
 	 */
 	@Override
-	public void setEscapeProcessing(boolean enable) throws SQLException {
+	public void setEscapeProcessing(final boolean enable) throws SQLException {
 		if (enable)
 			addWarning("setEscapeProcessing: JDBC escape syntax is not supported by this driver", "01M22");
 	}
@@ -1032,7 +1033,7 @@ public class MonetStatement
 	 *         ResultSet.FETCH_REVERSE, or ResultSet.FETCH_UNKNOWN
 	 */
 	@Override
-	public void setFetchDirection(int direction) throws SQLException {
+	public void setFetchDirection(final int direction) throws SQLException {
 		if (direction == ResultSet.FETCH_FORWARD ||
 		    direction == ResultSet.FETCH_REVERSE ||
 		    direction == ResultSet.FETCH_UNKNOWN)
@@ -1054,7 +1055,7 @@ public class MonetStatement
 	 *         is not satisfied.
 	 */
 	@Override
-	public void setFetchSize(int rows) throws SQLException {
+	public void setFetchSize(final int rows) throws SQLException {
 		if (rows >= 0 && !(getMaxRows() != 0 && rows > getMaxRows())) {
 			fetchSize = rows;
 		} else {
@@ -1081,7 +1082,7 @@ public class MonetStatement
 	 * @see #getMaxFieldSize()
 	 */
 	@Override
-	public void setMaxFieldSize(int max) throws SQLException {
+	public void setMaxFieldSize(final int max) throws SQLException {
 		if (max < 0)
 			throw new SQLException("Illegal max value: " + max, "M1M05");
 		if (max > 0)
@@ -1098,7 +1099,7 @@ public class MonetStatement
 	 * @see #getMaxRows()
 	 */
 	@Override
-	public void setMaxRows(int max) throws SQLException {
+	public void setMaxRows(final int max) throws SQLException {
 		if (max < 0)
 			throw new SQLException("Illegal max value: " + max, "M1M05");
 		maxRows = max;
@@ -1115,7 +1116,7 @@ public class MonetStatement
 	 *         condition seconds &gt;= 0 is not satisfied
 	 */
 	@Override
-	public void setQueryTimeout(int seconds) throws SQLException {
+	public void setQueryTimeout(final int seconds) throws SQLException {
 		if (seconds < 0)
 			throw new SQLException("Illegal timeout value: " + seconds, "M1M05");
 		queryTimeout = seconds;
@@ -1155,7 +1156,7 @@ public class MonetStatement
 	 *        and that the statement not be pooled if false
 	 */
 	@Override
-	public void setPoolable(boolean poolable) {
+	public void setPoolable(final boolean poolable) {
 		this.poolable = poolable;
 	}
 
@@ -1211,7 +1212,7 @@ public class MonetStatement
 	 *
 	 * @param reason the warning message
 	 */
-	private void addWarning(String reason, String sqlstate) {
+	private void addWarning(final String reason, final String sqlstate) {
 		SQLWarning warng = new SQLWarning(reason, sqlstate);
 		if (warnings == null) {
 			warnings = warng;
@@ -1252,10 +1253,10 @@ final class MonetVirtualResultSet extend
 	private boolean closed;
 
 	MonetVirtualResultSet(
-		Statement statement,
-		String[] columns,
-		String[] types,
-		String[][] results
+		final Statement statement,
+		final String[] columns,
+		final String[] types,
+		final String[][] results
 	) throws IllegalArgumentException {
 		super(statement, columns, types, results.length);
 		this.results = results;