File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\ConditionNot.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.expression;
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 /**
16 * A NOT condition.
17 */
18 public class ConditionNot extends Condition {
19
20 private Expression condition;
21
22 public ConditionNot(Expression condition) {
23 this.condition = condition;
24 }
25
26 public Expression getNotIfPossible(Session session) {
27 return condition;
28 }
29
30 public Value getValue(Session session) {
31 Value v = condition.getValue(session);
32 if (v == ValueNull.INSTANCE) {
33 return v;
34 }
35 return v.convertTo(Value.BOOLEAN).negate();
36 }
37
38 public void mapColumns(ColumnResolver resolver, int level) {
39 condition.mapColumns(resolver, level);
40 }
41
42 public Expression optimize(Session session) {
43 Expression e2 = condition.getNotIfPossible(session);
44 if (e2 != null) {
45 return e2.optimize(session);
46 }
47 Expression expr = condition.optimize(session);
48 if (expr.isConstant()) {
49 Value v = expr.getValue(session);
50 if (v == ValueNull.INSTANCE) {
51 return ValueExpression.getNull();
52 }
53 return ValueExpression.get(v.convertTo(Value.BOOLEAN).negate());
54 }
55 condition = expr;
56 return this;
57 }
58
59 public void setEvaluatable(TableFilter tableFilter, boolean b) {
60 condition.setEvaluatable(tableFilter, b);
61 }
62
63 public String getSQL() {
64 return "(NOT " + condition.getSQL() + ")";
65 }
66
67 public void updateAggregate(Session session) {
68 condition.updateAggregate(session);
69 }
70
71 public void addFilterConditions(TableFilter filter, boolean outerJoin) {
72 if (outerJoin) {
73 // can not optimize:
74 // select * from test t1 left join test t2 on t1.id = t2.id where
75 // not t2.id is not null
76 // to
77 // select * from test t1 left join test t2 on t1.id = t2.id and
78 // t2.id is not null
79 return;
80 }
81 super.addFilterConditions(filter, outerJoin);
82 }
83
84 public boolean isEverything(ExpressionVisitor visitor) {
85 return condition.isEverything(visitor);
86 }
87
88 public int getCost() {
89 return condition.getCost();
90 }
91
92 }
93