001package com.randomnoun.common.jexl.sql;
002
003/* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
004 * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
005 */
006
007import java.sql.Types;
008
009
010/**
011 * Objects of this type are used in the evaluation context passed into an SqlGenerator
012 * to define a database column used in a TopLevelExpression.
013 *
014 * <p>See the SqlGenerator class for detailed documentation.
015 *
016 * 
017 * @author knoxg
018 */
019public class SqlColumn
020{
021
022    /** These constants are copied directly from the java.sql.Types class;
023     *  note that we also add the new types DATEVALUE, TIMEVALUE, FIXEDDATEVALUE and FIXEDTIMEVALUE */
024    public static final int ARRAY = Types.ARRAY;
025    public static final int BIGINT = Types.BIGINT;
026    public static final int BINARY = Types.BINARY;
027    public static final int BIT = Types.BIT;
028    public static final int BLOB = Types.BLOB;
029    public static final int BOOLEAN = Types.BOOLEAN;
030    public static final int CHAR = Types.CHAR;
031    public static final int CLOB = Types.CLOB;
032    public static final int DATALINK = Types.DATALINK;
033    public static final int DATE = Types.DATE;
034    public static final int DECIMAL = Types.DECIMAL;
035    public static final int DISTINCT = Types.DISTINCT;
036    public static final int DOUBLE = Types.DOUBLE;
037    public static final int FLOAT = Types.FLOAT;
038    public static final int INTEGER = Types.INTEGER;
039    public static final int JAVA_OBJECT = Types.JAVA_OBJECT;
040    public static final int LONGVARBINARY = Types.LONGVARBINARY;
041    public static final int LONGVARCHAR = Types.LONGVARCHAR;
042    public static final int NULL = Types.NULL;
043    public static final int NUMERIC = Types.NUMERIC;
044    public static final int OTHER = Types.OTHER;
045    public static final int REAL = Types.REAL;
046    public static final int REF = Types.REF;
047    public static final int SMALLINT = Types.SMALLINT;
048    public static final int STRUCT = Types.STRUCT;
049    public static final int TIME = Types.TIME;
050    public static final int TIMESTAMP = Types.TIMESTAMP;
051    public static final int TINYINT = Types.TINYINT;
052    public static final int VARBINARY = Types.VARBINARY;
053    public static final int VARCHAR = Types.VARCHAR;
054
055    /** The range 5000-6000 is currently unused in java.sql.Types;
056     * (and doesn't look like it will ever be used), so I'm going to use
057     * this for custom types. Anything in here is treated specially in SqlGenerator
058     * when converting to SQL.
059     *
060     */
061    public static final int DATEVALUE = 5001; // perform conditional date ranges
062    public static final int TIMEVALUE = 5002; // perform conditional date ranges
063    public static final int FIXEDDATEVALUE = 5003; // as per DATEVALUE, stored as String (no TZ)
064    public static final int FIXEDTIMEVALUE = 5004; // as per TIMEVALUE, stored as String (no TZ)
065
066    /** The name of this database column */
067    protected String name = null;
068
069    /** The name of the table this fields belongs to (or null if not specifying tables) */
070    protected String table = null;
071
072    /** The type of this column. Corresponds to one of the public static final int constants
073     *  defined in this class. */
074    protected int dataType = VARCHAR;
075
076    /** Create a new column, of type VARCHAR
077     *
078     * @param name     The name of the column in the database
079     */
080    public SqlColumn(String name)
081    {
082        this.name = name;
083    }
084
085    /** Create a new column, with the supplied datatype
086     *
087     * @param name     The name of the column in the database
088     * @param dataType The datatype of the column. Corresponds to one of the public static final
089     *   int constants defined in this class.
090     */
091    public SqlColumn(String name, int dataType)
092    {
093        this.name = name;
094        this.dataType = dataType;
095    }
096
097    /** Create a new column, in a specific table, of type VARCHAR
098     *
099     * @param name     The name of the column in the database
100     * @param table    The name of the table in the database this column is in
101     */
102    public SqlColumn(String name, String table)
103    {
104        this.name = name;
105        this.table = table;
106    }
107
108    /** Create a new column, in a specific table, with the supplied datatype
109     *
110     * @param name     The name of the column in the database
111     * @param table    The name of the table in the database this column is in
112     * @param dataType The datatype of the column. Corresponds to one of the public static final
113     *   int constants defined in this class.
114     */
115    public SqlColumn(String name, String table, int dataType)
116    {
117        this.name = name;
118        this.table = table;
119        this.dataType = dataType;
120    }
121
122    /** Retrieves the name of this SqlColumn, as set by the constructor */
123    public String getName()
124    {
125        return name;
126    }
127
128    /** Retrieves the table of this SqlColumn, as set by the constructor */
129    public String getTable()
130    {
131        return table;
132    }
133
134    /** Retrieves the data type of this SqlColumn */
135    public int getDataType()
136    {
137        return dataType;
138    }
139
140    /** Either returns the name of this column, or table + "." + name, if a table has been set */
141    public String getFullName()
142    {
143        if (table == null)
144        {
145            return name;
146        }
147        else
148        {
149            return table + "." + name;
150        }
151    }
152
153    /** Retrieve a string representation of this column */
154    public String toString()
155    {
156        return getFullName();
157    }
158}