/
CommunicationPreparedStatement.java
190 lines (157 loc) · 6.48 KB
/
CommunicationPreparedStatement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*
*/
package org.eclipse.jnosql.communication.semistructured;
import jakarta.data.exceptions.NonUniqueResultException;
import org.eclipse.jnosql.communication.Params;
import org.eclipse.jnosql.communication.QueryException;
import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
/**
* An object that represents a precompiled Query statement.
*/
public final class CommunicationPreparedStatement {
private final CommunicationEntity entity;
private final SelectQuery selectQuery;
private final DeleteQuery deleteQuery;
private final PreparedStatementType type;
private final Params params;
private final String query;
private final List<String> paramsLeft;
private final Duration duration;
private final DatabaseManager manager;
private CommunicationPreparedStatement(CommunicationEntity entity,
SelectQuery selectQuery,
DeleteQuery deleteQuery,
PreparedStatementType type,
Params params,
String query,
List<String> paramsLeft,
Duration duration,
DatabaseManager manager) {
this.entity = entity;
this.selectQuery = selectQuery;
this.deleteQuery = deleteQuery;
this.type = type;
this.params = params;
this.query = query;
this.paramsLeft = paramsLeft;
this.manager = manager;
this.duration = duration;
}
/**
* Binds an argument to a positional parameter.
*
* @param name the parameter name
* @param value the parameter value
* @return the same query instance
* @throws NullPointerException when there is null parameter
*/
public CommunicationPreparedStatement bind(String name, Object value) {
Objects.requireNonNull(name, "name is required");
Objects.requireNonNull(value, "value is required");
paramsLeft.remove(name);
params.bind(name, value);
return this;
}
/**
* Returns the result as a single element otherwise it will return an {@link Optional#empty()}
*
* @return the single result
*/
public Stream<CommunicationEntity> result() {
if (!paramsLeft.isEmpty()) {
throw new QueryException("Check all the parameters before execute the query, params left: " + paramsLeft);
}
switch (type) {
case SELECT -> {
return manager.select(selectQuery);
}
case DELETE -> {
manager.delete(deleteQuery);
return Stream.empty();
}
case UPDATE -> {
return Stream.of(manager.update(entity));
}
case INSERT -> {
if (Objects.isNull(duration)) {
return Stream.of(manager.insert(entity));
} else {
return Stream.of(manager.insert(entity, duration));
}
}
default -> throw new UnsupportedOperationException("there is not support to operation type: " + type);
}
}
public Optional<CommunicationEntity> singleResult() {
Stream<CommunicationEntity> entities = result();
final Iterator<CommunicationEntity> iterator = entities.iterator();
if (!iterator.hasNext()) {
return Optional.empty();
}
final CommunicationEntity next = iterator.next();
if (!iterator.hasNext()) {
return Optional.of(next);
}
throw new NonUniqueResultException("The select returns more than one entity, select: " + query);
}
enum PreparedStatementType {
SELECT, DELETE, UPDATE, INSERT
}
@Override
public String toString() {
return query;
}
static CommunicationPreparedStatement select(
SelectQuery selectQuery,
Params params,
String query,
DatabaseManager manager) {
return new CommunicationPreparedStatement(null, selectQuery,
null, PreparedStatementType.SELECT, params, query,
params.getParametersNames(), null, manager);
}
static CommunicationPreparedStatement delete(DeleteQuery deleteQuery,
Params params,
String query,
DatabaseManager manager) {
return new CommunicationPreparedStatement(null, null,
deleteQuery, PreparedStatementType.DELETE, params, query,
params.getParametersNames(), null, manager);
}
static CommunicationPreparedStatement insert(CommunicationEntity entity,
Params params,
String query,
Duration duration,
DatabaseManager manager) {
return new CommunicationPreparedStatement(entity, null,
null, PreparedStatementType.INSERT, params, query,
params.getParametersNames(), duration, manager);
}
static CommunicationPreparedStatement update(CommunicationEntity entity,
Params params,
String query,
DatabaseManager manager) {
return new CommunicationPreparedStatement(entity, null,
null, PreparedStatementType.UPDATE, params, query,
params.getParametersNames(), null, manager);
}
}