Repository: marmotta Updated Branches: refs/heads/develop 1420a56d6 -> 174ad5abd
add missing substring function (accidently forgotten) Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/174ad5ab Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/174ad5ab Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/174ad5ab Branch: refs/heads/develop Commit: 174ad5abd80af45195887402154748459c9062ce Parents: 1420a56 Author: Sebastian Schaffert <[email protected]> Authored: Wed Oct 15 14:45:04 2014 +0200 Committer: Sebastian Schaffert <[email protected]> Committed: Wed Oct 15 14:45:04 2014 +0200 ---------------------------------------------------------------------- .../kiwi/sparql/function/string/NSubstring.java | 123 +++++++++++++++++++ ...marmotta.kiwi.sparql.function.NativeFunction | 3 +- 2 files changed, 125 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/174ad5ab/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/string/NSubstring.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/string/NSubstring.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/string/NSubstring.java new file mode 100644 index 0000000..afdddd9 --- /dev/null +++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/string/NSubstring.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.marmotta.kiwi.sparql.function.string; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.h2.H2Dialect; +import org.apache.marmotta.kiwi.persistence.mysql.MySQLDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.OPTypes; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.openrdf.query.algebra.evaluation.function.string.Substring; + +/** + * Add file description here! + * + * @author Sebastian Schaffert ([email protected]) + */ +public class NSubstring extends Substring implements NativeFunction { + + /** + * Return true if this function has available native support for the given dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect || dialect instanceof H2Dialect || dialect instanceof MySQLDialect; + } + + + /** + * Return a string representing how this function is translated into SQL in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if(args.length == 2) { + return String.format("substring(%1$s from %2$s)", args[0], args[1]); + } else if(args.length == 3) { + return String.format("substring(%1$s from %2$s for %3$s)", args[0], args[1], args[2]); + } + } else if (dialect instanceof H2Dialect) { + if(args.length == 2) { + return String.format("substring(%1$s,%2$s)", args[0], args[1]); + } else if(args.length == 3) { + return String.format("substring(%1$s,%2$s,%3$s)", args[0], args[1], args[2]); + } + } else if(dialect instanceof MySQLDialect) { + if(args.length == 2) { + return String.format("substring(%1$s,%2$s)", args[0], args[1]); + } else if(args.length == 3) { + return String.format("substring(%1$s,%2$s,%3$s)", args[0], args[1], args[2]); + } + } + throw new UnsupportedOperationException("SUBSTRING-AFTER not supported in dialect "+dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting inside KiWi. + * + * @return + */ + @Override + public OPTypes getReturnType() { + return OPTypes.STRING; + } + + /** + * Get the argument type of the function for the arg'th argument (starting to count at 0). + * This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public OPTypes getArgumentType(int arg) { + if(arg == 0) { + return OPTypes.STRING; + } else { + return OPTypes.INT; + } + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 3; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/174ad5ab/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction index 2a1d428..7fd19fb 100644 --- a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction +++ b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction @@ -60,4 +60,5 @@ org.apache.marmotta.kiwi.sparql.function.string.NStrBefore org.apache.marmotta.kiwi.sparql.function.string.NStrEnds org.apache.marmotta.kiwi.sparql.function.string.NStrLen org.apache.marmotta.kiwi.sparql.function.string.NStrStarts -org.apache.marmotta.kiwi.sparql.function.string.NUpperCase \ No newline at end of file +org.apache.marmotta.kiwi.sparql.function.string.NUpperCase +org.apache.marmotta.kiwi.sparql.function.string.NSubstring \ No newline at end of file
