use of org.hibernate.engine.jdbc.env.spi.JdbcEnvironment in project hibernate-orm by hibernate.
the class StandardTableExporter method getSqlCreateStrings.
@Override
public String[] getSqlCreateStrings(Table table, Metadata metadata) {
final QualifiedName tableName = new QualifiedNameParser.NameParts(Identifier.toIdentifier(table.getCatalog(), table.isCatalogQuoted()), Identifier.toIdentifier(table.getSchema(), table.isSchemaQuoted()), table.getNameIdentifier());
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
StringBuilder buf = new StringBuilder(tableCreateString(table.hasPrimaryKey())).append(' ').append(jdbcEnvironment.getQualifiedObjectNameFormatter().format(tableName, jdbcEnvironment.getDialect())).append(" (");
boolean isPrimaryKeyIdentity = table.hasPrimaryKey() && table.getIdentifierValue() != null && table.getIdentifierValue().isIdentityColumn(metadata.getIdentifierGeneratorFactory(), dialect);
// this is the much better form moving forward as we move to metamodel
//boolean isPrimaryKeyIdentity = hasPrimaryKey
// && table.getPrimaryKey().getColumnSpan() == 1
// && table.getPrimaryKey().getColumn( 0 ).isIdentity();
// Try to find out the name of the primary key in case the dialect needs it to create an identity
String pkColName = null;
if (table.hasPrimaryKey()) {
Column pkColumn = (Column) table.getPrimaryKey().getColumns().iterator().next();
pkColName = pkColumn.getQuotedName(dialect);
}
final Iterator columnItr = table.getColumnIterator();
boolean isFirst = true;
while (columnItr.hasNext()) {
final Column col = (Column) columnItr.next();
if (isFirst) {
isFirst = false;
} else {
buf.append(", ");
}
String colName = col.getQuotedName(dialect);
buf.append(colName).append(' ');
if (isPrimaryKeyIdentity && colName.equals(pkColName)) {
// to support dialects that have their own identity data type
if (dialect.getIdentityColumnSupport().hasDataTypeInIdentityColumn()) {
buf.append(col.getSqlType(dialect, metadata));
}
buf.append(' ').append(dialect.getIdentityColumnSupport().getIdentityColumnString(col.getSqlTypeCode(metadata)));
} else {
buf.append(col.getSqlType(dialect, metadata));
String defaultValue = col.getDefaultValue();
if (defaultValue != null) {
buf.append(" default ").append(defaultValue);
}
if (col.isNullable()) {
buf.append(dialect.getNullColumnString());
} else {
buf.append(" not null");
}
}
if (col.isUnique()) {
String keyName = Constraint.generateName("UK_", table, col);
UniqueKey uk = table.getOrCreateUniqueKey(keyName);
uk.addColumn(col);
buf.append(dialect.getUniqueDelegate().getColumnDefinitionUniquenessFragment(col));
}
if (col.getCheckConstraint() != null && dialect.supportsColumnCheck()) {
buf.append(" check (").append(col.getCheckConstraint()).append(")");
}
String columnComment = col.getComment();
if (columnComment != null) {
buf.append(dialect.getColumnComment(columnComment));
}
}
if (table.hasPrimaryKey()) {
buf.append(", ").append(table.getPrimaryKey().sqlConstraintString(dialect));
}
buf.append(dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment(table));
applyTableCheck(table, buf);
buf.append(')');
if (table.getComment() != null) {
buf.append(dialect.getTableComment(table.getComment()));
}
applyTableTypeString(buf);
List<String> sqlStrings = new ArrayList<String>();
sqlStrings.add(buf.toString());
applyComments(table, tableName, sqlStrings);
applyInitCommands(table, sqlStrings);
return sqlStrings.toArray(new String[sqlStrings.size()]);
}
use of org.hibernate.engine.jdbc.env.spi.JdbcEnvironment in project hibernate-orm by hibernate.
the class StandardTableExporter method getSqlDropStrings.
@Override
public String[] getSqlDropStrings(Table table, Metadata metadata) {
StringBuilder buf = new StringBuilder("drop table ");
if (dialect.supportsIfExistsBeforeTableName()) {
buf.append("if exists ");
}
final QualifiedName tableName = new QualifiedNameParser.NameParts(Identifier.toIdentifier(table.getCatalog(), table.isCatalogQuoted()), Identifier.toIdentifier(table.getSchema(), table.isSchemaQuoted()), table.getNameIdentifier());
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
buf.append(jdbcEnvironment.getQualifiedObjectNameFormatter().format(tableName, jdbcEnvironment.getDialect())).append(dialect.getCascadeConstraintsString());
if (dialect.supportsIfExistsAfterTableName()) {
buf.append(" if exists");
}
return new String[] { buf.toString() };
}
use of org.hibernate.engine.jdbc.env.spi.JdbcEnvironment in project hibernate-orm by hibernate.
the class DefaultUniqueDelegate method getAlterTableToDropUniqueKeyCommand.
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(uniqueKey.getTable().getQualifiedTableName(), dialect);
final StringBuilder buf = new StringBuilder("alter table ");
buf.append(tableName);
buf.append(getDropUnique());
if (dialect.supportsIfExistsBeforeConstraintName()) {
buf.append("if exists ");
}
buf.append(dialect.quote(uniqueKey.getName()));
if (dialect.supportsIfExistsAfterConstraintName()) {
buf.append(" if exists");
}
return buf.toString();
}
use of org.hibernate.engine.jdbc.env.spi.JdbcEnvironment in project hibernate-orm by hibernate.
the class TableStructure method registerExportables.
@Override
public void registerExportables(Database database) {
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final Namespace namespace = database.locateNamespace(logicalQualifiedTableName.getCatalogName(), logicalQualifiedTableName.getSchemaName());
Table table = namespace.locateTable(logicalQualifiedTableName.getObjectName());
if (table == null) {
table = namespace.createTable(logicalQualifiedTableName.getObjectName(), false);
}
this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(table.getQualifiedTableName(), dialect);
this.valueColumnNameText = logicalValueColumnNameIdentifier.render(dialect);
this.selectQuery = "select " + valueColumnNameText + " as id_val" + " from " + dialect.appendLockHint(LockMode.PESSIMISTIC_WRITE, tableNameText) + dialect.getForUpdateString();
this.updateQuery = "update " + tableNameText + " set " + valueColumnNameText + "= ?" + " where " + valueColumnNameText + "=?";
ExportableColumn valueColumn = new ExportableColumn(database, table, valueColumnNameText, LongType.INSTANCE);
table.addColumn(valueColumn);
table.addInitCommand(new InitCommand("insert into " + tableNameText + " values ( " + initialValue + " )"));
}
use of org.hibernate.engine.jdbc.env.spi.JdbcEnvironment in project hibernate-orm by hibernate.
the class PersistentTableBulkIdStrategy method initialize.
@Override
protected void initialize(MetadataBuildingOptions buildingOptions, SessionFactoryOptions sessionFactoryOptions) {
final StandardServiceRegistry serviceRegistry = buildingOptions.getServiceRegistry();
final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
final ConfigurationService configService = serviceRegistry.getService(ConfigurationService.class);
final String catalogName = configService.getSetting(CATALOG, StandardConverters.STRING, configService.getSetting(AvailableSettings.DEFAULT_CATALOG, StandardConverters.STRING));
final String schemaName = configService.getSetting(SCHEMA, StandardConverters.STRING, configService.getSetting(AvailableSettings.DEFAULT_SCHEMA, StandardConverters.STRING));
this.catalog = jdbcEnvironment.getIdentifierHelper().toIdentifier(catalogName);
this.schema = jdbcEnvironment.getIdentifierHelper().toIdentifier(schemaName);
this.dropIdTables = configService.getSetting(DROP_ID_TABLES, StandardConverters.BOOLEAN, false);
}
Aggregations