1 5 package org.h2.expression; 6 7 import java.sql.SQLException ; 8 9 import org.h2.command.dml.Query; 10 import org.h2.engine.Session; 11 import org.h2.result.LocalResult; 12 import org.h2.table.ColumnResolver; 13 import org.h2.table.TableFilter; 14 import org.h2.value.Value; 15 import org.h2.value.ValueBoolean; 16 17 20 21 public class ConditionExists extends Condition { 22 23 private Query query; 24 25 public ConditionExists(Query query) { 26 this.query = query; 27 } 28 29 public Value getValue(Session session) throws SQLException { 30 query.setSession(session); 31 LocalResult result = query.query(1); 32 try { 33 boolean r = result.getRowCount() > 0; 34 return ValueBoolean.get(r); 35 } finally { 36 result.close(); 37 } 38 } 39 40 public Expression optimize(Session session) throws SQLException { 41 query.prepare(); 42 return this; 43 } 44 45 public String getSQL() { 46 StringBuffer buff = new StringBuffer (); 47 buff.append("EXISTS("); 48 buff.append(query.getPlan()); 49 buff.append(")"); 50 return buff.toString(); 51 } 52 53 public void updateAggregate(Session session) { 54 } 57 58 public void mapColumns(ColumnResolver resolver, int level) throws SQLException { 59 query.mapColumns(resolver, level+1); 60 } 61 62 public void setEvaluatable(TableFilter tableFilter, boolean b) { 63 query.setEvaluatable(tableFilter, b); 64 } 65 66 public boolean isEverything(ExpressionVisitor visitor) { 67 return query.isEverything(visitor); 68 } 69 70 public int getCost() { 71 return 10 + (int)(10 * query.getCost()); 72 } 73 74 } 75 | Popular Tags |