[ 
https://issues.apache.org/jira/browse/RYA-466?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16374709#comment-16374709
 ] 

ASF GitHub Bot commented on RYA-466:
------------------------------------

Github user isper3at commented on a diff in the pull request:

    https://github.com/apache/incubator-rya/pull/275#discussion_r170314969
  
    --- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/utils/QueryInvestigator.java ---
    @@ -0,0 +1,104 @@
    +/**
    + * 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.rya.api.utils;
    +
    +import static java.util.Objects.requireNonNull;
    +
    +import java.util.regex.Pattern;
    +
    +import org.openrdf.query.MalformedQueryException;
    +import org.openrdf.query.parser.sparql.SPARQLParser;
    +
    +import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
    +import edu.umd.cs.findbugs.annotations.NonNull;
    +
    +/**
    + * A utility class that is used to glean insight into the structure of 
SPARQL queries.
    + */
    +@DefaultAnnotation(NonNull.class)
    +public class QueryInvestigator {
    +
    +    private static final SPARQLParser PARSER = new SPARQLParser();
    +
    +    private QueryInvestigator() { }
    +
    +    /**
    +     * Determines whether a SPARQL command is a CONSTRUCT or not.
    +     *
    +     * @param sparql - The SPARQL to evaluate. (not null)
    +     * @return {@code true} if the provided SPARQL is a CONSTRUCT query; 
otherwise {@code false}.
    +     * @throws MalformedQueryException The SPARQL is neither a well formed 
query or update.
    +     */
    +    public static boolean isConstruct(final String sparql) throws 
MalformedQueryException {
    +        requireNonNull(sparql);
    +
    +        try {
    +            // Constructs are queries, so try to create a ParsedQuery.
    +            PARSER.parseQuery(sparql, null);
    +
    +            // Check to see if the SPARQL looks like a CONSTRUCT query.
    +            return Pattern.matches(".*?construct.*?where.*", 
sparql.toLowerCase());
    +
    +        } catch(final MalformedQueryException queryE) {
    +            try {
    +                // Maybe it's an update.
    +                PARSER.parseUpdate(sparql, null);
    +
    +                // It was, so return false.
    +                return false;
    +
    +            } catch(final MalformedQueryException updateE) {
    +                // It's not. Actually malformed.
    +                throw queryE;
    +            }
    +        }
    +    }
    +
    +    /**
    +     * Determines whether a SPARQL command is an INSERT with a WHERE 
clause or not.
    +     *
    +     * @param sparql - The SPARQL to evaluate. (not null)
    +     * @return {@code true} if the provided SPARQL is an INSERT update; 
otherwise {@code false}.
    +     * @throws MalformedQueryException The SPARQL is neither a well formed 
query or update.
    +     */
    +    public static boolean isInsertWhere(final String sparql) throws 
MalformedQueryException {
    +        requireNonNull(sparql);
    +
    +        try {
    +            // Inserts are updated, so try to create a ParsedUpdate.
    +            PARSER.parseUpdate(sparql, null);
    +
    +            // Check to see if the SPARQL looks like an INSERT query.
    +            return Pattern.matches(".*?insert.*?where.*", 
sparql.toLowerCase());
    --- End diff --
    
    odd thought, what if the query is something like INSERT { a ?where b }


> Rya Streams Client - Stream results to file.
> --------------------------------------------
>
>                 Key: RYA-466
>                 URL: https://issues.apache.org/jira/browse/RYA-466
>             Project: Rya
>          Issue Type: Improvement
>            Reporter: Kevin Chilton
>            Assignee: Kevin Chilton
>            Priority: Major
>
> The Rya Streams Client's stream results command needs to be updated to allow 
> a user to stream Statement results to an NTriples file and Binding Sets to 
> the standard JSON representation.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to