File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\CommandRemote.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.io.IOException;
10 import java.util.ArrayList;
11 import org.h2.constant.SysProperties;
12 import org.h2.engine.SessionRemote;
13 import org.h2.expression.ParameterInterface;
14 import org.h2.expression.ParameterRemote;
15 import org.h2.message.DbException;
16 import org.h2.message.Trace;
17 import org.h2.result.ResultInterface;
18 import org.h2.result.ResultRemote;
19 import org.h2.util.New;
20 import org.h2.value.Transfer;
21 import org.h2.value.Value;
22
23 /**
24 * Represents the client-side part of a SQL statement.
25 * This class is not used in embedded mode.
26 */
27 public class CommandRemote implements CommandInterface {
28
29 private final ArrayList<Transfer> transferList;
30 private final ArrayList<ParameterInterface> parameters;
31 private final Trace trace;
32 private final String sql;
33 private final int fetchSize;
34 private SessionRemote session;
35 private int id;
36 private boolean isQuery;
37 private boolean readonly;
38 private final int created;
39
40 public CommandRemote(SessionRemote session, ArrayList<Transfer> transferList, String sql, int fetchSize) {
41 this.transferList = transferList;
42 trace = session.getTrace();
43 this.sql = sql;
44 parameters = New.arrayList();
45 prepare(session, true);
46 // set session late because prepare might fail - in this case we don't
47 // need to close the object
48 this.session = session;
49 this.fetchSize = fetchSize;
50 created = session.getLastReconnect();
51 }
52
53 private void prepare(SessionRemote s, boolean createParams) {
54 id = s.getNextId();
55 for (int i = 0, count = 0; i < transferList.size(); i++) {
56 try {
57 Transfer transfer = transferList.get(i);
58 if (createParams) {
59 s.traceOperation("SESSION_PREPARE_READ_PARAMS", id);
60 transfer.writeInt(SessionRemote.SESSION_PREPARE_READ_PARAMS).writeInt(id).writeString(sql);
61 } else {
62 s.traceOperation("SESSION_PREPARE", id);
63 transfer.writeInt(SessionRemote.SESSION_PREPARE).writeInt(id).writeString(sql);
64 }
65 s.done(transfer);
66 isQuery = transfer.readBoolean();
67 readonly = transfer.readBoolean();
68 int paramCount = transfer.readInt();
69 if (createParams) {
70 parameters.clear();
71 for (int j = 0; j < paramCount; j++) {
72 ParameterRemote p = new ParameterRemote(j);
73 p.readMetaData(transfer);
74 parameters.add(p);
75 }
76 }
77 } catch (IOException e) {
78 s.removeServer(e, i--, ++count);
79 }
80 }
81 }
82
83 public boolean isQuery() {
84 return isQuery;
85 }
86
87 public ArrayList<ParameterInterface> getParameters() {
88 return parameters;
89 }
90
91 private void prepareIfRequired() {
92 if (session.getLastReconnect() != created) {
93 // in this case we need to prepare again in every case
94 id = Integer.MIN_VALUE;
95 }
96 session.checkClosed();
97 if (id <= session.getCurrentId() - SysProperties.SERVER_CACHED_OBJECTS) {
98 // object is too old - we need to prepare again
99 prepare(session, false);
100 }
101 }
102
103 public ResultInterface getMetaData() {
104 synchronized (session) {
105 if (!isQuery) {
106 return null;
107 }
108 int objectId = session.getNextId();
109 ResultRemote result = null;
110 for (int i = 0, count = 0; i < transferList.size(); i++) {
111 prepareIfRequired();
112 Transfer transfer = transferList.get(i);
113 try {
114 session.traceOperation("COMMAND_GET_META_DATA", id);
115 transfer.writeInt(SessionRemote.COMMAND_GET_META_DATA).writeInt(id).writeInt(objectId);
116 session.done(transfer);
117 int columnCount = transfer.readInt();
118 result = new ResultRemote(session, transfer, objectId, columnCount, Integer.MAX_VALUE);
119 break;
120 } catch (IOException e) {
121 session.removeServer(e, i--, ++count);
122 }
123 }
124 session.autoCommitIfCluster();
125 return result;
126 }
127 }
128
129 public ResultInterface executeQuery(int maxRows, boolean scrollable) {
130 checkParameters();
131 synchronized (session) {
132 int objectId = session.getNextId();
133 ResultRemote result = null;
134 for (int i = 0, count = 0; i < transferList.size(); i++) {
135 prepareIfRequired();
136 Transfer transfer = transferList.get(i);
137 try {
138 session.traceOperation("COMMAND_EXECUTE_QUERY", id);
139 transfer.writeInt(SessionRemote.COMMAND_EXECUTE_QUERY).writeInt(id).writeInt(objectId).writeInt(
140 maxRows);
141 int fetch;
142 if (session.isClustered() || scrollable) {
143 fetch = Integer.MAX_VALUE;
144 } else {
145 fetch = fetchSize;
146 }
147 transfer.writeInt(fetch);
148 sendParameters(transfer);
149 session.done(transfer);
150 int columnCount = transfer.readInt();
151 if (result != null) {
152 result.close();
153 result = null;
154 }
155 result = new ResultRemote(session, transfer, objectId, columnCount, fetch);
156 if (readonly) {
157 break;
158 }
159 } catch (IOException e) {
160 session.removeServer(e, i--, ++count);
161 }
162 }
163 session.autoCommitIfCluster();
164 session.readSessionState();
165 return result;
166 }
167 }
168
169 public int executeUpdate() {
170 checkParameters();
171 synchronized (session) {
172 int updateCount = 0;
173 boolean autoCommit = false;
174 for (int i = 0, count = 0; i < transferList.size(); i++) {
175 prepareIfRequired();
176 Transfer transfer = transferList.get(i);
177 try {
178 session.traceOperation("COMMAND_EXECUTE_UPDATE", id);
179 transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id);
180 sendParameters(transfer);
181 session.done(transfer);
182 updateCount = transfer.readInt();
183 autoCommit = transfer.readBoolean();
184 } catch (IOException e) {
185 session.removeServer(e, i--, ++count);
186 }
187 }
188 session.setAutoCommitFromServer(autoCommit);
189 session.autoCommitIfCluster();
190 session.readSessionState();
191 return updateCount;
192 }
193 }
194
195 private void checkParameters() {
196 for (ParameterInterface p : parameters) {
197 p.checkSet();
198 }
199 }
200
201 private void sendParameters(Transfer transfer) throws IOException {
202 int len = parameters.size();
203 transfer.writeInt(len);
204 for (ParameterInterface p : parameters) {
205 transfer.writeValue(p.getParamValue());
206 }
207 }
208
209 public void close() {
210 if (session == null || session.isClosed()) {
211 return;
212 }
213 synchronized (session) {
214 session.traceOperation("COMMAND_CLOSE", id);
215 for (Transfer transfer : transferList) {
216 try {
217 transfer.writeInt(SessionRemote.COMMAND_CLOSE).writeInt(id);
218 } catch (IOException e) {
219 trace.error(e, "close");
220 }
221 }
222 }
223 session = null;
224 try {
225 for (ParameterInterface p : parameters) {
226 Value v = p.getParamValue();
227 if (v != null) {
228 v.close();
229 }
230 }
231 } catch (DbException e) {
232 trace.error(e, "close");
233 }
234 parameters.clear();
235 }
236
237 /**
238 * Cancel this current statement.
239 */
240 public void cancel() {
241 session.cancelStatement(id);
242 }
243
244 public String toString() {
245 return sql + Trace.formatParams(getParameters());
246 }
247
248 public int getCommandType() {
249 return UNKNOWN;
250 }
251
252 }
253