Accessing H2 database schema

2019-02-24 12:31发布

问题:

Im currently accessing an embedded H2 database in java using jOOQ to generate table classes etc. I can currently execute queries such as

create.select().from(TEST).fetch()

in my code, return results etc.

However I cant query the information schema. I can access it ok using the H2 console, and creating a view for a given information_schema table works fine. My question is how should I be accessing the information_schema eg to perform a query such as

SELECT * FROM INFORMATION_SCHEMA.TABLES

from within my java code? Do I have to create a view, can the schema be accessed through jooq-meta or some other means, or do I need to add something to the property file used by jooq-codegen (information_schema isn't created in the generated tables classes)? Or something else?


Edit: This is my first foray into using jooq and am coming from a MySQL background. What I'm effectively trying to achieve is to be able to run some unit tests to ensure DB related code works. So I want to access the database's metadata (schema) and return something without having to know if any user created tables exist.

回答1:

You can generate classes for the INFORMATION_SCHEMA just as any other schema. In fact, jOOQ-meta contains some of those classes required for reverse-engineering your database schema.

The manual's section about advanced code generation properties mentions how you can configure the code generator to load several "input schemata":

<database>
  <schemata>
    <schema>
      <inputSchema>...</inputSchema>
    </schema>
    [ <schema>...</schema> ... ]
  </schemata>
</database>