All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.jptools.database.metadata.TableMetaData Maven / Gradle / Ivy

/*
 * TableMetaData.java
 * 
 * Copyright by jptools, all rights reserved.
 */
package jptools.database.metadata;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * An object that can be used to get information about the types 
 * and properties of the columns in a ResultSet object.
 */
public class TableMetaData
{
    private String tablename;
    private DataMap autoIncrement;
    private DataMap caseSensitive;  
    private DataMap searchable;
    private DataMap currency;
    private DataMap nullable;
    private DataMap signed;
    private DataMap columnDisplaySize;
    private DataMap columnLabel; 
    private DataMap columnName;
    private DataMap schemaName;
    private DataMap precision;
    private DataMap scale;  
    private DataMap tableName;
    private DataMap catalogName;
    private DataMap columnType;
    private DataMap columnTypeName;
    private DataMap readOnly;
    private DataMap writable;
    private DataMap definitelyWritable;
    private DataMap columnClassName;
    private DataMap columnNamesAndPos;
    private List columnNames;

    
    /**
     * Default constructor for TableMetaData.
     * @param tablename the name of the table
     */
    public TableMetaData( String tablename )
    {
        this.tablename = tablename;
        autoIncrement = new DataMap();
        caseSensitive = new DataMap();  
        searchable = new DataMap();
        currency = new DataMap();
        nullable = new DataMap();
        signed = new DataMap();
        columnDisplaySize = new DataMap();
        columnLabel = new DataMap();
        columnName = new DataMap();
        schemaName = new DataMap();
        precision = new DataMap();
        scale = new DataMap();
        tableName = new DataMap();
        catalogName = new DataMap();
        columnType = new DataMap();
        columnTypeName = new DataMap();
        readOnly = new DataMap();
        writable = new DataMap();
        definitelyWritable = new DataMap();
        columnClassName = new DataMap();
        columnNamesAndPos = new DataMap();
        columnNames = new ArrayList();
    }

    
    /**
     * Returns the name of the table
     *
     * @return the name of the table
     */
    public String getTablename()
    {
        return tablename;
    }

    
    /**
     * Returns a list with the column names
     *
     * @return all column names of the table
     */
    public List getColumnNames()
    {
        return columnNames;
    }
    
    
    /**
     * Returns the number of columns in this ResultSet object.
     *
     * @return the number of columns
     */
    public int getColumnCount()
    {
        return columnNames.size();
    }


    /**
     * Indicates whether the designated column is automatically numbered, thus
     * read-only.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isAutoIncrement( int column )
    {
        return autoIncrement.getBoolean( column );
    }


    /**
     * Indicates whether the designated column is automatically numbered, thus
     * read-only.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isAutoIncrement( String name )
    {
        return isAutoIncrement( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the indication whether the designated column is automatically
     * numbered, thus read-only.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isAutoIncrement The flag autoIncrement to set.
     */
    public void setAutoIncrement( int column, boolean isAutoIncrement )
    {
        autoIncrement.put( column, isAutoIncrement );
    }

    
    /**
     * Indicates whether a column's case matters.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isCaseSensitive( int column )
    {
        return caseSensitive.getBoolean( column );
    }


    /**
     * Indicates whether a column's case matters.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isCaseSensitive( String name )
    {
        return isCaseSensitive( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the indication whether a column's case matters.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isCaseSensitive The caseSensitive to set.
     */
    public void setCaseSensitive( int column, boolean isCaseSensitive )
    {
        caseSensitive.put( column, isCaseSensitive );
    }

    
    /**
     * Indicates whether the designated column can be used in a where clause.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isSearchable( int column )
    {
        return searchable.getBoolean( column );
    }


    /**
     * Indicates whether the designated column can be used in a where clause.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isSearchable( String name )
    {
        return isSearchable( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the indication whether the designated column can be used in a where 
     * clause.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isSearchable The searchable to set.
     */
    public void setSearchable( int column, boolean isSearchable )
    {
        searchable.put( column, isSearchable );
    }

    
    /**
     * Indicates whether the designated column is a cash value.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isCurrency( int column )
    {
        return currency.getBoolean( column );
    }


    /**
     * Indicates whether the designated column is a cash value.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isCurrency( String name )
    {
        return isCurrency( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the indication  whether the designated column is a cash value.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isCurrency The currency to set.
     */
    public void setCurrency( int column, boolean isCurrency )
    {
        currency.put( column, isCurrency );
    }

    
    /**
     * Indicates the nullability of values in the designated column.        
     *
     * @param column the first column is 1, the second is 2, ...
     * @return the nullability status of the given column; one of
     *         columnNoNulls, columnNullable or
     *         columnNullableUnknown
     */
    public int isNullable( int column )
    {
        return nullable.getInt( column );
    }


    /**
     * Indicates the nullability of values in the designated column.        
     *
     * @param name the name of the column
     * @return the nullability status of the given column; one of
     *         columnNoNulls, columnNullable or
     *         columnNullableUnknown
     */
    public int isNullable( String name )
    {
        return isNullable( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the indication the nullability of values in the designated column.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isNullable The nullable to set.
     */
    public void setNullable( int column, int isNullable )
    {
        nullable.put( column, isNullable );
    }

    
    /**
     * Indicates whether values in the designated column are signed numbers.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isSigned( int column )
    {
        return signed.getBoolean( column );
    }

    
    /**
     * Indicates whether values in the designated column are signed numbers.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isSigned( String name )
    {
        return isSigned( columnNamesAndPos.getInt( name ) );
    }

    
    /**
     * Sets the indication whether values in the designated column are signed
     * numbers.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isSigned The signed to set.
     */
    public void setSigned( int column, boolean isSigned )
    {
        signed.put( column, isSigned );
    }

    
    /**
     * Indicates the designated column's normal maximum width in characters.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return the normal maximum number of characters allowed as the width
     *          of the designated column
     */
    public int getColumnDisplaySize( int column )
    {
        return columnDisplaySize.getInt( column );
    }


    /**
     * Indicates the designated column's normal maximum width in characters.
     *
     * @param name the name of the column
     * @return the normal maximum number of characters allowed as the width
     *          of the designated column
     */
    public int getColumnDisplaySize( String name )
    {
        return getColumnDisplaySize( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the indication the designated column's normal maximum width in
     * characters.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isColumnDisplaySize The columnDisplaySize to set.
     */
    public void setColumnDisplaySize( int column, int isColumnDisplaySize )
    {
        columnDisplaySize.put( column, isColumnDisplaySize );
    }

    
    /**
     * Gets the designated column's suggested title for use in printouts and
     * displays.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return the suggested column title
     */
    public String getColumnLabel( int column )
    {
        return columnLabel.getString( column );
    }


    /**
     * Gets the designated column's suggested title for use in printouts and
     * displays.
     *
     * @param name the name of the column
     * @return the suggested column title
     */
    public String getColumnLabel( String name )
    {
        return getColumnLabel( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the designated column's suggested title for use in printouts and
     * displays.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isColumnLabel The columnLabel to set.
     */
    public void setColumnLabel( int column, String isColumnLabel )
    {
        columnLabel.put( column, isColumnLabel );
    }

    
    /**
     * Get the designated column's name.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return column name
     */
    public String getColumnName( int column )
    {
        return columnName.getString( column );
    }

    
    /**
     * Check is a given column exits on the table.
     *
     * @param c the column name to test
     * @return true if the column exists
     */
    public boolean existColumnName( String c )
    {
        return this.columnNamesAndPos.containKey( c );
    }
    

    /**
     * Set the designated column's name.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param columnName The columnName to set.
     */
    public void setColumnName( int column, String columnName )
    {
        // convert to lower case because, oracle and mySQL are diffrent
        columnNames.add( columnName );
        columnNamesAndPos.put( columnName.toLowerCase(), column );
        this.columnName.put( column, columnName );
    }

    
    /**
     * Gets the designated column's table's schema.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return schema name or "" if not applicable
     */
    public String getSchemaName( int column )
    {
        return schemaName.getString( column );
    }


    /**
     * Gets the designated column's table's schema.
     *
     * @param name the name of the column
     * @return schema name or "" if not applicable
     */
    public String getSchemaName( String name )
    {
        return getSchemaName( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the designated column's table's schema.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param schemaName The schemaName to set.
     */
    public void setSchemaName( int column, String schemaName )
    {
        this.schemaName.put( column, schemaName );
    }

    
    /**
     * Gets the designated column's number of decimal digits.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return the designated column's number of decimal digits.
     */
    public int getPrecision( int column )
    {
        return precision.getInt( column );
    }


    /**
     * Gets the designated column's number of decimal digits.
     *
     * @param name the name of the column
     * @return the designated column's number of decimal digits
     */
    public int getPrecision( String name )
    {
        return getPrecision( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the designated column's number of decimal digits.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param precision The precision to set.
     */
    public void setPrecision( int column, int precision )
    {
        this.precision.put( column, precision );
    }

    
    /**
     * Gets the designated column's number of digits to right of the decimal
     * point.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return the designated column's number of digits to right of the decimal
     *         point.
     */
    public int getScale( int column )
    {
        return scale.getInt( column );
    }


    /**
     * Gets the designated column's number of digits to right of the decimal
     * point.
     *
     * @param name the name of the column
     * @return the designated column's number of digits to right of the decimal
     *         point.
     */
    public int getScale( String name )
    {
        return getScale( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the designated column's number of digits to right of the decimal
     * point.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param scale The scale to set.
     */
    public void setScale( int column, int scale )
    {
        this.scale.put( column, scale );
    }

    
    /**
     * Gets the designated column's table name. 
     *
     * @param column the first column is 1, the second is 2, ...
     * @return table name or "" if not applicable
     */
    public String getTableName( int column )
    {
        return tableName.getString( column );
    }

    
    /**
     * Gets the designated column's table name. 
     *
     * @param name the name of the column
     * @return table name or "" if not applicable
     */
    public String getTableName( String name )
    {
        return getTableName( columnNamesAndPos.getInt( name ) );
    }

    
    /**
     * Sets the designated column's table name.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param tableName The tableName to set.
     */
    public void setTableName( int column, String tableName )
    {
        this.tableName.put( column, tableName );
    }

    
    /**
     * Gets the designated column's table's catalog name.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return the name of the catalog for the table in which the given column
     *          appears or "" if not applicable
     */
    public String getCatalogName( int column )
    {
        return catalogName.getString( column );
    }


    /**
     * Gets the designated column's table's catalog name.
     *
     * @param name the name of the column
     * @return the name of the catalog for the table in which the given column
     *          appears or "" if not applicable
     */
    public String getCatalogName( String name )
    {
        return getCatalogName( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the designated column's table's catalog name.
     *
     * @param column the first column is 1, the second is 2, ...
     * @param catalogName the name of the catalog for the table in which the
     *        given column appears or "" if not applicable
     */
    public void setCatalogName( int column, String catalogName )
    {
        this.catalogName.put( column, catalogName );
    }


    /**
     * Retrieves the designated column's SQL type.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return SQL type from java.sql.Types
     * @see java.sql.Types
     */
    public int getColumnType( int column )
    {
        return columnType.getInt( column );
    }


    /**
     * Retrieves the designated column's SQL type.
     *
     * @param name the name of the column
     * @return SQL type from java.sql.Types
     * @see java.sql.Types
     */
    public int getColumnType( String name )
    {
        return getColumnType( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets the designated column's SQL type.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param columnType The columnType to set.
     */
    public void setColumnType( int column, int columnType )
    {
        this.columnType.put( column, columnType );
    }

    
    /**
     * Retrieves the designated column's database-specific type name.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return type name used by the database. If the column type is
     * a user-defined type, then a fully-qualified type name is returned.
     */
    public String getColumnTypeName( int column )
    {
        return columnTypeName.getString( column );
    }


    /**
     * Retrieves the designated column's database-specific type name.
     *
     * @param name the name of the column
     * @return type name used by the database. If the column type is
     * a user-defined type, then a fully-qualified type name is returned.
     */
    public String getColumnTypeName( String name )
    {
        return getColumnTypeName( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Gets the designated column's database-specific type name.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param columnTypeName The columnTypeName to set.
     */
    public void setColumnTypeName( int column, String columnTypeName )
    {
        this.columnTypeName.put( column, columnTypeName );
    }

    
    /**
     * Indicates whether the designated column is definitely not writable.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isReadOnly( int column )
    {
        return readOnly.getBoolean( column );
    }


    /**
     * Indicates whether the designated column is definitely not writable.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isReadOnly( String name )
    {
        return isReadOnly( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets whether the designated column is definitely not writable.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isReadOnly The readOnly to set.
     */
    public void setReadOnly( int column, boolean isReadOnly )
    {
        this.readOnly.put( column, isReadOnly );
    }

    
    /**
     * Indicates whether it is possible for a write on the designated column to
     * succeed.
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isWritable( int column )
    {
        return writable.getBoolean( column );
    }


    /**
     * Indicates whether it is possible for a write on the designated column to
     * succeed.
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isWritable( String name )
    {
        return isWritable( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets whether it is possible for a write on the designated column to
     * succeed.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isWritable The writable to set.
     */
    public void setWritable( int column, boolean isWritable )
    {
        this.writable.put( column, isWritable );
    }

    
    /**
     * Indicates whether a write on the designated column will definitely
     * succeed.  
     *
     * @param column the first column is 1, the second is 2, ...
     * @return true if so; false otherwise
     */
    public boolean isDefinitelyWritable( int column )
    {
        return definitelyWritable.getBoolean( column );
    }


    /**
     * Indicates whether a write on the designated column will definitely
     * succeed.  
     *
     * @param name the name of the column
     * @return true if so; false otherwise
     */
    public boolean isDefinitelyWritable( String name )
    {
        return isDefinitelyWritable( columnNamesAndPos.getInt( name ) );
    }


    /**
     * Sets whether a write on the designated column will definitely
     * succeed.
     * 
     * @param column the first column is 1, the second is 2, ...
     * @param isDefinitelyWritable The definitelyWritable to set.
     */
    public void setDefinitelyWritable( int column, boolean isDefinitelyWritable )
    {
        this.definitelyWritable.put( column, isDefinitelyWritable );
    }

    
    /**
     * 

Returns the fully-qualified name of the Java class whose instances * are manufactured if the method ResultSet.getObject * is called to retrieve a value from the column. * ResultSet.getObject may return a subclass of the class * returned by this method. * * @param column the first column is 1, the second is 2, ... * @return the fully-qualified name of the class in the Java programming * language that would be used by the method * ResultSet.getObject to retrieve the value in the specified * column. This is the class name used for custom mapping. * @since 1.2 */ public String getColumnClassName( int column ) { return columnClassName.getString( column ); } /** *

Returns the fully-qualified name of the Java class whose instances * are manufactured if the method ResultSet.getObject * is called to retrieve a value from the column. * ResultSet.getObject may return a subclass of the class * returned by this method. * * @param name the name of the column * @return the fully-qualified name of the class in the Java programming * language that would be used by the method * ResultSet.getObject to retrieve the value in the specified * column. This is the class name used for custom mapping. * @since 1.2 */ public String getColumnClassName( String name ) { return getColumnClassName( columnNamesAndPos.getInt( name ) ); } /** * Sets the fully-qualified name of the Java class whose instances * are manufactured if the method ResultSet.getObject * is called to retrieve a value from the column. * ResultSet.getObject may return a subclass of the class * returned by this method. * * @param column the first column is 1, the second is 2, ... * @param columnClassName The columnClassName to set. */ public void setColumnClassName( int column, String columnClassName ) { this.columnClassName.put( column, columnClassName ); } /** * Gets the position of the given column name. * * @param name the column name. * @return the position of the given column name. */ public int getColumnPosition( String name ) { return columnNamesAndPos.getInt( name ); } /** * Intern data container as a wraper class of a Map. */ private class DataMap { private Map data; /** * Default constructor for DataMap. */ public DataMap() { data = new HashMap(); } /** * Puts on the position the given value to the internal Map. * * @param pos the position * @param value the value */ public void put( int pos, boolean value ) { data.put( Integer.valueOf( pos ), Boolean.valueOf( value ) ); } /** * Puts on the position the given value to the internal Map. * * @param pos the position * @param value the value */ public void put( int pos, int value ) { data.put( Integer.valueOf( pos ), Integer.valueOf( value ) ); } /** * Puts on the position the given value to the internal Map. * * @param name the name * @param value the value */ public void put( String name, int value ) { data.put( name, Integer.valueOf( value ) ); } /** * Puts on the position the given value to the internal Map. * * @param pos the position * @param value the value */ public void put( int pos, String value ) { data.put( Integer.valueOf( pos ), value ); } /** * Gets the value on the given position as a boolean. * * @param pos the position * @return the value on the given position. */ public boolean getBoolean( int pos ) { return ( (Boolean)data.get( Integer.valueOf( pos ) ) ).booleanValue(); } /** * Gets the value on the given position as an int. * * @param pos the position * @return the value on the given position. */ public int getInt( int pos ) { Integer val = (Integer)data.get( Integer.valueOf( pos ) ); if( val!=null ) return val.intValue(); return Integer.MAX_VALUE; } /** * Gets the value on the given position as an int. * * @param name the name * @return the value on the given position. */ public int getInt( String name ) { Integer val = (Integer)data.get( name.toLowerCase() ); if( val!=null ) return val.intValue(); return Integer.MAX_VALUE; } /** * Gets the value on the given position as a String. * * @param pos the position * @return the value on the given position. */ public String getString( int pos ) { return ( (String)data.get( Integer.valueOf( pos ) ) ).toString(); } /** * Checks if a given key exists * * @param key the key to check * @return true if the key exists */ public boolean containKey( String key ) { if ( key == null ) return false; return data.containsKey( key.toLowerCase() ); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy