src/rdflib/plugins/sparql/update.py
1"""
2Code for carrying out Update Operations
3"""
4
5from __future__ import annotations
6
7from typing import TYPE_CHECKING, Iterator, Mapping, Optional, Sequence
8
9from rdflib.graph import Graph
10from rdflib.plugins.sparql.evaluate import evalBGP, evalPart
11from rdflib.plugins.sparql.evalutils import _fillTemplate, _join
12from rdflib.plugins.sparql.parserutils import CompValue
13from rdflib.plugins.sparql.sparql import FrozenDict, QueryContext, Update
14from rdflib.term import Identifier, URIRef, Variable
15
16
17def _graphOrDefault(ctx: QueryContext, g: str) -> Optional[Graph]:
18 if g == "DEFAULT":
19 return ctx.graph
20 else:
21 return ctx.dataset.get_context(g)
22
23
24def _graphAll(ctx: QueryContext, g: str) -> Sequence[Graph]:
25 """
26 return a list of graphs
27 """
28 if g == "DEFAULT":
29 # type error: List item 0 has incompatible type "Optional[Graph]"; expected "Graph"
30 return [ctx.graph] # type: ignore[list-item]
31 elif g == "NAMED":
32 return [
33 # type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
34 c
35 for c in ctx.dataset.contexts()
36 if c.identifier != ctx.graph.identifier # type: ignore[union-attr]
37 ]
38 elif g == "ALL":
39 return list(ctx.dataset.contexts())
40 else:
41 return [ctx.dataset.get_context(g)]
42
43
44def evalLoad(ctx: QueryContext, u: CompValue) -> None:
45 """
46 http://www.w3.org/TR/sparql11-update/#load
47 """
48
49 if TYPE_CHECKING:
50 assert isinstance(u.iri, URIRef)
51
52 if u.graphiri:
53 ctx.load(u.iri, default=False, into=u.graphiri)
54 else:
55 ctx.load(u.iri, default=True)
56
57
58def evalCreate(ctx: QueryContext, u: CompValue) -> None:
59 """
60 http://www.w3.org/TR/sparql11-update/#create
61 """
62 g = ctx.dataset.get_context(u.graphiri)
63 if len(g) > 0:
64 raise Exception("Graph %s already exists." % g.identifier)
65 raise Exception("Create not implemented!")
66
67
68def evalClear(ctx: QueryContext, u: CompValue) -> None:
69 """
70 http://www.w3.org/TR/sparql11-update/#clear
71 """
72 for g in _graphAll(ctx, u.graphiri):
73 g.remove((None, None, None))
74
75
76def evalDrop(ctx: QueryContext, u: CompValue) -> None:
77 """
78 http://www.w3.org/TR/sparql11-update/#drop
79 """
80 if ctx.dataset.store.graph_aware:
81 for g in _graphAll(ctx, u.graphiri):
82 ctx.dataset.store.remove_graph(g)
83 else:
84 evalClear(ctx, u)
85
86
87def evalInsertData(ctx: QueryContext, u: CompValue) -> None:
88 """
89 http://www.w3.org/TR/sparql11-update/#insertData
90 """
91 # add triples
92 g = ctx.graph
93 g += u.triples
94 # add quads
95 # u.quads is a dict of graphURI=>[triples]
96 for g in u.quads:
97 # type error: Argument 1 to "get_context" of "ConjunctiveGraph" has incompatible type "Optional[Graph]"; expected "Union[IdentifiedNode, str, None]"
98 cg = ctx.dataset.get_context(g) # type: ignore[arg-type]
99 cg += u.quads[g]
100
101
102def evalDeleteData(ctx: QueryContext, u: CompValue) -> None:
103 """
104 http://www.w3.org/TR/sparql11-update/#deleteData
105 """
106 # remove triples
107 g = ctx.graph
108 g -= u.triples
109
110 # remove quads
111 # u.quads is a dict of graphURI=>[triples]
112 for g in u.quads:
113 # type error: Argument 1 to "get_context" of "ConjunctiveGraph" has incompatible type "Optional[Graph]"; expected "Union[IdentifiedNode, str, None]"
114 cg = ctx.dataset.get_context(g) # type: ignore[arg-type]
115 cg -= u.quads[g]
116
117
118def evalDeleteWhere(ctx: QueryContext, u: CompValue) -> None:
119 """
120 http://www.w3.org/TR/sparql11-update/#deleteWhere
121 """
122
123 res: Iterator[FrozenDict] = evalBGP(ctx, u.triples)
124 for g in u.quads:
125 cg = ctx.dataset.get_context(g)
126 c = ctx.pushGraph(cg)
127 res = _join(res, list(evalBGP(c, u.quads[g])))
128
129 # type error: Incompatible types in assignment (expression has type "FrozenBindings", variable has type "QueryContext")
130 for c in res: # type: ignore[assignment]
131 g = ctx.graph
132 g -= _fillTemplate(u.triples, c)
133
134 for g in u.quads:
135 cg = ctx.dataset.get_context(c.get(g))
136 cg -= _fillTemplate(u.quads[g], c)
137
138
139def evalModify(ctx: QueryContext, u: CompValue) -> None:
140 originalctx = ctx
141
142 # Using replaces the dataset for evaluating the where-clause
143 dg: Optional[Graph]
144 if u.using:
145 otherDefault = False
146 for d in u.using:
147 if d.default:
148 if not otherDefault:
149 # replace current default graph
150 dg = Graph()
151 ctx = ctx.pushGraph(dg)
152 otherDefault = True
153
154 ctx.load(d.default, default=True)
155
156 elif d.named:
157 g = d.named
158 ctx.load(g, default=False)
159
160 # "The WITH clause provides a convenience for when an operation
161 # primarily refers to a single graph. If a graph name is specified
162 # in a WITH clause, then - for the purposes of evaluating the
163 # WHERE clause - this will define an RDF Dataset containing a
164 # default graph with the specified name, but only in the absence
165 # of USING or USING NAMED clauses. In the presence of one or more
166 # graphs referred to in USING clauses and/or USING NAMED clauses,
167 # the WITH clause will be ignored while evaluating the WHERE
168 # clause."
169 if not u.using and u.withClause:
170 g = ctx.dataset.get_context(u.withClause)
171 ctx = ctx.pushGraph(g)
172
173 res = evalPart(ctx, u.where)
174
175 if u.using:
176 if otherDefault:
177 ctx = originalctx # restore original default graph
178 if u.withClause:
179 g = ctx.dataset.get_context(u.withClause)
180 ctx = ctx.pushGraph(g)
181
182 for c in list(res):
183 # TODO: Make this more intentional and without the weird type checking logic
184 # once ConjunctiveGraph is removed and Dataset no longer inherits from
185 # Graph.
186 dg = ctx.graph if type(ctx.graph) is Graph else ctx.dataset.default_context
187 if u.delete:
188 # type error: Unsupported left operand type for - ("None")
189 # type error: Unsupported operand types for - ("Graph" and "Generator[Tuple[Identifier, Identifier, Identifier], None, None]")
190 dg -= _fillTemplate(u.delete.triples, c) # type: ignore[operator]
191
192 for g, q in u.delete.quads.items():
193 cg = ctx.dataset.get_context(c.get(g))
194 cg -= _fillTemplate(q, c)
195
196 if u.insert:
197 # type error: Unsupported left operand type for + ("None")
198 # type error: Unsupported operand types for + ("Graph" and "Generator[Tuple[Identifier, Identifier, Identifier], None, None]")
199 dg += _fillTemplate(u.insert.triples, c) # type: ignore[operator]
200
201 for g, q in u.insert.quads.items():
202 cg = ctx.dataset.get_context(c.get(g))
203 cg += _fillTemplate(q, c)
204
205
206def evalAdd(ctx: QueryContext, u: CompValue) -> None:
207 """
208
209 add all triples from src to dst
210
211 http://www.w3.org/TR/sparql11-update/#add
212 """
213 src, dst = u.graph
214
215 srcg = _graphOrDefault(ctx, src)
216 dstg = _graphOrDefault(ctx, dst)
217
218 # type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
219 if srcg.identifier == dstg.identifier: # type: ignore[union-attr]
220 return
221
222 # type error: Unsupported left operand type for + ("None")
223 dstg += srcg # type: ignore[operator]
224
225
226def evalMove(ctx: QueryContext, u: CompValue) -> None:
227 """
228
229 remove all triples from dst
230 add all triples from src to dst
231 remove all triples from src
232
233 http://www.w3.org/TR/sparql11-update/#move
234 """
235
236 src, dst = u.graph
237
238 srcg = _graphOrDefault(ctx, src)
239 dstg = _graphOrDefault(ctx, dst)
240
241 # type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
242 if srcg.identifier == dstg.identifier: # type: ignore[union-attr]
243 return
244
245 # type error: Item "None" of "Optional[Graph]" has no attribute "remove"
246 dstg.remove((None, None, None)) # type: ignore[union-attr]
247
248 # type error: Unsupported left operand type for + ("None")
249 dstg += srcg # type: ignore[operator]
250
251 if ctx.dataset.store.graph_aware:
252 # type error: Argument 1 to "remove_graph" of "Store" has incompatible type "Optional[Graph]"; expected "Graph"
253 ctx.dataset.store.remove_graph(srcg) # type: ignore[arg-type]
254 else:
255 # type error: Item "None" of "Optional[Graph]" has no attribute "remove"
256 srcg.remove((None, None, None)) # type: ignore[union-attr]
257
258
259def evalCopy(ctx: QueryContext, u: CompValue) -> None:
260 """
261
262 remove all triples from dst
263 add all triples from src to dst
264
265 http://www.w3.org/TR/sparql11-update/#copy
266 """
267
268 src, dst = u.graph
269
270 srcg = _graphOrDefault(ctx, src)
271 dstg = _graphOrDefault(ctx, dst)
272
273 # type error: Item "None" of "Optional[Graph]" has no attribute "remove"
274 if srcg.identifier == dstg.identifier: # type: ignore[union-attr]
275 return
276
277 # type error: Item "None" of "Optional[Graph]" has no attribute "remove"
278 dstg.remove((None, None, None)) # type: ignore[union-attr]
279
280 # type error: Unsupported left operand type for + ("None")
281 dstg += srcg # type: ignore[operator]
282
283
284def evalUpdate(
285 graph: Graph,
286 update: Update,
287 initBindings: Optional[Mapping[str, Identifier]] = None,
288) -> None:
289 """http://www.w3.org/TR/sparql11-update/#updateLanguage
290
291 'A request is a sequence of operations [...] Implementations MUST
292 ensure that operations of a single request are executed in a
293 fashion that guarantees the same effects as executing them in
294 lexical order.
295
296 Operations all result either in success or failure.
297
298 If multiple operations are present in a single request, then a
299 result of failure from any operation MUST abort the sequence of
300 operations, causing the subsequent operations to be ignored.'
301
302 This will return None on success and raise Exceptions on error
303
304 !!! warning "Security Considerations"
305
306 This method can access indirectly requested network endpoints, for
307 example, query processing will attempt to access network endpoints
308 specified in `SERVICE` directives.
309
310 When processing untrusted or potentially malicious queries, measures
311 should be taken to restrict network and file access.
312
313 For information on available security measures, see the RDFLib
314 [Security Considerations](../security_considerations.md)
315 documentation.
316
317 """
318
319 for u in update.algebra:
320 initBindings = dict((Variable(k), v) for k, v in (initBindings or {}).items())
321
322 ctx = QueryContext(graph, initBindings=initBindings)
323 ctx.prologue = u.prologue
324
325 try:
326 if u.name == "Load":
327 evalLoad(ctx, u)
328 elif u.name == "Clear":
329 evalClear(ctx, u)
330 elif u.name == "Drop":
331 evalDrop(ctx, u)
332 elif u.name == "Create":
333 evalCreate(ctx, u)
334 elif u.name == "Add":
335 evalAdd(ctx, u)
336 elif u.name == "Move":
337 evalMove(ctx, u)
338 elif u.name == "Copy":
339 evalCopy(ctx, u)
340 elif u.name == "InsertData":
341 evalInsertData(ctx, u)
342 elif u.name == "DeleteData":
343 evalDeleteData(ctx, u)
344 elif u.name == "DeleteWhere":
345 evalDeleteWhere(ctx, u)
346 elif u.name == "Modify":
347 evalModify(ctx, u)
348 else:
349 raise Exception("Unknown update operation: %s" % (u,))
350 except: # noqa: E722
351 if not u.silent:
352 raise