public void createPrimaryKey() { //Primary key constraint PrimaryKey pk = new PrimaryKey( table ); pk.setName( PK_ALIAS.toAliasString( table.getName() ) ); table.setPrimaryKey(pk); pk.addColumns( getKey().getColumnIterator() ); }
@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() ); boolean isPrimaryKeyIdentity = table.hasPrimaryKey() && table.getIdentifierValue() != null && table.getIdentifierValue().isIdentityColumn( metadata.getIdentifierGeneratorFactory(), dialect ); if ( table.hasPrimaryKey() ) { Column pkColumn = (Column) table.getPrimaryKey().getColumns().iterator().next(); pkColName = pkColumn.getQuotedName( dialect ); buf.append( ", " ); String colName = col.getQuotedName( dialect ); buf.append( col.getSqlType( dialect, metadata ) ); if ( table.hasPrimaryKey() ) { buf.append( ", " ) .append( table.getPrimaryKey().sqlConstraintString( dialect ) );
void createPrimaryKey() { if ( !isOneToMany() ) { PrimaryKey pk = new PrimaryKey( getCollectionTable() ); pk.addColumns( getKey().getColumnIterator() ); Iterator iter = getElement().getColumnIterator(); while ( iter.hasNext() ) { Object selectable = iter.next(); if ( selectable instanceof Column ) { Column col = (Column) selectable; if ( !col.isNullable() ) { pk.addColumn( col ); } else { return; } } } if ( pk.getColumnSpan() == getKey().getColumnSpan() ) { //for backward compatibility, allow a set with no not-null //element columns, using all columns in the row locater SQL //TODO: create an implicit not null constraint on all cols? } else { getCollectionTable().setPrimaryKey( pk ); } } else { //create an index on the key columns?? } }
@Override public String getExportIdentifier() { return StringHelper.qualify( getTable().getExportIdentifier(), "PK-" + getName() ); } }
private boolean isSameAsPrimaryKeyColumns(UniqueKey uniqueKey) { if ( primaryKey == null || ! primaryKey.columnIterator().hasNext() ) { // happens for many-to-many tables return false; } return primaryKey.getColumns().containsAll( uniqueKey.getColumns() ) && uniqueKey.getColumns().containsAll( primaryKey.getColumns() ); }
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) { StringBuilder buf = new StringBuilder( hasPrimaryKey() ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString() ) .append( ' ' ) .append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) ) .append( " (" ); boolean identityColumn = idValue != null && idValue.isIdentityColumn( p.getIdentifierGeneratorFactory(), dialect ); if ( hasPrimaryKey() && identityColumn ) { pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect ); Column col = (Column) iter.next(); buf.append( col.getQuotedName( dialect ) ) .append( ' ' ); if ( identityColumn && col.getQuotedName( dialect ).equals( pkname ) ) { if ( hasPrimaryKey() ) { buf.append( ", " ) .append( getPrimaryKey().sqlConstraintString( dialect ) );
Metadata metadata = database.getMetadata(); Iterator columnIterator = hibernateTable.getColumnIterator(); while (columnIterator.hasNext()) { org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next(); if (hibernateColumn.getName().equalsIgnoreCase(column.getName())) { String hibernateType = hibernateColumn.getSqlType(dialect, metadata); Matcher defaultValueMatcher = Pattern.compile("(?i) DEFAULT\\s+(.*)").matcher(hibernateType); if (defaultValueMatcher.find()) { DataType dataType = toDataType(hibernateType, hibernateColumn.getSqlTypeCode()); if (dataType == null) { throw new DatabaseException("Unable to find column data type for column " + hibernateColumn.getName()); column.setCertainDataType(false); org.hibernate.mapping.PrimaryKey hibernatePrimaryKey = hibernateTable.getPrimaryKey(); if (hibernatePrimaryKey != null) { boolean isPrimaryKeyColumn = false; for (org.hibernate.mapping.Column pkColumn : (List<org.hibernate.mapping.Column>) hibernatePrimaryKey.getColumns()) { if (pkColumn.getName().equalsIgnoreCase(hibernateColumn.getName())) { isPrimaryKeyColumn = true;
@Test public void testBasic() throws SQLException { JUnitUtil.assertIteratorContainsExactly( "There should be three tables!", metadata.getEntityBindings().iterator(), 3); Table table = HibernateUtil.getTable( metadata, JdbcUtil.toIdentifier(this, "BASIC")); Assert.assertEquals( JdbcUtil.toIdentifier(this, "BASIC"), JdbcUtil.toIdentifier(this, table.getName())); Assert.assertEquals(2, table.getColumnSpan()); Column basicColumn = table.getColumn(0); Assert.assertEquals( JdbcUtil.toIdentifier(this, "A"), JdbcUtil.toIdentifier(this, basicColumn.getName())); PrimaryKey key = table.getPrimaryKey(); Assert.assertNotNull("There should be a primary key!", key); Assert.assertEquals(key.getColumnSpan(), 1); Column column = key.getColumn(0); Assert.assertTrue(column.isUnique()); Assert.assertSame(basicColumn, column); }
private void alignColumns(Table referencedTable) { final int referencedPkColumnSpan = referencedTable.getPrimaryKey().getColumnSpan(); if ( referencedPkColumnSpan != getColumnSpan() ) { StringBuilder sb = new StringBuilder(); sb.append( "Foreign key (" ).append( getName() ).append( ":" ) .append( getTable().getName() ) .append( " [" ); appendColumns( sb, getColumnIterator() ); sb.append( "])" ) .append( ") must have same number of columns as the referenced primary key (" ) .append( referencedTable.getName() ) .append( " [" ); appendColumns( sb, referencedTable.getPrimaryKey().getColumnIterator() ); sb.append( "])" ); throw new MappingException( sb.toString() ); } Iterator fkCols = getColumnIterator(); Iterator pkCols = referencedTable.getPrimaryKey().getColumnIterator(); while ( pkCols.hasNext() ) { ( (Column) fkCols.next() ).setLength( ( (Column) pkCols.next() ).getLength() ); } }
try { Map<String, Object> primaryKeyRs = null; primaryKeyIterator = metaDataDialect.getPrimaryKeys(getCatalogForDBLookup(table.getCatalog(), defaultCatalog), getSchemaForDBLookup(table.getSchema(), defaultSchema), table.getName() ); key = new PrimaryKey(table); key.setName(name); key.setTable(table); if(table.getPrimaryKey()!=null) { throw new JdbcBinderException(table + " already has a primary key!"); //TODO: ignore ? if(!(name==key.getName() ) && name!=null && !name.equals(key.getName() ) ) { throw new JdbcBinderException("Duplicate names found for primarykey. Existing name: " + key.getName() + " JDBC name: " + name + " on table " + table); log.warn("The JDBC driver didn't report any primary key columns in " + table.getName() + ". Asking rev.eng. strategy" ); List<String> userPrimaryKey = RevEngUtils.getPrimaryKeyInfoInRevengStrategy(revengStrategy, table, defaultCatalog, defaultSchema); if(userPrimaryKey!=null && !userPrimaryKey.isEmpty()) { key = new PrimaryKey(table); key.setName(new Alias(15, "PK").toAliasString( table.getName())); key.setTable(table); if(table.getPrimaryKey()!=null) { throw new JdbcBinderException(table + " already has a primary key!"); //TODO: ignore ? key.addColumn(col);
TypeTranslator typeTranslator = sessionFactoryImplementor.getServiceRegistry() .getService( TypeTranslator.class ); datastoreProvider.setTableMetadata( table.getName(), table ); if ( table.hasPrimaryKey() ) { for ( Object pkColumn : table.getPrimaryKey().getColumns() ) { primaryKeys.add( ( (Column) pkColumn ).getName() ); while ( columnIterator.hasNext() ) { Column column = columnIterator.next(); columnNames.add( column.getName() ); Value value = column.getValue(); Type type = value.getType();
if ("table".equals(name)) { for (final Column column : this.currentColumns.values()) { this.currentTable.addColumn(column); this.currentTable.setPrimaryKey(this.primaryKey); this.tables.put(this.currentTable.getName(), this.currentTable); this.tableColumnTypes.put(this.currentTable.getName(), this.currentColumnTypes); this.primaryKey = null; this.currentTable = null; } else if ("column".equals(name)) { this.currentColumns.put(this.currentColumn.getName(), this.currentColumn); this.currentColumn = null; } else if ("name".equals(name)) { this.currentTable = new Table(itemName); } else if (this.currentColumn == null) { this.currentColumn = new Column(itemName); this.currentColumnTypes.put(this.currentColumn.getName(), sqlType); this.primaryKey = new PrimaryKey(); this.primaryKey.addColumn(column); } else if ("not-null".equals(name)) { final String columnName = this.chars.toString().trim();
PrimaryKeyColumn pkcol = mdTable.getPrimaryKey().getPrimaryKeyColumns().get(0); com.manydesigns.portofino.model.database.Column column = columnPKList.get(0); final PrimaryKey primaryKey = new PrimaryKey(); primaryKey.setName(pkName); //TODO quote? primaryKey.setTable(tab); tab.setPrimaryKey(primaryKey); SimpleValue id = (SimpleValue) col.getValue(); id.setNullValue("undefined"); tab.getPrimaryKey().addColumn(col); tab.setIdentifierValue(id); clazz.setIdentifier(id); clazz.setIdentifierProperty(prop);
while (tableMappings.hasNext()) { org.hibernate.mapping.Table hibernateTable = (org.hibernate.mapping.Table) tableMappings.next(); Iterator fkIterator = hibernateTable.getForeignKeyIterator(); while (fkIterator.hasNext()) { org.hibernate.mapping.ForeignKey hibernateForeignKey = (org.hibernate.mapping.ForeignKey) fkIterator.next(); Table currentTable = new Table().setName(hibernateTable.getName()); currentTable.setSchema(hibernateTable.getCatalog(), hibernateTable.getSchema()); fk.setForeignKeyTable(currentTable); for (Object column : hibernateForeignKey.getColumns()) { fk.addForeignKeyColumn(new liquibase.structure.core.Column(((org.hibernate.mapping.Column) column).getName())); fk.addPrimaryKeyColumn(new liquibase.structure.core.Column(((org.hibernate.mapping.Column) column).getName())); for (Object column : hibernateReferencedTable.getPrimaryKey().getColumns()) { fk.addPrimaryKeyColumn(new liquibase.structure.core.Column(((org.hibernate.mapping.Column) column).getName()));
public void prepareTemporaryTables(Mapping mapping, Dialect dialect) { if ( dialect.supportsTemporaryTables() ) { temporaryIdTableName = dialect.generateTemporaryTableName( getTable().getName() ); Table table = new Table(); table.setName( temporaryIdTableName ); Iterator itr = getTable().getPrimaryKey().getColumnIterator(); while( itr.hasNext() ) { Column column = (Column) itr.next(); table.addColumn( (Column) column.clone() ); } temporaryIdTableDDL = table.sqlTemporaryTableCreateString( dialect, mapping ); } }
ColumnType type = new ColumnType(mappedColumn.getSqlTypeCode(m_mapping), mappedColumn.getSqlType(m_dialect, m_mapping)); column.setColumnType(type); if (mappedColumn.getPrecision() != org.hibernate.mapping.Column.DEFAULT_PRECISION) { column.setColumnSize(mappedColumn.getPrecision()); boolean notNull = !mappedColumn.isNullable() || (owner.getPrimaryKey() != null && owner.getPrimaryKey().getColumns().contains(mappedColumn)); column.setIsNullable(!notNull);
void createPrimaryKey() { if ( !isOneToMany() ) { PrimaryKey pk = new PrimaryKey( getCollectionTable() ); pk.addColumns( getIdentifier().getColumnIterator() ); getCollectionTable().setPrimaryKey(pk); } // create an index on the key columns?? }
public boolean matches(ForeignKey fk) { if ( refTable.equalsIgnoreCase( fk.getReferencedTable().getName() ) ) { if ( fk.getColumnSpan() == references.size() ) { List fkRefs; if ( fk.isReferenceToPrimaryKey() ) { fkRefs = fk.getReferencedTable().getPrimaryKey().getColumns(); } else { fkRefs = fk.getReferencedColumns(); } for ( int i = 0; i < fk.getColumnSpan(); i++ ) { Column column = fk.getColumn( i ); Column ref = ( Column ) fkRefs.get( i ); if ( !hasReference( column, ref ) ) { return false; } } return true; } } return false; }
dialect.getTypeName( Types.VARCHAR, segmentValueLength, 0, 0 ) ); segmentColumn.setNullable( false ); table.addColumn( segmentColumn ); table.setPrimaryKey( new PrimaryKey( table ) ); table.getPrimaryKey().addColumn( segmentColumn );
if (table.getPrimaryKey()!=null) { keyColumns = table.getPrimaryKey().getColumns(); Iterator<?> iter = table.getColumnIterator(); while (iter.hasNext() ) { Column col = (Column) iter.next(); idPropertyname = revengStrategy.columnToPropertyName(tableIdentifier, pkc.getName() );