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.Iterator;
044: import java.util.Vector;
045:
046: import java.sql.SQLException;
047:
048: import com.quadcap.sql.Column;
049: import com.quadcap.sql.Database;
050: import com.quadcap.sql.Expression;
051: import com.quadcap.sql.Relation;
052: import com.quadcap.sql.Row;
053: import com.quadcap.sql.Session;
054: import com.quadcap.sql.Table;
055: import com.quadcap.sql.View;
056:
057: import com.quadcap.sql.index.BCursor;
058: import com.quadcap.sql.index.Btree;
059:
060: import com.quadcap.sql.types.*;
061:
062: import com.quadcap.util.Debug;
063:
064: /**
065: * A Cursor supporting the <code>getTables</code> function.
066: *
067: * @author Stan Bailes
068: */
069: public class MetaTables extends MetaCursor {
070: static Column[] cols = { new Column("TABLE_CAT", typeString),
071: new Column("TABLE_SCHEM", typeString),
072: new Column("TABLE_NAME", typeString),
073: new Column("TABLE_TYPE", typeString),
074: new Column("REMARKS", typeString) };
075:
076: static int[] sortColumns = { 4, 2, 3 };
077:
078: public MetaTables(Session session, Expression predicate)
079: throws SQLException {
080: super (session, predicate);
081: try {
082: addColumns(cols);
083: Database db = session.getDatabase();
084: session.getTableWriteLock("#Schema");
085: synchronized (db.getFile().getLock()) {
086: Iterator iter = db.getRelationNameIterator();
087: while (iter.hasNext()) {
088: String name = (String) iter.next();
089:
090: Relation r = db.getRelation(name);
091: Row row = makeRow(r);
092: if (rowMatch(row)) {
093: addRow(row);
094: }
095: }
096: }
097: sort();
098: } catch (ValueException e) {
099: Debug.print(e);
100: SQLException te = new SQLException(e.toString(), "Q000P");
101: te.setNextException(e);
102: throw te;
103: } catch (IOException e) {
104: Debug.print(e);
105: throw new SQLException(e.toString(), "Q000Q");
106: }
107: }
108:
109: public int[] getSortColumns() {
110: return sortColumns;
111: }
112:
113: Row makeRow(Relation r) throws SQLException {
114: Row row = new Row(5);
115: row.set(1, ValueNull.valueNull);
116: doTableName(2, row, r.getName());
117: row.set(4, new ValueString(r.getType()));
118: if (r instanceof View) {
119: row.set(5, new ValueString(((View) r).toString()));
120: } else {
121: row.set(5, ValueNull.valueNull);
122: }
123: return row;
124: }
125:
126: }
|