How to check if table exists in jOOQ?

After opening a database connection, I want to check if the database has recently been modified. I use H2, which automatically creates a database if it does not exist.

I tried this check:

db.Public.PUBLIC.getTables().isEmpty()

but returns a static list of tables (without querying the schema in the database).

I could write raw SQL to get a list of tables, but that would be specific to the database engine. Is there a universal alternative in jOOQ?

+4
source share
1 answer

You cannot use:

db.Public.PUBLIC.getTables().isEmpty()

Because the generated meta-information is not related to the database. Instead, you can take a look at DSLContext.meta(). In your case, you simply write:

DSL.using(configuration).meta().getTables().isEmpty();

, , , - , isEmpty(). :

int numberOfTables =
DSL.using(configuration)
   .select(count())
   .from("information_schema.tables")
   .where("table_schema = 'PUBLIC'")
   .fetchOne(0, int.class);
+4

Source: https://habr.com/ru/post/1548373/


All Articles