View Javadoc
1   package com.randomnoun.common.jexl.sql;
2   
3   /* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
4    * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
5    */
6   
7   import java.sql.Types;
8   
9   
10  /**
11   * Objects of this type are used in the evaluation context passed into an SqlGenerator
12   * to define a database column used in a TopLevelExpression.
13   *
14   * <p>See the SqlGenerator class for detailed documentation.
15   *
16   * 
17   * @author knoxg
18   */
19  public class SqlColumn
20  {
21  
22      /** These constants are copied directly from the java.sql.Types class;
23       *  note that we also add the new types MONEYVALUE, DATEVALUE, TIMEVALUE, FIXEDDATEVALUE and FIXEDTIMEVALUE */
24      public static final int ARRAY = Types.ARRAY;
25      public static final int BIGINT = Types.BIGINT;
26      public static final int BINARY = Types.BINARY;
27      public static final int BIT = Types.BIT;
28      public static final int BLOB = Types.BLOB;
29      public static final int BOOLEAN = Types.BOOLEAN;
30      public static final int CHAR = Types.CHAR;
31      public static final int CLOB = Types.CLOB;
32      public static final int DATALINK = Types.DATALINK;
33      public static final int DATE = Types.DATE;
34      public static final int DECIMAL = Types.DECIMAL;
35      public static final int DISTINCT = Types.DISTINCT;
36      public static final int DOUBLE = Types.DOUBLE;
37      public static final int FLOAT = Types.FLOAT;
38      public static final int INTEGER = Types.INTEGER;
39      public static final int JAVA_OBJECT = Types.JAVA_OBJECT;
40      public static final int LONGVARBINARY = Types.LONGVARBINARY;
41      public static final int LONGVARCHAR = Types.LONGVARCHAR;
42      public static final int NULL = Types.NULL;
43      public static final int NUMERIC = Types.NUMERIC;
44      public static final int OTHER = Types.OTHER;
45      public static final int REAL = Types.REAL;
46      public static final int REF = Types.REF;
47      public static final int SMALLINT = Types.SMALLINT;
48      public static final int STRUCT = Types.STRUCT;
49      public static final int TIME = Types.TIME;
50      public static final int TIMESTAMP = Types.TIMESTAMP;
51      public static final int TINYINT = Types.TINYINT;
52      public static final int VARBINARY = Types.VARBINARY;
53      public static final int VARCHAR = Types.VARCHAR;
54  
55      /** The range 5000-6000 is currently unused in java.sql.Types;
56       * (and doesn't look like it will ever be used), so I'm going to use
57       * this for custom types. Anything in here is treated specially in SqlGenerator
58       * when converting to SQL.
59       *
60       */
61      public static final int MONEYVALUE = 5000; // perform currency matching
62      public static final int DATEVALUE = 5001; // perform conditional date ranges
63      public static final int TIMEVALUE = 5002; // perform conditional date ranges
64      public static final int FIXEDDATEVALUE = 5003; // as per DATEVALUE, stored as String (no TZ)
65      public static final int FIXEDTIMEVALUE = 5004; // as per TIMEVALUE, stored as String (no TZ)
66  
67      /** The name of this database column */
68      private String name = null;
69  
70      /** If this object describes a MONEYVALUE column, this field contains the name of the
71       *  currency code column (this.name contains the amount). */
72      private String currencyCodeName = null;
73  
74      /** The name of the table this fields belongs to (or null if not specifying tables) */
75      private String table = null;
76  
77      /** The type of this column. Corresponds to one of the public static final int constants
78       *  defined in this class. */
79      private int dataType = VARCHAR;
80  
81      /** Create a new column, of type VARCHAR
82       *
83       * @param name     The name of the column in the database
84       */
85      public SqlColumn(String name)
86      {
87          this.name = name;
88      }
89  
90      /** Create a new column, with the supplied datatype
91       *
92       * @param name     The name of the column in the database
93       * @param dataType The datatype of the column. Corresponds to one of the public static final
94       *   int constants defined in this class.
95       */
96      public SqlColumn(String name, int dataType)
97      {
98          this.name = name;
99          this.dataType = dataType;
100     }
101 
102     /** Create a new column, in a specific table, of type VARCHAR
103      *
104      * @param name     The name of the column in the database
105      * @param table    The name of the table in the database this column is in
106      */
107     public SqlColumn(String name, String table)
108     {
109         this.name = name;
110         this.table = table;
111     }
112 
113     /** Create a new column, in a specific table, with the supplied datatype
114      *
115      * @param name     The name of the column in the database
116      * @param table    The name of the table in the database this column is in
117      * @param dataType The datatype of the column. Corresponds to one of the public static final
118      *   int constants defined in this class.
119      */
120     public SqlColumn(String name, String table, int dataType)
121     {
122         this.name = name;
123         this.table = table;
124         this.dataType = dataType;
125     }
126 
127     /** Sets the column name that contains the currency code, for a MONEYVALUE SqlColumn.
128      * If a table has been specified for this column, then the currency is <b>always</b>
129      * sourced from the same table.
130      *
131      * @param currencyCodeName the column name that contains the currency code
132      *
133      * @throws IllegalStateException if this method is called for a non-MONEYVALUE SqlColumn
134      */
135     public void setCurrencyCodeName(String currencyCodeName)
136     {
137         if (this.dataType != MONEYVALUE)
138         {
139             throw new IllegalStateException(
140                 "A currencyColumnName may only be set for a MONEYVALUE type");
141         }
142 
143         this.currencyCodeName = currencyCodeName;
144     }
145 
146     /** Retrieves the name of this SqlColumn, as set by the constructor */
147     public String getName()
148     {
149         return name;
150     }
151 
152     /** Retrieves the table of this SqlColumn, as set by the constructor */
153     public String getTable()
154     {
155         return table;
156     }
157 
158     /** Retrieves the currency column of this SqlColumn, as supplied by setCurrencyCodeName */
159     public String getCurrencyCodeName()
160     {
161         return currencyCodeName;
162     }
163 
164     /** Retrieves the data type of this SqlColumn */
165     public int getDataType()
166     {
167         return dataType;
168     }
169 
170     /** Either returns the name of this column, or table + "." + name, if a table has been set */
171     public String getFullName()
172     {
173         if (table == null)
174         {
175             return name;
176         }
177         else
178         {
179             return table + "." + name;
180         }
181     }
182 
183     /** Either returns the name of the currency column, or table + "." + currency name, if a table
184      *  has been set */
185     public String getFullCurrencyCodeName()
186     {
187         if (table == null)
188         {
189             return currencyCodeName;
190         }
191         else
192         {
193             return table + "." + currencyCodeName;
194         }
195     }
196 
197     /** Retrieve a string representation of this column */
198     public String toString()
199     {
200         return getFullName();
201     }
202 }