// Performs a query against a document using an QueryContext. private static void doContextQuery(Manager mgr, string query, QueryContext context) { // Perform a single query against the referenced container using // the referenced context. System.Console.WriteLine("Exercising query: '" + query + "'."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // Perform the query Results results = mgr.Query(null, query, context, new DocumentConfig()); // Iterate over the results while(results.MoveNext()) { System.Console.WriteLine(results.Current); } System.Console.WriteLine(results.Size + " results returned for query '" + query + "'."); }
private static void getDetails(Manager mgr, string query, QueryContext context) { // Perform a single query against the referenced container using // the referenced context. System.Console.WriteLine("Exercising query: '" + query + "'."); System.Console.WriteLine("Return to continue:"); System.Console.ReadLine(); // Perform the query Results results = mgr.Query(null, query, context, new DocumentConfig()); System.Console.WriteLine("\tProduct : Price : Inventory Level"); while(results.MoveNext()) { /// Retrieve the value as a document using(Document theDocument = results.Current.ToDocument()) { // Obtain information of interest from the document. Note that the // wildcard in the query expression allows us to not worry about what // namespace this document uses. string item = getValue(mgr, theDocument, "string(/*/product)", context); string price = getValue(mgr, theDocument, "string(/*/inventory/price)", context); string inventory = getValue(mgr, theDocument, "string(/*/inventory/inventory)", context); System.Console.WriteLine("\t" + item + " : " + price + " : " + inventory); } } System.Console.WriteLine(results.Size + " results returned for query '" + query + "'."); }
// Modifies an XML document that is stored in a DB XML container private static void doUpdateDocument(Manager mgr, Container container, string query, QueryContext context, Transaction txn) { System.Console.WriteLine("Updating documents for expression: '" + query + "'."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // query for all the documents that we want to update Results results = mgr.Query(txn, query, context, new DocumentConfig()); System.Console.WriteLine("Found " + results.Size + " matching the expression '" + query + "'."); // Get an update context. using(UpdateContext updateContext = mgr.CreateUpdateContext()) { while(results.MoveNext()) { Document document = results.Current.ToDocument(); // Retrieve the entire document as a single String object string docString = document.StringContent; System.Console.WriteLine("Updating document: "); System.Console.WriteLine(docString); // This next method just modifies the document string // in a small way. string newDocString = getNewDocument(mgr, document, context, docString); System.Console.WriteLine("Updating document..."); //Set the document's content to be the new document string document.StringContent = newDocString; // Now replace the document in the container container.UpdateDocument(txn, document, updateContext); System.Console.WriteLine("Document updated."); } } }
// Shows a timestamp for each record that matches the given XPath query. // The timestamp is stored as metadata on each document. This metadata was // added to the document when the example data was loaded into the container // using exampleLoadContainer.java. The timestamp represents the time // when the document was loaded into the container. private static void showTimeStamp(Manager mgr, string query, QueryContext context) { // Perform a single query against the referenced container using // the referenced context. The timestamp metadata attribute is then // displayed. System.Console.WriteLine("Exercising query: '" + query + "'."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); //Perform the query Results results = mgr.Query(null, query, context, new DocumentConfig()); while(results.MoveNext()) { using(Document document = results.Current.ToDocument()) { // We return the metadata as a MetaData object using(MetaData md = document.GetMetaData("http://dbxmlExamples/timestamp", "timeStamp")) { System.Console.WriteLine("Document " + document.Name + " stored on " + md.Value.ToDateTime()); } } } System.Console.WriteLine(results.Size + " results returned for query '" + query + "'."); }
// Method that deletes all documents from a DB XML container that match a given // XQuery. private static void doDeleteDocument(Manager mgr, Container container, string query, QueryContext context, Transaction txn) { System.Console.WriteLine("Deleting documents for expression: '" + query + "'."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // Perform our query. We'll delete any document contained in this result set. Results results = mgr.Query(txn, query, context, new DocumentConfig()); System.Console.WriteLine("Found " + results.Size + " matching the expression '" + query + "'."); // Get an update context. using(UpdateContext updateContext = mgr.CreateUpdateContext()) { while(results.MoveNext()) { Document document = results.Current.ToDocument(); string name = document.Name; System.Console.WriteLine("Deleting document: " + name + "."); // Peform the delete container.DeleteDocument(txn, document, updateContext); System.Console.WriteLine("Deleted document: " + name + "."); } } }
// Utility method that we use to make sure the documents that we thought // were deleted from the container are in fact deleted. private static void confirmDelete(Manager mgr, string query, QueryContext context) { System.Console.WriteLine("Confirming the delete."); System.Console.WriteLine("The query: '" + query + "' should return result set size 0."); Results results = mgr.Query(null, query, context, new DocumentConfig()); if(results.Size == 0) { System.Console.WriteLine("Result set size is 0. Deletion confirmed."); } else { System.Console.WriteLine("Result set size is " + results.Size + ". Deletion failed."); } }