bharos commented on code in PR #15037:
URL: https://github.com/apache/gravitino/pull/15037#discussion_r4631909377
##########
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:
Good point. `JdbcDatabaseOperations` is not a documented extension point, so
JDBC catalogs are expected to be rebuilt on upgrade. I added a note to the PR
description that custom dialects need to override the new no-arg method and
return SQL with one bind parameter.
##########
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:
Done. The test now runs with `ResultSet.next()` returning both true and
false, and asserts `exist()` returns the same value.
--
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]