Skip to content

Commit 4729ef4

Browse files
committed
Update SQL.java
1 parent 3699519 commit 4729ef4

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

  • src/main/java/io/github/intisy/utils/custom

src/main/java/io/github/intisy/utils/custom/SQL.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,4 +920,28 @@ private static String padRight(String s, int n) {
920920
}
921921
return sb.toString();
922922
}
923+
924+
public boolean tableExists(String tableName) {
925+
validateIdentifier(tableName);
926+
927+
try {
928+
DatabaseMetaData metaData = getConnection().getMetaData();
929+
String catalog = getConnection().getCatalog();
930+
String schemaPattern = (databaseType == DatabaseType.MYSQL) ? catalog : null;
931+
932+
try (ResultSet tables = metaData.getTables(
933+
catalog,
934+
schemaPattern,
935+
tableName,
936+
new String[]{"TABLE"})) {
937+
938+
boolean exists = tables.next();
939+
logger.debug("Table '" + tableName + "' " + (exists ? "exists" : "does not exist"));
940+
return exists;
941+
}
942+
} catch (SQLException e) {
943+
logger.error("Failed to check if table '" + tableName + "' exists: " + e.getMessage());
944+
throw new RuntimeException(e);
945+
}
946+
}
923947
}

0 commit comments

Comments
 (0)