1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Hashtable ; 47 import java.util.Vector ; 48 49 import java.sql.ResultSetMetaData ; 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.index.Btree; 53 54 import com.quadcap.sql.types.Value; 55 import com.quadcap.sql.types.ValueBoolean; 56 57 import com.quadcap.util.Debug; 58 59 64 public class View extends TupleImpl implements Relation, Externalizable { 65 Vector viewColumns; 66 TableExpression select; 67 68 static final int NOCHECK = -1; 69 static final int CASCADED = 0; 70 static final int LOCAL = 1; 71 72 int check; 73 74 77 public View() {} 78 79 83 public View(String name, Vector columns, TableExpression select, 84 int check) 85 { 86 super(name); 87 this.viewColumns = columns; 88 this.select = select; 89 this.check = check; 90 } 91 92 95 public void addColumns(Session session) throws SQLException { 96 if (columns.size() == 0) { 97 Cursor c = getCursor(session, null, null, null); 98 try { 99 addColumns(session, c); 100 } finally { 101 c.close(); 102 } 103 } 104 } 105 106 109 public void checkRow(Session session, Cursor cursor, Row row) throws SQLException { 110 if ((check != NOCHECK || session.getViewCheck()) 111 && select.isUpdatable()) { 112 session.setViewCheck(); 113 StaticCursor s = new StaticCursor(session, this, row); 114 s.next(); 115 Expression w = select.getWhere(); 116 if (w != null) { 117 Value v = w.getValue(session, s); 118 if (v instanceof ValueBoolean && ((ValueBoolean)v).isTrue()) { 119 } else { 121 throw new SQLException ("View check option violation", "44000"); 122 } 123 } 124 } 125 } 126 127 130 public void readExternal(ObjectInput in) 131 throws IOException , ClassNotFoundException 132 { 133 super.readExternal(in); 134 this.viewColumns = (Vector )in.readObject(); 135 this.select = (TableExpression)in.readObject(); 136 this.check = in.readInt(); 137 } 138 139 142 public void writeExternal(ObjectOutput out) throws IOException { 143 super.writeExternal(out); 144 out.writeObject(viewColumns); 145 out.writeObject(select); 146 out.writeInt(check); 147 } 148 149 152 public Cursor getCursor(Session session, Expression where, 153 String asName, Cursor cursor) 154 throws SQLException 155 { 156 Cursor c = select.getCursor(session, cursor); 157 158 String qualName = asName; 159 if (qualName == null) qualName = getName(); 160 String qPrefix = qualName; 161 if (qPrefix.length() > 0) qPrefix = qPrefix + "."; 162 163 Vector cols = viewColumns; 164 if (cols == null) { 165 cols = new Vector (); 166 Hashtable t = new Hashtable (); 167 for (int i = 1; i <= c.getColumnCount(); i++) { 168 String name = c.getColumn(i).getName(); 169 int idx = name.lastIndexOf('.'); 170 if (idx >= 0) name = name.substring(idx+1); 171 name = qPrefix + name; 172 if (t.get(name) != null) { 173 throw new SQLException ( 174 "A view column list must be specified if multiple " + 175 "columns have the same name: " + name, "42000"); 176 } 177 cols.addElement(name); 178 t.put(name, name); 179 } 180 } else { 181 cols = new Vector (); 182 for (int i = 0; i < viewColumns.size(); i++) { 183 cols.addElement(qPrefix + viewColumns.elementAt(i).toString()); 184 } 185 } 186 c = new ViewCursor(session, this, c, cols); 187 return c; 188 } 189 190 public void insertRow(Session session, Row row) 191 throws SQLException , IOException 192 { 193 Cursor c = getCursor(session, null, null, null); 194 try { 195 session.clearViewCheck(); 196 c.insertRow(row); 197 } finally { 198 c.close(); 199 } 200 } 201 202 public boolean isUpdatable() { 203 return select.isUpdatable(); 204 } 205 206 public boolean hasBlobs() { 207 return false; } 209 210 public Vector getBaseTables() { 211 Vector v = new Vector (); 212 select.getBaseTables(v); 213 return v; 214 } 215 216 public Expression getViewExpression() { 217 return select; 218 } 219 220 public String getType() { return "VIEW"; } 221 222 public void delete(Session session) {} 223 224 public String toString() { 225 StringBuffer sb = new StringBuffer ("VIEW "); 226 sb.append(getName()); 227 if (columns != null) { 228 sb.append('('); 229 for (int i = 0; i < columns.size(); i++) { 230 if (i > 0) sb.append(','); 231 Column col = (Column)columns.get(i); 232 sb.append(col.getShortName()); 233 } 234 sb.append(')'); 235 } 236 sb.append(" AS "); 237 sb.append(select.toString()); 238 return sb.toString(); 239 } 240 241 } 242 | Popular Tags |