Skip to content

Commit

Permalink
dbeaver#22963 Fix using try catch with resources
Browse files Browse the repository at this point in the history
  • Loading branch information
longhaseng52 committed Mar 21, 2024
1 parent 053e64c commit 789e5ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,14 @@ public ArrayList<String> getCollations() {
public void loadCharsets(@NotNull DBRProgressMonitor monitor) throws DBException {
charsets = new ArrayList<>();
try (JDBCSession session = DBUtils.openMetaSession(monitor, container, "Load charsets")) {
String sql = "select * from db_charset";
final JDBCPreparedStatement dbStat = session.prepareStatement(sql);
JDBCResultSet dbResult = dbStat.executeQuery();
try {
while (dbResult.next()) {
CubridCharset charset = new CubridCharset(this, dbResult);
charsets.add(charset);
}
} finally {
dbStat.close();
}
try (JDBCPreparedStatement dbStat = session.prepareStatement("select * from db_charset")) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
CubridCharset charset = new CubridCharset(this, dbResult);
charsets.add(charset);
}
}
}
} catch (SQLException e) {
throw new DBException("Load charsets failed", e);
}
Expand All @@ -159,19 +156,16 @@ public void loadCharsets(@NotNull DBRProgressMonitor monitor) throws DBException
public void loadCollations(@NotNull DBRProgressMonitor monitor) throws DBException {
collations = new LinkedHashMap<>();
try (JDBCSession session = DBUtils.openMetaSession(monitor, container, "Load collations")) {
String sql = "SHOW COLLATION";
final JDBCPreparedStatement dbStat = session.prepareStatement(sql);
JDBCResultSet dbResult = dbStat.executeQuery();
try {
while (dbResult.next()) {
String charsetName = JDBCUtils.safeGetString(dbResult, "charset");
CubridCharset charset = getCharset(charsetName);
CubridCollation collation = new CubridCollation(charset, dbResult);
collations.put(collation.getName(), collation);
charset.addCollation(collation);
}
} finally {
dbStat.close();
try (JDBCPreparedStatement dbStat = session.prepareStatement("show collation")) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
String charsetName = JDBCUtils.safeGetString(dbResult, "charset");
CubridCharset charset = getCharset(charsetName);
CubridCollation collation = new CubridCollation(charset, dbResult);
collations.put(collation.getName(), collation);
charset.addCollation(collation);
}
}
}
} catch (SQLException e) {
throw new DBException("Load collations failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,15 @@ public String getTableOrViewName(@Nullable GenericTableBase base) {
public List<GenericSchema> loadSchemas(@NotNull JDBCSession session, @NotNull GenericDataSource dataSource, @Nullable GenericCatalog catalog)
throws DBException {
List<GenericSchema> users = new ArrayList<>();
try {
final JDBCPreparedStatement dbStat = session.prepareStatement("select * from db_user");
try (JDBCPreparedStatement dbStat = session.prepareStatement("select * from db_user")) {
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
try {
try (JDBCResultSet dbResult = dbStat.getResultSet()) {
while (dbResult.next()) {
String name = JDBCUtils.safeGetStringTrimmed(dbResult, "name");
String description = JDBCUtils.safeGetStringTrimmed(dbResult, "comment");
CubridUser user = new CubridUser(dataSource, name, description);
users.add(user);
}
} finally {
dbStat.close();
}
} catch (SQLException e) {
log.error("Cannot load user", e);
Expand Down Expand Up @@ -400,24 +396,19 @@ public void loadProcedures(
@Nullable
@Override
public String getViewDDL(@NotNull DBRProgressMonitor monitor, @Nullable GenericView object, @Nullable Map<String, Object> options) throws DBException {
JDBCSession session = DBUtils.openMetaSession(monitor, object, "Load view ddl " + object.getName());

String ddl = "-- View definition not available";
String regex = "\\[|\\]";
try {
String sql= String.format("show create view %s", object.getName());
try (JDBCSession session = DBUtils.openMetaSession(monitor, object, "Load view ddl")) {
String sql = String.format("show create view %s", object.getName());
JDBCPreparedStatement dbStat = session.prepareStatement(sql);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
try {
try (JDBCResultSet dbResult = dbStat.getResultSet()) {
while(dbResult.next()) {
ddl = "create or replace view \"" + dbResult.getString("View") + "\" as " + dbResult.getString("Create View");
ddl = ddl.replaceAll(regex, "\"");
ddl = new CubridSQLFormatter(ddl).format();
break;
}
} finally {
dbStat.close();
}
} catch (SQLException e) {
e.printStackTrace();
Expand Down

0 comments on commit 789e5ca

Please sign in to comment.