Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature prepareupdate #1624

Merged
merged 3 commits into from Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
nicholascar marked this conversation as resolved.
Show resolved Hide resolved


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