01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.expression.Expression;
11: import org.h2.expression.Parameter;
12: import org.h2.result.LocalResult;
13: import org.h2.util.ObjectArray;
14: import org.h2.value.Value;
15:
16: /**
17: * Represents a single SQL statements.
18: * It wraps a prepared statement.
19: */
20: public class CommandContainer extends Command {
21:
22: private Prepared prepared;
23:
24: CommandContainer(Parser parser, String sql, Prepared prepared) {
25: super (parser, sql);
26: prepared.setCommand(this );
27: this .prepared = prepared;
28: }
29:
30: public ObjectArray getParameters() {
31: return prepared.getParameters();
32: }
33:
34: public boolean isTransactional() {
35: return prepared.isTransactional();
36: }
37:
38: public boolean isQuery() {
39: return prepared.isQuery();
40: }
41:
42: private void recompileIfRequired() throws SQLException {
43: if (prepared.needRecompile()) {
44: // TODO test with 'always recompile'
45: prepared.setModificationMetaId(0);
46: String sql = prepared.getSQL();
47: ObjectArray oldValues = prepared.getParameters();
48: Parser parser = new Parser(session);
49: prepared = parser.parseOnly(sql);
50: long mod = prepared.getModificationMetaId();
51: prepared.setModificationMetaId(0);
52: ObjectArray newParams = prepared.getParameters();
53: for (int i = 0; i < newParams.size(); i++) {
54: Value v = ((Expression) oldValues.get(i))
55: .getValue(session);
56: Parameter p = (Parameter) newParams.get(i);
57: p.setValue(v);
58: }
59: prepared.prepare();
60: prepared.setModificationMetaId(mod);
61: }
62: }
63:
64: public int update() throws SQLException {
65: recompileIfRequired();
66: // TODO query time: should keep lock time separate from running time
67: start();
68: prepared.checkParameters();
69: prepared.trace();
70: return prepared.update();
71: }
72:
73: public LocalResult query(int maxrows) throws SQLException {
74: recompileIfRequired();
75: // TODO query time: should keep lock time separate from running time
76: start();
77: prepared.checkParameters();
78: prepared.trace();
79: return prepared.query(maxrows);
80: }
81:
82: public boolean isReadOnly() {
83: return prepared.isReadOnly();
84: }
85:
86: public LocalResult queryMeta() throws SQLException {
87: return prepared.queryMeta();
88: }
89:
90: }
|