KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > meta > MetaColumns


1 package com.quadcap.sql.meta;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 import java.sql.ResultSetMetaData JavaDoc;
49 import java.sql.SQLException JavaDoc;
50
51 import com.quadcap.sql.Column;
52 import com.quadcap.sql.Database;
53 import com.quadcap.sql.Expression;
54 import com.quadcap.sql.Relation;
55 import com.quadcap.sql.Row;
56 import com.quadcap.sql.Session;
57 import com.quadcap.sql.StaticCursor;
58 import com.quadcap.sql.Table;
59
60 import com.quadcap.sql.index.BCursor;
61 import com.quadcap.sql.index.Btree;
62
63 import com.quadcap.sql.types.*;
64
65 import com.quadcap.util.Debug;
66
67 /**
68  * A Cursor supporting the <code>DatabaseMetaData.getColumns()</code>
69  * operation.
70  *
71  * @author Stan Bailes
72  */

73 public class MetaColumns extends MetaCursor {
74     static Column[] cols = {
75     new Column("TABLE_CAT", typeString), // 1
76
new Column("TABLE_SCHEM", typeString), // 2
77
new Column("TABLE_NAME", typeString), // 3
78
new Column("COLUMN_NAME", typeString), // 4
79
new Column("DATA_TYPE", typeShort), // 5
80
new Column("TYPE_NAME", typeString), // 6
81
new Column("COLUMN_SIZE", typeInt), // 7
82
new Column("BUFFER_LENGTH", typeAny), // 8
83
new Column("DECIMAL_DIGITS", typeInt), // 9
84
new Column("NUM_PREC_RADIX", typeInt), // 10
85
new Column("NULLABLE", typeInt), // 11
86
new Column("REMARKS", typeString), // 12
87
new Column("COLUMN_DEF", typeString), // 13
88
new Column("SQL_DATA_TYPE", typeInt), // 14
89
new Column("SQL_DATETIME_SUB", typeInt), // 15
90
new Column("CHAR_OCTET_LENGTH", typeInt), // 16
91
new Column("ORDINAL_POSITION", typeInt), // 17
92
new Column("IS_NULLABLE", typeString) // 18
93
};
94
95     static int[] sortColumns = { 2, 3, 17 };
96
97     public MetaColumns(Session session, Expression predicate)
98     throws SQLException JavaDoc
99     {
100     super(session, predicate);
101     try {
102         addColumns(cols);
103         Database db = session.getDatabase();
104             session.getTableWriteLock("#Schema");
105             synchronized (db.getFile().getLock()) {
106                 Iterator JavaDoc iter = db.getRelationNameIterator();
107                 while (iter.hasNext()) {
108                     Relation r = db.getRelation(iter.next().toString());
109                     if (r instanceof Table) {
110                         Table t = (Table)r;
111                         doTable(t);
112                     }
113                 }
114             }
115         sort();
116     } catch (ValueException e) {
117         Debug.print(e);
118         SQLException JavaDoc te = new SQLException JavaDoc(e.toString(), "Q000J");
119         te.setNextException(e);
120         throw te;
121     } catch (IOException JavaDoc e) {
122         Debug.print(e);
123         throw new SQLException JavaDoc(e.toString(), "Q000K");
124     }
125     }
126
127     public int[] getSortColumns() {
128     return sortColumns;
129     }
130
131     void doTable(Table t) throws SQLException JavaDoc {
132     for (int i = 1; i <= t.getColumnCount(); i++) {
133         Column col = t.getColumn(i);
134         Row row = doColumn(t, col);
135         if (rowMatch(row)) {
136         addRow(row);
137         }
138     }
139     }
140
141     Row doColumn(Table t, Column col) throws SQLException JavaDoc {
142     Type type = col.getType();
143     Row row = new Row(18);
144     row.set(1, ValueNull.valueNull);
145     doTableName(2, row, t.getName());
146     row.set(4, new ValueString(col.getShortName()));
147     row.set(5, new ValueShort(type.getJDBCType()));
148     row.set(6, new ValueString(type.getTypeName()));
149
150     Value size = new ValueInteger(type.getPrecision());
151     if (type instanceof TypeChar) {
152         size = new ValueInteger(((TypeChar)type).getMax());
153     } else if (type instanceof TypeVarChar) {
154         size = new ValueInteger(((TypeVarChar)type).getMax());
155     }
156     row.set(7, size);
157     row.set(8, ValueNull.valueNull);
158     row.set(9, new ValueInteger(type.getScale()));
159     row.set(10, new ValueInteger(10));
160     row.set(11, new ValueInteger(col.getNullable()));
161     row.set(12, ValueNull.valueNull);
162     Value defVal = ValueNull.valueNull;
163     Expression def = col.getDefault();
164     if (def != null) {
165             defVal = new ValueString(def.toString());
166     }
167     row.set(13, defVal);
168     row.set(14, ValueNull.valueNull);
169     row.set(15, ValueNull.valueNull);
170
171     Value max = ValueNull.valueNull;
172     if (type instanceof TypeChar) {
173         max = new ValueInteger(2 * ((TypeChar)type).getMax());
174     } else if (type instanceof TypeVarChar) {
175         max = new ValueInteger(2 * ((TypeVarChar)type).getMax());
176     }
177     row.set(16, max);
178     row.set(17, new ValueInteger(col.getColumn()));
179
180     String JavaDoc nullstr = "";
181     switch (col.getNullable()) {
182     case ResultSetMetaData.columnNoNulls:
183         nullstr = "NO";
184         break;
185     case ResultSetMetaData.columnNullable:
186         nullstr = "YES";
187         break;
188     }
189     row.set(18, new ValueString(nullstr));
190     return row;
191     }
192
193 }
194
Popular Tags