Skip to content

Commit

Permalink
[Improve][Jdbc] Optimize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
dailai committed Jul 29, 2024
1 parent 46859b2 commit 3fb2e68
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,12 @@ public static SeaTunnelRuntimeException formatDateError(String date, String fiel
params.put("field", field);
return new SeaTunnelRuntimeException(CommonErrorCode.FORMAT_DATE_ERROR, params);
}

public static SeaTunnelRuntimeException unsupportedMethod(
String identifier, String methodName) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("methodName", methodName);
return new SeaTunnelRuntimeException(CommonErrorCode.UNSUPPORTED_METHOD, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public enum CommonErrorCode implements SeaTunnelErrorCode {
FORMAT_DATETIME_ERROR(
"COMMON-33",
"The datetime format '<datetime>' of field '<field>' is not supported. Please check the datetime format."),
UNSUPPORTED_METHOD("COMMON-34", "'<identifier>' unsupported the method '<methodName>'"),
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.seatunnel.common.exception.CommonErrorCode.UNSUPPORTED_METHOD;

@Slf4j
public abstract class AbstractJdbcCatalog implements Catalog {
Expand Down Expand Up @@ -264,7 +265,7 @@ protected String getListDatabaseSql() {
}

protected String getDatabaseWithConditionSql(String databaseName) {
throw new UnsupportedOperationException();
throw CommonError.unsupportedMethod(this.catalogName, "getDatabaseWithConditionSql");
}

@Override
Expand Down Expand Up @@ -295,11 +296,14 @@ public boolean databaseExists(String databaseName) throws CatalogException {
return querySQLResultExists(
getUrlFromDatabaseName(databaseName),
getDatabaseWithConditionSql(databaseName));
} catch (UnsupportedOperationException e) {
log.warn(
"The catalog: {} is not supported the getDatabaseWithConditionSql for databaseExists",
this.catalogName);
return listDatabases().contains(databaseName);
} catch (SeaTunnelRuntimeException e) {
if (e.getSeaTunnelErrorCode().getCode().equals(UNSUPPORTED_METHOD.getCode())) {
log.warn(
"The catalog: {} is not supported the getDatabaseWithConditionSql for databaseExists",
this.catalogName);
return listDatabases().contains(databaseName);
}
throw e;
}
}

Expand All @@ -308,7 +312,7 @@ protected String getListTableSql(String databaseName) {
}

protected String getTableWithConditionSql(TablePath tablePath) {
throw new UnsupportedOperationException();
throw CommonError.unsupportedMethod(this.catalogName, "getTableWithConditionSql");
}

protected String getTableName(ResultSet rs) throws SQLException {
Expand Down Expand Up @@ -349,17 +353,20 @@ public boolean tableExists(TablePath tablePath) throws CatalogException {
try {
return querySQLResultExists(
this.getUrlFromDatabaseName(databaseName), getTableWithConditionSql(tablePath));
} catch (UnsupportedOperationException e1) {
log.warn(
"The catalog: {} is not supported the getTableWithConditionSql for tableExists ",
this.catalogName);
try {
return databaseExists(tablePath.getDatabaseName())
&& listTables(tablePath.getDatabaseName())
.contains(getTableName(tablePath));
} catch (DatabaseNotExistException e2) {
return false;
} catch (SeaTunnelRuntimeException e1) {
if (e1.getSeaTunnelErrorCode().getCode().equals(UNSUPPORTED_METHOD.getCode())) {
log.warn(
"The catalog: {} is not supported the getTableWithConditionSql for tableExists ",
this.catalogName);
try {
return databaseExists(tablePath.getDatabaseName())
&& listTables(tablePath.getDatabaseName())
.contains(getTableName(tablePath));
} catch (DatabaseNotExistException e2) {
return false;
}
}
throw e1;
}
}

Expand Down

0 comments on commit 3fb2e68

Please sign in to comment.