Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-1127] wrong Resultset.getResultSetType() for metadata'es result…
…set and Resultset from generated keys
  • Loading branch information
rusher committed Nov 22, 2023
1 parent 0acd7f2 commit fb673c2
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 23 deletions.
Expand Up @@ -292,7 +292,7 @@ public ResultSet executeQuery() throws SQLException {
if (currResult instanceof Result) {
return (Result) currResult;
}
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext());
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType);
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/org/mariadb/jdbc/DatabaseMetaData.java
Expand Up @@ -337,7 +337,12 @@ private ResultSet getImportedKeys(
return result;
});
return CompleteResult.createResultSet(
columnNames, dataTypes, arr, connection.getContext(), ColumnFlags.PRIMARY_KEY);
columnNames,
dataTypes,
arr,
connection.getContext(),
ColumnFlags.PRIMARY_KEY,
ResultSet.TYPE_SCROLL_INSENSITIVE);
}

private Map<String[], String> getExtImportedKeys(
Expand Down Expand Up @@ -547,7 +552,8 @@ private String dataTypeClause(String fullTypeColumnName) {
}

private ResultSet executeQuery(String sql) throws SQLException {
Statement stmt = connection.createStatement();
Statement stmt =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
CompleteResult rs = (CompleteResult) stmt.executeQuery(sql);
rs.setStatement(null); // bypass Hibernate statement tracking (CONJ-49)
rs.useAliasAsName();
Expand Down Expand Up @@ -3406,7 +3412,13 @@ public ResultSet getTypeInfo() {
}
};

return CompleteResult.createResultSet(columnNames, dataTypes, data, connection.getContext(), 0);
return CompleteResult.createResultSet(
columnNames,
dataTypes,
data,
connection.getContext(),
0,
ResultSet.TYPE_SCROLL_INSENSITIVE);
}

/**
Expand Down Expand Up @@ -3920,7 +3932,8 @@ public ResultSet getClientInfoProperties() {
}
};

return CompleteResult.createResultSet(columnNames, types, data, connection.getContext(), 0);
return CompleteResult.createResultSet(
columnNames, types, data, connection.getContext(), 0, ResultSet.TYPE_SCROLL_INSENSITIVE);
}

/**
Expand Down
Expand Up @@ -439,7 +439,7 @@ public ResultSet executeQuery() throws SQLException {
currResult = results.remove(0);
if (currResult instanceof Result) return (Result) currResult;
}
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext());
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/mariadb/jdbc/Statement.java
Expand Up @@ -161,7 +161,7 @@ public ResultSet executeQuery(String sql) throws SQLException {
executeInternal(sql, Statement.NO_GENERATED_KEYS);
currResult = results.remove(0);
if (currResult instanceof Result) return (Result) currResult;
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext());
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType);
}

/**
Expand Down Expand Up @@ -906,7 +906,8 @@ public ResultSet getGeneratedKeys() throws SQLException {
}
}
if (insertIds.isEmpty()) {
return new CompleteResult(new ColumnDecoder[0], new byte[0][], con.getContext());
return new CompleteResult(
new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType);
}

String[][] ids = insertIds.toArray(new String[0][]);
Expand All @@ -915,7 +916,8 @@ public ResultSet getGeneratedKeys() throws SQLException {
DataType.BIGINT,
ids,
con.getContext(),
ColumnFlags.AUTO_INCREMENT | ColumnFlags.UNSIGNED);
ColumnFlags.AUTO_INCREMENT | ColumnFlags.UNSIGNED,
resultSetType);
}

/**
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/org/mariadb/jdbc/client/result/CompleteResult.java
Expand Up @@ -109,9 +109,11 @@ private CompleteResult(ColumnDecoder[] metadataList, CompleteResult prev) {
* @param metadataList metadata
* @param data result-set data
* @param context connection context
* @param resultSetType result set type
*/
public CompleteResult(ColumnDecoder[] metadataList, byte[][] data, Context context) {
super(metadataList, data, context);
public CompleteResult(
ColumnDecoder[] metadataList, byte[][] data, Context context, int resultSetType) {
super(metadataList, data, context, resultSetType);
}

/**
Expand All @@ -122,12 +124,23 @@ public CompleteResult(ColumnDecoder[] metadataList, byte[][] data, Context conte
* @param data values
* @param context connection context
* @param flags column flags
* @param resultSetType result set type
* @return result-set
*/
public static ResultSet createResultSet(
String columnName, DataType columnType, String[][] data, Context context, int flags) {
String columnName,
DataType columnType,
String[][] data,
Context context,
int flags,
int resultSetType) {
return createResultSet(
new String[] {columnName}, new DataType[] {columnType}, data, context, flags);
new String[] {columnName},
new DataType[] {columnType},
data,
context,
flags,
resultSetType);
}

/**
Expand All @@ -141,10 +154,16 @@ public static ResultSet createResultSet(
* values that are represented as "1" or "0" strings
* @param context connection context
* @param flags column flags
* @param resultSetType result set type
* @return resultset
*/
public static ResultSet createResultSet(
String[] columnNames, DataType[] columnTypes, String[][] data, Context context, int flags) {
String[] columnNames,
DataType[] columnTypes,
String[][] data,
Context context,
int flags,
int resultSetType) {

int columnNameLength = columnNames.length;
ColumnDecoder[] columns = new ColumnDecoder[columnNameLength];
Expand Down Expand Up @@ -178,7 +197,7 @@ public static ResultSet createResultSet(
byte[] bb = baos.toByteArray();
rows.add(bb);
}
return new CompleteResult(columns, rows.toArray(new byte[0][0]), context);
return new CompleteResult(columns, rows.toArray(new byte[0][0]), context, resultSetType);
}

public CompleteResult useAliasAsName() {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/mariadb/jdbc/client/result/Result.java
Expand Up @@ -164,8 +164,9 @@ protected Result(ColumnDecoder[] metadataList, Result prev) {
* @param metadataList column metadata
* @param data raw data
* @param context connection context
* @param resultSetType result set type
*/
public Result(ColumnDecoder[] metadataList, byte[][] data, Context context) {
public Result(ColumnDecoder[] metadataList, byte[][] data, Context context, int resultSetType) {
this.metadataList = metadataList;
this.maxIndex = this.metadataList.length;
this.reader = null;
Expand All @@ -175,7 +176,7 @@ public Result(ColumnDecoder[] metadataList, byte[][] data, Context context) {
this.data = data;
this.dataSize = data.length;
this.statement = null;
this.resultSetType = TYPE_FORWARD_ONLY;
this.resultSetType = resultSetType;
this.closeOnCompletion = false;
this.traceEnable = false;
this.rowDecoder = TEXT_ROW_DECODER;
Expand Down
Expand Up @@ -140,6 +140,9 @@ public void metaUnsigned() throws SQLException {
public void primaryKeysTest() throws SQLException {
DatabaseMetaData meta = sharedConn.getMetaData();
ResultSet rs = meta.getPrimaryKeys(sharedConn.getCatalog(), null, "dbpk_test");
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());

int counter = 0;
while (rs.next()) {
counter++;
Expand Down Expand Up @@ -253,6 +256,8 @@ public void functionColumns() throws SQLException {
+ "RETURN CONCAT('Hello, ',s,'!')");
stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER
ResultSet rs = meta.getFunctionColumns(null, null, "hello", null);
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());

assertTrue(rs.next());
/* First row is for return value */
Expand Down Expand Up @@ -447,7 +452,8 @@ Get result sets using either method and compare (ignore minor differences INT vs
for (int i = 0; i < 2; i++) {
ResultSet rs = i == 0 ? rs1 : rs2;
assertTrue(rs.next());

Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
assertEquals(useCatalog ? "t1" : "def", rs.getString("PKTABLE_CAT"));
assertEquals(useCatalog ? null : "t1", rs.getString("PKTABLE_SCHEM"));
assertEquals("product", rs.getString("PKTABLE_NAME"));
Expand Down Expand Up @@ -586,6 +592,8 @@ public void exportedKeysTest() throws SQLException {

DatabaseMetaData dbmd = sharedConn.getMetaData();
ResultSet rs = dbmd.getExportedKeys(sharedConn.getCatalog(), null, "cross%");
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
assertTrue(rs.next());
assertEquals(sharedConn.getCatalog(), rs.getString("PKTABLE_CAT"));
assertNull(rs.getString("PKTABLE_SCHEM"));
Expand Down Expand Up @@ -744,6 +752,8 @@ public void importedKeysTest() throws SQLException {

DatabaseMetaData dbmd = sharedConn.getMetaData();
ResultSet rs = dbmd.getImportedKeys(sharedConn.getCatalog(), null, "fore_key0");
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
int counter = 0;
while (rs.next()) {
assertEquals("id", rs.getString("pkcolumn_name"));
Expand Down Expand Up @@ -777,6 +787,8 @@ public void testGetCatalogs() throws SQLException {
try (Connection con = createCon("&useCatalogTerm=Schema")) {
dbmd = con.getMetaData();
rs = dbmd.getCatalogs();
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
assertFalse(rs.next());
}
}
Expand All @@ -799,7 +811,8 @@ public void testGetTables() throws SQLException {

DatabaseMetaData dbmd = sharedConn.getMetaData();
ResultSet rs = dbmd.getTables(null, null, "prim_key", null);

Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
assertTrue(rs.next());
rs = dbmd.getTables("", null, "prim_key", null);
assertTrue(rs.next());
Expand Down Expand Up @@ -889,7 +902,8 @@ public void testGetColumns() throws SQLException {

DatabaseMetaData dbmd = sharedConn.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, "ta\nble'getcolumns", null);

Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
assertTrue(rs.next());
assertEquals(sharedConn.getCatalog(), rs.getString(1)); // TABLE_CAT
assertNull(rs.getString(2)); // TABLE_SCHEM
Expand Down Expand Up @@ -1258,6 +1272,8 @@ private void testTransformedBitIsBoolean(
}

private void testResultSetColumns(ResultSet rs, String spec) throws SQLException {
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
ResultSetMetaData rsmd = rs.getMetaData();
String[] tokens = spec.split(",");

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/StatementTest.java
Expand Up @@ -447,6 +447,29 @@ public void maxRows() throws SQLException {
assertEquals(10, i);
}

@Test
public void getGeneratedKeysType() throws SQLException {
try (java.sql.Statement stmt =
sharedConn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE,
ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
stmt.addBatch("DROP TABLE IF EXISTS table0_0;");
stmt.addBatch("CREATE TABLE table0_0(id INT AUTO_INCREMENT PRIMARY KEY,value INT);");
stmt.addBatch("INSERT INTO table0_0 VALUES(1, -179653912)");
stmt.addBatch("INSERT INTO table0_0 VALUES(2, 1207965915)");
stmt.executeBatch();
stmt.executeUpdate(
"INSERT INTO table0_0 (value) VALUES(667711856)", Statement.RETURN_GENERATED_KEYS);
try (ResultSet rs = stmt.getGeneratedKeys()) {
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, stmt.getResultSetType());
Assertions.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
Assertions.assertEquals(ResultSet.CONCUR_UPDATABLE, stmt.getResultSetConcurrency());
Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
}
}
}

@Test
public void largeMaxRows() throws SQLException {
Statement stmt = sharedConn.createStatement();
Expand Down
Expand Up @@ -30,8 +30,7 @@ static void afterAll() throws SQLException {
}

public static long getPID() {
String processName =
java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
return Long.parseLong(processName.split("@")[0]);
}

Expand Down
Expand Up @@ -32,7 +32,13 @@ public void metaQuery() throws SQLException {
}
};
ResultSet rs =
CompleteResult.createResultSet(columnNames, columnTypes, data, sharedConn.getContext(), 0);
CompleteResult.createResultSet(
columnNames,
columnTypes,
data,
sharedConn.getContext(),
0,
ResultSet.TYPE_SCROLL_INSENSITIVE);
assertTrue(rs.next());
assertEquals(data[0][0], rs.getString(1));
assertEquals(data[0][1], rs.getString(2));
Expand Down

0 comments on commit fb673c2

Please sign in to comment.