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.command.dml;
007:
008: import java.sql.ResultSet;
009: import java.sql.SQLException;
010:
011: import org.h2.command.Prepared;
012: import org.h2.engine.Session;
013: import org.h2.expression.Expression;
014: import org.h2.expression.ExpressionColumn;
015: import org.h2.expression.ExpressionVisitor;
016: import org.h2.result.LocalResult;
017: import org.h2.table.Column;
018: import org.h2.util.ObjectArray;
019: import org.h2.value.Value;
020: import org.h2.value.ValueArray;
021: import org.h2.value.ValueResultSet;
022:
023: /**
024: * This class represents the statement
025: * CALL.
026: */
027: public class Call extends Prepared {
028: private Expression value;
029: private ObjectArray expressions;
030:
031: public Call(Session session) {
032: super (session);
033: }
034:
035: public LocalResult queryMeta() throws SQLException {
036: LocalResult result = new LocalResult(session, expressions, 1);
037: result.done();
038: return result;
039: }
040:
041: public int update() throws SQLException {
042: Value v = value.getValue(session);
043: int type = v.getType();
044: switch (type) {
045: case Value.RESULT_SET:
046: case Value.ARRAY:
047: // this will throw an exception
048: // methods returning a result set may not be called like this.
049: return super .update();
050: case Value.UNKNOWN:
051: case Value.NULL:
052: return 0;
053: default:
054: return v.getInt();
055: }
056: }
057:
058: public LocalResult query(int maxrows) throws SQLException {
059: setCurrentRowNumber(1);
060: Value v = value.getValue(session);
061: if (v.getType() == Value.RESULT_SET) {
062: ResultSet rs = ((ValueResultSet) v).getResultSet();
063: return LocalResult.read(session, rs, maxrows);
064: } else if (v.getType() == Value.ARRAY) {
065: Value[] list = ((ValueArray) v).getList();
066: ObjectArray expr = new ObjectArray();
067: for (int i = 0; i < list.length; i++) {
068: Value e = list[i];
069: Column col = new Column("C" + (i + 1), e.getType(), e
070: .getPrecision(), e.getScale(), e
071: .getDisplaySize());
072: expr.add(new ExpressionColumn(session.getDatabase(),
073: col));
074: }
075: LocalResult result = new LocalResult(session, expr,
076: list.length);
077: result.addRow(list);
078: result.done();
079: return result;
080: }
081: LocalResult result = new LocalResult(session, expressions, 1);
082: Value[] row = new Value[1];
083: row[0] = v;
084: result.addRow(row);
085: result.done();
086: return result;
087: }
088:
089: public void prepare() throws SQLException {
090: value = value.optimize(session);
091: expressions = new ObjectArray();
092: expressions.add(value);
093: }
094:
095: public void setValue(Expression expression) {
096: value = expression;
097: }
098:
099: public boolean isQuery() {
100: return true;
101: }
102:
103: public boolean isTransactional() {
104: return true;
105: }
106:
107: public boolean isReadOnly() {
108: return value.isEverything(ExpressionVisitor.READONLY);
109:
110: }
111:
112: }
|