|
This won't be straight forward. JDBC's DatabaseMetaData does not have a way to easily get all constraints. You can use #getIndexInfo to get all of the indexes, including the index(es) defined by the unique constraint. However, most dialects provide only the INDEX_NAME in the ResultSet. Some dialects, like MySQL, simply use the constraint name. For those, skipping constraints that exist already works. However, most others do something different. Constraint name != index name (nor should it). Examples:
H2: [UK name]_INDEX_3 HSQL: SYS_IDX_[UK name]_65
I can't simply check if the index name contains the constraint name (could cause collisions if 2 constraints are named similarly) and we shouldn't attempt to know what the index name appends to the constraint name for each Dialect.
That leaves attempting to use the dialect's system tables (or similar concepts) to query for constraint names as a fallback. As you mentioned, it's fairly easy in HSQL (using INFORMATION_SCHEMA), but the rest of the Dialects would need audited.
|