1 5 package org.h2.table; 6 7 import java.sql.SQLException ; 8 9 import org.h2.command.Prepared; 10 import org.h2.command.dml.Query; 11 import org.h2.engine.Session; 12 import org.h2.expression.Expression; 13 import org.h2.index.Index; 14 import org.h2.index.IndexType; 15 import org.h2.index.ViewIndex; 16 import org.h2.message.Message; 17 import org.h2.result.Row; 18 import org.h2.schema.Schema; 19 import org.h2.util.ObjectArray; 20 import org.h2.util.StringUtils; 21 22 public class TableView extends Table { 23 24 private String querySQL; 25 private ObjectArray tables; 26 private String [] columnNames; 27 private boolean invalid; 28 private Query viewQuery; 29 private ViewIndex index; 30 31 public TableView(Schema schema, int id, String name, String querySQL, ObjectArray params, String [] columnNames, Session session) throws SQLException { 32 super(schema, id, name, false); 33 this.querySQL = querySQL; 34 this.columnNames = columnNames; 35 index = new ViewIndex(this, querySQL, params); 36 initColumnsAndTables(session); 37 } 38 39 private void initColumnsAndTables(Session session) throws SQLException { 40 Column[] cols; 41 removeViewFromTables(); 42 try { 43 Prepared p = session.prepare(querySQL); 44 if(!(p instanceof Query)) { 45 throw Message.getSyntaxError(querySQL, 0); 46 } 47 Query query = (Query)p; 48 tables = new ObjectArray(query.getTables()); 49 ObjectArray expressions = query.getExpressions(); 50 ObjectArray list = new ObjectArray(); 51 for(int i=0; i<query.getColumnCount(); i++) { 52 Expression expr = (Expression) expressions.get(i); 53 String name = null; 54 if(columnNames != null && columnNames.length > i) { 55 name = columnNames[i]; 56 } 57 if(name == null) { 58 name = expr.getAlias(); 59 } 60 int type = expr.getType(); 61 long precision = expr.getPrecision(); 62 int scale = expr.getScale(); 63 Column col = new Column(name, type, precision, scale); 64 col.setTable(this, i); 65 list.add(col); 66 } 67 cols = new Column[list.size()]; 68 list.toArray(cols); 69 invalid = false; 70 if(getId() != 0) { 71 addViewToTables(); 72 } 73 viewQuery = query; 74 } catch(SQLException e) { 75 tables = new ObjectArray(); 78 cols = new Column[0]; 79 invalid = true; 80 } 81 setColumns(cols); 82 } 83 84 public boolean getInvalid() { 85 return invalid; 86 } 87 88 public PlanItem getBestPlanItem(Session session, int[] masks) throws SQLException { 89 PlanItem item = new PlanItem(); 90 item.cost = index.getCost(session, masks); 91 item.index = index; 92 return item; 93 } 94 95 public String getCreateSQL() { 96 StringBuffer buff = new StringBuffer (); 97 buff.append("CREATE FORCE VIEW "); 98 buff.append(getSQL()); 99 if(comment != null) { 100 buff.append(" COMMENT "); 101 buff.append(StringUtils.quoteStringSQL(comment)); 102 } 103 if(columns.length>0) { 104 buff.append('('); 105 for(int i=0; i<columns.length; i++) { 106 if(i>0) { 107 buff.append(", "); 108 } 109 buff.append(columns[i].getSQL()); 110 } 111 buff.append(")"); 112 } 113 buff.append(" AS\n"); 114 buff.append(querySQL); 115 return buff.toString(); 116 } 117 118 public void checkRename() throws SQLException { 119 } 120 121 public void lock(Session session, boolean exclusive) throws SQLException { 122 } 124 125 public void close(Session session) throws SQLException { 126 } 127 128 public void unlock(Session s) { 129 } 130 131 public boolean isLockedExclusively() { 132 return false; 133 } 134 135 public void removeIndex(String indexName) throws SQLException { 136 throw Message.getUnsupportedException(); 137 } 138 139 public Index addIndex(Session session, String indexName, int indexId, Column[] cols, IndexType indexType, int headPos, String comment) throws SQLException { 140 throw Message.getUnsupportedException(); 141 } 142 143 public void removeRow(Session session, Row row) throws SQLException { 144 throw Message.getUnsupportedException(); 145 } 146 147 public void addRow(Session session, Row row) throws SQLException { 148 throw Message.getUnsupportedException(); 149 } 150 151 public void checkSupportAlter() throws SQLException { 152 throw Message.getUnsupportedException(); 154 } 155 156 public void truncate(Session session) throws SQLException { 157 throw Message.getUnsupportedException(); 158 } 159 160 public int getRowCount() { 161 throw Message.getInternalError(); 162 } 163 164 public boolean canGetRowCount() { 165 return false; 167 } 168 169 public boolean canDrop() { 170 return true; 171 } 172 173 public String getTableType() { 174 return Table.VIEW; 175 } 176 177 public void removeChildrenAndResources(Session session) throws SQLException { 178 removeViewFromTables(); 179 super.removeChildrenAndResources(session); 180 querySQL = null; 181 index = null; 182 invalidate(); 183 } 184 185 public Index getScanIndex(Session session) throws SQLException { 186 if(invalid) { 187 throw Message.getSQLException(Message.VIEW_IS_INVALID_1, getSQL()); 188 } 189 PlanItem item = getBestPlanItem(session, null); 190 return item.index; 191 } 192 193 public ObjectArray getIndexes() { 194 return null; 195 } 196 197 public ObjectArray getTables() { 198 return tables; 199 } 200 201 public void recompile(Session session) throws SQLException { 202 for(int i=0; i<tables.size(); i++) { 203 Table t = (Table)tables.get(i); 204 t.removeView(this); 205 } 206 tables.clear(); 207 initColumnsAndTables(session); 208 } 209 210 public long getMaxDataModificationId() { 211 if(invalid) { 212 throw Message.getInternalError(); 213 } 214 if(viewQuery == null) { 215 return Long.MAX_VALUE; 216 } 217 return viewQuery.getMaxDataModificationId(); 218 } 219 220 public Index getUniqueIndex() { 221 return null; 222 } 223 224 private void removeViewFromTables() { 225 if(tables != null) { 226 for(int i=0; i<tables.size(); i++) { 227 Table t = (Table)tables.get(i); 228 t.removeView(this); 229 } 230 tables.clear(); 231 } 232 } 233 234 private void addViewToTables() { 235 for(int i=0; i<tables.size(); i++) { 236 Table t = (Table)tables.get(i); 237 t.addView(this); 238 } 239 } 240 241 } 242 | Popular Tags |