public void SparqlUpdateGenericCreateInsertDeleteData() { IStorageProvider manager = this.GetManager(); GenericUpdateProcessor processor = new GenericUpdateProcessor(manager); SparqlUpdateCommandSet cmds = this._parser.ParseFromString("CREATE GRAPH <http://example.org/sparqlUpdate/created>; INSERT DATA { GRAPH <http://example.org/sparqlUpdate/created> { <http://example.org/s> <http://example.org/p> <http://example.org/o> } }"); processor.ProcessCommandSet(cmds); Graph g = new Graph(); manager.LoadGraph(g, "http://example.org/sparqlUpdate/created"); TestTools.ShowGraph(g); Assert.IsFalse(g.IsEmpty, "[" + manager.ToString() + "] Graph should not be empty"); Assert.AreEqual(1, g.Triples.Count, "[" + manager.ToString() + "] Graph should have 1 Triple"); cmds = this._parser.ParseFromString("DELETE DATA { GRAPH <http://example.org/sparqlUpdate/created> { <http://example.org/s> <http://example.org/p> <http://example.org/o> } }"); processor.ProcessCommandSet(cmds); Graph h = new Graph(); manager.LoadGraph(h, "http://example.org/sparqlUpdate/created"); TestTools.ShowGraph(h); Assert.IsTrue(h.IsEmpty, "[" + manager.ToString() + "] Graph should be empty"); }
/// <summary> /// Executes an Update against the Triple Store. /// </summary> /// <param name="update">SPARQL Update Command(s).</param> /// <remarks> /// As per the SPARQL 1.1 Update specification the command string may be a sequence of commands. /// </remarks> public void ExecuteUpdate(string update) { if (_manager is IUpdateableStorage) { if (!((PersistentGraphCollection)_graphs).IsSynced) { throw new SparqlUpdateException("Unable to execute a SPARQL Update as the in-memory view of the store is not synced with the underlying store, please invoked Flush() or Discard() and try again. Alternatively if you do not want to see in-memory changes reflected in update results you can invoke the Update() method directly on the underlying store by accessing it through the UnderlyingStore property."); } ((IUpdateableStorage)_manager).Update(update); } else { if (_updateProcessor == null) { _updateProcessor = new GenericUpdateProcessor(_manager); } if (_updateParser == null) { _updateParser = new SparqlUpdateParser(); } SparqlUpdateCommandSet cmds = _updateParser.ParseFromString(update); _updateProcessor.ProcessCommandSet(cmds); } }
protected override TaskResult RunTaskInternal() { SparqlUpdateParser parser = new SparqlUpdateParser(); this._cmds = parser.ParseFromString(this._update); GenericUpdateProcessor processor = new GenericUpdateProcessor(this._manager); processor.ProcessCommandSet(this._cmds); return(new TaskResult(true)); }
/// <summary> /// Processes a PATCH operation. /// </summary> /// <param name="context">HTTP Context.</param> public override void ProcessPatch(IHttpContext context) { // Work out the Graph URI we want to patch Uri graphUri = ResolveGraphUri(context); // If the Request has the SPARQL Update MIME Type then we can process it if (context.Request.ContentLength > 0) { if (context.Request.ContentType.Equals("application/sparql-update")) { // Try and parse the SPARQL Update // No error handling here as we assume the calling IHttpHandler does that String patchData; using (StreamReader reader = new StreamReader(context.Request.InputStream)) { patchData = reader.ReadToEnd(); reader.Close(); } SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(patchData); // Assuming that we've got here i.e. the SPARQL Updates are parseable then // we need to check that they actually affect the relevant Graph if (cmds.Commands.All(c => c.AffectsSingleGraph && c.AffectsGraph(graphUri))) { GenericUpdateProcessor processor = new GenericUpdateProcessor(_manager); processor.ProcessCommandSet(cmds); processor.Flush(); } else { // One/More commands either do no affect a Single Graph or don't affect the Graph // implied by the HTTP Request so give a 422 response context.Response.StatusCode = 422; return; } } else { // Don't understand other forms of PATCH requests context.Response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType; return; } } else { // Empty Request is a Bad Request context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } }
public void StorageStardogAmpersandsInDataTest() { try { Options.HttpDebugging = true; StardogConnector stardog = StardogTests.GetConnection();; //Save the Graph Graph g = new Graph(); String fragment = "@prefix : <http://example.org/> . [] :string \"This has & ampersands in it\" ."; g.LoadFromString(fragment); g.BaseUri = new Uri("http://example.org/ampersandGraph"); Console.WriteLine("Original Graph:"); TestTools.ShowGraph(g); stardog.SaveGraph(g); //Retrieve and check it round trips Graph h = new Graph(); stardog.LoadGraph(h, g.BaseUri); Console.WriteLine("Graph as retrieved from Stardog:"); TestTools.ShowGraph(h); Assert.AreEqual(g, h, "Graphs should be equal"); //Now try to delete the data from this Graph GenericUpdateProcessor processor = new GenericUpdateProcessor(stardog); SparqlUpdateParser parser = new SparqlUpdateParser(); processor.ProcessCommandSet(parser.ParseFromString("WITH <http://example.org/ampersandGraph> DELETE WHERE { ?s ?p ?o }")); Graph i = new Graph(); stardog.LoadGraph(i, g.BaseUri); Console.WriteLine("Graph as retrieved after the DELETE WHERE:"); TestTools.ShowGraph(i); Assert.AreNotEqual(g, i, "Graphs should not be equal"); Assert.AreNotEqual(h, i, "Graphs should not be equal"); } finally { Options.HttpDebugging = false; } }
/// <summary> /// Processes a PATCH operation /// </summary> /// <param name="context">HTTP Context</param> public override void ProcessPatch(HttpContext context) { //Work out the Graph URI we want to patch Uri graphUri = this.ResolveGraphUri(context); //If the Request has the SPARQL Update MIME Type then we can process it if (context.Request.ContentLength > 0) { if (context.Request.ContentType.Equals("application/sparql-update")) { //Try and parse the SPARQL Update //No error handling here as we assume the calling IHttpHandler does that String patchData; using (StreamReader reader = new StreamReader(context.Request.InputStream)) { patchData = reader.ReadToEnd(); reader.Close(); } SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(patchData); //Assuming that we've got here i.e. the SPARQL Updates are parseable then //we need to check that they actually affect the relevant Graph if (cmds.Commands.All(c => c.AffectsSingleGraph && c.AffectsGraph(graphUri))) { GenericUpdateProcessor processor = new GenericUpdateProcessor(this._manager); processor.ProcessCommandSet(cmds); processor.Flush(); } else { //One/More commands either do no affect a Single Graph or don't affect the Graph //implied by the HTTP Request so give a 422 response context.Response.StatusCode = 422; return; } } else { //Don't understand other forms of PATCH requests context.Response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType; return; } } else { //Empty Request is a Bad Request context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } }
/// <summary> /// Executes a set of SPARQL Update commands on the Store /// </summary> /// <param name="updates">SPARQL Update Commands</param> /// <remarks> /// <para> /// If the underlying Manager is an <see cref="IUpdateableGenericIOManager">IUpdateableGenericIOManager</see> then the managers own Update implementation will be used, otherwise dotNetRDF's approximated implementation for generic stores will be used. In the case of approximation exact feature support will vary depending on the underlying manager being used. /// </para> /// </remarks> public void ExecuteUpdate(SparqlUpdateCommandSet updates) { GenericUpdateProcessor processor = new GenericUpdateProcessor(this._manager); processor.ProcessCommandSet(updates); }