/
SelectMethodQueryProvider.java
53 lines (45 loc) · 1.97 KB
/
SelectMethodQueryProvider.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
/*
* 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.query.method;
import org.antlr.v4.runtime.tree.ParseTree;
import jakarta.data.Sort;
import jakarta.data.Direction;
import org.eclipse.jnosql.communication.query.SelectQuery;
import org.eclipse.jnosql.query.grammar.method.MethodParser;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
public final class SelectMethodQueryProvider extends AbstractMethodQueryProvider implements BiFunction<String, String, SelectQuery> {
private final List<Sort<?>> sorts = new ArrayList<>();
@Override
public SelectQuery apply(String query, String entity) {
Objects.requireNonNull(query, " query is required");
Objects.requireNonNull(entity, " entity is required");
runQuery(MethodQuery.of(query).get());
return new MethodSelectQuery(entity, sorts, where);
}
@Override
public void exitOrderName(MethodParser.OrderNameContext ctx) {
sorts.add(this.sort(ctx));
}
@Override
Function<MethodParser, ParseTree> getParserTree() {
return MethodParser::select;
}
private Sort sort(MethodParser.OrderNameContext context) {
String text = context.variable().getText();
Direction type = context.desc() == null ? Direction.ASC : Direction.DESC;
return Sort.of(getFormatField(text), type, false);
}
}