Skip to content

Commit

Permalink
Optimised internal method getParamIdx(). Instead of going through a l…
Browse files Browse the repository at this point in the history
…oop each time (for all result columns and parameters), we now calculate the internal array index value directly, which is much faster.

Also moved the getParamIdx() method to the end where all other non-JDBC API methods are listed.
  • Loading branch information
mvdvm committed Feb 22, 2024
1 parent 0af46ed commit 2e1ba1a
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/main/java/org/monetdb/jdbc/MonetPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
*
* @author Fabian Groffen
* @author Martin van Dinther
* @version 0.7
* @version 0.8
*/
public class MonetPreparedStatement
extends MonetStatement
Expand All @@ -83,7 +83,8 @@ public class MonetPreparedStatement
private final int id;
private final int size;

private int paramCount = 0;
private final int paramCount;
private final int paramStartIndex;
private final String[] paramValues;

/** A cache to reduce the number of ResultSetMetaData objects created
Expand Down Expand Up @@ -139,7 +140,7 @@ public class MonetPreparedStatement

/**
* For a PREPARE statement the server sends back a result set
* with info on all the parameters and/or result columns of a
* with info on all the result columns and parameters of a
* parameterized query. This result set however needs to be
* read in one DataBlockResponse due to protocol limitations.
* This requires the fetchSize needs to be set large enough
Expand All @@ -162,6 +163,9 @@ public class MonetPreparedStatement
id = ((MonetConnection.ResultSetResponse)header).id;
size = (int)((MonetConnection.ResultSetResponse)header).tuplecount;

int countParam = 0;
int firstParamOffset = 0;

// initialise blank finals
monetdbType = new String[size];
javaType = new int[size];
Expand Down Expand Up @@ -199,19 +203,29 @@ public class MonetPreparedStatement
column[i] = rs.getString(column_colnr);
// System.out.println("column " + i + " has value: " + column[i]);
/* when column[i] != null it is a result column of the prepared query,
when column[i] == null it is a parameter for the prepared statement, see getParamIdx(int). */
if (column[i] == null)
paramCount++;
* when column[i] == null it is a parameter for the prepared statement.
* Note that we always get the result columns (if any) first and
* next the parameters (if any) in the columns[].
*/
if (column[i] == null) {
countParam++;
if (countParam == 1)
firstParamOffset = i; // remember where the first parameter is stored
}
}
rs.close();
}
paramCount = countParam;
paramStartIndex = firstParamOffset;
// System.out.println("paramCount= " + paramCount + " paramStartIndex= " + paramStartIndex + "\n");

paramValues = new String[paramCount + 1]; // parameters start from 1

// PreparedStatements are by default poolable
poolable = true;
}


//== methods interface PreparedStatement

/**
Expand Down Expand Up @@ -322,27 +336,6 @@ public int executeUpdate(final String q) throws SQLException {
throw new SQLException("This method is not available in a PreparedStatement!", "M1M05");
}

/**
* Returns the index (0..size-1) in the backing arrays for the given
* parameter number or an SQLException when not found
*
* @param paramnr the parameter number
* @return the internal column array index number
* @throws SQLException if parameter number can not be found in the internal array
*/
private final int getParamIdx(final int paramnr) throws SQLException {
int curparam = 0;
for (int i = 0; i < size; i++) {
/* when column[i] == null it is a parameter, when column[i] != null it is a result column of the prepared query */
if (column[i] != null)
continue;
curparam++;
if (curparam == paramnr)
return i;
}
throw new SQLException("No such parameter with index: " + paramnr, "M1M05");
}

/**
* Retrieves a ResultSetMetaData object that contains information
* about the columns of the ResultSet object that will be returned
Expand Down Expand Up @@ -2304,6 +2297,21 @@ public long executeLargeUpdate() throws SQLException {

//== internal helper methods which do not belong to the JDBC interface

/**
* Returns the index (0..size-1) in the backing arrays for the given
* parameter number or an SQLException when not valid
*
* @param paramnr the parameter number
* @return the internal column array index number
* @throws SQLException if parameter number is out of bounds
*/
private final int getParamIdx(final int paramnr) throws SQLException {
if (paramnr < 1 || paramnr > paramCount || (paramnr + paramStartIndex > size))
throw new SQLException("No parameter with index: " + paramnr, "M1M05");

return paramnr + paramStartIndex -1;
}

/**
* Sets the given index with the supplied value. If the given index is
* out of bounds, and SQLException is thrown. The given value should
Expand All @@ -2315,7 +2323,7 @@ public long executeLargeUpdate() throws SQLException {
*/
private final void setValue(final int parameterIndex, final String val) throws SQLException {
if (parameterIndex < 1 || parameterIndex > paramCount)
throw new SQLException("No such parameter with index: " + parameterIndex, "M1M05");
throw new SQLException("No parameter with index: " + parameterIndex, "M1M05");

if (val != null)
paramValues[parameterIndex] = val;
Expand Down

0 comments on commit 2e1ba1a

Please sign in to comment.