KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > meta > MetaBestRowId


1 package com.quadcap.sql.meta;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import java.util.Enumeration JavaDoc;
44 import java.util.Vector JavaDoc;
45
46 import java.sql.DatabaseMetaData JavaDoc;
47 import java.sql.ResultSetMetaData JavaDoc;
48 import java.sql.SQLException JavaDoc;
49
50 import com.quadcap.sql.Column;
51 import com.quadcap.sql.Constraint;
52 import com.quadcap.sql.Database;
53 import com.quadcap.sql.Expression;
54 import com.quadcap.sql.PrimaryKeyConstraint;
55 import com.quadcap.sql.Relation;
56 import com.quadcap.sql.Row;
57 import com.quadcap.sql.Session;
58 import com.quadcap.sql.StaticCursor;
59 import com.quadcap.sql.Table;
60 import com.quadcap.sql.UniqueConstraint;
61
62 import com.quadcap.sql.index.BCursor;
63 import com.quadcap.sql.index.Btree;
64
65 import com.quadcap.sql.types.*;
66
67 import com.quadcap.util.Debug;
68
69 /**
70  * A Cursor supporting the <code>DatabaseMetaData.getBestRowId()</code>
71  * operation.
72  *
73  * @author Stan Bailes
74  */

75 public class MetaBestRowId extends MetaCursor {
76     static Column[] cols = {
77     new Column("SCOPE", typeShort), // 1
78
new Column("COLUMN_NAME", typeString), // 2
79
new Column("DATA_TYPE", typeShort), // 3
80
new Column("TYPE_NAME", typeString), // 4
81
new Column("COLUMN_SIZE", typeInt), // 5
82
new Column("BUFFER_LENGTH", typeAny), // 6
83
new Column("DECIMAL_DIGITS", typeInt), // 7
84
new Column("PSEUDO_COLUMN", typeShort) // 8
85
};
86
87     static int[] sortColumns = { 2 };
88
89     /**
90      * The constructor for this meta cursor finds the "best" index
91      * to use for this table, purely based on the index type
92      * (and possibly the 'nullable' flag). We don't do anything with
93      * scope -- best is best for us.
94      */

95     public MetaBestRowId(Session session, String JavaDoc tableName, int scope,
96                          boolean nullable)
97     throws SQLException JavaDoc
98     {
99     super(session, null);
100     try {
101         addColumns(cols);
102         Database db = session.getDatabase();
103             session.getTableWriteLock("#Schema");
104             tableName = tableName.toUpperCase();
105             Table t = (Table)db.getRelation(tableName);
106             if (t != null) {
107                 UniqueConstraint uc = null;
108                 int num = t.getNumConstraints();
109                 for (int i = 0; i < num; i++) {
110                     Constraint c = t.getConstraint(i);
111                     if (c instanceof UniqueConstraint) {
112                         if (nullable || checkNullable(c)) {
113                             uc = (UniqueConstraint)c;
114                             if (c instanceof PrimaryKeyConstraint) break;
115                         }
116                     }
117                 }
118                 if (uc != null) {
119                     int cnt = uc.getColumnCount();
120                     for (int i = 0; i < cnt; i++) {
121                         addRow(doColumn(uc.getColumn(i)));
122                     }
123                 }
124         }
125         sort();
126     } catch (ValueException e) {
127         Debug.print(e);
128         SQLException JavaDoc te = new SQLException JavaDoc(e.toString(), "Q000J");
129         te.setNextException(e);
130         throw te;
131     } catch (IOException JavaDoc e) {
132         Debug.print(e);
133         throw new SQLException JavaDoc(e.toString(), "Q000K");
134     }
135     }
136
137     /**
138      * Sort by name, I guess...
139      */

140     public int[] getSortColumns() {
141     return sortColumns;
142     }
143
144     /**
145      * Do any of the columns in this constraint allow nulls?
146      */

147     public boolean checkNullable(Constraint c) throws SQLException JavaDoc {
148         boolean ret = !(c instanceof PrimaryKeyConstraint);
149         int cnt = c.getColumnCount();
150         for (int i = 0; ret && i < cnt; i++) {
151             ret = c.getColumn(i).isNullable();
152         }
153         return ret;
154     }
155
156     /**
157      * Add one cursor row to describe the specified column.
158      */

159     Row doColumn(Column col) throws SQLException JavaDoc {
160     Type type = col.getType();
161     Row row = new Row(18);
162     row.set(1, new ValueShort(DatabaseMetaData.bestRowSession));
163     row.set(2, new ValueString(col.getShortName()));
164     row.set(3, new ValueShort(type.getJDBCType()));
165     row.set(4, new ValueString(type.getTypeName()));
166
167     Value size = new ValueInteger(type.getPrecision());
168     if (type instanceof TypeChar) {
169         size = new ValueInteger(((TypeChar)type).getMax());
170     } else if (type instanceof TypeVarChar) {
171         size = new ValueInteger(((TypeVarChar)type).getMax());
172     }
173     row.set(5, size);
174     row.set(6, ValueNull.valueNull);
175     row.set(7, new ValueShort(type.getScale()));
176     row.set(8, new ValueShort(DatabaseMetaData.bestRowUnknown));
177     return row;
178     }
179 }
180
Popular Tags