Skip to content

Commit

Permalink
Merge pull request #1624 from gjhiggins/feature-prepareupdate
Browse files Browse the repository at this point in the history
Feature prepareupdate
  • Loading branch information
nicholascar authored Dec 28, 2021
2 parents df07172 + 32e36db commit 75c545e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from . import operators
from . import parserutils

from .processor import prepareQuery, processUpdate
from .processor import prepareQuery, prepareUpdate, processUpdate

assert parser
assert operators
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from rdflib import Literal, Variable, URIRef, BNode

from rdflib.plugins.sparql.sparql import Prologue, Query
from rdflib.plugins.sparql.sparql import Prologue, Query, Update
from rdflib.plugins.sparql.parserutils import CompValue, Expr
from rdflib.plugins.sparql.operators import (
and_,
Expand Down Expand Up @@ -761,7 +761,7 @@ def translateUpdate(q, base=None, initNs=None):

res.append(translateUpdate1(u, prologue))

return res
return Update(prologue, res)


def translateQuery(q, base=None, initNs=None):
Expand Down
9 changes: 9 additions & 0 deletions rdflib/plugins/sparql/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def prepareQuery(queryString, initNs={}, base=None) -> Query:
return ret


def prepareUpdate(updateString, initNs={}, base=None):
"""
Parse and translate a SPARQL Update
"""
ret = translateUpdate(parseUpdate(updateString), base, initNs)
ret._original_args = (updateString, initNs, base)
return ret


def processUpdate(graph, updateString, initBindings={}, initNs={}, base=None):
"""
Process a SPARQL Update Request
Expand Down
10 changes: 10 additions & 0 deletions rdflib/plugins/sparql/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,13 @@ class Query:
def __init__(self, prologue, algebra):
self.prologue = prologue
self.algebra = algebra


class Update:
"""
A parsed and translated update
"""

def __init__(self, prologue, algebra):
self.prologue = prologue
self.algebra = algebra
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def evalUpdate(graph, update, initBindings={}):
"""

for u in update:
for u in update.algebra:

initBindings = dict((Variable(k), v) for k, v in initBindings.items())

Expand Down
43 changes: 43 additions & 0 deletions test/test_sparql_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from rdflib.plugins.sparql import prepareUpdate, prepareQuery
from rdflib.namespace import FOAF
from rdflib import (
Graph,
URIRef,
)


def test_prepare_update():

q = prepareUpdate(
"""\
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{ <http://example/book3> dc:title "A new book" ;
dc:creator "A.N.Other" .
} ;
""",
initNs={},
)

g = Graph()
g.update(q, initBindings={})
assert len(g) == 2


def test_prepare_query():

q = prepareQuery(
"SELECT ?name WHERE { ?person foaf:knows/foaf:name ?name . }",
initNs={"foaf": FOAF},
)

g = Graph()
g.parse(
location=os.path.join(os.path.dirname(__file__), "..", "examples", "foaf.n3"),
format="n3",
)

tim = URIRef("http://www.w3.org/People/Berners-Lee/card#i")

assert len(list(g.query(q, initBindings={"person": tim}))) == 50

0 comments on commit 75c545e

Please sign in to comment.