Example usage for org.hibernate.mapping Column getSqlTypeCode

List of usage examples for org.hibernate.mapping Column getSqlTypeCode

Introduction

In this page you can find the example usage for org.hibernate.mapping Column getSqlTypeCode.

Prototype

public int getSqlTypeCode(Mapping mapping) throws MappingException 

Source Link

Usage

From source file:corner.migration.services.impl.fragment.AbstractMigrateFragment.java

License:Apache License

/**
 * ?SQL//w  w w .  ja va 2  s .  c  o  m
 * @param col 
 * @return SQL 
 * @see Table#sqlCreateString(Dialect, Mapping, String, String)
 */
protected String getSQLType(Column col) {
    //      boolean identityColumn = idValue != null && idValue.isIdentityColumn( dialect );
    boolean identityColumn = false;
    StringBuffer buf = new StringBuffer();
    Dialect dialect = getDialect();
    Mapping p = this.getSessionFactory();
    buf.append(col.getQuotedName(dialect)).append(' ');

    String pkname = "id";
    if (identityColumn && col.getQuotedName(dialect).equals(pkname)) {
        // to support dialects that have their own identity data type
        if (dialect.hasDataTypeInIdentityColumn()) {
            buf.append(col.getSqlType(dialect, p));
        }
        buf.append(' ').append(dialect.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");
        }

    }

    boolean useUniqueConstraint = col.isUnique() && (!col.isNullable() || dialect.supportsNotNullUnique());
    if (useUniqueConstraint) {
        if (dialect.supportsUnique()) {
            buf.append(" unique");
        } else {
            //               UniqueKey uk = getOrCreateUniqueKey( col.getQuotedName( dialect ) + '_' );
            //               uk.addColumn( 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));
    }
    return buf.toString();
}

From source file:org.beangle.commons.orm.hibernate.internal.SchemaValidator.java

License:Open Source License

private void validateColumns(Table table, Dialect dialect, Mapping mapping, TableMetadata tableInfo) {
    Iterator<?> iter = table.getColumnIterator();
    Set<ColumnMetadata> processed = CollectUtils.newHashSet();
    String tableName = Table.qualify(tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName());
    while (iter.hasNext()) {
        Column col = (Column) iter.next();
        ColumnMetadata columnInfo = tableInfo.getColumnMetadata(col.getName());
        if (columnInfo == null) {
            reporter.append("Missing column: " + col.getName() + " in " + tableName);
        } else {/*from   w  w w.jav  a2 s.co  m*/
            final boolean typesMatch = col.getSqlType(dialect, mapping).toLowerCase()
                    .startsWith(columnInfo.getTypeName().toLowerCase())
                    || columnInfo.getTypeCode() == col.getSqlTypeCode(mapping);
            if (!typesMatch) {
                reporter.append("Wrong column type in " + tableName + " for column " + col.getName()
                        + ". Found: " + columnInfo.getTypeName().toLowerCase() + ", expected: "
                        + col.getSqlType(dialect, mapping));
            }
            processed.add(columnInfo);
        }
    }
    // 
    try {
        Field field = tableInfo.getClass().getField("columns");
        @SuppressWarnings("unchecked")
        Set<ColumnMetadata> allColumns = CollectUtils
                .newHashSet(((Map<String, ColumnMetadata>) field.get(tableInfo)).values());
        allColumns.removeAll(processed);
        if (!allColumns.isEmpty()) {
            for (ColumnMetadata col : allColumns) {
                reporter.append("Extra column " + col.getName() + " in " + tableName);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}