KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > expression > ConditionExists


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.expression;
6
7 import java.sql.SQLException JavaDoc;
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 /**
18  * @author Thomas
19  */

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 JavaDoc {
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 JavaDoc {
41         query.prepare();
42         return this;
43     }
44
45     public String JavaDoc getSQL() {
46         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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         // TODO exists: is it allowed that the subquery contains aggregates? probably not
55
// select id from test group by id having exists (select * from test2 where id=count(test.id))
56
}
57     
58     public void mapColumns(ColumnResolver resolver, int level) throws SQLException JavaDoc {
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