/
QueryParser.java
88 lines (79 loc) · 4.29 KB
/
QueryParser.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
/*
* Copyright (c) 2024 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.
*
*/
package org.eclipse.jnosql.communication.semistructured;
import org.eclipse.jnosql.communication.QueryException;
import java.util.Objects;
import java.util.stream.Stream;
/**
* A query parser to column database type, this class will convert a String to an operation in {@link DatabaseManager}.
*/
public final class QueryParser {
private final SelectQueryParser select = new SelectQueryParser();
private final DeleteQueryParser delete = new DeleteQueryParser();
private final InsertQueryParser insert = new InsertQueryParser();
private final UpdateQueryParser update = new UpdateQueryParser();
/**
* Executes a query and returns the result, when the operations are <b>insert</b>, <b>update</b> and <b>select</b>
* command it will return the result of the operation when the command is <b>delete</b> it will return an empty collection.
*
* @param query the query as {@link String}
* @param manager the manager
* @param observer the observer
* @return the result of the operation if delete it will always return an empty list
* @throws NullPointerException when there is parameter null
* @throws IllegalArgumentException when the query has value parameters
* @throws QueryException when there is error in the syntax
*/
public Stream<CommunicationEntity> query(String query, DatabaseManager manager, CommunicationObserverParser observer) {
validation(query, manager, observer);
String command = query.substring(0, 6);
return switch (command) {
case "select" -> select.query(query, manager, observer);
case "delete" -> delete.query(query, manager, observer);
case "insert" -> insert.query(query, manager, observer);
case "update" -> update.query(query, manager, observer);
default ->
throw new QueryException(String.format("The command was not recognized at the query %s ", query));
};
}
/**
* Executes a query and returns a {@link CommunicationPreparedStatement}, when the operations are <b>insert</b>, <b>update</b> and <b>select</b>
* command it will return the result of the operation when the command is <b>delete</b> it will return an empty collection.
*
* @param query the query as {@link String}
* @param manager the manager
* @param observer the observer
* @return a {@link CommunicationPreparedStatement} instance
* @throws NullPointerException when there is parameter null
* @throws IllegalArgumentException when the query has value parameters
* @throws QueryException when there is error in the syntax
*/
public CommunicationPreparedStatement prepare(String query, DatabaseManager manager, CommunicationObserverParser observer) {
validation(query, manager, observer);
String command = query.substring(0, 6);
return switch (command) {
case "select" -> select.prepare(query, manager, observer);
case "delete" -> delete.prepare(query, manager, observer);
case "insert" -> insert.prepare(query, manager, observer);
case "update" -> update.prepare(query, manager, observer);
default ->
throw new QueryException(String.format("The command was not recognized at the query %s ", query));
};
}
private void validation(String query, DatabaseManager manager, CommunicationObserverParser observer) {
Objects.requireNonNull(query, "query is required");
Objects.requireNonNull(manager, "manager is required");
Objects.requireNonNull(observer, "manager is observer");
if (query.length() < 6) {
throw new QueryException(String.format("The query %s is invalid", query));
}
}
}