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.ArrayList;
00044
import java.util.Iterator;
00045
import java.util.List;
00046
import java.util.Vector;
00047
00048
import java.sql.ResultSetMetaData;
00049
import java.sql.SQLException;
00050
00051
import com.quadcap.sql.Column;
00052
import com.quadcap.sql.Database;
00053
import com.quadcap.sql.Expression;
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 public class MetaColumns extends MetaCursor {
00074 static Column[]
cols = {
00075
new Column(
"TABLE_CAT", typeString),
00076
new Column(
"TABLE_SCHEM", typeString),
00077
new Column(
"TABLE_NAME", typeString),
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(
"NUM_PREC_RADIX", typeInt),
00085
new Column(
"NULLABLE", typeInt),
00086
new Column(
"REMARKS", typeString),
00087
new Column(
"COLUMN_DEF", typeString),
00088
new Column(
"SQL_DATA_TYPE", typeInt),
00089
new Column(
"SQL_DATETIME_SUB", typeInt),
00090
new Column(
"CHAR_OCTET_LENGTH", typeInt),
00091
new Column(
"ORDINAL_POSITION", typeInt),
00092
new Column(
"IS_NULLABLE", typeString)
00093 };
00094
00095 static int[]
sortColumns = { 2, 3, 17 };
00096
00097 public MetaColumns(
Session session,
Expression predicate)
00098
throws SQLException
00099 {
00100 super(session, predicate);
00101
try {
00102 addColumns(
cols);
00103
Database db = session.
getDatabase();
00104 session.
getTableWriteLock(
"#Schema");
00105
synchronized (db.getFile().getLock()) {
00106 Iterator iter = db.
getRelationNameIterator();
00107
while (iter.hasNext()) {
00108
Relation r = db.
getRelation(iter.next().toString());
00109
if (r instanceof
Table) {
00110
Table t = (
Table)r;
00111
doTable(t);
00112 }
00113 }
00114 }
00115
sort();
00116 }
catch (
ValueException e) {
00117
Debug.print(e);
00118 SQLException te =
new SQLException(e.toString(),
"Q000J");
00119 te.setNextException(e);
00120
throw te;
00121 }
catch (IOException e) {
00122
Debug.print(e);
00123
throw new SQLException(e.toString(),
"Q000K");
00124 }
00125 }
00126
00127 public int[]
getSortColumns() {
00128
return sortColumns;
00129 }
00130
00131 void doTable(
Table t)
throws SQLException {
00132
for (
int i = 1; i <= t.getColumnCount(); i++) {
00133
Column col = t.getColumn(i);
00134
Row row =
doColumn(t, col);
00135
if (rowMatch(row)) {
00136 addRow(row);
00137 }
00138 }
00139 }
00140
00141 Row doColumn(
Table t,
Column col)
throws SQLException {
00142
Type type = col.getType();
00143
Row row =
new Row(18);
00144 row.
set(1,
ValueNull.valueNull);
00145 doTableName(2, row, t.getName());
00146 row.
set(4,
new ValueString(col.getShortName()));
00147 row.
set(5,
new ValueShort(type.
getJDBCType()));
00148 row.
set(6,
new ValueString(type.
getTypeName()));
00149
00150
Value size =
new ValueInteger(type.
getPrecision());
00151
if (type instanceof
TypeChar) {
00152 size =
new ValueInteger(((
TypeChar)type).getMax());
00153 }
else if (type instanceof
TypeVarChar) {
00154 size =
new ValueInteger(((
TypeVarChar)type).getMax());
00155 }
00156 row.
set(7, size);
00157 row.
set(8,
ValueNull.valueNull);
00158 row.
set(9,
new ValueInteger(type.
getScale()));
00159 row.
set(10,
new ValueInteger(10));
00160 row.
set(11,
new ValueInteger(col.getNullable()));
00161 row.
set(12,
ValueNull.valueNull);
00162
Value defVal =
ValueNull.valueNull;
00163
Expression def = col.getDefault();
00164
if (def != null) {
00165 defVal =
new ValueString(def.
toString());
00166 }
00167 row.
set(13, defVal);
00168 row.
set(14,
ValueNull.valueNull);
00169 row.
set(15,
ValueNull.valueNull);
00170
00171
Value max =
ValueNull.valueNull;
00172
if (type instanceof
TypeChar) {
00173 max =
new ValueInteger(2 * ((
TypeChar)type).getMax());
00174 }
else if (type instanceof
TypeVarChar) {
00175 max =
new ValueInteger(2 * ((
TypeVarChar)type).getMax());
00176 }
00177 row.
set(16, max);
00178 row.
set(17,
new ValueInteger(col.getColumn()));
00179
00180 String nullstr =
"";
00181
switch (col.getNullable()) {
00182
case ResultSetMetaData.columnNoNulls:
00183 nullstr =
"NO";
00184
break;
00185
case ResultSetMetaData.columnNullable:
00186 nullstr =
"YES";
00187
break;
00188 }
00189 row.
set(18,
new ValueString(nullstr));
00190
return row;
00191 }
00192
00193 }