File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\CommandContainer.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.command;
8
9 import java.util.ArrayList;
10 import org.h2.api.DatabaseEventListener;
11 import org.h2.expression.Parameter;
12 import org.h2.expression.ParameterInterface;
13 import org.h2.result.ResultInterface;
14 import org.h2.value.Value;
15 import org.h2.value.ValueNull;
16
17 /**
18 * Represents a single SQL statements.
19 * It wraps a prepared statement.
20 */
21 class CommandContainer extends Command {
22
23 private Prepared prepared;
24 private boolean readOnlyKnown;
25 private boolean readOnly;
26
27 CommandContainer(Parser parser, String sql, Prepared prepared) {
28 super(parser, sql);
29 prepared.setCommand(this);
30 this.prepared = prepared;
31 }
32
33 public ArrayList<? extends ParameterInterface> getParameters() {
34 return prepared.getParameters();
35 }
36
37 public boolean isTransactional() {
38 return prepared.isTransactional();
39 }
40
41 public boolean isQuery() {
42 return prepared.isQuery();
43 }
44
45 private void recompileIfRequired() {
46 if (prepared.needRecompile()) {
47 // TODO test with 'always recompile'
48 prepared.setModificationMetaId(0);
49 String sql = prepared.getSQL();
50 ArrayList<Parameter> oldParams = prepared.getParameters();
51 Parser parser = new Parser(session);
52 prepared = parser.parse(sql);
53 long mod = prepared.getModificationMetaId();
54 prepared.setModificationMetaId(0);
55 ArrayList<Parameter> newParams = prepared.getParameters();
56 for (int i = 0, size = newParams.size(); i < size; i++) {
57 Parameter old = oldParams.get(i);
58 if (old.isValueSet()) {
59 Value v = old.getValue(session);
60 Parameter p = newParams.get(i);
61 p.setValue(v);
62 }
63 }
64 prepared.prepare();
65 prepared.setModificationMetaId(mod);
66 }
67 }
68
69 public int update() {
70 recompileIfRequired();
71 setProgress(DatabaseEventListener.STATE_STATEMENT_START);
72 start();
73 session.setLastScopeIdentity(ValueNull.INSTANCE);
74 prepared.checkParameters();
75 int updateCount = prepared.update();
76 prepared.trace(startTime, updateCount);
77 setProgress(DatabaseEventListener.STATE_STATEMENT_END);
78 return updateCount;
79 }
80
81 public ResultInterface query(int maxrows) {
82 recompileIfRequired();
83 setProgress(DatabaseEventListener.STATE_STATEMENT_START);
84 start();
85 prepared.checkParameters();
86 ResultInterface result = prepared.query(maxrows);
87 prepared.trace(startTime, result.getRowCount());
88 setProgress(DatabaseEventListener.STATE_STATEMENT_END);
89 return result;
90 }
91
92 public boolean isReadOnly() {
93 if (!readOnlyKnown) {
94 readOnly = prepared.isReadOnly();
95 readOnlyKnown = true;
96 }
97 return readOnly;
98 }
99
100 public ResultInterface queryMeta() {
101 return prepared.queryMeta();
102 }
103
104 public boolean isCacheable() {
105 return prepared.isCacheable();
106 }
107
108 public int getCommandType() {
109 return prepared.getType();
110 }
111
112 }
113