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.ImportedKeyConstraint;
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
00073
00074 public class MetaCrossReference extends MetaCursor {
00075 static Column[]
cols = {
00076
new Column(
"PKTABLE_CAT", typeString),
00077
new Column(
"PKTABLE_SCHEM", typeString),
00078
new Column(
"PKTABLE_NAME", typeString),
00079
new Column(
"PKCOLUMN_NAME", typeString),
00080
new Column(
"FKTABLE_CAT", typeString),
00081
new Column(
"FKTABLE_SCHEM", typeString),
00082
new Column(
"FKTABLE_NAME", typeString),
00083
new Column(
"FKCOLUMN_NAME", typeString),
00084
new Column(
"KEY_SEQ", typeShort),
00085
new Column(
"UPDATE_RULE", typeShort),
00086
new Column(
"DELETE_RULE", typeShort),
00087
new Column(
"FK_NAME", typeString),
00088
new Column(
"PK_NAME", typeString),
00089
new Column(
"DEFERRABILITY", typeShort)
00090 };
00091
00092 static int[]
sortColumns = { 1, 2, 3, 9 };
00093
00094 public MetaCrossReference(
Session session,
Expression predicate)
00095
throws SQLException
00096 {
00097 super(session, predicate);
00098
try {
00099 addColumns(
cols);
00100
Database db = session.
getDatabase();
00101 session.
getTableWriteLock(
"#Schema");
00102
synchronized (db.getFile().getLock()) {
00103 Iterator iter = db.
getRelationNameIterator();
00104
while (iter.hasNext()) {
00105 String name = (String)iter.next();
00106
Relation r = db.
getRelation(name);
00107
if (r instanceof
Table) {
00108
Table t = (
Table)r;
00109
int num = t.
getNumConstraints();
00110
for (
int i = 0; i < num; i++) {
00111
Constraint c = t.
getConstraint(i);
00112
if (c instanceof
ImportedKeyConstraint) {
00113
doConstraint(t, (
ImportedKeyConstraint)c);
00114 }
00115 }
00116 }
00117 }
00118 }
00119
sort();
00120 }
catch (
ValueException e) {
00121
Debug.print(e);
00122 SQLException te =
new SQLException(e.toString(),
"Q000L");
00123 te.setNextException(e);
00124
throw te;
00125 }
catch (IOException e) {
00126
Debug.print(e);
00127
throw new SQLException(e.toString(),
"Q000M");
00128 }
00129 }
00130
00131 public int[]
getSortColumns() {
00132
return sortColumns;
00133 }
00134
00135 void doConstraint(
Table t,
ImportedKeyConstraint c)
00136
throws SQLException, IOException
00137 {
00138
int[] colIndices = c.getColumns();
00139
int[] fCols = c.getFCols(session.
getDatabase());
00140
Table f = c.getFTable(session.
getDatabase());
00141
for (
int i = 0; i < colIndices.length; i++) {
00142
Column col = t.getColumn(colIndices[i]);
00143
Column fcol = f.
getColumn(fCols[i]);
00144
Row row =
doColumn(t, f, c, i, col, fcol);
00145
if (rowMatch(row)) {
00146 addRow(row);
00147 }
00148 }
00149 }
00150
00151 Row doColumn(
Table t,
Table f,
ImportedKeyConstraint c,
int pos,
Column col,
00152
Column fcol)
00153
throws SQLException
00154 {
00155
Row row =
new Row(14);
00156 row.
set(1,
ValueNull.valueNull);
00157 doTableName(2, row, f.getName());
00158 row.
set(4,
new ValueString(fcol.getShortName()));
00159
00160 row.
set(5,
ValueNull.valueNull);
00161 doTableName(6, row, t.getName());
00162 row.
set(8,
new ValueString(col.getShortName()));
00163
00164 row.
set(9,
new ValueInteger(pos+1));
00165
00166
int spec = c.getSpec();
00167
int ud = DatabaseMetaData.importedKeyRestrict;
00168
if ((spec &
Constraint.CASCADE) != 0) {
00169 ud = DatabaseMetaData.importedKeyCascade;
00170 }
00171
ValueInteger v =
new ValueInteger(ud);
00172 row.
set(10, v);
00173 row.
set(11, v);
00174
00175 row.
set(12,
new ValueString(c.getName()));
00176 row.
set(13,
ValueNull.valueNull);
00177
00178 ud = DatabaseMetaData.importedKeyNotDeferrable;
00179
if ((spec &
Constraint.DEFERRABLE) != 0) {
00180
if ((spec &
Constraint.INIT_DEFERRED) != 0) {
00181 ud = DatabaseMetaData.importedKeyInitiallyDeferred;
00182 }
else {
00183 ud = DatabaseMetaData.importedKeyInitiallyImmediate;
00184 }
00185 }
00186 row.
set(14,
new ValueInteger(ud));
00187
return row;
00188 }
00189 }