KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > JdbcColumnMetaData


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.db;
31
32 import java.sql.DatabaseMetaData JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.sql.Types JavaDoc;
36
37 /**
38  * Represents a JDBC column metadata
39  */

40 public class JdbcColumnMetaData {
41   private final JdbcTableMetaData _table;
42
43   private final String JavaDoc _name;
44
45   private final int _jdbcType;
46
47   private final int _length;
48
49   private final boolean _isNotNull;
50   private final boolean _isUnsigned;
51   private final boolean _isZeroFill;
52
53   private boolean _isPrimaryKey;
54   private boolean _isIndex;
55   private boolean _isUnique;
56
57   /**
58    * @param rs the ResultSet from a DatabaseMetaData.getColumns call
59    */

60   public JdbcColumnMetaData(JdbcTableMetaData table, ResultSet JavaDoc rs)
61     throws SQLException JavaDoc
62   {
63     _table = table;
64
65     // COLUMN_NAME
66
_name = rs.getString(4);
67     // DATA_TYPE
68
_jdbcType = rs.getInt(5);
69     // COLUMN_SIZE
70
_length = rs.getInt(7);
71
72     // NULLABLE
73
_isNotNull = rs.getInt(11) == DatabaseMetaData.columnNoNulls;
74
75     // TYPE_NAME
76
String JavaDoc type = rs.getString(6).toLowerCase();
77
78     _isUnsigned = type.indexOf("unsigned") >= 0;
79     _isZeroFill = type.indexOf("zerofill") >= 0;
80   }
81
82   /**
83    * Returns the column's name.
84    */

85   public String JavaDoc getName()
86   {
87     return _name;
88   }
89
90   /**
91    * Returns the column's table
92    */

93   public JdbcTableMetaData getTable()
94   {
95     return _table;
96   }
97
98   /**
99    * Returns the column length.
100    */

101   public int getLength()
102   {
103     return _length;
104   }
105
106   /**
107    * Returns true if the column is nullable.
108    */

109   public boolean isNotNull()
110   {
111     return _isNotNull;
112   }
113
114   /**
115    * Returns true for a primary key.
116    */

117   public boolean isPrimaryKey()
118   {
119     return _isPrimaryKey;
120   }
121
122   /**
123    * Set true for a primary key.
124    */

125   void setPrimaryKey(boolean isPrimaryKey)
126   {
127     _isPrimaryKey = isPrimaryKey;
128   }
129
130   /**
131    * Returns true for an index
132    */

133   public boolean isIndex()
134   {
135     return _isIndex;
136   }
137
138   /**
139    * Set true for an index
140    */

141   void setIndex(boolean isIndex)
142   {
143     _isIndex = isIndex;
144   }
145
146   /**
147    * Returns true for a unique column
148    */

149   public boolean isUnique()
150   {
151     return _isUnique;
152   }
153
154   /**
155    * Set true for a unique column
156    */

157   void setUnique(boolean isUnique)
158   {
159     _isUnique = isUnique;
160   }
161
162   /**
163    * Returns the JDBC type.
164    */

165   public int getJdbcType()
166   {
167     return _jdbcType;
168   }
169
170   /**
171    * Returns true for numeric data types.
172    */

173   public static boolean isNumeric(int jdbcType)
174   {
175     switch (jdbcType) {
176       case Types.BIT:
177       case Types.TINYINT:
178       case Types.SMALLINT:
179       case Types.INTEGER:
180       case Types.BIGINT:
181       case Types.DOUBLE:
182       case Types.FLOAT:
183       case Types.REAL:
184         return true;
185       default:
186         return false;
187     }
188   }
189
190   /**
191    * Returns true for numeric data types.
192    */

193   public boolean isNumeric()
194   {
195     return isNumeric(_jdbcType);
196   }
197
198   /**
199    * Returns true for float data types.
200    */

201   public boolean isFloat()
202   {
203     switch (_jdbcType) {
204     case Types.DOUBLE:
205     case Types.FLOAT:
206     case Types.REAL:
207       return true;
208     default:
209       return false;
210     }
211   }
212
213   /**
214    * Returns true for unsigned.
215    */

216   public boolean isUnsigned()
217   {
218     return _isUnsigned;
219   }
220
221   /**
222    * Returns true for zerofill
223    */

224   public boolean isZeroFill()
225   {
226     return _isZeroFill;
227   }
228
229   /**
230    * Returns true for blob data types.
231    */

232   public static boolean isBlob(int jdbcType)
233   {
234     switch (jdbcType) {
235       // php/142s
236
// php/142v
237
case Types.LONGVARBINARY:
238       case Types.LONGVARCHAR:
239       case Types.BLOB:
240       case Types.CLOB:
241         return true;
242       default:
243         return false;
244     }
245   }
246
247   /**
248    * Returns true for blob data types.
249    */

250   public boolean isBlob()
251   {
252     return isBlob(_jdbcType);
253   }
254
255   public String JavaDoc toString()
256   {
257     return "JdbcColumnMetaData[" + getName() + "]";
258   }
259 }
260
261
Popular Tags