Debian Codegraph @

src/main/org/h2/command/CommandList.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command;
7
8 import java.util.ArrayList;
9 import java.util.HashSet;
10 import java.util.Set;
11 import org.h2.engine.DbObject;
12 import org.h2.engine.SessionLocal;
13 import org.h2.expression.Parameter;
14 import org.h2.expression.ParameterInterface;
15 import org.h2.result.ResultInterface;
16 import org.h2.result.ResultWithGeneratedKeys;
17
18 /**
19 * Represents a list of SQL statements.
20 */
21 class CommandList extends Command {
22
23 private CommandContainer command;
24 private final ArrayList<Prepared> commands;
25 private final ArrayList<Parameter> parameters;
26 private String remaining;
27 private Command remainingCommand;
28
29 CommandList(SessionLocal session, String sql, CommandContainer command, ArrayList<Prepared> commands,
30 ArrayList<Parameter> parameters, String remaining) {
31 super(session, sql);
32 this.command = command;
33 this.commands = commands;
34 this.parameters = parameters;
35 this.remaining = remaining;
36 }
37
38 @Override
39 public ArrayList<? extends ParameterInterface> getParameters() {
40 return parameters;
41 }
42
43 private void executeRemaining() {
44 for (Prepared prepared : commands) {
45 prepared.prepare();
46 if (prepared.isQuery()) {
47 prepared.query(0);
48 } else {
49 prepared.update();
50 }
51 }
52 if (remaining != null) {
53 remainingCommand = session.prepareLocal(remaining);
54 remaining = null;
55 if (remainingCommand.isQuery()) {
56 remainingCommand.query(0);
57 } else {
58 remainingCommand.update(null);
59 }
60 }
61 }
62
63 @Override
64 public ResultWithGeneratedKeys update(Object generatedKeysRequest) {
65 ResultWithGeneratedKeys result = command.executeUpdate(null);
66 executeRemaining();
67 return result;
68 }
69
70 @Override
71 public ResultInterface query(long maxrows) {
72 ResultInterface result = command.query(maxrows);
73 executeRemaining();
74 return result;
75 }
76
77 @Override
78 public void stop() {
79 command.stop();
80 for (Prepared prepared : commands) {
81 CommandContainer.clearCTE(session, prepared);
82 }
83 if (remainingCommand != null) {
84 remainingCommand.stop();
85 }
86 }
87
88 @Override
89 public boolean isQuery() {
90 return command.isQuery();
91 }
92
93 @Override
94 public boolean isTransactional() {
95 return true;
96 }
97
98 @Override
99 public boolean isReadOnly() {
100 return false;
101 }
102
103 @Override
104 public ResultInterface queryMeta() {
105 return command.queryMeta();
106 }
107
108 @Override
109 public int getCommandType() {
110 return command.getCommandType();
111 }
112
113 @Override
114 public Set<DbObject> getDependencies() {
115 HashSet<DbObject> dependencies = new HashSet<>();
116 for (Prepared prepared : commands) {
117 prepared.collectDependencies(dependencies);
118 }
119 return dependencies;
120 }
121
122 @Override
123 protected boolean isRetryable() {
124 if (!command.isRetryable()) {
125 return false;
126 }
127 for (Prepared prepared : commands) {
128 if (!prepared.isRetryable()) {
129 return false;
130 }
131 }
132 return remainingCommand == null || remainingCommand.isRetryable();
133 }
134
135 }