001: package com.quadcap.sql.meta;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Vector;
047:
048: import java.sql.ResultSetMetaData;
049: import java.sql.SQLException;
050:
051: import com.quadcap.sql.Column;
052: import com.quadcap.sql.Database;
053: import com.quadcap.sql.Expression;
054: import com.quadcap.sql.Relation;
055: import com.quadcap.sql.Row;
056: import com.quadcap.sql.Session;
057: import com.quadcap.sql.StaticCursor;
058: import com.quadcap.sql.Table;
059:
060: import com.quadcap.sql.index.BCursor;
061: import com.quadcap.sql.index.Btree;
062:
063: import com.quadcap.sql.types.*;
064:
065: import com.quadcap.util.Debug;
066:
067: /**
068: * A Cursor supporting the <code>DatabaseMetaData.getColumns()</code>
069: * operation.
070: *
071: * @author Stan Bailes
072: */
073: public class MetaColumns extends MetaCursor {
074: static Column[] cols = { new Column("TABLE_CAT", typeString), // 1
075: new Column("TABLE_SCHEM", typeString), // 2
076: new Column("TABLE_NAME", typeString), // 3
077: new Column("COLUMN_NAME", typeString), // 4
078: new Column("DATA_TYPE", typeShort), // 5
079: new Column("TYPE_NAME", typeString), // 6
080: new Column("COLUMN_SIZE", typeInt), // 7
081: new Column("BUFFER_LENGTH", typeAny), // 8
082: new Column("DECIMAL_DIGITS", typeInt), // 9
083: new Column("NUM_PREC_RADIX", typeInt), // 10
084: new Column("NULLABLE", typeInt), // 11
085: new Column("REMARKS", typeString), // 12
086: new Column("COLUMN_DEF", typeString), // 13
087: new Column("SQL_DATA_TYPE", typeInt), // 14
088: new Column("SQL_DATETIME_SUB", typeInt), // 15
089: new Column("CHAR_OCTET_LENGTH", typeInt), // 16
090: new Column("ORDINAL_POSITION", typeInt), // 17
091: new Column("IS_NULLABLE", typeString) // 18
092: };
093:
094: static int[] sortColumns = { 2, 3, 17 };
095:
096: public MetaColumns(Session session, Expression predicate)
097: throws SQLException {
098: super (session, predicate);
099: try {
100: addColumns(cols);
101: Database db = session.getDatabase();
102: session.getTableWriteLock("#Schema");
103: synchronized (db.getFile().getLock()) {
104: Iterator iter = db.getRelationNameIterator();
105: while (iter.hasNext()) {
106: Relation r = db.getRelation(iter.next().toString());
107: if (r instanceof Table) {
108: Table t = (Table) r;
109: doTable(t);
110: }
111: }
112: }
113: sort();
114: } catch (ValueException e) {
115: Debug.print(e);
116: SQLException te = new SQLException(e.toString(), "Q000J");
117: te.setNextException(e);
118: throw te;
119: } catch (IOException e) {
120: Debug.print(e);
121: throw new SQLException(e.toString(), "Q000K");
122: }
123: }
124:
125: public int[] getSortColumns() {
126: return sortColumns;
127: }
128:
129: void doTable(Table t) throws SQLException {
130: for (int i = 1; i <= t.getColumnCount(); i++) {
131: Column col = t.getColumn(i);
132: Row row = doColumn(t, col);
133: if (rowMatch(row)) {
134: addRow(row);
135: }
136: }
137: }
138:
139: Row doColumn(Table t, Column col) throws SQLException {
140: Type type = col.getType();
141: Row row = new Row(18);
142: row.set(1, ValueNull.valueNull);
143: doTableName(2, row, t.getName());
144: row.set(4, new ValueString(col.getShortName()));
145: row.set(5, new ValueShort(type.getJDBCType()));
146: row.set(6, new ValueString(type.getTypeName()));
147:
148: Value size = new ValueInteger(type.getPrecision());
149: if (type instanceof TypeChar) {
150: size = new ValueInteger(((TypeChar) type).getMax());
151: } else if (type instanceof TypeVarChar) {
152: size = new ValueInteger(((TypeVarChar) type).getMax());
153: }
154: row.set(7, size);
155: row.set(8, ValueNull.valueNull);
156: row.set(9, new ValueInteger(type.getScale()));
157: row.set(10, new ValueInteger(10));
158: row.set(11, new ValueInteger(col.getNullable()));
159: row.set(12, ValueNull.valueNull);
160: Value defVal = ValueNull.valueNull;
161: Expression def = col.getDefault();
162: if (def != null) {
163: defVal = new ValueString(def.toString());
164: }
165: row.set(13, defVal);
166: row.set(14, ValueNull.valueNull);
167: row.set(15, ValueNull.valueNull);
168:
169: Value max = ValueNull.valueNull;
170: if (type instanceof TypeChar) {
171: max = new ValueInteger(2 * ((TypeChar) type).getMax());
172: } else if (type instanceof TypeVarChar) {
173: max = new ValueInteger(2 * ((TypeVarChar) type).getMax());
174: }
175: row.set(16, max);
176: row.set(17, new ValueInteger(col.getColumn()));
177:
178: String nullstr = "";
179: switch (col.getNullable()) {
180: case ResultSetMetaData.columnNoNulls:
181: nullstr = "NO";
182: break;
183: case ResultSetMetaData.columnNullable:
184: nullstr = "YES";
185: break;
186: }
187: row.set(18, new ValueString(nullstr));
188: return row;
189: }
190:
191: }
|