Search in sources :

Example 1 with QualifiedName

use of org.hibernate.boot.model.relational.QualifiedName in project hibernate-orm by hibernate.

the class SequenceStyleGenerator method configure.

// Configurable implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
    final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
    final Dialect dialect = jdbcEnvironment.getDialect();
    this.identifierType = type;
    boolean forceTableUse = ConfigurationHelper.getBoolean(FORCE_TBL_PARAM, params, false);
    final QualifiedName sequenceName = determineSequenceName(params, dialect, jdbcEnvironment);
    final int initialValue = determineInitialValue(params);
    int incrementSize = determineIncrementSize(params);
    final String optimizationStrategy = determineOptimizationStrategy(params, incrementSize);
    incrementSize = determineAdjustedIncrementSize(optimizationStrategy, incrementSize);
    if (dialect.supportsSequences() && !forceTableUse) {
        if (!dialect.supportsPooledSequences() && OptimizerFactory.isPooledOptimizer(optimizationStrategy)) {
            forceTableUse = true;
            LOG.forcingTableUse();
        }
    }
    this.databaseStructure = buildDatabaseStructure(type, params, jdbcEnvironment, forceTableUse, sequenceName, initialValue, incrementSize);
    this.optimizer = OptimizerFactory.buildOptimizer(optimizationStrategy, identifierType.getReturnedClass(), incrementSize, ConfigurationHelper.getInt(INITIAL_PARAM, params, -1));
    this.databaseStructure.prepare(optimizer);
}
Also used : QualifiedName(org.hibernate.boot.model.relational.QualifiedName) Dialect(org.hibernate.dialect.Dialect) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 2 with QualifiedName

use of org.hibernate.boot.model.relational.QualifiedName 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 3 with QualifiedName

use of org.hibernate.boot.model.relational.QualifiedName 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)

Aggregations

QualifiedName (org.hibernate.boot.model.relational.QualifiedName)3 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)3 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Dialect (org.hibernate.dialect.Dialect)1 Column (org.hibernate.mapping.Column)1 UniqueKey (org.hibernate.mapping.UniqueKey)1