Example #1
0
        private SparqlUpdateCommand TryParseDeleteCommand(SparqlUpdateParserContext context, bool allowData)
        {
            IToken     next       = context.Tokens.Dequeue();
            List <Uri> usings     = new List <Uri>();
            List <Uri> usingNamed = new List <Uri>();

            if (allowData)
            {
                //We are allowed to have an DELETE DATA command here so check for it
                if (next.TokenType == Token.DATA)
                {
                    return(this.TryParseDeleteDataCommand(context));
                }
            }
            else
            {
                if (next.TokenType == Token.DATA)
                {
                    throw ParserHelper.Error("The DATA keyword is not permitted here as this INSERT command forms part of a modification command", next);
                }
            }

            if (next.TokenType == Token.WHERE)
            {
                //Parse the WHERE pattern which serves as both the selection and deletion pattern in this case
                context.Tokens.Dequeue();
                GraphPattern where = this.TryParseModifyTemplate(context);

                //Then return the command
                return(new DeleteCommand(where, where));
            }
            else
            {
                //Get the Modification Template
                GraphPattern deletions = this.TryParseModifyTemplate(context);

                //Then we expect a WHERE keyword
                next = context.Tokens.Dequeue();
                if (next.TokenType == Token.USING)
                {
                    foreach (KeyValuePair <Uri, bool> kvp in this.TryParseUsingStatements(context))
                    {
                        if (kvp.Value)
                        {
                            usingNamed.Add(kvp.Key);
                        }
                        else
                        {
                            usings.Add(kvp.Key);
                        }
                    }
                    next = context.Tokens.Dequeue();
                }
                if (next.TokenType == Token.WHERE)
                {
                    //Now parse the WHERE pattern
                    SparqlQueryParserContext subContext = new SparqlQueryParserContext(context.Tokens);
                    subContext.Query.BaseUri                        = context.BaseUri;
                    subContext.Query.NamespaceMap                   = context.NamespaceMap;
                    subContext.ExpressionParser.NamespaceMap        = context.NamespaceMap;
                    subContext.ExpressionParser.ExpressionFactories = context.ExpressionFactories;
                    subContext.ExpressionFactories                  = context.ExpressionFactories;
                    GraphPattern where = context.QueryParser.TryParseGraphPattern(subContext, context.Tokens.LastTokenType != Token.LEFTCURLYBRACKET);

                    //And finally return the command
                    DeleteCommand cmd = new DeleteCommand(deletions, where);
                    usings.ForEach(u => cmd.AddUsingUri(u));
                    usingNamed.ForEach(u => cmd.AddUsingNamedUri(u));
                    return(cmd);
                }
                else if (next.TokenType == Token.INSERT)
                {
                    InsertCommand insertCmd = (InsertCommand)this.TryParseInsertCommand(context, false);
                    ModifyCommand cmd       = new ModifyCommand(deletions, insertCmd.InsertPattern, insertCmd.WherePattern);
                    insertCmd.UsingUris.ToList().ForEach(u => cmd.AddUsingUri(u));
                    insertCmd.UsingNamedUris.ToList().ForEach(u => cmd.AddUsingNamedUri(u));
                    return(cmd);
                }
                else
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a WHERE keyword as part of a DELETE command", next);
                }
            }
        }