-
Notifications
You must be signed in to change notification settings - Fork 72
/
UpdateQueryParser.java
120 lines (89 loc) · 4.09 KB
/
UpdateQueryParser.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
/*
*
* Copyright (c) 2017 Otávio Santana and others
* 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.jnosql.diana.api.column.query;
import org.jnosql.diana.api.Params;
import org.jnosql.diana.api.QueryException;
import org.jnosql.diana.api.column.ColumnEntity;
import org.jnosql.diana.api.column.ColumnFamilyManager;
import org.jnosql.diana.api.column.ColumnFamilyManagerAsync;
import org.jnosql.diana.api.column.ColumnObserverParser;
import org.jnosql.diana.api.column.ColumnPreparedStatement;
import org.jnosql.diana.api.column.ColumnPreparedStatementAsync;
import org.jnosql.query.Condition;
import org.jnosql.query.JSONValue;
import org.jnosql.query.UpdateQuery;
import org.jnosql.query.UpdateQuerySupplier;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import static java.util.Collections.singletonList;
final class UpdateQueryParser extends ConditionQueryParser {
private final UpdateQuerySupplier supplier;
UpdateQueryParser() {
this.supplier = UpdateQuerySupplier.getSupplier();
}
List<ColumnEntity> query(String query, ColumnFamilyManager manager, ColumnObserverParser observer) {
UpdateQuery updateQuery = supplier.apply(query);
Params params = new Params();
ColumnEntity entity = getEntity(params, updateQuery, observer);
if (params.isNotEmpty()) {
throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
}
return singletonList(manager.update(entity));
}
void queryAsync(String query, ColumnFamilyManagerAsync manager,
Consumer<List<ColumnEntity>> callBack, ColumnObserverParser observer) {
UpdateQuery updateQuery = supplier.apply(query);
Params params = new Params();
ColumnEntity entity = getEntity(params, updateQuery, observer);
if (params.isNotEmpty()) {
throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
}
manager.update(entity, c -> callBack.accept(singletonList(c)));
}
ColumnPreparedStatement prepare(String query, ColumnFamilyManager manager, ColumnObserverParser observer) {
Params params = new Params();
UpdateQuery updateQuery = supplier.apply(query);
ColumnEntity entity = getEntity(params, updateQuery, observer);
return DefaultColumnPreparedStatement.update(entity, params, query, manager);
}
ColumnPreparedStatementAsync prepareAsync(String query, ColumnFamilyManagerAsync manager,
ColumnObserverParser observer) {
Params params = new Params();
UpdateQuery updateQuery = supplier.apply(query);
ColumnEntity entity = getEntity(params, updateQuery, observer);
return DefaultColumnPreparedStatementAsync.update(entity, params, query, manager);
}
private ColumnEntity getEntity(Params params, UpdateQuery updateQuery, ColumnObserverParser observer) {
String columnFamily = observer.fireEntity(updateQuery.getEntity());
return getEntity(new UpdateQueryConditionSupplier(updateQuery), columnFamily, params, observer);
}
private class UpdateQueryConditionSupplier implements ConditionQuerySupplier {
private final UpdateQuery query;
private UpdateQueryConditionSupplier(UpdateQuery query) {
this.query = query;
}
@Override
public List<Condition> getConditions() {
return query.getConditions();
}
@Override
public Optional<JSONValue> getValue() {
return query.getValue();
}
}
}