KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > table > Column


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.table;
30
31 import com.caucho.amber.manager.AmberPersistenceUnit;
32 import com.caucho.amber.type.Type;
33 import com.caucho.config.ConfigException;
34 import com.caucho.config.LineConfigException;
35 import com.caucho.java.JavaWriter;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.L10N;
38
39 import javax.sql.DataSource JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.sql.Connection JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Statement JavaDoc;
45
46 /**
47  * Column configuration.
48  */

49 public class Column {
50   private static final L10N L = new L10N(Column.class);
51
52   private Table _table;
53
54   private String JavaDoc _name;
55
56   private String JavaDoc _configLocation;
57
58   private Type _type;
59
60   private boolean _isPrimaryKey;
61
62   private String JavaDoc _sqlType;
63
64   private boolean _isNotNull;
65   private boolean _isUnique;
66   private int _length;
67   private int _precision;
68   private int _scale;
69
70   private String JavaDoc _generatorType;
71   private String JavaDoc _generator;
72
73   // getter/setter stuff
74
private String JavaDoc _fieldName;
75
76
77   Column(Table table, String JavaDoc name)
78   {
79     _table = table;
80     _name = name;
81   }
82
83   /**
84    * Creates the column.
85    *
86    * @param table the owning table
87    * @param name the column sql name
88    * @param type the column's type
89    */

90   Column(Table table, String JavaDoc name, Type type)
91   {
92     _table = table;
93     _name = name;
94     _type = type;
95   }
96
97   /**
98    * Returns the owning table.
99    */

100   public Table getTable()
101   {
102     return _table;
103   }
104
105   /**
106    * Gets the column name.
107    */

108   public String JavaDoc getName()
109   {
110     return _name;
111   }
112
113   /**
114    * Sets the column name.
115    */

116   public void setName(String JavaDoc name)
117   {
118     _name = name;
119   }
120
121   /**
122    * Sets the config location.
123    */

124   public void setConfigLocation(String JavaDoc location)
125   {
126     _configLocation = location;
127   }
128
129   /**
130    * Returns the type.
131    */

132   public Type getType()
133   {
134     return _type;
135   }
136
137   /**
138    * Sets the primary key property.
139    */

140   public void setPrimaryKey(boolean isPrimaryKey)
141   {
142     _isPrimaryKey = isPrimaryKey;
143   }
144
145   /**
146    * Return true for a primary key column.
147    */

148   public boolean isPrimaryKey()
149   {
150     return _isPrimaryKey;
151   }
152
153   /**
154    * Sets the generator type.
155    */

156   public void setGeneratorType(String JavaDoc type)
157   {
158     _generatorType = type;
159   }
160
161   /**
162    * Generates the insert name.
163    */

164   public String JavaDoc generateInsertName()
165   {
166     return _name;
167   }
168
169   /**
170    * Sets the sql type for create table
171    */

172   public void setSQLType(String JavaDoc sqlType)
173   {
174     _sqlType = sqlType;
175   }
176
177   /**
178    * Gets the sql type for the create table
179    */

180   public String JavaDoc getSQLType()
181   {
182     return _sqlType;
183   }
184
185   /**
186    * Sets the length property.
187    */

188   public void setLength(int length)
189   {
190     _length = length;
191   }
192
193   /**
194    * Gets the length property.
195    */

196   public int getLength()
197   {
198     return _length;
199   }
200
201   /**
202    * Sets the not-null property.
203    */

204   public void setNotNull(boolean isNotNull)
205   {
206     _isNotNull = isNotNull;
207   }
208
209   /**
210    * Gets the not-null property.
211    */

212   public boolean isNotNull()
213   {
214     return _isNotNull;
215   }
216
217   /**
218    * Set the precision property.
219    */

220   public void setPrecision(int precision) {
221     _precision = precision;
222   }
223
224   /**
225    * Gets the precision property.
226    */

227   public int getPrecision() {
228     return _precision;
229   }
230
231   /**
232    * Set the scale property
233    */

234   public void setScale(int scale) {
235     _scale = scale;
236   }
237
238   /**
239    * Get the scale property
240    */

241   public int getScale() {
242     return _scale;
243   }
244
245   /**
246    * Sets the unique property.
247    */

248   public void setUnique(boolean isUnique)
249   {
250     _isUnique = isUnique;
251   }
252
253   /**
254    * Gets the unique property.
255    */

256   public boolean isUnique()
257   {
258     return _isUnique;
259   }
260
261   /**
262    * Generates the clause to create the column.
263    */

264   String JavaDoc generateCreateTableSQL(AmberPersistenceUnit manager)
265   {
266     CharBuffer cb = new CharBuffer();
267     cb.append(_name + " ");
268     String JavaDoc sqlType = _sqlType;
269
270     if (_sqlType != null)
271       sqlType = _sqlType;
272     else {
273       sqlType = _type.generateCreateColumnSQL(manager, _length, _precision, _scale);
274     }
275
276     if ("identity".equals(_generatorType)) {
277       cb.append(manager.getMetaData().createIdentitySQL(sqlType));
278     } else {
279       cb.append(sqlType);
280     }
281
282     if (isPrimaryKey()) {
283       cb.append(" primary key");
284     } else if (! "identity".equals(_generatorType)) {
285       if (isNotNull())
286         cb.append(" not null");
287       if (isUnique())
288         cb.append(" unique");
289     }
290
291     return cb.toString();
292   }
293
294   /**
295    * Creates the table if missing.
296    */

297   void validateDatabase(AmberPersistenceUnit amberPersistenceUnit)
298     throws ConfigException
299   {
300     try {
301       DataSource JavaDoc ds = amberPersistenceUnit.getDataSource();
302       Connection JavaDoc conn = ds.getConnection();
303       try {
304         Statement JavaDoc stmt = conn.createStatement();
305
306         String JavaDoc sql = "select " + getName() + " from " + _table.getName() + " where 1=0";
307
308         try {
309           // If the table exists, return
310

311           ResultSet JavaDoc rs = stmt.executeQuery(sql);
312           rs.close();
313           return;
314         } catch (SQLException JavaDoc e) {
315           throw error(L.l("'{0}' is not a valid database column in table '{1}'. Either the table needs to be created or the create-database-tables attribute must be set.\n\n {2}\n\n{3}",
316                           getName(),
317                           getTable().getName(),
318                           sql,
319                           e.toString()),
320                       e);
321         }
322       } finally {
323         conn.close();
324       }
325     } catch (ConfigException e) {
326       throw e;
327     } catch (Exception JavaDoc e) {
328       throw new ConfigException(e);
329     }
330   }
331
332   /**
333    * Generates the clause to load the column.
334    */

335   public String JavaDoc generateSelect(String JavaDoc id)
336   {
337     if (id != null)
338       return id + "." + _name;
339     else
340       return _name;
341   }
342
343   /**
344    * Generates the where clause.
345    */

346   public String JavaDoc generateMatchArgWhere(String JavaDoc id)
347   {
348     if (id != null)
349       return id + "." + _name + "=?";
350     else
351       return _name + "=?";
352   }
353
354   /**
355    * Generates the update clause.
356    */

357   public String JavaDoc generateUpdateSet()
358   {
359     return _name + "=?";
360   }
361
362   /**
363    * Generates the update clause setting to null.
364    */

365   public String JavaDoc generateUpdateSetNull()
366   {
367     return _name + "=null";
368   }
369
370   /**
371    * Generates the prologue.
372    */

373   public void generatePrologue(JavaWriter out)
374     throws IOException JavaDoc
375   {
376   }
377
378   /**
379    * Returns the field name.
380    */

381   public String JavaDoc getFieldName()
382   {
383     return "__amber_" + getName();
384   }
385
386   /**
387    * Generates a string to load the type as a property.
388    */

389   public void generateSet(JavaWriter out, String JavaDoc pstmt,
390                           String JavaDoc index, String JavaDoc value)
391     throws IOException JavaDoc
392   {
393     if (value != null)
394       _type.generateSet(out, pstmt, index, value);
395     else
396       _type.generateSetNull(out, pstmt, index);
397   }
398
399   /**
400    * Generates a string to load the type as a property.
401    */

402   public void generateSetVersion(JavaWriter out,
403                                  String JavaDoc pstmt,
404                                  String JavaDoc index,
405                                  String JavaDoc value)
406     throws IOException JavaDoc
407   {
408     _type.generateSetVersion(out, pstmt, index, value);
409   }
410
411   /**
412    * Generates a string to load the type as a property.
413    */

414   public int generateLoad(JavaWriter out, String JavaDoc rs,
415                           String JavaDoc indexVar, int index)
416     throws IOException JavaDoc
417   {
418     return _type.generateLoad(out, rs, indexVar, index);
419   }
420
421   /**
422    * Converts to the object key.
423    */

424   public Object JavaDoc toObjectKey(long value)
425   {
426     return getType().toObject(value);
427   }
428
429   protected ConfigException error(String JavaDoc msg, Throwable JavaDoc e)
430   {
431     if (_configLocation != null)
432       return new LineConfigException(_configLocation + msg, e);
433     else if (_table.getLocation() != null)
434       return new LineConfigException(_table.getLocation() + msg, e);
435     else
436       return new ConfigException(msg, e);
437   }
438
439   /**
440    * Returns the name.
441    */

442   public String JavaDoc toString()
443   {
444     return "Column[" + getName() + "]";
445   }
446 }
447
Popular Tags