http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiDialect.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiDialect.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiDialect.java index 05670bb..470c2d0 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiDialect.java +++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiDialect.java @@ -1,4 +1,4 @@ -/* +/** * 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 @@ -32,12 +32,13 @@ import java.util.Set; * method should return a PreparedStatement that can be executed on the respective JDBC connection * <p/> * Author: Sebastian Schaffert + * Version 5 is implemented in MARMOTTA 584 */ public abstract class KiWiDialect { private static Logger log = LoggerFactory.getLogger(KiWiDialect.class); - public final static int VERSION = 4; + public final static int VERSION = 5; private Properties statements; @@ -64,21 +65,21 @@ public abstract class KiWiDialect { /** * Return the name of the driver class (used for properly initialising JDBC connections) - * @return the fully-qualified class name of the JDBC driver. + * @return */ public abstract String getDriverClass(); /** * Return true if batched commits are supported by this dialect. - * @return {@code true} if batch commits are supported + * @return */ public abstract boolean isBatchSupported(); /** * Return true in case the database supports creating arrays with ARRAY[...] - * @return {@code true} if {@code ARRAY[]} is supported + * @return */ public boolean isArraySupported() { return false; @@ -86,7 +87,7 @@ public abstract class KiWiDialect { /** * Return the contents of the SQL create script used for initialising an empty database - * @return SQL Command String to create the KiWi Database Tables/Indexes/... + * @return */ public String getCreateScript(String scriptName) { return getScript("create_"+scriptName+"_tables.sql"); @@ -95,7 +96,7 @@ public abstract class KiWiDialect { /** * Return the contents of the SQL drop script used for cleaning up all traces of the KiWi triple store - * @return SQL Command String to drop the KiWi Database Tables/Indexes/... + * @return */ public String getDropScript(String scriptName) { return getScript("drop_"+scriptName+"_tables.sql"); @@ -104,7 +105,7 @@ public abstract class KiWiDialect { /** * Return the contents of the SQL script with the given file name (relative to the current class) - * @return SQL Command String loaded from the script in the classpath + * @return */ protected String getScript(String scriptName) { try { @@ -173,10 +174,10 @@ public abstract class KiWiDialect { * Return the database specific operator for matching a text against a regular expression. * * - * @param text the text to match against - * @param pattern the regex-pattern to look for - * @param flags regex flags, such as {@code i} of case-insensitiv - * @return SQL Operator String for a regex test. + * @param text + * @param pattern + * @param flags + * @return */ public abstract String getRegexp(String text, String pattern, String flags); @@ -184,24 +185,24 @@ public abstract class KiWiDialect { /** * Return true in case the SPARQL RE flags contained in the given string are supported. * - * @param flags the regex flags to test for - * @return {@code true} if the provided regex flags are supported + * @param flags + * @return */ public abstract boolean isRegexpSupported(String flags); /** * Return the database specific case insensitive like comparison, e.g. ILIKE in Postgres. * - * @param text the text to match against - * @param pattern the LIKE pattern to match - * @return SQL Operator String for a case-insensitive LIKE + * @param text + * @param pattern + * @return */ public abstract String getILike(String text, String pattern); /** * Return the name of the aggregate function for group concatenation (string_agg in postgres, GROUP_CONCAT in MySQL) - * @return SQL Operator String for group concatenation + * @return */ public abstract String getGroupConcat(String value, String separator, boolean distinct); @@ -209,24 +210,26 @@ public abstract class KiWiDialect { /** * Return the SQL timezone value for a KiWiDateLiteral, corrected by the timezone offset. In PostgreSQL, this is * e.g. computed by (ALIAS.tvalue + ALIAS.tzoffset * INTERVAL '1 second') - * @param alias the alias to reslove - * @return SQL Operator String to convert a DateLiteral to it's timezone specific value + * @param alias + * @return */ public abstract String getDateTimeTZ(String alias); /** * Get the query string that can be used for validating that a JDBC connection to this database is still valid. * Typically, this should be an inexpensive operation like "SELECT 1", - * @return SQL Validation Query String + * @return */ public abstract String getValidationQuery(); /** * Return true in case the database system supports using cursors for queries over large data tables. - * @return {@code true} if cursors are supported + * @return */ public boolean isCursorSupported() { return false; } + + }
http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java index 42dd4ed..393a268 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java +++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java @@ -1,19 +1,18 @@ -/* - * 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 +/** + * 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 + * 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. + * 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.sail; @@ -48,7 +47,6 @@ public class KiWiValueFactory implements ValueFactory { private Random anonIdGenerator; - private KiWiStore store; private KiWiTripleRegistry registry; @@ -61,22 +59,22 @@ public class KiWiValueFactory implements ValueFactory { switch (store.getPersistence().getConfiguration().getRegistryStrategy()) { case DATABASE: log.info("KiWi Value Factory: database registry"); - registry = new DBTripleRegistry(store); + registry = new DBTripleRegistry(store); break; case CACHE: log.info("KiWi Value Factory: cache registry"); - registry = new CacheTripleRegistry(store.getPersistence().getCacheManager()); + registry = new CacheTripleRegistry(store.getPersistence().getCacheManager()); break; case LOCAL: log.info("KiWi Value Factory: in-memory registry"); - registry = new LocalTripleRegistry(); + registry = new LocalTripleRegistry(); break; default: log.info("KiWi Value Factory: in-memory registry"); - registry = new LocalTripleRegistry(); + registry = new LocalTripleRegistry(); } - this.store = store; + this.store = store; this.defaultContext = defaultContext; } @@ -85,7 +83,7 @@ public class KiWiValueFactory implements ValueFactory { KiWiConnection connection = store.getPersistence().getConnection(); connection.setAutoCommit(true); return connection; - } catch(SQLException ex) { + } catch (SQLException ex) { log.error("could not acquire database connection", ex); throw new RuntimeException(ex); } @@ -107,7 +105,7 @@ public class KiWiValueFactory implements ValueFactory { */ @Override public BNode createBNode() { - return createBNode(Long.toHexString(System.currentTimeMillis())+Integer.toHexString(anonIdGenerator.nextInt(1000))); + return createBNode(Long.toHexString(System.currentTimeMillis()) + Integer.toHexString(anonIdGenerator.nextInt(1000))); } /** @@ -124,20 +122,20 @@ public class KiWiValueFactory implements ValueFactory { // is still volatile KiWiUriResource result = connection.loadUriResource(uri); - if(result == null) { + if (result == null) { result = new KiWiUriResource(uri); connection.storeNode(result); } - if(result.getId() < 0) { + if (result.getId() < 0) { log.error("node ID is null!"); } return result; } catch (SQLException e) { - log.error("database error, could not load URI resource",e); - throw new IllegalStateException("database error, could not load URI resource",e); + log.error("database error, could not load URI resource", e); + throw new IllegalStateException("database error, could not load URI resource", e); } finally { releaseConnection(connection); } @@ -145,22 +143,23 @@ public class KiWiValueFactory implements ValueFactory { } /** - * Creates a new URI from the supplied namespace and local name. Calling this - * method is funtionally equivalent to calling + * Creates a new URI from the supplied namespace and local name. Calling + * this method is funtionally equivalent to calling * {@link #createURI(String) createURI(namespace+localName)}, but allows the * ValueFactory to reuse supplied namespace and local name strings whenever - * possible. Note that the values returned by {@link org.openrdf.model.URI#getNamespace()} and - * {@link org.openrdf.model.URI#getLocalName()} are not necessarily the same as the values that - * are supplied to this method. + * possible. Note that the values returned by + * {@link org.openrdf.model.URI#getNamespace()} and + * {@link org.openrdf.model.URI#getLocalName()} are not necessarily the same + * as the values that are supplied to this method. * * @param namespace The URI's namespace. * @param localName The URI's local name. - * @throws IllegalArgumentException If the supplied namespace and localname do not resolve to a legal - * (absolute) URI. + * @throws IllegalArgumentException If the supplied namespace and localname + * do not resolve to a legal (absolute) URI. */ @Override public URI createURI(String namespace, String localName) { - return createURI(namespace+localName); + return createURI(namespace + localName); } /** @@ -177,19 +176,19 @@ public class KiWiValueFactory implements ValueFactory { // is still volatile KiWiAnonResource result = connection.loadAnonResource(nodeID); - if(result == null) { + if (result == null) { result = new KiWiAnonResource(nodeID); connection.storeNode(result); } - if(result.getId() < 0) { + if (result.getId() < 0) { log.error("node ID is null!"); } return result; } catch (SQLException e) { - log.error("database error, could not load anonymous resource",e); - throw new IllegalStateException("database error, could not load anonymous resource",e); + log.error("database error, could not load anonymous resource", e); + throw new IllegalStateException("database error, could not load anonymous resource", e); } finally { releaseConnection(connection); } @@ -207,13 +206,14 @@ public class KiWiValueFactory implements ValueFactory { } /** - * Creates a typed {@link org.openrdf.model.Literal} out of the supplied object, mapping the - * runtime type of the object to the appropriate XML Schema type. If no - * mapping is available, the method returns a literal with the string - * representation of the supplied object as the value, and - * {@link org.openrdf.model.vocabulary.XMLSchema#STRING} as the datatype. Recognized types are - * {@link Boolean}, {@link Byte}, {@link Double}, {@link Float}, - * {@link Integer}, {@link Long}, {@link Short}, {@link javax.xml.datatype.XMLGregorianCalendar } + * Creates a typed {@link org.openrdf.model.Literal} out of the supplied + * object, mapping the runtime type of the object to the appropriate XML + * Schema type. If no mapping is available, the method returns a literal + * with the string representation of the supplied object as the value, and + * {@link org.openrdf.model.vocabulary.XMLSchema#STRING} as the datatype. + * Recognized types are {@link Boolean}, {@link Byte}, {@link Double}, {@link Float}, + * {@link Integer}, {@link Long}, {@link Short}, {@link javax.xml.datatype.XMLGregorianCalendar + * } * , and {@link java.util.Date}. * * @param object an object to be converted to a typed literal. @@ -221,10 +221,10 @@ public class KiWiValueFactory implements ValueFactory { * @since 2.7.0 */ public Literal createLiteral(Object object) { - if(object instanceof XMLGregorianCalendar) { - return createLiteral((XMLGregorianCalendar)object); + if (object instanceof XMLGregorianCalendar) { + return createLiteral((XMLGregorianCalendar) object); } else { - return createLiteral(object,null,LiteralCommons.getXSDType(object.getClass())); + return createLiteral(object, null, LiteralCommons.getXSDType(object.getClass())); } } @@ -243,9 +243,9 @@ public class KiWiValueFactory implements ValueFactory { /** * Creates a new literal with the supplied label and language attribute. * - * @param label The literal's label. - * @param language The literal's language attribute, or <tt>null</tt> if the literal - * doesn't have a language. + * @param label The literal's label. + * @param language The literal's language attribute, or <tt>null</tt> if the + * literal doesn't have a language. */ @Override public Literal createLiteral(String label, String language) { @@ -257,19 +257,19 @@ public class KiWiValueFactory implements ValueFactory { /** * Creates a new literal with the supplied label and datatype. * - * @param label The literal's label. - * @param datatype The literal's datatype, or <tt>null</tt> if the literal doesn't - * have a datatype. + * @param label The literal's label. + * @param datatype The literal's datatype, or <tt>null</tt> if the literal + * doesn't have a datatype. */ @Override public Literal createLiteral(String label, URI datatype) { - return createLiteral(label,null,datatype.stringValue()); + return createLiteral(label, null, datatype.stringValue()); } - /** - * Internal createLiteral method for different datatypes. This method distinguishes based on the Java class - * type and the type argument passed as argument how to load and possibly create the new literal. + * Internal createLiteral method for different datatypes. This method + * distinguishes based on the Java class type and the type argument passed + * as argument how to load and possibly create the new literal. * * @param value * @param lang @@ -279,7 +279,7 @@ public class KiWiValueFactory implements ValueFactory { */ private <T> KiWiLiteral createLiteral(T value, String lang, String type) { Locale locale; - if(lang != null) { + if (lang != null) { try { Locale.Builder builder = new Locale.Builder(); builder.setLanguageTag(lang); @@ -295,7 +295,7 @@ public class KiWiValueFactory implements ValueFactory { KiWiLiteral result; - final KiWiUriResource rtype = type==null ? null: (KiWiUriResource)createURI(type); + final KiWiUriResource rtype = type == null ? null : (KiWiUriResource) createURI(type); final KiWiConnection connection = aqcuireConnection(); try { @@ -306,15 +306,15 @@ public class KiWiValueFactory implements ValueFactory { // FIXME: MARMOTTA-39 (this is to avoid a NullPointerException in the following if-clauses) result = connection.loadLiteral(value.toString(), lang, rtype); - if(result == null) { + if (result == null) { result = new KiWiStringLiteral(value.toString(), locale, rtype); } - } else if(value instanceof Date || value instanceof DateTime ||type.equals(Namespaces.NS_XSD+"dateTime") || type.equals(Namespaces.NS_XSD+"date") || type.equals(Namespaces.NS_XSD+"time")) { + } else if (value instanceof Date || value instanceof DateTime || type.equals(Namespaces.NS_XSD + "dateTime") || type.equals(Namespaces.NS_XSD + "date") || type.equals(Namespaces.NS_XSD + "time")) { // parse if necessary final DateTime dvalue; - if(value instanceof DateTime) { + if (value instanceof DateTime) { dvalue = ((DateTime) value).withMillisOfDay(0); - } else if(value instanceof Date || value instanceof Calendar) { + } else if (value instanceof Date || value instanceof Calendar) { dvalue = new DateTime(value); } else { dvalue = ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(value.toString()).withMillisOfSecond(0); @@ -322,91 +322,94 @@ public class KiWiValueFactory implements ValueFactory { result = connection.loadLiteral(dvalue); - if(result == null) { - result= new KiWiDateLiteral(dvalue, rtype); + if (result == null) { + result = new KiWiDateLiteral(dvalue, rtype); } - } else if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass()) || - Long.class.equals(value.getClass()) || long.class.equals(value.getClass()) || - type.equals(Namespaces.NS_XSD+"integer") || type.equals(Namespaces.NS_XSD+"long")) { + } else if (Integer.class.equals(value.getClass()) || int.class.equals(value.getClass()) + || Long.class.equals(value.getClass()) || long.class.equals(value.getClass()) + || type.equals(Namespaces.NS_XSD + "integer") || type.equals(Namespaces.NS_XSD + "long")) { long ivalue = 0; - if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) { - ivalue = (Integer)value; - } else if(Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) { - ivalue = (Long)value; + if (Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) { + ivalue = (Integer) value; + } else if (Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) { + ivalue = (Long) value; } else { ivalue = Long.parseLong(value.toString()); } - result = connection.loadLiteral(ivalue); - if(result == null) { - result= new KiWiIntLiteral(ivalue, rtype); + if (result == null) { + result = new KiWiIntLiteral(ivalue, rtype); } - } else if(Double.class.equals(value.getClass()) || double.class.equals(value.getClass()) || - Float.class.equals(value.getClass()) || float.class.equals(value.getClass()) || - type.equals(Namespaces.NS_XSD+"double") || type.equals(Namespaces.NS_XSD+"float") || - type.equals(Namespaces.NS_XSD+"decimal")) { + } else if (Double.class.equals(value.getClass()) || double.class.equals(value.getClass()) + || Float.class.equals(value.getClass()) || float.class.equals(value.getClass()) + || type.equals(Namespaces.NS_XSD + "double") || type.equals(Namespaces.NS_XSD + "float") + || type.equals(Namespaces.NS_XSD + "decimal")) { double dvalue = 0.0; - if(Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) { - dvalue = (Float)value; - } else if(Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) { - dvalue = (Double)value; + if (Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) { + dvalue = (Float) value; + } else if (Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) { + dvalue = (Double) value; } else { dvalue = Double.parseDouble(value.toString()); } - result = connection.loadLiteral(dvalue); - if(result == null) { - result= new KiWiDoubleLiteral(dvalue, rtype); + if (result == null) { + result = new KiWiDoubleLiteral(dvalue, rtype); } - } else if(Boolean.class.equals(value.getClass()) || boolean.class.equals(value.getClass()) || - type.equals(Namespaces.NS_XSD+"boolean")) { + } else if (Boolean.class.equals(value.getClass()) || boolean.class.equals(value.getClass()) + || type.equals(Namespaces.NS_XSD + "boolean")) { boolean bvalue = false; - if(Boolean.class.equals(value.getClass()) || boolean.class.equals(value.getClass())) { - bvalue = (Boolean)value; + if (Boolean.class.equals(value.getClass()) || boolean.class.equals(value.getClass())) { + bvalue = (Boolean) value; } else { bvalue = Boolean.parseBoolean(value.toString()); } - result = connection.loadLiteral(bvalue); - if(result == null) { - result= new KiWiBooleanLiteral(bvalue, rtype); + if (result == null) { + result = new KiWiBooleanLiteral(bvalue, rtype); + } + } else if (isGeometry(value.toString()) || type.equals(Namespaces.NS_GEO + "wktLiteral")) { + boolean isGeo = true; + result = connection.loadLiteral(isGeo, value.toString(), lang, rtype); + + if (result == null) { + result = new KiWiGeometryLiteral(value.toString(), locale, rtype); } } else { result = connection.loadLiteral(value.toString(), lang, rtype); - if(result == null) { + if (result == null) { result = new KiWiStringLiteral(value.toString(), locale, rtype); } } - } catch(IllegalArgumentException ex) { + } catch (IllegalArgumentException ex) { // malformed number or date log.warn("malformed argument for typed literal of type {}: {}", rtype.stringValue(), value); - KiWiUriResource mytype = (KiWiUriResource)createURI(Namespaces.NS_XSD+"string"); + KiWiUriResource mytype = (KiWiUriResource) createURI(Namespaces.NS_XSD + "string"); result = connection.loadLiteral(value.toString(), lang, mytype); - if(result == null) { + if (result == null) { result = new KiWiStringLiteral(value.toString(), locale, mytype); } } - if(result.getId() < 0) { + if (result.getId() < 0) { connection.storeNode(result); } return result; - } catch (SQLException e) { - log.error("database error, could not load literal",e); - throw new IllegalStateException("database error, could not load literal",e); + log.error("database error, could not load literal", e); + throw new IllegalStateException("database error, could not load literal", e); } finally { releaseConnection(connection); } @@ -421,31 +424,31 @@ public class KiWiValueFactory implements ValueFactory { */ @Override public Literal createLiteral(boolean value) { - return createLiteral(value,null,LiteralCommons.getXSDType(Boolean.class)); + return createLiteral(value, null, LiteralCommons.getXSDType(Boolean.class)); } /** - * Creates a new <tt>xsd:byte</tt>-typed literal representing the - * specified value. + * Creates a new <tt>xsd:byte</tt>-typed literal representing the specified + * value. * * @param value The value for the literal. * @return An <tt>xsd:byte</tt>-typed literal for the specified value. */ @Override public Literal createLiteral(byte value) { - return createLiteral((int)value,null,LiteralCommons.getXSDType(Byte.class)); + return createLiteral((int) value, null, LiteralCommons.getXSDType(Byte.class)); } /** - * Creates a new <tt>xsd:short</tt>-typed literal representing the - * specified value. + * Creates a new <tt>xsd:short</tt>-typed literal representing the specified + * value. * * @param value The value for the literal. * @return An <tt>xsd:short</tt>-typed literal for the specified value. */ @Override public Literal createLiteral(short value) { - return createLiteral((int)value,null,LiteralCommons.getXSDType(Short.class)); + return createLiteral((int) value, null, LiteralCommons.getXSDType(Short.class)); } /** @@ -457,31 +460,31 @@ public class KiWiValueFactory implements ValueFactory { */ @Override public Literal createLiteral(int value) { - return createLiteral(value,null,LiteralCommons.getXSDType(Integer.class)); + return createLiteral(value, null, LiteralCommons.getXSDType(Integer.class)); } /** - * Creates a new <tt>xsd:long</tt>-typed literal representing the - * specified value. + * Creates a new <tt>xsd:long</tt>-typed literal representing the specified + * value. * * @param value The value for the literal. * @return An <tt>xsd:long</tt>-typed literal for the specified value. */ @Override public Literal createLiteral(long value) { - return createLiteral(value,null,LiteralCommons.getXSDType(Long.class)); + return createLiteral(value, null, LiteralCommons.getXSDType(Long.class)); } /** - * Creates a new <tt>xsd:float</tt>-typed literal representing the - * specified value. + * Creates a new <tt>xsd:float</tt>-typed literal representing the specified + * value. * * @param value The value for the literal. * @return An <tt>xsd:float</tt>-typed literal for the specified value. */ @Override public Literal createLiteral(float value) { - return createLiteral(value,null,LiteralCommons.getXSDType(Float.class)); + return createLiteral(value, null, LiteralCommons.getXSDType(Float.class)); } /** @@ -493,7 +496,7 @@ public class KiWiValueFactory implements ValueFactory { */ @Override public Literal createLiteral(double value) { - return createLiteral(value,null,LiteralCommons.getXSDType(Double.class)); + return createLiteral(value, null, LiteralCommons.getXSDType(Double.class)); } /** @@ -507,20 +510,20 @@ public class KiWiValueFactory implements ValueFactory { public Literal createLiteral(XMLGregorianCalendar calendar) { Date value = calendar.toGregorianCalendar().getTime(); - return createLiteral(value,null,LiteralCommons.getXSDType(Date.class)); + return createLiteral(value, null, LiteralCommons.getXSDType(Date.class)); } /** * Creates a new statement with the supplied subject, predicate and object. * - * @param subject The statement's subject. + * @param subject The statement's subject. * @param predicate The statement's predicate. - * @param object The statement's object. + * @param object The statement's object. * @return The created statement. */ @Override public Statement createStatement(Resource subject, URI predicate, Value object) { - if(defaultContext != null) { + if (defaultContext != null) { return createStatement(subject, predicate, object, createURI(defaultContext)); } else { return createStatement(subject, predicate, object, null); @@ -531,26 +534,27 @@ public class KiWiValueFactory implements ValueFactory { * Creates a new statement with the supplied subject, predicate and object * and associated context. * - * @param subject The statement's subject. + * @param subject The statement's subject. * @param predicate The statement's predicate. - * @param object The statement's object. - * @param context The statement's context. + * @param object The statement's object. + * @param context The statement's context. * @return The created statement. */ @Override public Statement createStatement(Resource subject, URI predicate, Value object, Resource context) { - return new ContextStatementImpl(subject,predicate,object,context); + return new ContextStatementImpl(subject, predicate, object, context); } /** - * Creates a new statement with the supplied subject, predicate and object and associated context. This is a - * specialised form of createStatement that allows the existance check for a triple to run in the same connection - * as the rest of the repository operations. + * Creates a new statement with the supplied subject, predicate and object + * and associated context. This is a specialised form of createStatement + * that allows the existance check for a triple to run in the same + * connection as the rest of the repository operations. * - * @param subject The statement's subject. + * @param subject The statement's subject. * @param predicate The statement's predicate. - * @param object The statement's object. - * @param context The statement's context. + * @param object The statement's object. + * @param context The statement's context. * @return The created statement. */ public Statement createStatement(Resource subject, URI predicate, Value object, Resource context, KiWiConnection connection) { @@ -558,28 +562,28 @@ public class KiWiValueFactory implements ValueFactory { IntArray cacheKey = IntArray.createSPOCKey(subject, predicate, object, context); - KiWiResource ksubject = convert(subject); + KiWiResource ksubject = convert(subject); KiWiUriResource kpredicate = convert(predicate); - KiWiNode kobject = convert(object); - KiWiResource kcontext = convert(context); + KiWiNode kobject = convert(object); + KiWiResource kcontext = convert(context); - KiWiTriple result = new KiWiTriple(ksubject,kpredicate,kobject,kcontext); + KiWiTriple result = new KiWiTriple(ksubject, kpredicate, kobject, kcontext); synchronized (registry) { long tripleId = registry.lookupKey(cacheKey); - if(tripleId >= 0) { + if (tripleId >= 0) { // try getting id from registry result.setId(tripleId); registry.registerKey(cacheKey, connection.getTransactionId(), result.getId()); } else { // not found in registry, try loading from database - result.setId(connection.getTripleId(ksubject,kpredicate,kobject,kcontext)); + result.setId(connection.getTripleId(ksubject, kpredicate, kobject, kcontext)); } // triple has no id from registry or database, so we create one and flag it for reasoning - if(result.getId() < 0) { + if (result.getId() < 0) { result.setId(connection.getNextSequence()); result.setNewTriple(true); @@ -591,16 +595,18 @@ public class KiWiValueFactory implements ValueFactory { } catch (SQLException e) { log.error("database error, could not load triple", e); - throw new IllegalStateException("database error, could not load triple",e); + throw new IllegalStateException("database error, could not load triple", e); } } /** - * Remove a statement from the triple registry. Called when the statement is deleted and the transaction commits. + * Remove a statement from the triple registry. Called when the statement is + * deleted and the transaction commits. + * * @param triple */ protected void removeStatement(KiWiTriple triple) { - if(triple.getId() >= 0) { + if (triple.getId() >= 0) { IntArray cacheKey = IntArray.createSPOCKey(triple.getSubject(), triple.getPredicate(), triple.getObject(), triple.getContext()); synchronized (registry) { @@ -614,37 +620,46 @@ public class KiWiValueFactory implements ValueFactory { registry.releaseTransaction(connection.getTransactionId()); } - public KiWiResource convert(Resource r) { - return (KiWiResource)convert((Value)r); + return (KiWiResource) convert((Value) r); } public KiWiUriResource convert(URI r) { - return (KiWiUriResource)convert((Value)r); + return (KiWiUriResource) convert((Value) r); } public KiWiNode convert(Value value) { - if(value == null) { + if (value == null) { return null; - } else if(value instanceof KiWiNode) { - return (KiWiNode)value; - } else if(value instanceof URI) { - return (KiWiUriResource)createURI(value.stringValue()); - } else if(value instanceof BNode) { - return (KiWiAnonResource)createBNode(value.stringValue()); - } else if(value instanceof Literal) { - Literal l = (Literal)value; - return createLiteral(l.getLabel(),l.getLanguage(), l.getDatatype() != null ? l.getDatatype().stringValue(): null); + } else if (value instanceof KiWiNode) { + return (KiWiNode) value; + } else if (value instanceof URI) { + return (KiWiUriResource) createURI(value.stringValue()); + } else if (value instanceof BNode) { + return (KiWiAnonResource) createBNode(value.stringValue()); + } else if (value instanceof Literal) { + Literal l = (Literal) value; + return createLiteral(l.getLabel(), l.getLanguage(), l.getDatatype() != null ? l.getDatatype().stringValue() : null); } else { throw new IllegalArgumentException("the value passed as argument does not have the correct type"); } } - public void close() { - } + + /** + * + * @param value + * @return + */ + public boolean isGeometry(String value) { + if (value.contains("POINT(") || value.contains("MULTILINESTRING(") || value.contains("MULTIPOLYGON(")) { + return true; + } + return false; + } } http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/upgrade_base_004_005.sql ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/upgrade_base_004_005.sql b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/upgrade_base_004_005.sql new file mode 100644 index 0000000..f177498 --- /dev/null +++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/upgrade_base_004_005.sql @@ -0,0 +1,18 @@ +-- 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. + +ALTER TABLE nodes ADD tzoffset int default 0 AFTER tvalue; + +UPDATE METADATA SET mvalue = '3' WHERE mkey = 'version'; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/create_base_tables.sql ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/create_base_tables.sql b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/create_base_tables.sql index 440a56d..55dce09 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/create_base_tables.sql +++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/create_base_tables.sql @@ -81,5 +81,5 @@ CREATE INDEX idx_namespaces_uri ON namespaces(uri); CREATE INDEX idx_namespaces_prefix ON namespaces(prefix); -- insert initial metadata -INSERT INTO metadata(mkey,mvalue) VALUES ('version','4'); +INSERT INTO metadata(mkey,mvalue) VALUES ('version','5'); INSERT INTO metadata(mkey,mvalue) VALUES ('created',DATE_FORMAT(now(),'%Y-%m-%d %H:%i:%s') ); http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/upgrade_base_004_005.sql ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/upgrade_base_004_005.sql b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/upgrade_base_004_005.sql new file mode 100644 index 0000000..95a1b51 --- /dev/null +++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/upgrade_base_004_005.sql @@ -0,0 +1,18 @@ +-- 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. + +ALTER TABLE nodes ADD COLUMN tzoffset int default 0 AFTER tvalue; + +UPDATE METADATA SET mvalue = '3' WHERE mkey = 'version'; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/create_base_tables.sql ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/create_base_tables.sql b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/create_base_tables.sql index bad2d05..9649ca6 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/create_base_tables.sql +++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/create_base_tables.sql @@ -12,7 +12,15 @@ -- 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. -CREATE TYPE nodetype AS ENUM ('uri','bnode','string','int','double','date','boolean'); + +-- MARMOTA 584: GeoSPARQL Support. Alter DB to support GeoSpatial Queries. + +CREATE TYPE nodetype AS ENUM ('uri','bnode','string','int','double','date','boolean','geom'); + + +--necessary for use geometry queries +CREATE EXTENSION postgis; + -- requires super user privileges: -- CREATE EXTENSION IF NOT EXISTS pgcrypto; @@ -29,6 +37,7 @@ CREATE TABLE nodes ( ltype bigint REFERENCES nodes(id), lang varchar(5), createdAt timestamp NOT NULL DEFAULT now(), + gvalue geometry, PRIMARY KEY(id) ); @@ -103,5 +112,5 @@ DO INSTEAD NOTHING; -- a function for cleaning up table rows without incoming references -- insert initial metadata -INSERT INTO metadata(mkey,mvalue) VALUES ('version','4'); +INSERT INTO metadata(mkey,mvalue) VALUES ('version','5'); INSERT INTO metadata(mkey,mvalue) VALUES ('created',to_char(now(),'yyyy-MM-DD HH:mm:ss TZ') ); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties index 99a6ffb..b0ce1e8 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties +++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties @@ -24,21 +24,23 @@ meta.get = SELECT mvalue FROM metadata WHERE mkey = ?; meta.version = SELECT mvalue FROM metadata WHERE mkey = 'version'; # load entities -load.node_by_id = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE id = ? -load.nodes_by_ids = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE id IN(%s) LIMIT %d +load.node_by_id = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE id = ? +load.nodes_by_ids = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE id IN(%s) LIMIT %d -load.uri_by_uri = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE ntype = 'uri' AND svalue = ? +load.uri_by_uri = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE ntype = 'uri' AND svalue = ? -load.bnode_by_anonid = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE ntype = 'bnode' AND svalue = ? +load.bnode_by_anonid = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE ntype = 'bnode' AND svalue = ? -load.literal_by_v = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE ntype = 'string' AND svalue = ? AND (lang IS NULL OR lang = '') AND ltype IS NULL -load.literal_by_vl = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE svalue = ? AND lang = ? -load.literal_by_vt = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE svalue = ? AND ltype = ? +load.literal_by_v = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE ntype = 'string' AND svalue = ? AND (lang IS NULL OR lang = '') AND ltype IS NULL +load.literal_by_vl = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE svalue = ? AND lang = ? +load.literal_by_vt = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE svalue = ? AND ltype = ? -load.literal_by_iv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE ivalue = ? AND (lang IS NULL OR lang = '') AND ltype = ? -load.literal_by_dv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE dvalue = ? AND (lang IS NULL OR lang = '') AND ltype = ? -load.literal_by_tv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE tvalue = ? AND tzoffset = ? AND (lang IS NULL OR lang = '') AND ltype = ? -load.literal_by_bv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt FROM nodes WHERE bvalue = ? AND (lang IS NULL OR lang = '') AND ltype = ? +load.literal_by_iv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE ivalue = ? AND (lang IS NULL OR lang = '') AND ltype = ? +load.literal_by_dv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE dvalue = ? AND (lang IS NULL OR lang = '') AND ltype = ? +load.literal_by_tv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE tvalue = ? AND tzoffset = ? AND (lang IS NULL OR lang = '') AND ltype = ? +load.literal_by_bv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE bvalue = ? AND (lang IS NULL OR lang = '') AND ltype = ? + +load.literal_by_gv = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,tzoffset,bvalue,lang,ltype,createdAt,ST_AsText(gvalue) FROM nodes WHERE gvalue = ? AND ltype = ? load.namespace_prefix = SELECT id,prefix,uri,createdAt FROM namespaces WHERE prefix = ?; load.namespace_uri = SELECT id,prefix,uri,createdAt FROM namespaces WHERE uri = ?; @@ -55,6 +57,8 @@ store.dliteral = INSERT INTO nodes (id,ntype,svalue,dvalue,ltype,createdAt store.bliteral = INSERT INTO nodes (id,ntype,svalue,bvalue,ltype,createdAt) VALUES (?,'boolean',?,?,?,?) store.tliteral = INSERT INTO nodes (id,ntype,svalue,tvalue,tzoffset,ltype,createdAt) VALUES (?,'date',?,?,?,?,?) +store.gliteral = INSERT INTO nodes (id,ntype,svalue,dvalue,ivalue,lang,ltype,createdAt,gvalue) VALUES (?,'geom',?,?,?,?,?,?,ST_GeomFromText(?)) + store.namespace = INSERT INTO namespaces (id,prefix,uri,createdAt) VALUES (?,?,?,?) store.triple = INSERT INTO triples (id,subject,predicate,object,context,inferred,createdAt) VALUES (?,?,?,?,?,?,?) http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/upgrade_base_004_005.sql ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/upgrade_base_004_005.sql b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/upgrade_base_004_005.sql new file mode 100644 index 0000000..8408946 --- /dev/null +++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/upgrade_base_004_005.sql @@ -0,0 +1,25 @@ +-- 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. + +--add geom nodetype +CREATE TYPE nodetype AS ENUM ('uri','bnode','string','int','double','date','boolean','geom'); + +--necessary for use spatial queries +CREATE EXTENSION postgis; + +--adding geometry colum +ALTER TABLE nodes ADD COLUMN gvalue geometry; + +UPDATE METADATA SET mvalue = '5' WHERE mkey = 'version'; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/marmotta/blob/796c598e/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/DialectTest.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/DialectTest.java b/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/DialectTest.java index 357263f..e4d5e6b 100644 --- a/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/DialectTest.java +++ b/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/DialectTest.java @@ -77,7 +77,7 @@ public class DialectTest { Assert.assertNotNull(migrateScript); Assert.assertFalse("".equals(migrateScript)); - String migrateScript2 = dialect.getMigrationScript(4,"base"); + String migrateScript2 = dialect.getMigrationScript(5,"base"); Assert.assertNotNull(migrateScript2); Assert.assertTrue("".equals(migrateScript2));
