Accessing H2 database schema

孤街醉人 提交于 2019-12-01 22:52:50

问题


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>


来源:https://stackoverflow.com/questions/12066043/accessing-h2-database-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!