Skip to contentMethod: validateQueryParams(String)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.query;
19:
20: import cz.cvut.kbss.ontodriver.ResultSet;
21: import cz.cvut.kbss.ontodriver.Statement;
22: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
23: import cz.cvut.kbss.ontodriver.rdf4j.connector.StatementExecutor;
24: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
25: import org.eclipse.rdf4j.query.MalformedQueryException;
26: import org.eclipse.rdf4j.query.QueryEvaluationException;
27: import org.eclipse.rdf4j.query.QueryLanguage;
28: import org.eclipse.rdf4j.query.TupleQueryResult;
29: import org.eclipse.rdf4j.query.parser.ParsedBooleanQuery;
30: import org.eclipse.rdf4j.query.parser.QueryParserUtil;
31:
32: import java.util.Objects;
33:
34: public class Rdf4jStatement implements Statement {
35:
36: private StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
37: private boolean inferenceDisabled = false;
38: private final StatementExecutor queryExecutor;
39: private ResultSet resultSet;
40:
41: private boolean open;
42:
43: public Rdf4jStatement(StatementExecutor queryExecutor) {
44: this.queryExecutor = queryExecutor;
45: this.open = true;
46: }
47:
48: @Override
49: public ResultSet executeQuery(String sparql) throws OntoDriverException {
50: ensureOpen();
51: validateQueryParams(sparql);
52: closeCurrentResultSet();
53: this.resultSet = determineResult(sparql);
54: return resultSet;
55: }
56:
57: private ResultSet determineResult(String sparql) throws Rdf4jDriverException {
58: if (isAskQuery(sparql)) {
59: return new AskResultSet(queryExecutor.executeBooleanQuery(querySpec(sparql)), this);
60: } else {
61: final TupleQueryResult tqr = queryExecutor.executeSelectQuery(querySpec(sparql));
62: try {
63: return new SelectResultSet(tqr, this);
64: } catch (QueryEvaluationException e) {
65: throw new Rdf4jDriverException(e);
66: }
67: }
68: }
69:
70: QuerySpecification querySpec(String sparql) {
71: return QuerySpecification.query(sparql).includeInference(!inferenceDisabled);
72: }
73:
74: private static boolean isAskQuery(String query) throws Rdf4jDriverException {
75: try {
76: return QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, null) instanceof ParsedBooleanQuery;
77: } catch (MalformedQueryException e) {
78: throw new Rdf4jDriverException("Invalid query \"" + query + "\".", e);
79: }
80: }
81:
82: @Override
83: public void executeUpdate(String sparql) throws OntoDriverException {
84: ensureOpen();
85: validateQueryParams(sparql);
86: closeCurrentResultSet();
87: queryExecutor.executeUpdate(querySpec(sparql));
88: }
89:
90: @Override
91: public void useOntology(StatementOntology ontology) {
92: this.targetOntology = ontology;
93: }
94:
95: @Override
96: public StatementOntology getStatementOntology() {
97: return targetOntology;
98: }
99:
100: private static void validateQueryParams(String sparql) {
101: Objects.requireNonNull(sparql);
102:• if (sparql.isEmpty()) {
103: throw new IllegalArgumentException("Query string cannot be empty.");
104: }
105: }
106:
107: @Override
108: public boolean isOpen() {
109: return open;
110: }
111:
112: @Override
113: public void close() throws OntoDriverException {
114: if (!open) {
115: return;
116: }
117: this.open = false;
118: closeCurrentResultSet();
119: }
120:
121: private void closeCurrentResultSet() throws OntoDriverException {
122: if (resultSet != null) {
123: resultSet.close();
124: this.resultSet = null;
125: }
126: }
127:
128: public void setUseTransactionalOntology() {
129: ensureOpen();
130: this.targetOntology = StatementOntology.TRANSACTIONAL;
131: }
132:
133: public boolean useTransactionalOntology() {
134: ensureOpen();
135: return targetOntology == StatementOntology.TRANSACTIONAL;
136: }
137:
138: public void setUseBackupOntology() {
139: ensureOpen();
140: this.targetOntology = StatementOntology.SHARED;
141: }
142:
143: public boolean useBackupOntology() {
144: ensureOpen();
145: return targetOntology == StatementOntology.SHARED;
146: }
147:
148: @Override
149: public void disableInference() {
150: this.inferenceDisabled = true;
151: }
152:
153: @Override
154: public boolean isInferenceDisabled() {
155: return inferenceDisabled;
156: }
157:
158: void ensureOpen() {
159: if (!open) {
160: throw new IllegalStateException("This statement is closed.");
161: }
162: }
163: }