Skip to content

Commit

Permalink
Fix for Bug#36936407, PrepareCall method doesn't work as expected whe…
Browse files Browse the repository at this point in the history
…n DB name is involved.

Change-Id: I63896d891cda5a2315b4ae3e936192a7df8899a9
  • Loading branch information
Axyoan Marcelo committed Sep 2, 2024
1 parent 2276e81 commit db834bf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Version 9.1.0

- Fix for Bug#36936407, PrepareCall method doesn't work as expected when DB name is involved.

- WL#16490, OpenID Connect authentication support.

- Fix for Bug#112790 (Bug#35936477), Statement.getGeneratedKeys() returns unexpected value.
Expand Down
11 changes: 8 additions & 3 deletions src/main/user-impl/java/com/mysql/cj/jdbc/CallableStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import com.mysql.cj.telemetry.TelemetrySpan;
import com.mysql.cj.telemetry.TelemetrySpanName;
import com.mysql.cj.util.SearchMode;
import com.mysql.cj.util.StringInspector;
import com.mysql.cj.util.StringUtils;
import com.mysql.cj.util.Util;

Expand Down Expand Up @@ -540,9 +541,13 @@ private void generateParameterMap() throws SQLException {
if ("?".equals(param)) {
questionMarkCount = 1;
} else {
questionMarkCount = StringUtils
.stripCommentsAndHints(param, "'\"", "'\"", !this.session.getServerSession().isNoBackslashEscapesSet()).codePoints()
.filter(c -> c == '?').count();
StringInspector strInspector = new StringInspector(param, "'\"", "'\"", "",
this.session.getServerSession().isNoBackslashEscapesSet() ? SearchMode.__MRK_COM_MYM_HNT_WS
: SearchMode.__BSE_MRK_COM_MYM_HNT_WS);
while (strInspector.indexOfIgnoreCase("?") >= 0) {
questionMarkCount++;
strInspector.incrementPosition();
}
}
for (int j = 0; j < questionMarkCount; j++) {
this.placeholderToParameterIndexMap[placeholderCount++] = startIndex + i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

package testsuite.regression;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -47,6 +49,7 @@
import com.mysql.cj.conf.PropertyKey;
import com.mysql.cj.exceptions.MysqlErrorNumbers;
import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.util.StringUtils;

import testsuite.BaseTestCase;

Expand Down Expand Up @@ -1869,4 +1872,38 @@ public void testBug111107() throws Exception {
}
}

/**
* Tests fix for Bug#36936407, PrepareCall method doesn't work as expected when DB name is involved.
*
* @throws Exception
*/
@Test
public void testBug36936407() throws Exception {
assumeTrue(!StringUtils.isNullOrEmpty(this.conn.getCatalog()) || !StringUtils.isNullOrEmpty(this.conn.getSchema()),
"Test URL must specify a database.");

createProcedure("testBug36936407", "(IN a VARCHAR(10)) BEGIN SELECT a; END");
final String database = !StringUtils.isNullOrEmpty(this.conn.getCatalog()) ? this.conn.getCatalog() : this.conn.getSchema();

/*
* Case 1: Database specified in the URL (default test URL should specify a database).
*/
try (Connection testConn = getConnectionWithProps("")) {
assertDoesNotThrow(() -> testConn.prepareCall("{ CALL " + database + ".testBug36936407(\"MySQL?\") }").execute());

assertDoesNotThrow(() -> testConn.prepareCall("{ CALL testBug36936407(\"MySQL?\") }").execute());
}

/*
* Case 2: Database not specified in the URL.
*/
try (Connection testConn = getConnectionWithProps(getNoDbUrl(dbUrl), "")) {
assertDoesNotThrow(() -> testConn.prepareCall("{ CALL " + database + ".testBug36936407(\"MySQL?\") }").execute());

assertThrows(SQLException.class, () -> testConn.prepareCall("{ CALL testBug36936407(\"MySQL?\") }").execute());
testConn.createStatement().execute("USE " + database); // Not recommended by the JDBC, but the test report relies on it.
assertDoesNotThrow(() -> testConn.prepareCall("{ CALL testBug36936407(\"MySQL?\") }").execute());
}
}

}

0 comments on commit db834bf

Please sign in to comment.