KickJava   Java API By Example, From Geeks To Geeks.

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


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.engine.Session;
10 import org.h2.table.ColumnResolver;
11 import org.h2.table.TableFilter;
12 import org.h2.value.Value;
13 import org.h2.value.ValueNull;
14
15 public class ConditionNot extends Condition {
16
17     private Expression condition;
18
19     public ConditionNot(Expression condition) {
20         this.condition = condition;
21     }
22
23     public void createIndexConditions(TableFilter filter) {
24         // TODO optimization: in some cases, index conditions can be created here
25
}
26
27     public Value getValue(Session session) throws SQLException JavaDoc {
28         Value v = condition.getValue(session);
29         if(v == ValueNull.INSTANCE) {
30             return v;
31         }
32         return v.convertTo(Value.BOOLEAN).negate();
33     }
34
35     public void mapColumns(ColumnResolver resolver, int level) throws SQLException JavaDoc {
36         condition.mapColumns(resolver, level);
37     }
38
39     public Expression optimize(Session session) throws SQLException JavaDoc {
40         // TODO optimization: some cases are maybe possible to optimize futher: (NOT ID >= 5)
41
Expression expr = condition.optimize(session);
42         if(expr.isConstant()) {
43             Value v = expr.getValue(session);
44             if(v == ValueNull.INSTANCE) {
45                 return ValueExpression.NULL;
46             }
47             return ValueExpression.get(v.convertTo(Value.BOOLEAN).negate());
48         }
49         condition = expr;
50         return this;
51     }
52
53     public void setEvaluatable(TableFilter tableFilter, boolean b) {
54         condition.setEvaluatable(tableFilter, b);
55     }
56
57     public String JavaDoc getSQL() {
58         return "(NOT " + condition.getSQL() + ")";
59     }
60
61     public void updateAggregate(Session session) throws SQLException JavaDoc {
62         condition.updateAggregate(session);
63     }
64     
65     public void addFilterConditions(TableFilter filter, boolean outerJoin) {
66         if(outerJoin) {
67             // can not optimize:
68
// select * from test t1 left join test t2 on t1.id = t2.id where not t2.id is not null
69
// to
70
// select * from test t1 left join test t2 on t1.id = t2.id and t2.id is not null
71
return;
72         }
73         super.addFilterConditions(filter, outerJoin);
74     }
75
76     public boolean isEverything(ExpressionVisitor visitor) {
77         return condition.isEverything(visitor);
78     }
79     
80     public int getCost() {
81         return condition.getCost();
82     }
83
84 }
85
Popular Tags