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:
045: import java.sql.DatabaseMetaData;
046: import java.sql.ResultSetMetaData;
047: import java.sql.SQLException;
048:
049: import com.quadcap.sql.Column;
050: import com.quadcap.sql.Constraint;
051: import com.quadcap.sql.Database;
052: import com.quadcap.sql.Expression;
053: import com.quadcap.sql.IndexConstraint;
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>getIndexInfo</code> function.
069: *
070: * @author Stan Bailes
071: */
072: public class MetaIndexInfo extends MetaCursor {
073: static Column[] cols = { new Column("TABLE_CAT", typeString), // 1
074: new Column("TABLE_SCHEM", typeString), // 2
075: new Column("TABLE_NAME", typeString), // 3
076: new Column("NON_UNIQUE", typeBinary), // 4
077: new Column("INDEX_QUALIFIER", typeString), // 5
078: new Column("INDEX_NAME", typeString), // 6
079: new Column("TYPE", typeShort), // 7
080: new Column("ORDINAL_POSITION", typeInt), // 8
081: new Column("COLUMN_NAME", typeString), // 9
082: new Column("ASC_OR_DESC", typeString), // 10
083: new Column("CARDINALITY", typeInt), // 11
084: new Column("PAGES", typeInt), // 12
085: new Column("FILTER_CONDITION", typeString) // 13
086: };
087:
088: static int[] sortColumns = { 4, 7, 6, 8 };
089:
090: public MetaIndexInfo(Session session, Expression predicate)
091: throws SQLException {
092: super (session, predicate);
093: try {
094: addColumns(cols);
095: Database db = session.getDatabase();
096: session.getTableWriteLock("#Schema");
097: synchronized (db.getFile().getLock()) {
098: Iterator iter = db.getRelationNameIterator();
099: while (iter.hasNext()) {
100: String name = (String) iter.next();
101: Relation r = db.getRelation(name);
102: if (r instanceof Table) {
103: Table t = (Table) r;
104: int num = t.getNumConstraints();
105: for (int i = 0; i < num; i++) {
106: Constraint c = t.getConstraint(i);
107: if (c instanceof IndexConstraint) {
108: doConstraint(t, (IndexConstraint) c);
109: }
110: }
111: }
112: }
113: }
114: sort();
115: } catch (ValueException e) {
116: Debug.print(e);
117: SQLException te = new SQLException(e.toString(), "Q000N");
118: te.setNextException(e);
119: throw te;
120: } catch (IOException e) {
121: Debug.print(e);
122: throw new SQLException(e.toString(), "Q000O");
123: }
124: }
125:
126: public int[] getSortColumns() {
127: return sortColumns;
128: }
129:
130: void doConstraint(Table t, IndexConstraint c) throws SQLException {
131: int[] colIndices = c.getColumns();
132: for (int i = 0; i < colIndices.length; i++) {
133: Column col = t.getColumn(colIndices[i]);
134: Row row = doColumn(t, c, i, col);
135: if (rowMatch(row)) {
136: addRow(row);
137: }
138: }
139: }
140:
141: Row doColumn(Table t, IndexConstraint c, int pos, Column col)
142: throws SQLException {
143: Type type = col.getType();
144: Row row = new Row(18);
145: row.set(1, ValueNull.valueNull);
146: doTableName(2, row, t.getName());
147: row.set(4, (c.isUnique() ? ValueBoolean.falseBoolean
148: : ValueBoolean.trueBoolean));
149: if (c.isGlobal()) {
150: row.set(5, new ValueString("global"));
151: } else {
152: row.set(5, ValueNull.valueNull);
153: }
154: String iname = c.getName();
155: if (iname == null)
156: iname = "";
157: row.set(6, new ValueString(iname));
158: row.set(7, new ValueShort(DatabaseMetaData.tableIndexOther));
159: row.set(8, new ValueShort(pos + 1));
160: row.set(9, new ValueString(col.getShortName()));
161: row.set(10, new ValueString("A"));
162: row.set(11, ValueNull.valueNull);
163: row.set(12, ValueNull.valueNull);
164: row.set(13, ValueNull.valueNull);
165: return row;
166: }
167:
168: }
|