Skip to content

Commit

Permalink
[CONJ-494] Handle PrepareStatement.getParameterMetaData() if query ca…
Browse files Browse the repository at this point in the history
…nnot be PREPAREd
  • Loading branch information
rusher committed Jun 15, 2017
1 parent c60cb78 commit 11897e6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/mariadb/jdbc/MariaDbParameterMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,20 @@ public MariaDbParameterMetaData(ColumnInformation[] parametersInformation) {
this.parametersInformation = parametersInformation;
}

private void checkAvailable() throws SQLException {
if(this.parametersInformation == null) {
throw new SQLException("Parameter metadata not available for these statement", "S1C00");
}
}

@Override
public int getParameterCount() throws SQLException {
checkAvailable();
return parametersInformation.length;
}

private ColumnInformation getParameterInformation(int param) throws SQLException {
checkAvailable();
if (param >= 1 && param <= parametersInformation.length) {
return parametersInformation[param - 1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,14 @@ public ParameterMetaData getParameterMetaData() throws SQLException {
}

private void setParametersData() throws SQLException {
MariaDbPreparedStatementServer ssps = new MariaDbPreparedStatementServer(connection, this.sqlQuery,
ResultSet.TYPE_SCROLL_INSENSITIVE, true);
resultSetMetaData = ssps.getMetaData();
parameterMetaData = ssps.getParameterMetaData();
ssps.close();
try (MariaDbPreparedStatementServer ssps = new MariaDbPreparedStatementServer(connection, this.sqlQuery,
ResultSet.TYPE_SCROLL_INSENSITIVE, true)) {
resultSetMetaData = ssps.getMetaData();
parameterMetaData = ssps.getParameterMetaData();
} catch (SQLException sqle) {
//if statement cannot be prepared
parameterMetaData = new MariaDbParameterMetaData(null);
}
}


Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/mariadb/jdbc/DriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,38 @@ public void preparedTest() throws SQLException {
assertEquals(2, prepStmt.getParameterMetaData().getParameterCount());
}

@Test
public void parameterMetaDataNotPreparable() throws SQLException {
Assume.assumeFalse(sharedUsePrepare());
Statement stmt = sharedConnection.createStatement();
ResultSet rs = stmt.executeQuery("SHOW SESSION STATUS WHERE Variable_name in ('Prepared_stmt_count','Com_stmt_prepare', 'Com_stmt_close')");
rs.next();
int preparedStmtCount = rs.getInt(2);
rs.next();
int comStmtPrepare = rs.getInt(2);
rs.next();
int comStmtClose = rs.getInt(2);

//statement that cannot be prepared
try (PreparedStatement pstmt = sharedConnection.prepareStatement(
"select TMP.field1 from (select ? from dual) TMP")) {
ParameterMetaData parameterMetaData = pstmt.getParameterMetaData();
try {
parameterMetaData.getParameterCount();
fail();
} catch (SQLException sqle) {
assertEquals("S1C00", sqle.getSQLState());
}
rs = stmt.executeQuery("SHOW SESSION STATUS WHERE Variable_name in ('Prepared_stmt_count','Com_stmt_prepare', 'Com_stmt_close')");
rs.next();
assertEquals(preparedStmtCount, rs.getInt(2));
rs.next();
assertEquals(comStmtPrepare + 1, rs.getInt(2));
rs.next();
assertEquals(comStmtClose, rs.getInt(2));
}
}


@Test
public void streamingResultSet() throws Exception {
Expand Down

0 comments on commit 11897e6

Please sign in to comment.