Skip to content

Commit

Permalink
[Improve][Connector-v2] Throw Exception in sql query for JdbcCatalog …
Browse files Browse the repository at this point in the history
…of tb or db exists query
  • Loading branch information
dailai committed Sep 13, 2024
1 parent 655cd01 commit e3488f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.seatunnel.common.exception.CommonErrorCode;
import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;
import org.apache.seatunnel.common.utils.JdbcUrlUtil;
import org.apache.seatunnel.common.utils.SeaTunnelException;
import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.utils.CatalogUtils;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -279,9 +280,7 @@ public boolean databaseExists(String databaseName) throws CatalogException {
return false;
}
try {
return querySQLResultExists(
getUrlFromDatabaseName(databaseName),
getDatabaseWithConditionSql(databaseName));
return querySQLResultExists(defaultUrl, getDatabaseWithConditionSql(databaseName));
} catch (SeaTunnelRuntimeException e) {
if (e.getSeaTunnelErrorCode().getCode().equals(UNSUPPORTED_METHOD.getCode())) {
log.warn(
Expand All @@ -290,6 +289,8 @@ public boolean databaseExists(String databaseName) throws CatalogException {
return listDatabases().contains(databaseName);
}
throw e;
} catch (SQLException e) {
throw new SeaTunnelException("Failed to querySQLResult", e);
}
}

Expand Down Expand Up @@ -350,6 +351,8 @@ && listTables(tablePath.getDatabaseName())
}
}
throw e1;
} catch (SQLException e) {
throw new SeaTunnelException("Failed to querySQLResult", e);
}
}

Expand Down Expand Up @@ -566,14 +569,10 @@ protected List<String> queryString(String url, String sql, ResultSetConsumer<Str
}
}

protected boolean querySQLResultExists(String dbUrl, String sql) {
try (PreparedStatement stmt = getConnection(dbUrl).prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
} catch (Exception e) {
log.info("query exists error", e);
return false;
protected boolean querySQLResultExists(String dbUrl, String sql) throws SQLException {
try (PreparedStatement stmt = getConnection(dbUrl).prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ public boolean databaseExists(String databaseName) throws CatalogException {

@Override
public boolean tableExists(TablePath tablePath) throws CatalogException {
return querySQLResultExists(
this.getUrlFromDatabaseName(tablePath.getDatabaseName()),
getTableWithConditionSql(tablePath));
try {
return querySQLResultExists(
this.getUrlFromDatabaseName(tablePath.getDatabaseName()),
getTableWithConditionSql(tablePath));
} catch (SQLException e) {
throw new SeaTunnelException("Failed to querySQLResult", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import org.apache.seatunnel.api.table.catalog.exception.DatabaseNotExistException;
import org.apache.seatunnel.api.table.catalog.exception.TableAlreadyExistException;
import org.apache.seatunnel.common.utils.JdbcUrlUtil;
import org.apache.seatunnel.common.utils.SeaTunnelException;
import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.oracle.OracleCatalog;

import java.sql.SQLException;
import java.util.List;

import static org.apache.seatunnel.shade.com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -52,9 +54,13 @@ protected String getDatabaseWithConditionSql(String databaseName) {

@Override
public boolean tableExists(TablePath tablePath) throws CatalogException {
return querySQLResultExists(
this.getUrlFromDatabaseName(tablePath.getDatabaseName()),
getTableWithConditionSql(tablePath));
try {
return querySQLResultExists(
this.getUrlFromDatabaseName(tablePath.getDatabaseName()),
getTableWithConditionSql(tablePath));
} catch (SQLException e) {
throw new SeaTunnelException("Failed to querySQLResult", e);
}
}

@Override
Expand Down

0 comments on commit e3488f1

Please sign in to comment.