Skip to content

Method: SoqlFunctionTranslator()

1: /*
2: * JOPA
3: * Copyright (C) 2023 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.jopa.query.soql;
19:
20: import cz.cvut.kbss.jopa.exception.QueryParserException;
21:
22: import java.util.Locale;
23: import java.util.Map;
24:
25: /**
26: * Translates SOQL functions to SPARQL functions.
27: */
28: class SoqlFunctionTranslator {
29:
30: private static final Map<String, String> FUNCTION_MAP = Map.of(
31: SoqlConstants.Functions.UPPER, "UCASE",
32: SoqlConstants.Functions.LOWER, "LCASE",
33: SoqlConstants.Functions.LENGTH, "STRLEN",
34: SoqlConstants.Functions.ABS, "ABS",
35: SoqlConstants.Functions.CEIL, "CEIL",
36: SoqlConstants.Functions.FLOOR, "FLOOR",
37: SoqlConstants.Functions.LANG, "lang"
38: );
39:
40: private SoqlFunctionTranslator() {
41: throw new AssertionError();
42: }
43:
44: /**
45: * Gets a SPARQL function equivalent to the specified SOQL function.
46: *
47: * @param soqlFunction SOQL function name
48: * @return Matching SPARQL function name
49: * @throws QueryParserException If the specified function has no SPARQL equivalent here
50: */
51: static String getSparqlFunction(String soqlFunction) {
52: final String fName = soqlFunction.toUpperCase(Locale.ROOT);
53: if (!FUNCTION_MAP.containsKey(fName)) {
54: throw new QueryParserException("Unsupported SOQL function '" + soqlFunction + "'.");
55: }
56: return FUNCTION_MAP.get(fName);
57: }
58: }