www.pudn.com > dataswap0616.rar > Column.java


package com.gemt.dataswap.dataset; 
 
import java.sql.DatabaseMetaData; 
 
public class Column { 
    /** 
     * Indicates that the column might not allow NULL values. 
     */ 
    public static final Nullable NO_NULLS = new Nullable("noNulls"); 
    /** 
     * Indicates that the column definitely allows NULL values. 
     */ 
    public static final Nullable NULLABLE = new Nullable("nullable"); 
    /** 
     * Indicates that the nullability of columns is unknown. 
     */ 
    public static final Nullable NULLABLE_UNKNOWN = new Nullable("nullableUnknown"); 
 
    private final String _columnName; 
    private final String _fieldName; 
    private final String _dataType; 
    private final String _sqlTypeName; 
    private final Nullable _nullable; 
 
    /** 
     * Creates a Column object. This contructor set nullable to true. 
     * 
     * @param columnName the column name 
     * @param dataType the data type 
     */ 
    public Column(String columnName, String dataType, String fieldName) 
    { 
        _columnName = columnName; 
        _dataType = dataType; 
        _nullable = NULLABLE_UNKNOWN; 
        _sqlTypeName = DataType.DEFAULT_TYPE; 
        _fieldName = fieldName; 
    } 
 
    /** 
     * Creates a Column object. 
     */ 
    public Column(String columnName, String dataType, Nullable nullable, String fieldName) 
    { 
        _columnName = columnName; 
        _dataType = dataType; 
        _sqlTypeName = DataType.DEFAULT_TYPE; 
        _nullable = nullable; 
        _fieldName = fieldName; 
    } 
 
    /** 
     * Creates a Column object. 
     */ 
    public Column(String columnName, String dataType, String sqlTypeName, 
            Nullable nullable, String fieldName) 
    { 
        _columnName = columnName; 
        _dataType = dataType; 
        _sqlTypeName = sqlTypeName; 
        _nullable = nullable; 
        _fieldName = fieldName; 
    } 
 
    public String getFieldName() 
	{ 
		return _fieldName; 
	} 
 
	/** 
     * Returns this column name. 
     */ 
    public String getColumnName() 
    { 
        return _columnName; 
    } 
 
    /** 
     * Returns this column data type. 
     */ 
    public String getDataType() 
    { 
        return _dataType; 
    } 
 
    /** 
     * Returns this column sql data type name. 
     */ 
    public String getSqlTypeName() 
    { 
        return _sqlTypeName; 
    } 
 
    /** 
     * Returns true if this column is nullable. 
     */ 
    public Nullable getNullable() 
    { 
        return _nullable; 
    } 
 
    /** 
     * Returns the appropriate Nullable constant according specified JDBC 
     * DatabaseMetaData constant. 
     * 
     * @param nullable one of the following constants 
     * {@link java.sql.DatabaseMetaData#columnNoNulls}, 
     * {@link java.sql.DatabaseMetaData#columnNullable}, 
     * {@link java.sql.DatabaseMetaData#columnNullableUnknown} 
     */ 
    public static Nullable nullableValue(int nullable) 
    { 
        switch (nullable) 
        { 
            case DatabaseMetaData.columnNoNulls: 
                return NO_NULLS; 
 
            case DatabaseMetaData.columnNullable: 
                return NULLABLE; 
 
            case DatabaseMetaData.columnNullableUnknown: 
                return NULLABLE_UNKNOWN; 
 
            default: 
                throw new IllegalArgumentException("Unknown constant value " 
                        + nullable); 
        } 
    } 
 
    /** 
     * Returns the appropriate Nullable constant. 
     * 
     * @param nullable true if null is allowed 
     */ 
    public static Nullable nullableValue(boolean nullable) 
    { 
        return nullable ? NULLABLE : NO_NULLS; 
    } 
 
    //////////////////////////////////////////////////////////////////////////// 
    // Object class 
 
    public String toString() 
    { 
        return "(" + _columnName + ", " + _dataType + ", " + _nullable + ")"; 
//        return _columnName; 
    } 
 
    public boolean equals(Object o) 
    { 
        if (this == o) return true; 
        if (!(o instanceof Column)) return false; 
 
        final Column column = (Column)o; 
 
        if (!_columnName.equals(column._columnName)) return false; 
        if (!_dataType.equals(column._dataType)) return false; 
        if (!_nullable.equals(column._nullable)) return false; 
        if (!_sqlTypeName.equals(column._sqlTypeName)) return false; 
 
        return true; 
    } 
 
    public int hashCode() 
    { 
        int result; 
        result = _columnName.hashCode(); 
        result = 29 * result + _dataType.hashCode(); 
        result = 29 * result + _sqlTypeName.hashCode(); 
        result = 29 * result + _nullable.hashCode(); 
        return result; 
    } 
 
    public static class Nullable 
    { 
 
        private final String _name; 
 
        private Nullable(String name) 
        { 
            _name = name; 
        } 
 
        //////////////////////////////////////////////////////////////////////////// 
        // Object class 
 
        public String toString() 
        { 
            return _name; 
        } 
    }	 
}