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.PrimaryKeyConstraint;
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.Btree;
061: import com.quadcap.sql.index.BCursor;
062:
063: import com.quadcap.sql.types.*;
064:
065: import com.quadcap.util.Debug;
066:
067: /**
068: * A Cursor supporting the <code>getPrimaryKeys</code> function.
069: *
070: * @author Stan Bailes
071: */
072: public class MetaPrimaryKeys 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("COLUMN_NAME", typeString), // 4
077: new Column("KEY_SEQ", typeShort), // 5
078: new Column("PK_NAME", typeString) // 6
079: };
080:
081: static int[] sortColumns = { 4 };
082:
083: public MetaPrimaryKeys(Session session, Expression predicate)
084: throws SQLException {
085: super (session, predicate);
086: try {
087: addColumns(cols);
088: Database db = session.getDatabase();
089: session.getTableWriteLock("#Schema");
090: synchronized (db.getFile().getLock()) {
091: Iterator iter = db.getRelationNameIterator();
092: while (iter.hasNext()) {
093: String name = (String) iter.next();
094: Relation r = db.getRelation(name);
095: if (r instanceof Table) {
096: Table t = (Table) r;
097: int num = t.getNumConstraints();
098: for (int i = 0; i < num; i++) {
099: Constraint c = t.getConstraint(i);
100: if (c instanceof PrimaryKeyConstraint) {
101: doConstraint(t,
102: (PrimaryKeyConstraint) c);
103: }
104: }
105: }
106: }
107: }
108: sort();
109: } catch (ValueException e) {
110: Debug.print(e);
111: SQLException te = new SQLException(e.toString(), "Q000N");
112: te.setNextException(e);
113: throw te;
114: } catch (IOException e) {
115: Debug.print(e);
116: throw new SQLException(e.toString(), "Q000O");
117: }
118: }
119:
120: public int[] getSortColumns() {
121: return sortColumns;
122: }
123:
124: void doConstraint(Table t, PrimaryKeyConstraint c)
125: throws SQLException {
126: int[] colIndices = c.getColumns();
127: for (int i = 0; i < colIndices.length; i++) {
128: Column col = t.getColumn(colIndices[i]);
129: Row row = doColumn(t, c, i, col);
130: if (rowMatch(row)) {
131: addRow(row);
132: }
133: }
134: }
135:
136: Row doColumn(Table t, PrimaryKeyConstraint c, int pos, Column col)
137: throws SQLException {
138: Type type = col.getType();
139: Row row = new Row(6);
140: row.set(1, ValueNull.valueNull);
141: doTableName(2, row, t.getName());
142: row.set(4, new ValueString(col.getShortName()));
143: row.set(5, new ValueShort(pos + 1));
144: row.set(6, new ValueString(c.getName()));
145: return row;
146: }
147:
148: }
|