Search in sources :

Example 16 with JdbcEnvironment

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()]);
}
Also used : Column(org.hibernate.mapping.Column) UniqueKey(org.hibernate.mapping.UniqueKey) QualifiedName(org.hibernate.boot.model.relational.QualifiedName) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 17 with JdbcEnvironment

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() };
}
Also used : QualifiedName(org.hibernate.boot.model.relational.QualifiedName) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 18 with JdbcEnvironment

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();
}
Also used : JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 19 with JdbcEnvironment

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 + " )"));
}
Also used : Table(org.hibernate.mapping.Table) InitCommand(org.hibernate.boot.model.relational.InitCommand) Dialect(org.hibernate.dialect.Dialect) ExportableColumn(org.hibernate.id.ExportableColumn) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) Namespace(org.hibernate.boot.model.relational.Namespace)

Example 20 with JdbcEnvironment

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);
}
Also used : ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Aggregations

JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)29 Iterator (java.util.Iterator)6 Namespace (org.hibernate.boot.model.relational.Namespace)5 Dialect (org.hibernate.dialect.Dialect)5 Column (org.hibernate.mapping.Column)5 Table (org.hibernate.mapping.Table)5 Identifier (org.hibernate.boot.model.naming.Identifier)4 QualifiedName (org.hibernate.boot.model.relational.QualifiedName)3 Sequence (org.hibernate.boot.model.relational.Sequence)3 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EntityMode (org.hibernate.EntityMode)2 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)2 Database (org.hibernate.boot.model.relational.Database)2 InitCommand (org.hibernate.boot.model.relational.InitCommand)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)2 ExtractedDatabaseMetaData (org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData)2 FilterDefinition (org.hibernate.engine.spi.FilterDefinition)2