001: /*
002: * tinySQLResultSetMetaData
003: *
004: * This is the tinySQL Result Set Meta Data class.
005: *
006: * A lot of this code is based on or directly taken from
007: * George Reese's (borg@imaginary.com) mSQL driver.
008: *
009: * So, it's probably safe to say:
010: *
011: * Portions of this code Copyright (c) 1996 George Reese
012: *
013: * The rest of it:
014: *
015: * Copyright 1996, Brian C. Jepson
016: * (bjepson@ids.net)
017: * $Author: davis $
018: * $Date: 2004/12/18 21:32:53 $
019: * $Revision: 1.1 $
020: *
021: * This library is free software; you can redistribute it and/or
022: * modify it under the terms of the GNU Lesser General Public
023: * License as published by the Free Software Foundation; either
024: * version 2.1 of the License, or (at your option) any later version.
025: *
026: * This library is distributed in the hope that it will be useful,
027: * but WITHOUT ANY WARRANTY; without even the implied warranty of
028: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029: * Lesser General Public License for more details.
030: *
031: * You should have received a copy of the GNU Lesser General Public
032: * License along with this library; if not, write to the Free Software
033: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
034: *
035: */
036:
037: package com.sqlmagic.tinysql;
038:
039: import java.sql.SQLException;
040: import java.sql.Types;
041:
042: public class tinySQLResultSetMetaData implements
043: java.sql.ResultSetMetaData {
044:
045: /**
046: *
047: * The result set.
048: *
049: */
050: private tsResultSet tsql;
051:
052: /**
053: *
054: * Constructs a tinySQLResultSet; requires a tsResultSet object
055: * @param result the tsResultSet object
056: *
057: */
058: public tinySQLResultSetMetaData(tsResultSet result) {
059: tsql = result;
060: }
061:
062: /**
063: *
064: * Returns the number of columns in this result set.
065: * @see java.sqlResultSetMetaData#getColumnCount
066: * @return number of columns
067: *
068: */
069: public int getColumnCount() throws SQLException {
070: return tsql.getColumnCount();
071: }
072:
073: /**
074: *
075: * Is the column an autoincrement (identity, counter) column?
076: * @see java.sql.ResultSetMetaData#isAutoIncrement
077: * @return false - tinySQL does not support autoincrement columns
078: *
079: */
080: public boolean isAutoIncrement(int column) throws SQLException {
081: return false;
082: }
083:
084: /**
085: *
086: * Is case significant in column names?
087: * @see java.sql.ResultSetMetaData#isCaseSensitive
088: * @return true
089: *
090: */
091: public boolean isCaseSensitive(int column) throws SQLException {
092: return true;
093: }
094:
095: /**
096: *
097: * Can the column be used in a where clause?
098: * @see java.sql.ResultSetMetaData#isSearchable
099: * @return
100: *
101: */
102: public boolean isSearchable(int column) throws SQLException {
103: return true;
104: }
105:
106: /**
107: *
108: * Is the column some sort of currency?
109: * @see java.sql.ResultSetMetaData#isCurrency
110: * @return tinySQL doesn't have such things, so it's false
111: *
112: */
113: public boolean isCurrency(int column) throws SQLException {
114: return false;
115: }
116:
117: /**
118: *
119: * Determines if the column in question is nullable. tinySQL
120: * does not yet support nulls.
121: * @see java.sql.ResultSetMetaData#isNullable
122: * @return columnNoNulls, columnNullable, or columnNullableUnknown
123: *
124: */
125: public int isNullable(int column) throws SQLException {
126: return columnNoNulls;
127: }
128:
129: /**
130: *
131: * All tinySQL integers are signed, so this returns true.
132: * @see java.sql.ResultSetMetaData#isSigned
133: * @return true
134: *
135: */
136: public boolean isSigned(int column) throws SQLException {
137: return true;
138: }
139:
140: /**
141: *
142: * Gives the display size for this column.
143: * @see java.sql.ResultSetMetaData#getColumnDisplaySize
144: *
145: */
146: public int getColumnDisplaySize(int column) throws SQLException {
147:
148: // get a column object. Remember, tinySQL uses a column
149: // offset of zero, but JDBC columns start numbering at one.
150: // That's why there's a -1 in the columnAtIndex invocation.
151: //
152: tsColumn col = tsql.columnAtIndex(column - 1);
153: return col.size;
154:
155: }
156:
157: /**
158: *
159: * This returns the column name in the form table_name.column_name.
160: * @see java.sql.ResultSetMetaData#getColumnLabel
161: * @param column the column whose label is wanted
162: * @return the fully qualified column name
163: *
164: */
165: public String getColumnLabel(int column) throws SQLException {
166:
167: // get the column, return its table and name, separated by a '.'
168: //
169: tsColumn col = tsql.columnAtIndex(column - 1);
170: return (col.tableName + "." + col.name);
171: }
172:
173: /**
174: * The name of a given column
175: * @see java.sql.ResultSetMetaData#getColumnName
176: * @param column the column whose name is wanted
177: * @return the name of the requested column
178: */
179: public String getColumnName(int column) throws SQLException {
180:
181: // get the column and return its name
182: //
183: String columnName;
184: int dotAt;
185: String msg;
186: tsColumn col = tsql.columnAtIndex(column - 1);
187: dotAt = col.name.indexOf(".");
188: columnName = col.name;
189: if (dotAt > -1)
190: columnName = col.name.substring(dotAt + 1);
191: if (col.alias != (String) null)
192: if (!col.alias.equals(columnName))
193: return col.alias;
194: columnName = tinySQLGlobals.getLongName(columnName);
195: return columnName;
196: }
197:
198: /**
199: *
200: * What's the column's schema? This is not applicable to tinySQL,
201: * so it returns an empty string.
202: *
203: */
204: public String getSchemaName(int column) throws SQLException {
205: return "";
206: }
207:
208: /**
209: *
210: * What's the column's precision? Use size.
211: *
212: */
213: public int getPrecision(int column) throws SQLException {
214: tsColumn col = tsql.columnAtIndex(column - 1);
215: return col.size;
216: }
217:
218: /**
219: *
220: * What's a column's number of digits to right of decimal?
221: *
222: */
223: public int getScale(int column) throws SQLException {
224: tsColumn col = tsql.columnAtIndex(column - 1);
225: return col.decimalPlaces;
226: }
227:
228: /**
229: *
230: * Gives the name of the table to which this column belongs.
231: * @see java.sql.ResultSetMetaData#getTableName
232: * @param column the column of the field this information is needed for
233: * @return the table name
234: *
235: */
236: public String getTableName(int column) throws SQLException {
237:
238: // retrieve the column info and return the table name
239: //
240: tsColumn col = tsql.columnAtIndex(column - 1);
241: return col.tableName;
242: }
243:
244: /**
245: *
246: * Return the column's table catalog name. Not supported by tinySQL
247: *
248: */
249: public String getCatalogName(int column) throws SQLException {
250: throw new SQLException("tinySQL does not support catalogues.");
251: }
252:
253: /**
254: *
255: * Gives the column type using the types in java.sql.Types.
256: * @see java.sqlTypes
257: * @see java.sql.ResultSetMetaData#getColumnType
258: * @exception SQLException thrown for any number of reasons
259: * @param column the column type information is needed on
260: * @return the type as listed in java.sql.Types
261: *
262: */
263: public int getColumnType(int column) throws SQLException {
264:
265: // get the column info object
266: //
267: tsColumn col = tsql.columnAtIndex(column - 1);
268: return col.type;
269: }
270:
271: /**
272: *
273: * Gives the column type as a string.
274: * @see java.sql.ResultSetMetaData#getColumnTypeName
275: * @exception SQLException thrown at you
276: * @param column the column for which the type name is wanted
277: * @return the name of the column type
278: *
279: * @author Thomas Morgner <mgs@sherito.org>: This function does
280: * not properly return type names - everything except INTEGER or
281: * CHAR returns "NULL"
282: */
283: public String getColumnTypeName(int column) throws SQLException {
284:
285: // just call getColumnType, and report on what it said
286: //
287: switch (getColumnType(column)) {
288:
289: case Types.INTEGER:
290: return "INT";
291:
292: case Types.CHAR:
293: return "CHAR";
294:
295: case Types.FLOAT:
296: return "FLOAT";
297:
298: case Types.DATE:
299: return "DATE";
300: default:
301: return "NULL";
302: }
303: }
304:
305: /**
306: *
307: * Is the column definitely not writable? This has no meaning
308: * in tinySQL
309: *
310: */
311: public boolean isReadOnly(int column) throws SQLException {
312: return false;
313: }
314:
315: /**
316: *
317: * Is the column potentially writable? This has no meaning
318: * in tinySQL
319: *
320: */
321: public boolean isWritable(int column) throws SQLException {
322: return true;
323: }
324:
325: /**
326: *
327: * Is the column definitely writable? This has no meaning
328: * in tinySQL
329: *
330: */
331: public boolean isDefinitelyWritable(int column) throws SQLException {
332: return true;
333: }
334:
335: //--------------------------JDBC 2.0-----------------------------------
336:
337: /**
338: * JDBC 2.0
339: *
340: * <p>Returns the fully-qualified name of the Java class whose instances
341: * are manufactured if the method <code>ResultSet.getObject</code>
342: * is called to retrieve a value
343: * from the column. <code>ResultSet.getObject</code> may return a subclass of the
344: * class returned by this method.
345: *
346: * @return the fully-qualified name of the class in the Java programming
347: * language that would be used by the method
348: * <code>ResultSet.getObject</code> to retrieve the value in the specified
349: * column. This is the class name used for custom mapping.
350: * @exception SQLException if a database access error occurs
351: */
352: public String getColumnClassName(int column) throws SQLException {
353: throw new SQLException(
354: "tinySQL does not support getColumnClassName.");
355: }
356: }
|