File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\ConditionExists.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.command.dml.Query;
10 import org.h2.engine.Session;
11 import org.h2.result.ResultInterface;
12 import org.h2.table.ColumnResolver;
13 import org.h2.table.TableFilter;
14 import org.h2.util.StringUtils;
15 import org.h2.value.Value;
16 import org.h2.value.ValueBoolean;
17
18 /**
19 * An 'exists' condition as in WHERE EXISTS(SELECT ...)
20 */
21 public class ConditionExists extends Condition {
22
23 private final Query query;
24
25 public ConditionExists(Query query) {
26 this.query = query;
27 }
28
29 public Value getValue(Session session) {
30 query.setSession(session);
31 ResultInterface result = query.query(1);
32 session.addTemporaryResult(result);
33 boolean r = result.getRowCount() > 0;
34 return ValueBoolean.get(r);
35 }
36
37 public Expression optimize(Session session) {
38 query.prepare();
39 return this;
40 }
41
42 public String getSQL() {
43 return "EXISTS(\n" + StringUtils.indent(query.getPlanSQL(), 4, false) + ")";
44 }
45
46 public void updateAggregate(Session session) {
47 // TODO exists: is it allowed that the subquery contains aggregates?
48 // probably not
49 // select id from test group by id having exists (select * from test2
50 // where id=count(test.id))
51 }
52
53 public void mapColumns(ColumnResolver resolver, int level) {
54 query.mapColumns(resolver, level + 1);
55 }
56
57 public void setEvaluatable(TableFilter tableFilter, boolean b) {
58 query.setEvaluatable(tableFilter, b);
59 }
60
61 public boolean isEverything(ExpressionVisitor visitor) {
62 return query.isEverything(visitor);
63 }
64
65 public int getCost() {
66 return query.getCostAsExpression();
67 }
68
69 }
70