001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.expression;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.command.dml.Query;
011: import org.h2.constant.ErrorCode;
012: import org.h2.engine.Session;
013: import org.h2.message.Message;
014: import org.h2.result.LocalResult;
015: import org.h2.table.ColumnResolver;
016: import org.h2.table.TableFilter;
017: import org.h2.value.Value;
018: import org.h2.value.ValueArray;
019: import org.h2.value.ValueNull;
020:
021: /**
022: * A query returning a single value.
023: * Subqueries are used inside other statements.
024: */
025: public class Subquery extends Expression {
026:
027: private Query query;
028:
029: public Subquery(Query query) {
030: this .query = query;
031: }
032:
033: public Value getValue(Session session) throws SQLException {
034: query.setSession(session);
035: LocalResult result = query.query(2);
036: try {
037: int rowcount = result.getRowCount();
038: if (rowcount > 1) {
039: throw Message
040: .getSQLException(ErrorCode.SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW);
041: }
042: Value v;
043: if (rowcount <= 0) {
044: v = ValueNull.INSTANCE;
045: } else {
046: result.next();
047: Value[] values = result.currentRow();
048: if (result.getVisibleColumnCount() == 1) {
049: v = values[0];
050: } else {
051: v = ValueArray.get(values);
052: }
053: }
054: return v;
055: } finally {
056: result.close();
057: }
058: }
059:
060: public int getType() {
061: return getExpression().getType();
062: }
063:
064: public void mapColumns(ColumnResolver resolver, int level)
065: throws SQLException {
066: query.mapColumns(resolver, level + 1);
067: }
068:
069: public Expression optimize(Session session) throws SQLException {
070: query.prepare();
071: return this ;
072: }
073:
074: public void setEvaluatable(TableFilter tableFilter, boolean b) {
075: query.setEvaluatable(tableFilter, b);
076: }
077:
078: public int getScale() {
079: return getExpression().getScale();
080: }
081:
082: public long getPrecision() {
083: return getExpression().getPrecision();
084: }
085:
086: public int getDisplaySize() {
087: return getExpression().getDisplaySize();
088: }
089:
090: public String getSQL() {
091: return "(" + query.getPlanSQL() + ")";
092: }
093:
094: public void updateAggregate(Session session) throws SQLException {
095: query.updateAggregate(session);
096: }
097:
098: private Expression getExpression() {
099: return (Expression) query.getExpressions().get(0);
100: }
101:
102: public boolean isEverything(ExpressionVisitor visitor) {
103: return query.isEverything(visitor);
104: }
105:
106: public Query getQuery() {
107: return query;
108: }
109:
110: public int getCost() {
111: return 10 + (int) (10 * query.getCost());
112: }
113:
114: }
|