test table creation in lowercase on Oracle

This commit is contained in:
Jörg Prante 2024-04-29 15:17:22 +02:00
parent 6c064531ab
commit 0dd8138237
2 changed files with 56 additions and 50 deletions

View file

@ -14,6 +14,9 @@ import org.xbib.jdbc.query.OptionsOverride;
import org.xbib.jdbc.query.Schema; import org.xbib.jdbc.query.Schema;
import org.xbib.jdbc.test.CommonTest; import org.xbib.jdbc.test.CommonTest;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Exercise Database functionality with a real Oracle database. * Exercise Database functionality with a real Oracle database.
*/ */
@ -78,4 +81,16 @@ public class OracleTest extends CommonTest {
return null; return null;
}); });
} }
@Test
public void testTableExists() {
db.dropTableQuietly("dbtest");
new Schema().addTable("dbtest").addColumn("pk").primaryKey().schema().execute(db);
assertTrue(db.tableExists("dbtest"));
db.dropTableQuietly("dbtest");
assertFalse(db.tableExists("dbtest"));
db.ddl("create table \"dbtest\" (pk varchar(1))").execute();
assertTrue(db.tableExists("dbtest"));
db.dropTableQuietly("dbtest");
}
} }

View file

@ -200,12 +200,22 @@ public class DatabaseImpl implements Database {
public boolean tableExists(String tableName, String schemaName) throws DatabaseException { public boolean tableExists(String tableName, String schemaName) throws DatabaseException {
if (tableName != null && connection != null) { if (tableName != null && connection != null) {
try { try {
// schema based check
DatabaseMetaData metadata = connection.getMetaData(); DatabaseMetaData metadata = connection.getMetaData();
String normalizedTable = flavor().normalizeTableName(tableName); String normalizedTable = flavor().normalizeTableName(tableName);
ResultSet resultSet = metadata.getTables(connection.getCatalog(), schemaName, normalizedTable, new String[]{"TABLE", "VIEW"}); try (ResultSet resultSet = metadata.getTables(connection.getCatalog(), schemaName, normalizedTable, new String[]{"TABLE", "VIEW"})) {
while (resultSet.next()) { while (resultSet.next()) {
if (normalizedTable.equals(resultSet.getString("TABLE_NAME"))) { if (normalizedTable.equals(resultSet.getString("TABLE_NAME"))) {
return true; return true;
}
}
}
// otherwise, do a simple check
try (ResultSet resultSet = metadata.getTables(null, null, tableName, null) ) {
while (resultSet.next()) {
if (tableName.equals(resultSet.getString("TABLE_NAME"))) {
return true;
}
} }
} }
} catch (SQLException exc) { } catch (SQLException exc) {
@ -455,20 +465,15 @@ public class DatabaseImpl implements Database {
return; return;
} }
params.forEach((k, v) -> { params.forEach((k, v) -> {
if (v instanceof String) { switch (v) {
sql.argString(k, (String) v); case String s -> sql.argString(k, s);
} else if (v instanceof Integer) { case Integer i -> sql.argInteger(k, i);
sql.argInteger(k, (Integer) v); case Long l -> sql.argLong(k, l);
} else if (v instanceof Long) { case Boolean b -> sql.argBoolean(k, b);
sql.argLong(k, (Long) v); case LocalDate localDate -> sql.argLocalDate(k, localDate);
} else if (v instanceof Boolean) { case LocalDateTime localDateTime -> sql.argLocalDateTime(k, localDateTime);
sql.argBoolean(k, (Boolean) v); case null, default ->
} else if (v instanceof LocalDate) { throw new DatabaseException("unknown type for param: " + (v != null ? v.getClass() : "null"));
sql.argLocalDate(k, (LocalDate) v);
} else if (v instanceof LocalDateTime) {
sql.argLocalDateTime(k, (LocalDateTime) v);
} else {
throw new DatabaseException("unknown type for param: " + (v != null ? v.getClass() : "null"));
} }
}); });
} }
@ -478,22 +483,15 @@ public class DatabaseImpl implements Database {
return; return;
} }
params.forEach((k, v) -> { params.forEach((k, v) -> {
if (v instanceof String) { switch (v) {
sql.argString(k, (String) v); case String s -> sql.argString(k, s);
} else if (v instanceof Integer) { case Integer i -> sql.argInteger(k, i);
sql.argInteger(k, (Integer) v); case Long l -> sql.argLong(k, l);
} else if (v instanceof Long) { case Boolean b -> sql.argBoolean(k, b);
sql.argLong(k, (Long) v); case LocalDate localDate -> sql.argLocalDate(k, localDate);
} else if (v instanceof Boolean) { case LocalDateTime localDateTime -> sql.argLocalDateTime(k, localDateTime);
sql.argBoolean(k, (Boolean) v); case null -> sql.argNull(k);
} else if (v instanceof LocalDate) { default -> throw new DatabaseException("unknown type for param: " + v.getClass());
sql.argLocalDate(k, (LocalDate) v);
} else if (v instanceof LocalDateTime) {
sql.argLocalDateTime(k, (LocalDateTime) v);
} else if (v == null) {
sql.argNull(k);
} else {
throw new DatabaseException("unknown type for param: " + v.getClass());
} }
}); });
} }
@ -503,22 +501,15 @@ public class DatabaseImpl implements Database {
return; return;
} }
params.forEach((k, v) -> { params.forEach((k, v) -> {
if (v instanceof String) { switch (v) {
sql.argString(k, (String) v); case String s -> sql.argString(k, s);
} else if (v instanceof Integer) { case Integer i -> sql.argInteger(k, i);
sql.argInteger(k, (Integer) v); case Long l -> sql.argLong(k, l);
} else if (v instanceof Long) { case Boolean b -> sql.argBoolean(k, b);
sql.argLong(k, (Long) v); case LocalDate localDate -> sql.argLocalDate(k, localDate);
} else if (v instanceof Boolean) { case LocalDateTime localDateTime -> sql.argLocalDateTime(k, localDateTime);
sql.argBoolean(k, (Boolean) v); case null -> sql.argNull(k);
} else if (v instanceof LocalDate) { default -> throw new DatabaseException("unknown type for param: " + v.getClass());
sql.argLocalDate(k, (LocalDate) v);
} else if (v instanceof LocalDateTime) {
sql.argLocalDateTime(k, (LocalDateTime) v);
} else if (v == null) {
sql.argNull(k);
} else {
throw new DatabaseException("unknown type for param: " + v.getClass());
} }
}); });
} }