00001
package com.quadcap.sql.meta;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
00043
import java.util.Iterator;
00044
00045
import java.sql.DatabaseMetaData;
00046
import java.sql.ResultSetMetaData;
00047
import java.sql.SQLException;
00048
00049
import com.quadcap.sql.Column;
00050
import com.quadcap.sql.Constraint;
00051
import com.quadcap.sql.Database;
00052
import com.quadcap.sql.Expression;
00053
import com.quadcap.sql.IndexConstraint;
00054
import com.quadcap.sql.Relation;
00055
import com.quadcap.sql.Row;
00056
import com.quadcap.sql.Session;
00057
import com.quadcap.sql.StaticCursor;
00058
import com.quadcap.sql.Table;
00059
00060
import com.quadcap.sql.index.BCursor;
00061
import com.quadcap.sql.index.Btree;
00062
00063
import com.quadcap.sql.types.*;
00064
00065
import com.quadcap.util.Debug;
00066
00067
00068
00069
00070
00071
00072 public class MetaIndexInfo extends MetaCursor {
00073 static Column[]
cols = {
00074
new Column(
"TABLE_CAT", typeString),
00075
new Column(
"TABLE_SCHEM", typeString),
00076
new Column(
"TABLE_NAME", typeString),
00077
new Column(
"NON_UNIQUE", typeBinary),
00078
new Column(
"INDEX_QUALIFIER", typeString),
00079
new Column(
"INDEX_NAME", typeString),
00080
new Column(
"TYPE", typeShort),
00081
new Column(
"ORDINAL_POSITION", typeInt),
00082
new Column(
"COLUMN_NAME", typeString),
00083
new Column(
"ASC_OR_DESC", typeString),
00084
new Column(
"CARDINALITY", typeInt),
00085
new Column(
"PAGES", typeInt),
00086
new Column(
"FILTER_CONDITION", typeString)
00087 };
00088
00089 static int[]
sortColumns = { 4, 7, 6, 8 };
00090
00091 public MetaIndexInfo(
Session session,
Expression predicate)
00092
throws SQLException
00093 {
00094 super(session, predicate);
00095
try {
00096 addColumns(
cols);
00097
Database db = session.
getDatabase();
00098 session.
getTableWriteLock(
"#Schema");
00099
synchronized (db.getFile().getLock()) {
00100 Iterator iter = db.
getRelationNameIterator();
00101
while (iter.hasNext()) {
00102 String name = (String)iter.next();
00103
Relation r = db.
getRelation(name);
00104
if (r instanceof
Table) {
00105
Table t = (
Table)r;
00106
int num = t.
getNumConstraints();
00107
for (
int i = 0; i < num; i++) {
00108
Constraint c = t.
getConstraint(i);
00109
if (c instanceof
IndexConstraint) {
00110
doConstraint(t, (
IndexConstraint)c);
00111 }
00112 }
00113 }
00114 }
00115 }
00116
sort();
00117 }
catch (
ValueException e) {
00118
Debug.print(e);
00119 SQLException te =
new SQLException(e.toString(),
"Q000N");
00120 te.setNextException(e);
00121
throw te;
00122 }
catch (IOException e) {
00123
Debug.print(e);
00124
throw new SQLException(e.toString(),
"Q000O");
00125 }
00126 }
00127
00128 public int[]
getSortColumns() {
00129
return sortColumns;
00130 }
00131
00132 void doConstraint(
Table t,
IndexConstraint c)
throws SQLException {
00133
int[] colIndices = c.getColumns();
00134
for (
int i = 0; i < colIndices.length; i++) {
00135
Column col = t.getColumn(colIndices[i]);
00136
Row row =
doColumn(t, c, i, col);
00137
if (rowMatch(row)) {
00138 addRow(row);
00139 }
00140 }
00141 }
00142
00143 Row doColumn(
Table t,
IndexConstraint c,
int pos,
Column col)
00144
throws SQLException
00145 {
00146
Type type = col.getType();
00147
Row row =
new Row(18);
00148 row.
set(1,
ValueNull.valueNull);
00149 doTableName(2, row, t.getName());
00150 row.
set(4, (c.isUnique() ?
00151
ValueBoolean.falseBoolean :
00152
ValueBoolean.trueBoolean));
00153
if (c.isGlobal()) {
00154 row.
set(5,
new ValueString(
"global"));
00155 }
else {
00156 row.
set(5,
ValueNull.valueNull);
00157 }
00158 String iname = c.getName();
00159
if (iname == null) iname =
"";
00160 row.
set(6,
new ValueString(iname));
00161 row.
set(7,
new ValueShort(DatabaseMetaData.tableIndexOther));
00162 row.
set(8,
new ValueShort(pos+1));
00163 row.
set(9,
new ValueString(col.getShortName()));
00164 row.
set(10,
new ValueString(
"A"));
00165 row.
set(11,
ValueNull.valueNull);
00166 row.
set(12,
ValueNull.valueNull);
00167 row.
set(13,
ValueNull.valueNull);
00168
return row;
00169 }
00170
00171 }