File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\Subquery.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 java.util.ArrayList;
10 import org.h2.command.dml.Query;
11 import org.h2.constant.ErrorCode;
12 import org.h2.engine.Session;
13 import org.h2.message.DbException;
14 import org.h2.result.ResultInterface;
15 import org.h2.table.ColumnResolver;
16 import org.h2.table.TableFilter;
17 import org.h2.value.Value;
18 import org.h2.value.ValueArray;
19 import org.h2.value.ValueNull;
20
21 /**
22 * A query returning a single value.
23 * Subqueries are used inside other statements.
24 */
25 public class Subquery extends Expression {
26
27 private final Query query;
28 private Expression expression;
29
30 public Subquery(Query query) {
31 this.query = query;
32 }
33
34 public Value getValue(Session session) {
35 query.setSession(session);
36 ResultInterface result = query.query(2);
37 try {
38 int rowcount = result.getRowCount();
39 if (rowcount > 1) {
40 throw DbException.get(ErrorCode.SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW);
41 }
42 Value v;
43 if (rowcount <= 0) {
44 v = ValueNull.INSTANCE;
45 } else {
46 result.next();
47 Value[] values = result.currentRow();
48 if (result.getVisibleColumnCount() == 1) {
49 v = values[0];
50 } else {
51 v = ValueArray.get(values);
52 }
53 }
54 return v;
55 } finally {
56 result.close();
57 }
58 }
59
60 public int getType() {
61 return getExpression().getType();
62 }
63
64 public void mapColumns(ColumnResolver resolver, int level) {
65 query.mapColumns(resolver, level + 1);
66 }
67
68 public Expression optimize(Session session) {
69 query.prepare();
70 return this;
71 }
72
73 public void setEvaluatable(TableFilter tableFilter, boolean b) {
74 query.setEvaluatable(tableFilter, b);
75 }
76
77 public int getScale() {
78 return getExpression().getScale();
79 }
80
81 public long getPrecision() {
82 return getExpression().getPrecision();
83 }
84
85 public int getDisplaySize() {
86 return getExpression().getDisplaySize();
87 }
88
89 public String getSQL() {
90 return "(" + query.getPlanSQL() + ")";
91 }
92
93 public void updateAggregate(Session session) {
94 query.updateAggregate(session);
95 }
96
97 private Expression getExpression() {
98 if (expression == null) {
99 ArrayList<Expression> expressions = query.getExpressions();
100 int columnCount = query.getColumnCount();
101 if (columnCount == 1) {
102 expression = expressions.get(0);
103 } else {
104 Expression[] list = new Expression[columnCount];
105 for (int i = 0; i < columnCount; i++) {
106 list[i] = expressions.get(i);
107 }
108 expression = new ExpressionList(list);
109 }
110 }
111 return expression;
112 }
113
114 public boolean isEverything(ExpressionVisitor visitor) {
115 return query.isEverything(visitor);
116 }
117
118 public Query getQuery() {
119 return query;
120 }
121
122 public int getCost() {
123 return query.getCostAsExpression();
124 }
125
126 public Expression[] getExpressionColumns(Session session) {
127 return getExpression().getExpressionColumns(session);
128 }
129 }
130