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.Enumeration;
044: import java.util.Vector;
045:
046: import java.sql.DatabaseMetaData;
047: import java.sql.ResultSetMetaData;
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.Column;
051: import com.quadcap.sql.Constraint;
052: import com.quadcap.sql.Database;
053: import com.quadcap.sql.Expression;
054: import com.quadcap.sql.PrimaryKeyConstraint;
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: import com.quadcap.sql.UniqueConstraint;
061:
062: import com.quadcap.sql.index.BCursor;
063: import com.quadcap.sql.index.Btree;
064:
065: import com.quadcap.sql.types.*;
066:
067: import com.quadcap.util.Debug;
068:
069: /**
070: * A Cursor supporting the <code>DatabaseMetaData.getBestRowId()</code>
071: * operation.
072: *
073: * @author Stan Bailes
074: */
075: public class MetaBestRowId extends MetaCursor {
076: static Column[] cols = { new Column("SCOPE", typeShort), // 1
077: new Column("COLUMN_NAME", typeString), // 2
078: new Column("DATA_TYPE", typeShort), // 3
079: new Column("TYPE_NAME", typeString), // 4
080: new Column("COLUMN_SIZE", typeInt), // 5
081: new Column("BUFFER_LENGTH", typeAny), // 6
082: new Column("DECIMAL_DIGITS", typeInt), // 7
083: new Column("PSEUDO_COLUMN", typeShort) // 8
084: };
085:
086: static int[] sortColumns = { 2 };
087:
088: /**
089: * The constructor for this meta cursor finds the "best" index
090: * to use for this table, purely based on the index type
091: * (and possibly the 'nullable' flag). We don't do anything with
092: * scope -- best is best for us.
093: */
094: public MetaBestRowId(Session session, String tableName, int scope,
095: boolean nullable) throws SQLException {
096: super (session, null);
097: try {
098: addColumns(cols);
099: Database db = session.getDatabase();
100: session.getTableWriteLock("#Schema");
101: tableName = tableName.toUpperCase();
102: Table t = (Table) db.getRelation(tableName);
103: if (t != null) {
104: UniqueConstraint uc = null;
105: int num = t.getNumConstraints();
106: for (int i = 0; i < num; i++) {
107: Constraint c = t.getConstraint(i);
108: if (c instanceof UniqueConstraint) {
109: if (nullable || checkNullable(c)) {
110: uc = (UniqueConstraint) c;
111: if (c instanceof PrimaryKeyConstraint)
112: break;
113: }
114: }
115: }
116: if (uc != null) {
117: int cnt = uc.getColumnCount();
118: for (int i = 0; i < cnt; i++) {
119: addRow(doColumn(uc.getColumn(i)));
120: }
121: }
122: }
123: sort();
124: } catch (ValueException e) {
125: Debug.print(e);
126: SQLException te = new SQLException(e.toString(), "Q000J");
127: te.setNextException(e);
128: throw te;
129: } catch (IOException e) {
130: Debug.print(e);
131: throw new SQLException(e.toString(), "Q000K");
132: }
133: }
134:
135: /**
136: * Sort by name, I guess...
137: */
138: public int[] getSortColumns() {
139: return sortColumns;
140: }
141:
142: /**
143: * Do any of the columns in this constraint allow nulls?
144: */
145: public boolean checkNullable(Constraint c) throws SQLException {
146: boolean ret = !(c instanceof PrimaryKeyConstraint);
147: int cnt = c.getColumnCount();
148: for (int i = 0; ret && i < cnt; i++) {
149: ret = c.getColumn(i).isNullable();
150: }
151: return ret;
152: }
153:
154: /**
155: * Add one cursor row to describe the specified column.
156: */
157: Row doColumn(Column col) throws SQLException {
158: Type type = col.getType();
159: Row row = new Row(18);
160: row.set(1, new ValueShort(DatabaseMetaData.bestRowSession));
161: row.set(2, new ValueString(col.getShortName()));
162: row.set(3, new ValueShort(type.getJDBCType()));
163: row.set(4, new ValueString(type.getTypeName()));
164:
165: Value size = new ValueInteger(type.getPrecision());
166: if (type instanceof TypeChar) {
167: size = new ValueInteger(((TypeChar) type).getMax());
168: } else if (type instanceof TypeVarChar) {
169: size = new ValueInteger(((TypeVarChar) type).getMax());
170: }
171: row.set(5, size);
172: row.set(6, ValueNull.valueNull);
173: row.set(7, new ValueShort(type.getScale()));
174: row.set(8, new ValueShort(DatabaseMetaData.bestRowUnknown));
175: return row;
176: }
177: }
|