001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.lib.db;
031:
032: import java.sql.DatabaseMetaData;
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.sql.Types;
036:
037: /**
038: * Represents a JDBC column metadata
039: */
040: public class JdbcColumnMetaData {
041: private final JdbcTableMetaData _table;
042:
043: private final String _name;
044:
045: private final int _jdbcType;
046:
047: private final int _length;
048:
049: private final boolean _isNotNull;
050: private final boolean _isUnsigned;
051: private final boolean _isZeroFill;
052:
053: private boolean _isPrimaryKey;
054: private boolean _isIndex;
055: private boolean _isUnique;
056:
057: /**
058: * @param rs the ResultSet from a DatabaseMetaData.getColumns call
059: */
060: public JdbcColumnMetaData(JdbcTableMetaData table, ResultSet rs)
061: throws SQLException {
062: _table = table;
063:
064: // COLUMN_NAME
065: _name = rs.getString(4);
066: // DATA_TYPE
067: _jdbcType = rs.getInt(5);
068: // COLUMN_SIZE
069: _length = rs.getInt(7);
070:
071: // NULLABLE
072: _isNotNull = rs.getInt(11) == DatabaseMetaData.columnNoNulls;
073:
074: // TYPE_NAME
075: String type = rs.getString(6).toLowerCase();
076:
077: _isUnsigned = type.indexOf("unsigned") >= 0;
078: _isZeroFill = type.indexOf("zerofill") >= 0;
079: }
080:
081: /**
082: * Returns the column's name.
083: */
084: public String getName() {
085: return _name;
086: }
087:
088: /**
089: * Returns the column's table
090: */
091: public JdbcTableMetaData getTable() {
092: return _table;
093: }
094:
095: /**
096: * Returns the column length.
097: */
098: public int getLength() {
099: return _length;
100: }
101:
102: /**
103: * Returns true if the column is nullable.
104: */
105: public boolean isNotNull() {
106: return _isNotNull;
107: }
108:
109: /**
110: * Returns true for a primary key.
111: */
112: public boolean isPrimaryKey() {
113: return _isPrimaryKey;
114: }
115:
116: /**
117: * Set true for a primary key.
118: */
119: void setPrimaryKey(boolean isPrimaryKey) {
120: _isPrimaryKey = isPrimaryKey;
121: }
122:
123: /**
124: * Returns true for an index
125: */
126: public boolean isIndex() {
127: return _isIndex;
128: }
129:
130: /**
131: * Set true for an index
132: */
133: void setIndex(boolean isIndex) {
134: _isIndex = isIndex;
135: }
136:
137: /**
138: * Returns true for a unique column
139: */
140: public boolean isUnique() {
141: return _isUnique;
142: }
143:
144: /**
145: * Set true for a unique column
146: */
147: void setUnique(boolean isUnique) {
148: _isUnique = isUnique;
149: }
150:
151: /**
152: * Returns the JDBC type.
153: */
154: public int getJdbcType() {
155: return _jdbcType;
156: }
157:
158: /**
159: * Returns true for numeric data types.
160: */
161: public static boolean isNumeric(int jdbcType) {
162: switch (jdbcType) {
163: case Types.BIT:
164: case Types.TINYINT:
165: case Types.SMALLINT:
166: case Types.INTEGER:
167: case Types.BIGINT:
168: case Types.DOUBLE:
169: case Types.FLOAT:
170: case Types.REAL:
171: return true;
172: default:
173: return false;
174: }
175: }
176:
177: /**
178: * Returns true for numeric data types.
179: */
180: public boolean isNumeric() {
181: return isNumeric(_jdbcType);
182: }
183:
184: /**
185: * Returns true for float data types.
186: */
187: public boolean isFloat() {
188: switch (_jdbcType) {
189: case Types.DOUBLE:
190: case Types.FLOAT:
191: case Types.REAL:
192: return true;
193: default:
194: return false;
195: }
196: }
197:
198: /**
199: * Returns true for unsigned.
200: */
201: public boolean isUnsigned() {
202: return _isUnsigned;
203: }
204:
205: /**
206: * Returns true for zerofill
207: */
208: public boolean isZeroFill() {
209: return _isZeroFill;
210: }
211:
212: /**
213: * Returns true for blob data types.
214: */
215: public static boolean isBlob(int jdbcType) {
216: switch (jdbcType) {
217: // php/142s
218: // php/142v
219: case Types.LONGVARBINARY:
220: case Types.LONGVARCHAR:
221: case Types.BLOB:
222: case Types.CLOB:
223: return true;
224: default:
225: return false;
226: }
227: }
228:
229: /**
230: * Returns true for blob data types.
231: */
232: public boolean isBlob() {
233: return isBlob(_jdbcType);
234: }
235:
236: public String toString() {
237: return "JdbcColumnMetaData[" + getName() + "]";
238: }
239: }
|