Database schema navigation in Java
An important part of jOOQ is jooq-meta, the database schema navigation module. This is used by the code generator to discover relevant schema objects. I was asked several times why I rolled my own instead of using other libraries, such as SchemaCrawler or SchemaSpy, and indeed it’s a pity I cannot rely on other stable third party products. Here are some thoughts on database schema navigation:
The SQL-92 standard defines how RDBMS should implement an INFORMATION_SCHEMA containing their dictionary tables. And indeed, some RDBMS do implement parts of the standard specification. These RDBMS ship with some implementation of the standard.
Close to the standard
- HSQLDB: very close to the true standard
- Postgres: close to the standard, with some tweaks (also has proprietary dictionary tables)
- SQL Server: close to the standard but quite incomplete (also has proprietary dictionary tables)
Liberal interpretation of the standard
- H2 (some backwards-incompatible changes, recently)
- MySQL (only since 5.0, also has proprietary dictionary tables)
Other RDBMS provide their own idea of dictionary tables. This is something very tricky for schema navigation tools like jOOQ, to get a hold of. The dictionary table landscape can be described like this (my biased opinion):
Neat and well-documented dictionary tables
- DB2: These dictionary tables somehow look like the standard, with different names. They feel intuitive.
- Oracle: In my opinion has a better set of dictionary views than the ones proposed by the standard. Very easy to understand and well-documented all over the Internet
- SQLite: There are no dictionary tables, but the SQLite stored procedures are very simple to use. It’s a simple database, after all
Hard to understand, not well-documented dictionary tables
- Derby: Created the notion of conglomerates instead of using normal database-speak, such as relations, keys, etc.
- MySQL: the old mysql schema was quite a pain. Fortunately, this is no longer true with MySQL 5.0
- Ingres: Well… Ingres is an old database. Usability was not one of the main things in the 70’s…
- Sybase SQL Anywhere: Lots of objects that have to be joined in complicated relations. Documentation is scarce
- Sybase ASE: Even more difficult than SQL Anywhere. Some data can only be obtained with “tricks”
The variety of dictionary tables seems to scream for standard abstraction. While the SQL-92 standard could in fact be implemented on most of these RDBMS, JDBC abstraction is even better. JDBC knows of the DatabaseMetaData object and allows for navigating database schemata easily. Unfortunately, every now and then, this API will throw a SQLFeatureNotSupportedException. There is no general rule about which JDBC driver implements how much of this API and when a workaround is needed. For jOOQ code generation, these facts make this API quite useless.
There are some other tools in the open source world, as mentioned previously. Here are some drawbacks of using those tools in jOOQ:
- Both tools that I know of are licensed with LGPL, which is not nicely compatible with jOOQ’s Apache 2 license.
- Both tools navigate the entity-relationships very well, but seem to lack support for many non-standard constructs, such as UDT’s, advanced stored procedure usage (e.g. returning cursors, UDT’s, etc), ARRAY’s
- SchemaCrawler supports only 8 RDBMS, jOOQ has 12 now
- Both tools are rather inactive. See here and here
For more information, visit their sites:
Because of the above reasons, jOOQ ships with its own database schema navigation: jooq-meta. This module can be used independently as an alternative to JDBC’s DatabaseMetaData, SchemaCrawler or SchemaSpy. jooq-meta uses jOOQ-crafted queries to navigate database meta-data, hence it is also part of the integration test suite. As an example, see how the Ingres foreign key relationships are navigated with jooq-meta:
Result<Record> result = create() .select( IirefConstraints.REF_CONSTRAINT_NAME.trim(), IirefConstraints.UNIQUE_CONSTRAINT_NAME.trim(), IirefConstraints.REF_TABLE_NAME.trim(), IiindexColumns.COLUMN_NAME.trim()) .from(IICONSTRAINTS) .join(IIREF_CONSTRAINTS) .on(Iiconstraints.CONSTRAINT_NAME.equal(IirefConstraints.REF_CONSTRAINT_NAME)) .and(Iiconstraints.SCHEMA_NAME.equal(IirefConstraints.REF_SCHEMA_NAME)) .join(IICONSTRAINT_INDEXES) .on(Iiconstraints.CONSTRAINT_NAME.equal(IiconstraintIndexes.CONSTRAINT_NAME)) .and(Iiconstraints.SCHEMA_NAME.equal(IiconstraintIndexes.SCHEMA_NAME)) .join(IIINDEXES) .on(IiconstraintIndexes.INDEX_NAME.equal(Iiindexes.INDEX_NAME)) .and(IiconstraintIndexes.SCHEMA_NAME.equal(Iiindexes.INDEX_OWNER)) .join(IIINDEX_COLUMNS) .on(Iiindexes.INDEX_NAME.equal(IiindexColumns.INDEX_NAME)) .and(Iiindexes.INDEX_OWNER.equal(IiindexColumns.INDEX_OWNER)) .where(Iiconstraints.SCHEMA_NAME.equal(getSchemaName())) .and(Iiconstraints.CONSTRAINT_TYPE.equal("R")) .orderBy( IirefConstraints.REF_TABLE_NAME.asc(), IirefConstraints.REF_CONSTRAINT_NAME.asc(), IiindexColumns.KEY_SEQUENCE.asc()) .fetch();
Once more it can be said that the world of RDBMS is very heterogeneous. Database abstraction in Java is established only to a certain degree in technologies such as JDBC, Hibernate/JPA, and third party libraries such as SchemaCrawler, SchemaSpy, and jooq-meta.
That was awesome, right? Share it with your friends!
- 1,191,423 hits
- It's a small step for a library, but a huge step for mankind. #jOOR 0.9.5 has been released: buff.ly/1A0AeLm 30 minutes ago
- RT @virtualJUG: NoSQL for BigData? You haven't asked operations have you? ow.ly/JRqbO 39 minutes ago
Get Back in Control of Your SQL
Top Posts & Pages
- 10 Common Mistakes Java Developers Make when Writing SQL
- SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT
- 10 More Common Mistakes Java Developers Make when Writing SQL
- 10 Subtle Best Practices when Coding Java
- Java 8 Friday: 10 Subtle Mistakes When Using the Streams API
- The 10 Most Popular DB Engines (SQL and NoSQL)
- Do You Really Understand SQL's GROUP BY and HAVING clauses?
- NoSQL? No, SQL! - How to Calculate Running Totals
- Java 8 Friday Goodies: Map Enhancements
- Does Java 8 Still Need LINQ? Or is it Better than LINQ?
- The Java Legacy is Constantly Growing March 5, 2015
- Avoid Recursion in ConcurrentHashMap.computeIfAbsent() March 4, 2015
- jOOQ – Ein alternativer Weg mit Java und SQL zu arbeiten March 2, 2015
- How to use SQL PIVOT to Compare Two Tables in Your Database February 26, 2015
- How to Extract a Date Part in SQL February 25, 2015
- jOOQ vs. Slick – Pros and Cons of Each Approach February 24, 2015
- Thou Shalt Not Name Thy Method “Equals” February 18, 2015
- jOOQ Tuesdays: Raoul-Gabriel Urma Explores What Java 8 and English have in Common February 17, 2015
- 10 SQL Articles Everyone Must Read February 13, 2015
- “NoSQL Injection” – What 40000 Unsecured MongoDB Databases Mean for our Industry February 12, 2015