KickJava   Java API By Example, From Geeks To Geeks.

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


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 //-//#ifdef JDK11
44
//-import com.sun.java.util.collections.Vector;
45
//#else
46
import java.util.Vector JavaDoc;
47 //#endif
48

49 import java.sql.ResultSet JavaDoc;
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.Column;
53 import com.quadcap.sql.Cursor;
54 import com.quadcap.sql.Database;
55 import com.quadcap.sql.Expression;
56 import com.quadcap.sql.Relation;
57 import com.quadcap.sql.Row;
58 import com.quadcap.sql.Session;
59 import com.quadcap.sql.StaticCursor;
60 import com.quadcap.sql.Table;
61
62 import com.quadcap.sql.types.*;
63
64 import com.quadcap.util.Debug;
65
66 /**
67  * Base class for all cursors used in this package.
68  *
69  * @author Stan Bailes
70  */

71 public class MetaCursor extends StaticCursor {
72     static TypeVarChar typeString = new TypeVarChar(-1);
73     static TypeInt typeInt = new TypeInt();
74     static TypeSmallInt typeShort = new TypeSmallInt();
75     static TypeAny typeAny = new TypeAny();
76     static TypeBinary typeBinary = new TypeBinary(1);
77     
78     StaticCursor sc;
79     Expression predicate;
80
81     /**
82      * Constructor for a typical meta cursor with a predicate
83      */

84     public MetaCursor(Session session, Expression predicate)
85     throws SQLException JavaDoc
86     {
87     super(session, new Vector JavaDoc());
88         try {
89             session.getTableWriteLock("#Schema");
90         } catch (IOException JavaDoc e) {
91             throw new SQLException JavaDoc(e.toString());
92         }
93     this.session = session;
94     this.predicate = predicate;
95     Vector JavaDoc vec = new Vector JavaDoc();
96     vec.addElement(null);
97     sc = new StaticCursor(session, vec);
98     }
99     
100     /**
101      * Constructor for empty metacursor
102      */

103     public static MetaCursor create(Session session, Column[] cols)
104         throws SQLException JavaDoc {
105         MetaCursor c = new MetaCursor(session, null);
106         c.addColumns(cols);
107         return c;
108     }
109
110     /**
111      * Matcher function (compare the row against the supplied predicate)
112      */

113     boolean rowMatch(Row row) throws SQLException JavaDoc {
114     if (predicate == null) return true;
115     sc.updateRow(0, row);
116     sc.absolute(1);
117     Value v = predicate.getValue(session, sc);
118     return (v instanceof ValueBoolean && ((ValueBoolean)v).isTrue());
119     }
120
121     /**
122      * Add a list of columns
123      */

124     void addColumns(Column[] cols) throws SQLException JavaDoc {
125     for (int i = 0; i < cols.length; i++) {
126         addColumn(cols[i]);
127         sc.addColumn(cols[i]);
128     }
129     }
130
131     /**
132      * Break down schema and table name into adjacent columns in the
133      * result set
134      */

135     void doTableName(int col, Row row, String JavaDoc name) throws SQLException JavaDoc {
136     int idx = name.indexOf('.');
137     if (idx > 0) {
138         row.set(col, new ValueString(name.substring(0, idx)));
139         row.set(col+1, new ValueString(name.substring(idx+1)));
140     } else {
141         row.set(col, new ValueString(""));
142         row.set(col+1, new ValueString(name));
143     }
144     }
145
146     static Column[] cColumnPrivileges = {
147     new Column("TABLE_CAT", typeString), // 1
148
new Column("TABLE_SCHEM", typeString), // 2
149
new Column("TABLE_NAME", typeString), // 3
150
new Column("COLUMN_NAME", typeString), // 4
151
new Column("GRANTOR", typeString), // 5
152
new Column("GRANTEE", typeString), // 6
153
new Column("PRIVILEGE", typeString), // 7
154
new Column("IS_GRANTABLE", typeString) // 8
155
};
156     
157     static Column[] cCatalogs = {
158     new Column("TABLE_CAT", typeString) // 1
159
};
160     
161     static Column[] cProcedureColumns = {
162     new Column("PROCEDURE_CAT", typeString), // 1
163
new Column("PROCEDURE_SCHEM", typeString), // 2
164
new Column("PROCEDURE_NAME", typeString), // 3
165
new Column("COLUMN_NAME", typeString), // 4
166
new Column("COLUMN_TYPE", typeShort), // 5
167
new Column("DATA_TYPE", typeShort), // 6
168
new Column("TYPE_NAME", typeString), // 7
169
new Column("PRECISION", typeInt), // 8
170
new Column("LENGTH", typeInt), // 9
171
new Column("SCALE", typeShort), // 10
172
new Column("RADIX", typeInt), // 11
173
new Column("NULLABLE", typeShort), // 12
174
new Column("REMARKS", typeString), // 13
175
};
176
177     static Column[] cProcedures = {
178     new Column("PROCEDURE_CAT", typeString), // 1
179
new Column("PROCEDURE_SCHEM", typeString), // 2
180
new Column("PROCEDURE_NAME", typeString), // 3
181
new Column("reserved1", typeString), // 4
182
new Column("reserved2", typeString), // 5
183
new Column("reserved3", typeString), // 6
184
new Column("REMARKS", typeString), // 7
185
new Column("PROCEDURE_TYPE", typeShort) // 8
186
};
187
188     static Column[] cTablePrivileges = {
189     new Column("TABLE_CAT", typeString), // 1
190
new Column("TABLE_SCHEM", typeString), // 2
191
new Column("TABLE_NAME", typeString), // 3
192
new Column("GRANTOR", typeString), // 4
193
new Column("GRANTEE", typeString), // 5
194
new Column("PRIVILEGE", typeString), // 6
195
new Column("IS_GRANTABLE", typeString) // 7
196
};
197
198     static Column[] cUdts= {
199     new Column("TYPE_CAT", typeString), // 1
200
new Column("TYPE_SCHEM", typeString), // 2
201
new Column("TYPE_NAME", typeString), // 3
202
new Column("CLASS_NAME", typeString), // 4
203
new Column("DATA_TYPE", typeShort), // 5
204
new Column("REMARKS", typeString) // 6
205
};
206     
207     static Column[] cVersionColumns = {
208         new Column("SCOPE", typeShort), // 1
209
new Column("COLUMN_NAME", typeString), // 2
210
new Column("DATA_TYPE", typeShort), // 3
211
new Column("TYPE_NAME", typeString), // 4
212
new Column("COLUMN_SIZE", typeInt), // 5
213
new Column("BUFFER_LENGTH", typeInt), // 6
214
new Column("DECIMAL_DIGITS", typeInt), // 7
215
new Column("PSEUDO_COLUMN", typeShort) // 8
216
};
217
218     /**
219      * Information schema
220      */

221     public static Cursor find(Session session, String JavaDoc name)
222         throws SQLException JavaDoc
223     {
224         if (name.equalsIgnoreCase("SYSTEM.CATALOGS")) {
225             return create(session, cCatalogs);
226     } else if (name.equalsIgnoreCase("SYSTEM.COLUMNS")) {
227             return new MetaColumns(session, null);
228     } else if (name.equalsIgnoreCase("SYSTEM.COLUMNPRIVILEGES")) {
229             return create(session, cColumnPrivileges);
230     } else if (name.equalsIgnoreCase("SYSTEM.CROSSREFERENCE")) {
231             return new MetaCrossReference(session, null);
232     } else if (name.equalsIgnoreCase("SYSTEM.INDEXINFO")) {
233             return new MetaIndexInfo(session, null);
234     } else if (name.equalsIgnoreCase("SYSTEM.PRIMARYKEYS")) {
235             return new MetaPrimaryKeys(session, null);
236     } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURECOLUMNS")) {
237             return create(session, cProcedureColumns);
238     } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURES")) {
239             return create(session, cProcedures);
240     } else if (name.equalsIgnoreCase("SYSTEM.SCHEMAS")) {
241             return new MetaSchemas(session);
242     } else if (name.equalsIgnoreCase("SYSTEM.TABLES")) {
243             return new MetaTables(session, null);
244     } else if (name.equalsIgnoreCase("SYSTEM.TABLEPRIVILEGES")) {
245             return create(session, cTablePrivileges);
246     } else if (name.equalsIgnoreCase("SYSTEM.TABLETYPES")) {
247             return new MetaTableTypes(session);
248     } else if (name.equalsIgnoreCase("SYSTEM.VERSIONCOLUMNS")) {
249             return create(session, cVersionColumns);
250     } else if (name.equalsIgnoreCase("SYSTEM.TYPEINFO")) {
251             return new MetaTypes(session);
252     } else if (name.equalsIgnoreCase("SYSTEM.UDTS")) {
253             return create(session, cUdts);
254         }
255         return null;
256     }
257 }
258
Popular Tags