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: //-//#ifdef JDK11
044: //-import com.sun.java.util.collections.Vector;
045: //#else
046: import java.util.Vector; //#endif
047:
048: import java.sql.ResultSet;
049: import java.sql.SQLException;
050:
051: import com.quadcap.sql.Column;
052: import com.quadcap.sql.Cursor;
053: import com.quadcap.sql.Database;
054: import com.quadcap.sql.Expression;
055: import com.quadcap.sql.Relation;
056: import com.quadcap.sql.Row;
057: import com.quadcap.sql.Session;
058: import com.quadcap.sql.StaticCursor;
059: import com.quadcap.sql.Table;
060:
061: import com.quadcap.sql.types.*;
062:
063: import com.quadcap.util.Debug;
064:
065: /**
066: * Base class for all cursors used in this package.
067: *
068: * @author Stan Bailes
069: */
070: public class MetaCursor extends StaticCursor {
071: static TypeVarChar typeString = new TypeVarChar(-1);
072: static TypeInt typeInt = new TypeInt();
073: static TypeSmallInt typeShort = new TypeSmallInt();
074: static TypeAny typeAny = new TypeAny();
075: static TypeBinary typeBinary = new TypeBinary(1);
076:
077: StaticCursor sc;
078: Expression predicate;
079:
080: /**
081: * Constructor for a typical meta cursor with a predicate
082: */
083: public MetaCursor(Session session, Expression predicate)
084: throws SQLException {
085: super (session, new Vector());
086: try {
087: session.getTableWriteLock("#Schema");
088: } catch (IOException e) {
089: throw new SQLException(e.toString());
090: }
091: this .session = session;
092: this .predicate = predicate;
093: Vector vec = new Vector();
094: vec.addElement(null);
095: sc = new StaticCursor(session, vec);
096: }
097:
098: /**
099: * Constructor for empty metacursor
100: */
101: public static MetaCursor create(Session session, Column[] cols)
102: throws SQLException {
103: MetaCursor c = new MetaCursor(session, null);
104: c.addColumns(cols);
105: return c;
106: }
107:
108: /**
109: * Matcher function (compare the row against the supplied predicate)
110: */
111: boolean rowMatch(Row row) throws SQLException {
112: if (predicate == null)
113: return true;
114: sc.updateRow(0, row);
115: sc.absolute(1);
116: Value v = predicate.getValue(session, sc);
117: return (v instanceof ValueBoolean && ((ValueBoolean) v)
118: .isTrue());
119: }
120:
121: /**
122: * Add a list of columns
123: */
124: void addColumns(Column[] cols) throws SQLException {
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 name) throws SQLException {
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 = { new Column("TABLE_CAT", typeString) // 1
158: };
159:
160: static Column[] cProcedureColumns = {
161: new Column("PROCEDURE_CAT", typeString), // 1
162: new Column("PROCEDURE_SCHEM", typeString), // 2
163: new Column("PROCEDURE_NAME", typeString), // 3
164: new Column("COLUMN_NAME", typeString), // 4
165: new Column("COLUMN_TYPE", typeShort), // 5
166: new Column("DATA_TYPE", typeShort), // 6
167: new Column("TYPE_NAME", typeString), // 7
168: new Column("PRECISION", typeInt), // 8
169: new Column("LENGTH", typeInt), // 9
170: new Column("SCALE", typeShort), // 10
171: new Column("RADIX", typeInt), // 11
172: new Column("NULLABLE", typeShort), // 12
173: new Column("REMARKS", typeString), // 13
174: };
175:
176: static Column[] cProcedures = {
177: new Column("PROCEDURE_CAT", typeString), // 1
178: new Column("PROCEDURE_SCHEM", typeString), // 2
179: new Column("PROCEDURE_NAME", typeString), // 3
180: new Column("reserved1", typeString), // 4
181: new Column("reserved2", typeString), // 5
182: new Column("reserved3", typeString), // 6
183: new Column("REMARKS", typeString), // 7
184: new Column("PROCEDURE_TYPE", typeShort) // 8
185: };
186:
187: static Column[] cTablePrivileges = {
188: new Column("TABLE_CAT", typeString), // 1
189: new Column("TABLE_SCHEM", typeString), // 2
190: new Column("TABLE_NAME", typeString), // 3
191: new Column("GRANTOR", typeString), // 4
192: new Column("GRANTEE", typeString), // 5
193: new Column("PRIVILEGE", typeString), // 6
194: new Column("IS_GRANTABLE", typeString) // 7
195: };
196:
197: static Column[] cUdts = { new Column("TYPE_CAT", typeString), // 1
198: new Column("TYPE_SCHEM", typeString), // 2
199: new Column("TYPE_NAME", typeString), // 3
200: new Column("CLASS_NAME", typeString), // 4
201: new Column("DATA_TYPE", typeShort), // 5
202: new Column("REMARKS", typeString) // 6
203: };
204:
205: static Column[] cVersionColumns = { new Column("SCOPE", typeShort), // 1
206: new Column("COLUMN_NAME", typeString), // 2
207: new Column("DATA_TYPE", typeShort), // 3
208: new Column("TYPE_NAME", typeString), // 4
209: new Column("COLUMN_SIZE", typeInt), // 5
210: new Column("BUFFER_LENGTH", typeInt), // 6
211: new Column("DECIMAL_DIGITS", typeInt), // 7
212: new Column("PSEUDO_COLUMN", typeShort) // 8
213: };
214:
215: /**
216: * Information schema
217: */
218: public static Cursor find(Session session, String name)
219: throws SQLException {
220: if (name.equalsIgnoreCase("SYSTEM.CATALOGS")) {
221: return create(session, cCatalogs);
222: } else if (name.equalsIgnoreCase("SYSTEM.COLUMNS")) {
223: return new MetaColumns(session, null);
224: } else if (name.equalsIgnoreCase("SYSTEM.COLUMNPRIVILEGES")) {
225: return create(session, cColumnPrivileges);
226: } else if (name.equalsIgnoreCase("SYSTEM.CROSSREFERENCE")) {
227: return new MetaCrossReference(session, null);
228: } else if (name.equalsIgnoreCase("SYSTEM.INDEXINFO")) {
229: return new MetaIndexInfo(session, null);
230: } else if (name.equalsIgnoreCase("SYSTEM.PRIMARYKEYS")) {
231: return new MetaPrimaryKeys(session, null);
232: } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURECOLUMNS")) {
233: return create(session, cProcedureColumns);
234: } else if (name.equalsIgnoreCase("SYSTEM.PROCEDURES")) {
235: return create(session, cProcedures);
236: } else if (name.equalsIgnoreCase("SYSTEM.SCHEMAS")) {
237: return new MetaSchemas(session);
238: } else if (name.equalsIgnoreCase("SYSTEM.TABLES")) {
239: return new MetaTables(session, null);
240: } else if (name.equalsIgnoreCase("SYSTEM.TABLEPRIVILEGES")) {
241: return create(session, cTablePrivileges);
242: } else if (name.equalsIgnoreCase("SYSTEM.TABLETYPES")) {
243: return new MetaTableTypes(session);
244: } else if (name.equalsIgnoreCase("SYSTEM.VERSIONCOLUMNS")) {
245: return create(session, cVersionColumns);
246: } else if (name.equalsIgnoreCase("SYSTEM.TYPEINFO")) {
247: return new MetaTypes(session);
248: } else if (name.equalsIgnoreCase("SYSTEM.UDTS")) {
249: return create(session, cUdts);
250: }
251: return null;
252: }
253: }
|