Search in sources :

Example 11 with QualifiedTableName

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

the class Table method sqlAlterStrings.

public Iterator sqlAlterStrings(Dialect dialect, Metadata metadata, TableInformation tableInfo, String defaultCatalog, String defaultSchema) throws HibernateException {
    final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
    Identifier quotedCatalog = catalog != null && catalog.isQuoted() ? new Identifier(tableInfo.getName().getCatalogName().getText(), true) : tableInfo.getName().getCatalogName();
    Identifier quotedSchema = schema != null && schema.isQuoted() ? new Identifier(tableInfo.getName().getSchemaName().getText(), true) : tableInfo.getName().getSchemaName();
    Identifier quotedTable = name != null && name.isQuoted() ? new Identifier(tableInfo.getName().getObjectName().getText(), true) : tableInfo.getName().getObjectName();
    final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(new QualifiedTableName(quotedCatalog, quotedSchema, quotedTable), dialect);
    StringBuilder root = new StringBuilder(dialect.getAlterTableString(tableName)).append(' ').append(dialect.getAddColumnString());
    Iterator iter = getColumnIterator();
    List results = new ArrayList();
    while (iter.hasNext()) {
        final Column column = (Column) iter.next();
        final ColumnInformation columnInfo = tableInfo.getColumn(Identifier.toIdentifier(column.getName(), column.isQuoted()));
        if (columnInfo == null) {
            // the column doesnt exist at all.
            StringBuilder alter = new StringBuilder(root.toString()).append(' ').append(column.getQuotedName(dialect)).append(' ').append(column.getSqlType(dialect, metadata));
            String defaultValue = column.getDefaultValue();
            if (defaultValue != null) {
                alter.append(" default ").append(defaultValue);
            }
            if (column.isNullable()) {
                alter.append(dialect.getNullColumnString());
            } else {
                alter.append(" not null");
            }
            if (column.isUnique()) {
                String keyName = Constraint.generateName("UK_", this, column);
                UniqueKey uk = getOrCreateUniqueKey(keyName);
                uk.addColumn(column);
                alter.append(dialect.getUniqueDelegate().getColumnDefinitionUniquenessFragment(column));
            }
            if (column.hasCheckConstraint() && dialect.supportsColumnCheck()) {
                alter.append(" check(").append(column.getCheckConstraint()).append(")");
            }
            String columnComment = column.getComment();
            if (columnComment != null) {
                alter.append(dialect.getColumnComment(columnComment));
            }
            alter.append(dialect.getAddColumnSuffixString());
            results.add(alter.toString());
        }
    }
    if (results.isEmpty()) {
        log.debugf("No alter strings for table : %s", getQuotedName());
    }
    return results.iterator();
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Identifier(org.hibernate.boot.model.naming.Identifier) ColumnInformation(org.hibernate.tool.schema.extract.spi.ColumnInformation) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 12 with QualifiedTableName

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

the class CheckForExistingForeignKeyTest method testKeyWithSameNameNotExists.

/**
 * Check detection of existing foreign keys with the same name exists.
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testKeyWithSameNameNotExists() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    foreignKey.setName("objectId2id_1");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(new ForeignKeyInformationImpl(new Identifier("objectId2id_2", false), new ArrayList<>()));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    // foreignKey name with same name should match
    boolean found = (boolean) method.invoke(new SchemaMigrator(), foreignKey, tableInformation);
    Assert.assertFalse("Key should not be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) ForeignKeyInformationImpl(org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) Test(org.junit.Test)

Example 13 with QualifiedTableName

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

the class CheckForExistingForeignKeyTest method getColumnInformation.

private ColumnInformation getColumnInformation(String tableName, String columnName) {
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    TableInformation containingTableInformation = new TableInformationImpl(null, null, new QualifiedTableName(schemaName, new Identifier(tableName, false)), false, null);
    Identifier columnIdentifier = new Identifier(columnName, false);
    int typeCode = 0;
    String typeName = null;
    int columnSize = 0;
    int decimalDigits = 0;
    TruthValue nullable = null;
    ColumnInformationImpl columnInformation = new ColumnInformationImpl(containingTableInformation, columnIdentifier, typeCode, typeName, columnSize, decimalDigits, nullable);
    return columnInformation;
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) TruthValue(org.hibernate.boot.model.TruthValue) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) ColumnInformationImpl(org.hibernate.tool.schema.extract.internal.ColumnInformationImpl) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName)

Example 14 with QualifiedTableName

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

the class CheckForExistingForeignKeyTest method testCheckForNotExistingForeignKeyOne2One.

/**
 * Check detection of not existing foreign key with the same mappings for a simple mapping (table1.objectId =>
 * table2.id).
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testCheckForNotExistingForeignKeyOne2One() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    // Make sure the match is not successful based on key name
    foreignKey.setName("objectId2id_1");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(getForeignKeyInformation("table2", "blah", "blahKey_001"));
    fks.add(getForeignKeyInformation("table3", "id", "blahKey_002"));
    fks.add(getForeignKeyInformation("table3", "blah", "blahKey_003"));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    AbstractSchemaMigrator schemaMigrator = new SchemaMigrator();
    // Check single-column-key to single-column-key, existing (table1.objectId => table2.id)
    boolean found = (boolean) method.invoke(schemaMigrator, foreignKey, tableInformation);
    Assert.assertFalse("Key should not be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Test(org.junit.Test)

Example 15 with QualifiedTableName

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

the class CheckForExistingForeignKeyTest method testKeyWithSameNameExists.

/**
 * Check detection of existing foreign keys with the same name exists.
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testKeyWithSameNameExists() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    foreignKey.setName("objectId2id");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(new ForeignKeyInformationImpl(new Identifier("objectId2id", false), new ArrayList<>()));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    // foreignKey name with same name should match
    boolean found = (boolean) method.invoke(new SchemaMigrator(), foreignKey, tableInformation);
    Assert.assertTrue("Key should be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) ForeignKeyInformationImpl(org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) Test(org.junit.Test)

Aggregations

QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)15 Identifier (org.hibernate.boot.model.naming.Identifier)10 ArrayList (java.util.ArrayList)8 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)6 Table (org.hibernate.mapping.Table)6 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)6 Name (org.hibernate.boot.model.relational.Namespace.Name)5 TableInformationImpl (org.hibernate.tool.schema.extract.internal.TableInformationImpl)5 ForeignKeyInformation (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation)5 Test (org.junit.Test)5 Method (java.lang.reflect.Method)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 IdentifierHelper (org.hibernate.engine.jdbc.env.spi.IdentifierHelper)4 Column (org.hibernate.mapping.Column)4 ForeignKey (org.hibernate.mapping.ForeignKey)4 InformationExtractor (org.hibernate.tool.schema.extract.spi.InformationExtractor)4 AbstractSchemaMigrator (org.hibernate.tool.schema.internal.AbstractSchemaMigrator)4 ColumnInformation (org.hibernate.tool.schema.extract.spi.ColumnInformation)3 HashMap (java.util.HashMap)2