00001
package com.quadcap.sql;
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.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import java.util.Enumeration;
00047
import java.util.Hashtable;
00048
import java.util.Vector;
00049
00050
import java.sql.SQLException;
00051
00052
import com.quadcap.sql.types.Type;
00053
import com.quadcap.sql.types.TypeAny;
00054
import com.quadcap.sql.types.Value;
00055
00056
import com.quadcap.util.Debug;
00057
00058
00059
00060
00061
00062
00063
00064 public class ItemsCursor extends CursorImpl {
00065 Cursor cursor;
00066 ItemsRow currentRow = null;
00067 int[]
map = null;
00068 Vector
items;
00069
00070 public ItemsCursor(
Session session,
Cursor cursor, Vector gitems)
00071
throws SQLException
00072 {
00073 super(session,
cursor.
getName());
00074
this.session = session;
00075
this.cursor =
cursor;
00076
this.items =
expandWildCard(session,
cursor, gitems);
00077
this.map =
new int[
items.size()];
00078
for (
int i = 0; i <
items.size(); i++) {
00079
SelectItem item = (
SelectItem)
items.elementAt(i);
00080
Expression e = item.
getExpression();
00081
if (e instanceof
NameExpression) {
00082 String name = ((
NameExpression)e).getName();
00083 String asName = item.
getAsName();
00084
if (asName == null) asName = name;
00085
Column col =
cursor.
getColumn(name);
00086
if (col == null) {
00087
00088
Debug.println(0,
cursor.toString());
00089
00090
throw new SQLException(
"Bad column name: " + name,
00091
"42000");
00092 }
00093 addColumn(
new Column(asName, col));
00094
map[i] = col.
getColumn();
00095 }
else {
00096 String asName = item.
getAsName();
00097
if (asName == null) {
00098 asName =
"Column" + (i+1);
00099 }
00100 addColumn(
new Column(asName, e.
getType(session,
cursor)));
00101
map[i] = -1;
00102 }
00103 }
00104 }
00105
00106 final Vector
expandWildCard(
Session session,
Cursor cursor, Vector items)
00107
throws SQLException
00108 {
00109 Vector nitems =
new Vector();
00110
for (
int i = 0; i <
items.size(); i++) {
00111
SelectItem item = (
SelectItem)
items.elementAt(i);
00112
Expression e = item.
getExpression();
00113 String name =
"Column" + (i + 1);
00114
if (e instanceof
NameExpression) {
00115 name = ((
NameExpression)e).getName();
00116
if (name.endsWith(
".*")) {
00117 String rname = name.substring(0, name.length() - 2);
00118 String qname =
00119 session.
getConnection().
resolveName(rname) +
".";
00120 rname = rname +
".";
00121
boolean found =
false;
00122
for (
int j = 1; j <=
cursor.
getColumnCount(); j++) {
00123
Column col =
cursor.
getColumn(j);
00124
if (col.
isJoinColumn())
continue;
00125 String sname = col.
getShortName();
00126 String cname = col.
getName();
00127
if (sname.startsWith(rname) || sname.startsWith(qname)) {
00128 found =
true;
00129 nitems.addElement(
00130
new SelectItem(
new NameExpression(sname)));
00131 }
else if (cname.startsWith(qname) || cname.startsWith(rname)) {
00132 found =
true;
00133 nitems.addElement(
00134
new SelectItem(
new NameExpression(cname)));
00135 }
00136 }
00137
if (!found) {
00138
throw new SQLException(
"Bad column name: " + name,
00139
"42000");
00140 }
00141 }
else {
00142 nitems.addElement(item);
00143 }
00144 }
else {
00145 nitems.addElement(item);
00146 }
00147 }
00148
return nitems;
00149 }
00150
00151 public Row getEmptyAggregate() throws SQLException {
00152
return new ItemsRow(session,
items, null, null,
map);
00153 }
00154
00155 public void updateRow(
Row row)
throws SQLException {
00156
for (
int i = 1; i <=
map.length; i++) {
00157
Value v = row.item(i);
00158
currentRow.
set(i, v);
00159 }
00160
cursor.
updateRow(
currentRow.
getBaseRow());
00161 }
00162
00163 public void insertRow(
Row row)
throws SQLException {
00164
if (
currentRow == null) {
00165
makeCurrentRow();
00166 }
00167
for (
int i = 1; i <=
map.length; i++) {
00168
Value v = row.item(i);
00169
currentRow.
set(i, v);
00170 }
00171
cursor.
insertRow(
currentRow.
getBaseRow());
00172 }
00173
00174 public void deleteRow() throws SQLException {
00175
cursor.
deleteRow();
00176 }
00177
00178 public boolean isWritable(
int column)
throws SQLException {
00179
return cursor.
isWritable(
map[column-1]);
00180 }
00181
00182 public void beforeFirst() throws SQLException {
00183
cursor.
beforeFirst();
00184 }
00185
00186 public void afterLast() throws SQLException {
00187
cursor.
afterLast();
00188 }
00189
00190 final void makeFirstRow() throws SQLException {
00191
Row r =
cursor.
getRow();
00192
currentRow =
new ItemsRow(session,
items,
cursor, r,
map);
00193
for (
int i = 0; i <
map.length; i++) {
00194
if (
map[i] == -1) {
00195
Column col = getColumn(i+1);
00196
Value v =
currentRow.
item(i+1);
00197 col.
setType(v.
getType());
00198 }
00199 }
00200 }
00201
00202 final void makeCurrentRow() throws SQLException {
00203
currentRow =
new ItemsRow(session,
items,
cursor, null,
map);
00204 }
00205
00206 public boolean next() throws SQLException {
00207
return cursor.
next();
00208 }
00209
00210 public Row getRow() throws SQLException {
00211
if (
currentRow == null) {
00212
makeFirstRow();
00213 }
else {
00214
currentRow.
nextRow(
cursor.
getRow());
00215 }
00216
return currentRow;
00217 }
00218
00219 public void setOuterCursor(
Cursor c) {
00220 super.setOuterCursor(c);
00221
if (
cursor != null)
cursor.
setOuterCursor(c);
00222 }
00223
00224 public void close() throws SQLException {
00225
cursor.
close();
00226 }
00227
00228 public long size() throws SQLException {
return cursor.
size(); }
00229
00230 public Cursor getBaseCursor() {
return cursor; }
00231
00232
00233 public String
toString() {
00234
return super.toString() +
" : (cursor = " +
cursor +
")";
00235 }
00236
00237
00238 }