Skip to content

Commit 5d0f1a7

Browse files
authored
[#6629] Improvement(jdbc-catalog): Optimize load database method in JdbcDatabaseOperations (#6642)
<!-- 1. Title: [#<issue>] <type>(<scope>): <subject> Examples: - "[#123] feat(operator): support xxx" - "[#233] fix: check null before access result in xxx" - "[MINOR] refactor: fix typo in variable name" - "[MINOR] docs: fix typo in README" - "[#255] test: fix flaky test NameOfTheTest" Reference: https://www.conventionalcommits.org/en/v1.0.0/ 2. If the PR is unfinished, please mark this PR as draft. --> ### What changes were proposed in this pull request? Optimize load database method in JdbcDatabaseOperations ### Why are the changes needed? Fix: #6629 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? N/A
1 parent 20718cf commit 5d0f1a7

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/DatabaseOperation.java

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ void create(String databaseName, String comment, Map<String, String> properties)
5858
/** @return The list name of databases. */
5959
List<String> listDatabases();
6060

61+
/**
62+
* Checks if the specified database exists.
63+
*
64+
* @param databaseName The name of the database to check.
65+
* @return true if the database exists; false otherwise.
66+
*/
67+
boolean exist(String databaseName);
68+
6169
/**
6270
* @param databaseName The name of the database to check.
6371
* @return information object of the JDBC database.

catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@ public List<String> listDatabases() {
107107
}
108108
}
109109

110+
@Override
111+
public boolean exist(String databaseName) {
112+
try (final Connection connection = this.dataSource.getConnection()) {
113+
String query = generateDatabaseExistSql(databaseName);
114+
try (Statement statement = connection.createStatement()) {
115+
try (ResultSet resultSet = statement.executeQuery(query)) {
116+
return resultSet.next();
117+
}
118+
}
119+
} catch (SQLException sqlException) {
120+
throw this.exceptionMapper.toGravitinoException(sqlException);
121+
}
122+
}
123+
124+
protected String generateDatabaseExistSql(String databaseName) {
125+
return String.format(
126+
"SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '%s'", databaseName);
127+
}
128+
110129
protected void dropDatabase(String databaseName, boolean cascade) {
111130
try (final Connection connection = getConnection()) {
112131
JdbcConnectorUtils.executeUpdate(connection, generateDropDatabaseSql(databaseName, cascade));
@@ -176,16 +195,11 @@ protected String generateDropDatabaseSql(String databaseName, boolean cascade) {
176195
*/
177196
@Override
178197
public JdbcSchema load(String databaseName) throws NoSuchSchemaException {
179-
List<String> allDatabases = listDatabases();
180-
String dbName =
181-
allDatabases.stream()
182-
.filter(db -> db.equals(databaseName))
183-
.findFirst()
184-
.orElseThrow(
185-
() -> new NoSuchSchemaException("Database %s could not be found", databaseName));
186-
198+
if (!exist(databaseName)) {
199+
throw new NoSuchSchemaException("Database %s could not be found", databaseName);
200+
}
187201
return JdbcSchema.builder()
188-
.withName(dbName)
202+
.withName(databaseName)
189203
.withProperties(ImmutableMap.of())
190204
.withAuditInfo(AuditInfo.EMPTY)
191205
.build();

catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void create(String databaseName, String comment, Map<String, String> prop
6767
Preconditions.checkArgument(exist(databaseName), "Database %s does not exist", databaseName);
6868
}
6969

70+
@Override
7071
public boolean exist(String databaseName) {
7172
return new File(dbPath + "/" + databaseName).exists();
7273
}

catalogs/catalog-jdbc-postgresql/src/main/java/org/apache/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ protected Connection getConnection() throws SQLException {
155155
return connection;
156156
}
157157

158+
@Override
159+
protected String generateDatabaseExistSql(String databaseName) {
160+
return String.format(
161+
"SELECT n.datname FROM pg_catalog.pg_database n where n.datname='%s'", databaseName);
162+
}
163+
158164
@Override
159165
protected boolean supportSchemaComment() {
160166
return true;

0 commit comments

Comments
 (0)