SQL解释器
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
从jOOQ 3.13开始,实现了一个SQL解释器,它可以解释SQL语言的一个子集(主要是DDL语句),并维护数据库元模型的最新内存表示。
该解释器通过各种 DSLContext.meta()
方法提供,例如可以如下使用
// Using the parser Meta meta1 = create.meta( "create table t (i int)", "alter table t add primary key (i)", "create table u (i int references t)" ); Meta meta2 = create.meta( createTable("t").column("i", INTEGER), alterTable("t").add(primaryKey("i")), createTable("u").column("i", INTEGER).constraint(foreignKey("i").references("t")) );
解释是增量的。在任何预先存在的 org.jooq.Meta
模型上,可以调用 apply()
来派生模型的新版本。这包括能够组合不同的模型源,包括基于JDBC java.sql.DatabaseMetaData
的模型。
// Get live access to your current Connection's DatabaseMetaData Meta meta1 = create.meta(); // Create a new model representation with an interpreted, additional table // The query is not executed! The DDL is only interpreted in jOOQ, and a derived meta model is created from it Meta meta2 = meta1.apply("create table t (i int)"); // Create another new model representation, with a table removed // Again, none of these queries are executed. Meta meta3 = meta2.apply("drop table u cascade");
最后,如果你想将元模型导出为不同的支持格式之一,你可以使用
// The JAXB annotated XML version of your "information schema": InformationSchema is = meta.informationSchema(); // A set of DDL queries that can be used to reproduce your schema on any database and dialect: Queries queries = meta.ddl();
可以使用不同的元模型源,包括jOOQ API构建的DDL语句,或者一组将使用SQL解析器动态解析的SQL字符串。这些来源可以是数据库变更管理脚本,例如Flyway或jOOQ管理的脚本。
结果类型是运行时 org.jooq.Meta
模型,它可以访问普通的jOOQ API,包括目录和模式或表。然后,这些对象可以与任何其他jOOQ API一起使用,例如,计算数据库中所有表中的所有行
for (Table<?> table : meta.getTables()) System.out.println(table + " has " + create.fetchCount(table) + " rows");
有关解释器配置标志集,请参阅关于解释器设置的部分。
反馈
您对此页面有任何反馈吗?我们很乐意听到您的意见!