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 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 DATEVALUE = 5001; // perform conditional date ranges
62      public static final int TIMEVALUE = 5002; // perform conditional date ranges
63      public static final int FIXEDDATEVALUE = 5003; // as per DATEVALUE, stored as String (no TZ)
64      public static final int FIXEDTIMEVALUE = 5004; // as per TIMEVALUE, stored as String (no TZ)
65  
66      /** The name of this database column */
67      protected String name = null;
68  
69      /** The name of the table this fields belongs to (or null if not specifying tables) */
70      protected String table = null;
71  
72      /** The type of this column. Corresponds to one of the public static final int constants
73       *  defined in this class. */
74      protected int dataType = VARCHAR;
75  
76      /** Create a new column, of type VARCHAR
77       *
78       * @param name     The name of the column in the database
79       */
80      public SqlColumn(String name)
81      {
82          this.name = name;
83      }
84  
85      /** Create a new column, with the supplied datatype
86       *
87       * @param name     The name of the column in the database
88       * @param dataType The datatype of the column. Corresponds to one of the public static final
89       *   int constants defined in this class.
90       */
91      public SqlColumn(String name, int dataType)
92      {
93          this.name = name;
94          this.dataType = dataType;
95      }
96  
97      /** Create a new column, in a specific table, of type VARCHAR
98       *
99       * @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 }