KickJava   Java API By Example, From Geeks To Geeks.

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


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.message.Message;
12 import org.h2.result.LocalResult;
13 import org.h2.table.ColumnResolver;
14 import org.h2.table.TableFilter;
15 import org.h2.value.Value;
16 import org.h2.value.ValueArray;
17 import org.h2.value.ValueNull;
18
19 /**
20  * @author Thomas
21  */

22
23 public class Subquery extends Expression {
24
25     private Query query;
26
27     public Subquery(Query query) {
28         this.query = query;
29     }
30
31     public Value getValue(Session session) throws SQLException JavaDoc {
32         query.setSession(session);
33         LocalResult result = query.query(2);
34         try {
35             int rowcount = result.getRowCount();
36             if(rowcount > 1) {
37                 throw Message.getSQLException(Message.SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW);
38             }
39             Value v;
40             if(rowcount <= 0) {
41                 v = ValueNull.INSTANCE;
42             } else {
43                 result.next();
44                 Value[] values = result.currentRow();
45                 if(result.getVisibleColumnCount() == 1) {
46                     v = values[0];
47                 } else {
48                     v = ValueArray.get(values);
49                 }
50             }
51             return v;
52         } finally {
53             result.close();
54         }
55     }
56
57     public int getType() {
58         return getExpression().getType();
59     }
60
61     public void mapColumns(ColumnResolver resolver, int level) throws SQLException JavaDoc {
62         query.mapColumns(resolver, level + 1);
63     }
64
65     public Expression optimize(Session session) throws SQLException JavaDoc {
66         query.prepare();
67         return this;
68     }
69
70     public void setEvaluatable(TableFilter tableFilter, boolean b) {
71         query.setEvaluatable(tableFilter, b);
72     }
73
74     public int getScale() {
75         return getExpression().getScale();
76     }
77
78     public long getPrecision() {
79         return getExpression().getPrecision();
80     }
81
82     public String JavaDoc getSQL() {
83         return "(" + query.getPlan() +")";
84     }
85
86     public void updateAggregate(Session session) {
87         // TODO exists: is it possible that the subquery contains related aggregates? probably not
88
}
89
90     private Expression getExpression() {
91         return (Expression) query.getExpressions().get(0);
92     }
93
94     public boolean isEverything(ExpressionVisitor visitor) {
95         return query.isEverything(visitor);
96     }
97
98     public Query getQuery() {
99         return query;
100     }
101
102     public int getCost() {
103         return 10 + (int)(10 * query.getCost());
104     }
105
106 }
107
Popular Tags