001/* 002 * Copyright (c) 2008-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2012-01-19 22:39:48 +0000 (Thu, 19 Jan 2012) $' 007 * '$Revision: 29258 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.util.sql; 031 032/** 033 * 034 * A generic representation of an SQL column. 035 * 036 * @author Daniel Crawl 037 * @version $Id: Column.java 29258 2012-01-19 22:39:48Z crawl $ 038 * 039 */ 040 041public class Column 042{ 043 /** Construct a Column with a type. */ 044 public Column(Type type) 045 { 046 _initialize(type, false, false, -1, null, false); 047 } 048 049 /** Construct a Column with a type and specify auto-increment. */ 050 public Column(Type type, boolean autoIncrement) 051 { 052 _initialize(type, autoIncrement, false, -1, null, false); 053 } 054 055 /** Construct a Column with a type and specify auto-increment and 056 * primary key. 057 */ 058 public Column(Type type, boolean autoIncrement, boolean primary) 059 { 060 _initialize(type, autoIncrement, primary, -1, null, false); 061 } 062 063 /** Construct a Column with a type and specify auto-increment, 064 * primary key, and if null values are allowed. 065 */ 066 public Column(Type type, boolean autoIncrement, boolean primary, 067 boolean nullAllowed) 068 { 069 _initialize(type, autoIncrement, primary, -1, null, nullAllowed); 070 } 071 072 /** Construct a Column with a type and type length. */ 073 public Column(Type type, int length) 074 { 075 _initialize(type, false, false, length, null, false); 076 } 077 078 /** Construct a Column with a type and type length, and specifying if 079 * can be null. 080 */ 081 public Column(Type type, int length, boolean nullAllowed) 082 { 083 _initialize(type, false, false, length, null, nullAllowed); 084 } 085 086 /** Construct a Column with a type and type length, and specifying if 087 * can be null or a primary key. 088 */ 089 public Column(Type type, int length, boolean nullAllowed, boolean primary) 090 { 091 _initialize(type, false, primary, length, null, nullAllowed); 092 } 093 094 /** Construct a Column with a type and a default value. */ 095 public Column(Type type, String defaultValue) 096 { 097 _initialize(type, false, false, -1, defaultValue, false); 098 } 099 100 /** Get the default value. Returns null if none exists. */ 101 public String getDefaultValue() 102 { 103 return _default; 104 } 105 106 /** Get the length of the column's type. Returns -1 if not specified. */ 107 public int getLength() 108 { 109 return _length; 110 } 111 112 /** Get the name. If the column has not been added to a table, 113 * returns null. 114 */ 115 public String getName() 116 { 117 return _name; 118 } 119 120 /** Get the containing table. If the column has not been added to a, 121 * then return null. 122 */ 123 public Table getTable() 124 { 125 return _table; 126 } 127 128 /** Get the data type. */ 129 public Type getType() 130 { 131 return _type; 132 } 133 134 /** Returns true if column value is auto-incremented. */ 135 public boolean isAutoIncrement() 136 { 137 return _autoIncrement; 138 } 139 140 /** Returns true if column can have null values. */ 141 public boolean isNullAllowed() 142 { 143 return _nullAllowed; 144 } 145 146 /** Returns true if column is primary key. */ 147 public boolean isPrimaryKey() 148 { 149 return _primary; 150 } 151 152 /** Set the name. */ 153 public void setName(String name) 154 { 155 _name = name; 156 } 157 158 /** Get a string representation of the column. */ 159 public String toString() 160 { 161 StringBuilder retval = new StringBuilder("column "); 162 retval.append(_name); 163 retval.append(" "); 164 retval.append("type="); 165 retval.append(_type.toString()); 166 retval.append(" length="); 167 retval.append(_length); 168 retval.append(" nullable="); 169 retval.append(_nullAllowed); 170 retval.append(" primary="); 171 retval.append(_primary); 172 retval.append(" autoinc="); 173 retval.append(_autoIncrement); 174 retval.append(" default="); 175 if(_default != null) 176 { 177 retval.append(_default); 178 } 179 else 180 { 181 retval.append("null"); 182 } 183 retval.append(" table="); 184 if(_table != null) 185 { 186 retval.append(_table.getName()); 187 } 188 else 189 { 190 retval.append("null"); 191 } 192 return retval.toString(); 193 } 194 195 //////////////////////////////////////////////////////////////////////// 196 //// public variables 197 198 /** Data types. */ 199 public enum Type 200 { 201 Boolean, 202 Binary, 203 Blob, 204 TextBlob, 205 Integer, 206 Timestamp, 207 VarBinary, 208 Varchar, 209 } 210 211 /** An auto-incrementing integer column. */ 212 public static final Column AUTOINCID = 213 new Column(Column.Type.Integer, true, true); 214 215 /** A boolean column. */ 216 public static final Column BOOLEAN = new Column(Column.Type.Boolean); 217 218 /** A BLOB column. */ 219 public static final Column BLOB = new Column(Column.Type.Blob); 220 221 /** An integer column. */ 222 public static final Column INTEGER = new Column(Column.Type.Integer); 223 224 /** An MD5 text column. */ 225 public static final Column MD5_TEXT = new Column(Column.Type.Varchar, 32); 226 227 /** An MD5 binary column. */ 228 public static final Column MD5_BINARY = new Column(Column.Type.VarBinary, 16, true); 229 230 /** An integer column that can be null. */ 231 public static final Column NULLABLE_INTEGER = 232 new Column(Column.Type.Integer, false, false, true); 233 234 /** An MD5 text column that can be null. */ 235 public static final Column NULLABLE_MD5_TEXT = 236 new Column(Column.Type.Varchar, 32, true); 237 238 /** A text column that can be null. Maximum length is 255 characters. */ 239 public static final Column NULLABLE_TEXT = 240 new Column(Column.Type.Varchar, 255, true); 241 242 /** A primary key integer column. */ 243 public static final Column PK_INTEGER = 244 new Column(Column.Type.Integer, false, true); 245 246 /** A primary key MD5 text column. */ 247 public static final Column PK_MD5_TEXT = new Column(Column.Type.Varchar, 32, 248 false, true); 249 250 public static final Column PK_MD5_BINARY = new Column(Column.Type.VarBinary, 16, 251 false, true); 252 253 /** A primary key text column. Maximum length is 255 characters. */ 254 public static final Column PK_TEXT = new Column(Column.Type.Varchar, 255, 255 false, true); 256 257 /** A text column. Maximum length is 255 characters. */ 258 public static final Column TEXT = new Column(Column.Type.Varchar, 255); 259 260 /** A text column with no maximum length. */ 261 public static final Column TEXT_UNLIMITED = new Column(Column.Type.TextBlob); 262 263 /** A timestamp column. */ 264 public static final Column TIMESTAMP = new Column(Column.Type.Timestamp); 265 266 /** A column for text UUIDs that are primary keys. */ 267 public static final Column PK_UUID_TEXT = new Column(Column.Type.Varchar, 36, false, true); 268 269 /** A column for text UUIDs. */ 270 public static final Column UUID_TEXT = new Column(Column.Type.Varchar, 36); 271 272 //////////////////////////////////////////////////////////////////////// 273 //// package methods 274 275 Column(Column column) 276 { 277 _autoIncrement = column._autoIncrement; 278 _default = column._default; 279 _length = column._length; 280 _name = column._name; 281 _nullAllowed = column._nullAllowed; 282 _primary = column._primary; 283 _table = column._table; 284 _type = column._type; 285 } 286 287 /** Set the default value. */ 288 void _setDefaultValue(String value) 289 { 290 _default = value; 291 } 292 293 /** Set the table. */ 294 void _setTable(Table table) 295 { 296 _table = table; 297 } 298 299 //////////////////////////////////////////////////////////////////////// 300 //// private methods 301 302 /** Initialize a column. */ 303 private void _initialize(Type type, boolean autoIncrement, boolean primary, 304 int length, String defaultValue, boolean nullAllowed) 305 { 306 _type = type; 307 _autoIncrement = autoIncrement; 308 _primary = primary; 309 _length = length; 310 _default = defaultValue; 311 _nullAllowed = nullAllowed; 312 } 313 314 //////////////////////////////////////////////////////////////////////// 315 //// private variables 316 317 /** The column data type. */ 318 private Type _type; 319 320 /** If true, column value is auto-incremented during an insert. */ 321 private boolean _autoIncrement; 322 323 /** If true, column is a primary key. */ 324 private boolean _primary; 325 326 /** The length of column type. */ 327 private int _length; 328 329 /** If true, column value can be null. */ 330 private boolean _nullAllowed; 331 332 /** The default value. */ 333 private String _default; 334 335 /** The parent table. This is null until column added to a table. */ 336 private Table _table; 337 338 /** The column name. This is null until column added to a table. */ 339 private String _name; 340}