org.hibernate.cfg.reveng.dialect.H2MetaDataDialect Maven / Gradle / Ivy
package org.hibernate.cfg.reveng.dialect;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.hibernate.tool.util.ReflectHelper;
/**
* MetaData dialect that work around tweaks in the H2 database.
*
* @author Max Rydahl Andersen
*
*/
public class H2MetaDataDialect extends JDBCMetaDataDialect {
private static final String SPKSQ_H2_1_X =
"SELECT " +
" idx.TABLE_CATALOG TABLE_CAT, " +
" idx.TABLE_SCHEMA TABLE_SCHEM, " +
" idx.TABLE_NAME, " +
" idx.COLUMN_NAME " +
"FROM " +
" INFORMATION_SCHEMA.INDEXES idx, " +
" INFORMATION_SCHEMA.COLUMNS cols " +
"WHERE " +
" idx.TABLE_CATALOG = cols.TABLE_CATALOG AND " +
" idx.TABLE_SCHEMA = cols.TABLE_SCHEMA AND " +
" idx.TABLE_NAME = cols.TABLE_NAME AND " +
" idx.PRIMARY_KEY = TRUE AND " +
" cols.COLUMN_DEFAULT like '%NEXT VALUE FOR%' ";
private static final String SPKSQ_H2_2_X =
"SELECT " +
" idx.TABLE_CATALOG TABLE_CAT, " +
" idx.TABLE_SCHEMA TABLE_SCHEM, " +
" idx.TABLE_NAME, " +
" cols.COLUMN_NAME " +
"FROM " +
" INFORMATION_SCHEMA.INDEXES idx, " +
" INFORMATION_SCHEMA.INDEX_COLUMNS idx_cols, " +
" INFORMATION_SCHEMA.COLUMNS cols " +
"WHERE " +
" idx.TABLE_CATALOG = cols.TABLE_CATALOG AND " +
" idx.TABLE_SCHEMA = cols.TABLE_SCHEMA AND " +
" idx.TABLE_NAME = cols.TABLE_NAME AND " +
" idx.INDEX_TYPE_NAME = 'PRIMARY KEY' AND " +
" cols.COLUMN_NAME = idx_cols.COLUMN_NAME AND " +
" cols.IS_IDENTITY = 'YES'";
private static boolean understandsCatalogName = true;
private String suggested_primary_key_strategy_query = null;
public H2MetaDataDialect() {
super();
try {
Class> constants = ReflectHelper.classForName( "org.h2.engine.Constants" );
Integer build = (Integer)constants.getDeclaredField( "BUILD_ID" ).get( null );
if ( build.intValue() < 55 ) {
understandsCatalogName = false;
}
suggested_primary_key_strategy_query = build.intValue() > 200 ? SPKSQ_H2_2_X : SPKSQ_H2_1_X;
}
catch( Throwable e ) {
// ignore (probably H2 not in the classpath)
}
}
protected void putTableType(Map element, ResultSet tableRs) throws SQLException {
String tableType = tableRs.getString("TABLE_TYPE");
if ("BASE TABLE".equals(tableType)) {
tableType = "TABLE";
}
element.put("TABLE_TYPE", tableType);
}
protected void putTablePart(Map element, ResultSet tableRs) throws SQLException {
super.putTablePart( element, tableRs );
if ( !understandsCatalogName ) {
element.put( "TABLE_CAT", null );
}
}
protected void putExportedKeysPart(Map element, ResultSet rs) throws SQLException {
super.putExportedKeysPart( element, rs );
if ( !understandsCatalogName ) {
element.put( "PKTABLE_CAT", null );
}
}
public Iterator