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.Enumeration;
00044
import java.util.Vector;
00045
00046
import java.sql.DatabaseMetaData;
00047
import java.sql.ResultSetMetaData;
00048
import java.sql.SQLException;
00049
00050
import com.quadcap.sql.Column;
00051
import com.quadcap.sql.Constraint;
00052
import com.quadcap.sql.Database;
00053
import com.quadcap.sql.Expression;
00054
import com.quadcap.sql.PrimaryKeyConstraint;
00055
import com.quadcap.sql.Relation;
00056
import com.quadcap.sql.Row;
00057
import com.quadcap.sql.Session;
00058
import com.quadcap.sql.StaticCursor;
00059
import com.quadcap.sql.Table;
00060
import com.quadcap.sql.UniqueConstraint;
00061
00062
import com.quadcap.sql.index.BCursor;
00063
import com.quadcap.sql.index.Btree;
00064
00065
import com.quadcap.sql.types.*;
00066
00067
import com.quadcap.util.Debug;
00068
00069
00070
00071
00072
00073
00074
00075 public class MetaBestRowId extends MetaCursor {
00076 static Column[]
cols = {
00077
new Column(
"SCOPE", typeShort),
00078
new Column(
"COLUMN_NAME", typeString),
00079
new Column(
"DATA_TYPE", typeShort),
00080
new Column(
"TYPE_NAME", typeString),
00081
new Column(
"COLUMN_SIZE", typeInt),
00082
new Column(
"BUFFER_LENGTH", typeAny),
00083
new Column(
"DECIMAL_DIGITS", typeInt),
00084
new Column(
"PSEUDO_COLUMN", typeShort)
00085 };
00086
00087 static int[]
sortColumns = { 2 };
00088
00089
00090
00091
00092
00093
00094
00095 public MetaBestRowId(
Session session, String tableName,
int scope,
00096
boolean nullable)
00097
throws SQLException
00098 {
00099 super(session, null);
00100
try {
00101 addColumns(
cols);
00102
Database db = session.
getDatabase();
00103 session.
getTableWriteLock(
"#Schema");
00104 tableName = tableName.toUpperCase();
00105
Table t = (
Table)db.
getRelation(tableName);
00106
if (t != null) {
00107
UniqueConstraint uc = null;
00108
int num = t.
getNumConstraints();
00109
for (
int i = 0; i < num; i++) {
00110
Constraint c = t.
getConstraint(i);
00111
if (c instanceof
UniqueConstraint) {
00112
if (nullable ||
checkNullable(c)) {
00113 uc = (
UniqueConstraint)c;
00114
if (c instanceof
PrimaryKeyConstraint)
break;
00115 }
00116 }
00117 }
00118
if (uc != null) {
00119
int cnt = uc.
getColumnCount();
00120
for (
int i = 0; i < cnt; i++) {
00121 addRow(
doColumn(uc.
getColumn(i)));
00122 }
00123 }
00124 }
00125
sort();
00126 }
catch (
ValueException e) {
00127
Debug.print(e);
00128 SQLException te =
new SQLException(e.toString(),
"Q000J");
00129 te.setNextException(e);
00130
throw te;
00131 }
catch (IOException e) {
00132
Debug.print(e);
00133
throw new SQLException(e.toString(),
"Q000K");
00134 }
00135 }
00136
00137
00138
00139
00140 public int[]
getSortColumns() {
00141
return sortColumns;
00142 }
00143
00144
00145
00146
00147 public boolean checkNullable(
Constraint c)
throws SQLException {
00148
boolean ret = !(c instanceof
PrimaryKeyConstraint);
00149
int cnt = c.getColumnCount();
00150
for (
int i = 0; ret && i < cnt; i++) {
00151 ret = c.getColumn(i).isNullable();
00152 }
00153
return ret;
00154 }
00155
00156
00157
00158
00159 Row doColumn(
Column col)
throws SQLException {
00160
Type type = col.getType();
00161
Row row =
new Row(18);
00162 row.
set(1,
new ValueShort(DatabaseMetaData.bestRowSession));
00163 row.
set(2,
new ValueString(col.getShortName()));
00164 row.
set(3,
new ValueShort(type.
getJDBCType()));
00165 row.
set(4,
new ValueString(type.
getTypeName()));
00166
00167
Value size =
new ValueInteger(type.
getPrecision());
00168
if (type instanceof
TypeChar) {
00169 size =
new ValueInteger(((
TypeChar)type).getMax());
00170 }
else if (type instanceof
TypeVarChar) {
00171 size =
new ValueInteger(((
TypeVarChar)type).getMax());
00172 }
00173 row.
set(5, size);
00174 row.
set(6,
ValueNull.valueNull);
00175 row.
set(7,
new ValueShort(type.
getScale()));
00176 row.
set(8,
new ValueShort(DatabaseMetaData.bestRowUnknown));
00177
return row;
00178 }
00179 }