Java Code Examples for org.hibernate.dialect.Dialect#supportsColumnCheck()

The following examples show how to use org.hibernate.dialect.Dialect#supportsColumnCheck() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Table.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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();
}
 
Example 2
Source File: Table.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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 );

	// Try to find out the name of the primary key to create it as identity if the IdentityGenerator is used
	String pkname = null;
	if ( hasPrimaryKey() && identityColumn ) {
		pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect );
	}

	Iterator iter = getColumnIterator();
	while ( iter.hasNext() ) {
		Column col = (Column) iter.next();

		buf.append( col.getQuotedName( dialect ) )
				.append( ' ' );

		if ( identityColumn && col.getQuotedName( dialect ).equals( pkname ) ) {
			// to support dialects that have their own identity data type
			if ( dialect.getIdentityColumnSupport().hasDataTypeInIdentityColumn() ) {
				buf.append( col.getSqlType( dialect, p ) );
			}
			buf.append( ' ' )
					.append( dialect.getIdentityColumnSupport().getIdentityColumnString( col.getSqlTypeCode( p ) ) );
		}
		else {

			buf.append( col.getSqlType( dialect, p ) );

			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_", this, col );
			UniqueKey uk = getOrCreateUniqueKey( keyName );
			uk.addColumn( col );
			buf.append( dialect.getUniqueDelegate()
					.getColumnDefinitionUniquenessFragment( col ) );
		}
			
		if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
			buf.append( " check (" )
					.append( col.getCheckConstraint() )
					.append( ")" );
		}

		String columnComment = col.getComment();
		if ( columnComment != null ) {
			buf.append( dialect.getColumnComment( columnComment ) );
		}

		if ( iter.hasNext() ) {
			buf.append( ", " );
		}

	}
	if ( hasPrimaryKey() ) {
		buf.append( ", " )
				.append( getPrimaryKey().sqlConstraintString( dialect ) );
	}

	buf.append( dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment( this ) );

	if ( dialect.supportsTableCheck() ) {
		for ( String checkConstraint : checkConstraints ) {
			buf.append( ", check (" )
					.append( checkConstraint )
					.append( ')' );
		}
	}

	buf.append( ')' );

	if ( comment != null ) {
		buf.append( dialect.getTableComment( comment ) );
	}

	return buf.append( dialect.getTableTypeString() ).toString();
}
 
Example 3
Source File: Table.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
								String defaultSchema)
		throws HibernateException {

	StringBuffer root = new StringBuffer( "alter table " )
			.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
			.append( ' ' )
			.append( dialect.getAddColumnString() );

	Iterator iter = getColumnIterator();
	List results = new ArrayList();
	while ( iter.hasNext() ) {
		Column column = (Column) iter.next();

		ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

		if ( columnInfo == null ) {
			// the column doesnt exist at all.
			StringBuffer alter = new StringBuffer( root.toString() )
					.append( ' ' )
					.append( column.getQuotedName( dialect ) )
					.append( ' ' )
					.append( column.getSqlType( dialect, p ) );

			String defaultValue = column.getDefaultValue();
			if ( defaultValue != null ) {
				alter.append( " default " ).append( defaultValue );
			}

			if ( column.isNullable() ) {
				alter.append( dialect.getNullColumnString() );
			}
			else {
				alter.append( " not null" );
			}

			boolean useUniqueConstraint = column.isUnique() &&
					dialect.supportsUnique() &&
					( !column.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				alter.append( " unique" );
			}

			if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				alter.append( " check(" )
						.append( column.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = column.getComment();
			if ( columnComment != null ) {
				alter.append( dialect.getColumnComment( columnComment ) );
			}

			results.add( alter.toString() );
		}

	}

	return results.iterator();
}
 
Example 4
Source File: Table.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
								String defaultSchema)
		throws HibernateException {

	StringBuffer root = new StringBuffer( "alter table " )
			.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
			.append( ' ' )
			.append( dialect.getAddColumnString() );

	Iterator iter = getColumnIterator();
	List results = new ArrayList();
	while ( iter.hasNext() ) {
		Column column = (Column) iter.next();

		ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

		if ( columnInfo == null ) {
			// the column doesnt exist at all.
			StringBuffer alter = new StringBuffer( root.toString() )
					.append( ' ' )
					.append( column.getQuotedName( dialect ) )
					.append( ' ' )
					.append( column.getSqlType( dialect, p ) );

			String defaultValue = column.getDefaultValue();
			if ( defaultValue != null ) {
				alter.append( " default " ).append( defaultValue );
			}

			if ( column.isNullable() ) {
				alter.append( dialect.getNullColumnString() );
			}
			else {
				alter.append( " not null" );
			}

			boolean useUniqueConstraint = column.isUnique() &&
					dialect.supportsUnique() &&
					( !column.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				alter.append( " unique" );
			}

			if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				alter.append( " check(" )
						.append( column.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = column.getComment();
			if ( columnComment != null ) {
				alter.append( dialect.getColumnComment( columnComment ) );
			}

			results.add( alter.toString() );
		}

	}

	return results.iterator();
}
 
Example 5
Source File: Table.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
								String defaultSchema)
		throws HibernateException {

	StringBuffer root = new StringBuffer( "alter table " )
			.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
			.append( ' ' )
			.append( dialect.getAddColumnString() );

	Iterator iter = getColumnIterator();
	List results = new ArrayList();
	while ( iter.hasNext() ) {
		Column column = (Column) iter.next();

		ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

		if ( columnInfo == null ) {
			// the column doesnt exist at all.
			StringBuffer alter = new StringBuffer( root.toString() )
					.append( ' ' )
					.append( column.getQuotedName( dialect ) )
					.append( ' ' )
					.append( column.getSqlType( dialect, p ) );

			String defaultValue = column.getDefaultValue();
			if ( defaultValue != null ) {
				alter.append( " default " ).append( defaultValue );
			}

			if ( column.isNullable() ) {
				alter.append( dialect.getNullColumnString() );
			}
			else {
				alter.append( " not null" );
			}

			boolean useUniqueConstraint = column.isUnique() &&
					dialect.supportsUnique() &&
					( !column.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				alter.append( " unique" );
			}

			if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				alter.append( " check(" )
						.append( column.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = column.getComment();
			if ( columnComment != null ) {
				alter.append( dialect.getColumnComment( columnComment ) );
			}

			results.add( alter.toString() );
		}

	}

	return results.iterator();
}
 
Example 6
Source File: Table.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
								String defaultSchema)
		throws HibernateException {

	StringBuffer root = new StringBuffer( "alter table " )
			.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
			.append( ' ' )
			.append( dialect.getAddColumnString() );

	Iterator iter = getColumnIterator();
	List results = new ArrayList();
	while ( iter.hasNext() ) {
		Column column = (Column) iter.next();

		ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

		if ( columnInfo == null ) {
			// the column doesnt exist at all.
			StringBuffer alter = new StringBuffer( root.toString() )
					.append( ' ' )
					.append( column.getQuotedName( dialect ) )
					.append( ' ' )
					.append( column.getSqlType( dialect, p ) );

			String defaultValue = column.getDefaultValue();
			if ( defaultValue != null ) {
				alter.append( " default " ).append( defaultValue );
			}

			if ( column.isNullable() ) {
				alter.append( dialect.getNullColumnString() );
			}
			else {
				alter.append( " not null" );
			}

			boolean useUniqueConstraint = column.isUnique() &&
					dialect.supportsUnique() &&
					( !column.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				alter.append( " unique" );
			}

			if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				alter.append( " check(" )
						.append( column.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = column.getComment();
			if ( columnComment != null ) {
				alter.append( dialect.getColumnComment( columnComment ) );
			}

			results.add( alter.toString() );
		}

	}

	return results.iterator();
}
 
Example 7
Source File: Table.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
								String defaultSchema)
		throws HibernateException {

	StringBuffer root = new StringBuffer( "alter table " )
			.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
			.append( ' ' )
			.append( dialect.getAddColumnString() );

	Iterator iter = getColumnIterator();
	List results = new ArrayList();
	while ( iter.hasNext() ) {
		Column column = (Column) iter.next();

		ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

		if ( columnInfo == null ) {
			// the column doesnt exist at all.
			StringBuffer alter = new StringBuffer( root.toString() )
					.append( ' ' )
					.append( column.getQuotedName( dialect ) )
					.append( ' ' )
					.append( column.getSqlType( dialect, p ) );

			String defaultValue = column.getDefaultValue();
			if ( defaultValue != null ) {
				alter.append( " default " ).append( defaultValue );
			}

			if ( column.isNullable() ) {
				alter.append( dialect.getNullColumnString() );
			}
			else {
				alter.append( " not null" );
			}

			boolean useUniqueConstraint = column.isUnique() &&
					dialect.supportsUnique() &&
					( !column.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				alter.append( " unique" );
			}

			if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				alter.append( " check(" )
						.append( column.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = column.getComment();
			if ( columnComment != null ) {
				alter.append( dialect.getColumnComment( columnComment ) );
			}

			results.add( alter.toString() );
		}

	}

	return results.iterator();
}
 
Example 8
Source File: Table.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
								String defaultSchema)
		throws HibernateException {

	StringBuffer root = new StringBuffer( "alter table " )
			.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
			.append( ' ' )
			.append( dialect.getAddColumnString() );

	Iterator iter = getColumnIterator();
	List results = new ArrayList();
	while ( iter.hasNext() ) {
		Column column = (Column) iter.next();

		ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

		if ( columnInfo == null ) {
			// the column doesnt exist at all.
			StringBuffer alter = new StringBuffer( root.toString() )
					.append( ' ' )
					.append( column.getQuotedName( dialect ) )
					.append( ' ' )
					.append( column.getSqlType( dialect, p ) );

			String defaultValue = column.getDefaultValue();
			if ( defaultValue != null ) {
				alter.append( " default " ).append( defaultValue );

				if ( column.isNullable() ) {
					alter.append( dialect.getNullColumnString() );
				}
				else {
					alter.append( " not null" );
				}

			}

			boolean useUniqueConstraint = column.isUnique() &&
					dialect.supportsUnique() &&
					( !column.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				alter.append( " unique" );
			}

			if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				alter.append( " check(" )
						.append( column.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = column.getComment();
			if ( columnComment != null ) {
				alter.append( dialect.getColumnComment( columnComment ) );
			}

			results.add( alter.toString() );
		}

	}

	return results.iterator();
}