
apoc.dv.JDBCResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apoc Show documentation
Show all versions of apoc Show documentation
A collection of useful Neo4j Procedures
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package apoc.dv;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.MatchResult;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
public class JDBCResource extends VirtualizedResource {
private final String queryParsed;
public JDBCResource(String name, Map config) {
super(
name,
(String) config.get("url"),
(String) config.get("desc"),
(List) config.get("labels"),
(String) config.get("query"),
getParameters(config),
"JDBC");
this.queryParsed = parseQuery(config);
}
private static List getParameters(Map config) {
final String query = (String) config.get("query");
final long questionMarks = countForQuestionMarks(query);
if (questionMarks > 0) {
return LongStream.range(0, questionMarks).mapToObj(i -> "?").collect(Collectors.toList());
}
return PLACEHOLDER_PATTERN
.matcher(query)
.results()
.map(MatchResult::group)
.map(String::trim)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
}
private static String parseQuery(Map config) {
String query = (String) config.get("query");
return parseQuery(query);
}
private static String parseQuery(String query) {
return PLACEHOLDER_PATTERN.matcher(query).replaceAll("?");
}
@Override
public int numOfQueryParams() {
final int countForQuestionMarks = countForQuestionMarks(query);
final int countForMapParameters = countForMapParameters(query);
if (countForQuestionMarks > 0 && countForMapParameters > 0) {
throw new IllegalArgumentException(
"The query is mixing parameters with `$` and `?` please use just one notation");
}
return countForQuestionMarks > 0 ? countForQuestionMarks : countForMapParameters;
}
private static int countForQuestionMarks(String query) {
if (StringUtils.isBlank(query)) {
return 0;
}
return (int) Stream.of(query.split("[ (),]"))
.filter(Objects::nonNull)
.map(String::trim)
.filter(s -> "?".equals(s))
.count();
}
private static int countForMapParameters(String query) {
if (StringUtils.isBlank(query)) {
return 0;
}
return (int) PLACEHOLDER_PATTERN.matcher(query).results().count();
}
@Override
protected Map getProcedureParameters(Object queryParams, Map config) {
final long countForQuestionMarks = countForQuestionMarks(query);
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy