yuqi1129 commented on code in PR #15037:
URL: https://github.com/apache/gravitino/pull/15037#discussion_r4626176941
##########
catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/TestJdbcDatabaseOperations.java:
##########
@@ -131,6 +110,29 @@ protected Set<String> createSysDatabaseNameSet() {
"Schema commented: catalog does not support schema comments",
exception.getMessage());
}
+ @Test
+ public void testExistBindsDatabaseNameAsParameter() throws SQLException {
+ String databaseName = "o'reilly\\db";
+ ResultSet resultSet = Mockito.mock(ResultSet.class);
+ PreparedStatement statement = Mockito.mock(PreparedStatement.class);
+ Connection connection = Mockito.mock(Connection.class);
+ DataSource dataSource = Mockito.mock(DataSource.class);
+ Mockito.when(dataSource.getConnection()).thenReturn(connection);
+
Mockito.when(connection.prepareStatement(Mockito.anyString())).thenReturn(statement);
+ Mockito.when(statement.executeQuery()).thenReturn(resultSet);
+ JdbcDatabaseOperations operations = new MinimalDatabaseOperations();
+ operations.initialize(dataSource, EXCEPTION_MAPPER,
Collections.emptyMap());
+
+ Assertions.assertFalse(operations.exist(databaseName));
Review Comment:
Could we also cover a matching database? The mocked `ResultSet.next()`
defaults to false, so the current test would still pass if `exist()` bound the
name correctly but always returned false. Running this case for both values of
`next()` and asserting the corresponding return value would cover that
regression.
##########
catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java:
##########
@@ -114,21 +115,25 @@ public List<String> listDatabases() {
@Override
public boolean exist(String databaseName) {
- try (final Connection connection = this.dataSource.getConnection()) {
- String query = generateDatabaseExistSql(databaseName);
- try (Statement statement = connection.createStatement()) {
- try (ResultSet resultSet = statement.executeQuery(query)) {
- return resultSet.next();
- }
+ try (final Connection connection = this.dataSource.getConnection();
+ PreparedStatement statement =
connection.prepareStatement(generateDatabaseExistSql())) {
+ statement.setString(1, databaseName);
+ try (ResultSet resultSet = statement.executeQuery()) {
+ return resultSet.next();
}
} catch (SQLException sqlException) {
throw this.exceptionMapper.toGravitinoException(sqlException);
}
}
- protected String generateDatabaseExistSql(String databaseName) {
- return String.format(
- "SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '%s'",
databaseName);
+ /**
+ * Returns the query that checks whether a database exists. The query takes
the database name as
+ * its only bind parameter. The default implementation is based on MySQL
syntax.
+ *
+ * @return the parameterized database existence query.
+ */
+ protected String generateDatabaseExistSql() {
Review Comment:
Do we support running third-party JDBC catalogs compiled against an earlier
version of this base class? A subclass that overrides only
`generateDatabaseExistSql(String)` can still load, but inherited `exist()` will
now call this no-arg method and silently use the default MySQL-style query. If
catalogs are expected to rebuild on upgrade, could we mention in the PR
description that custom dialects need to override the no-arg method and return
SQL with one bind parameter?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]