Beispiel #1
0
 private void ExecuteSparql()
 {
     SummaryMessage = "Running query...";
     ResultColumnNames.Clear();
     SparqlQueryResultsString = String.Empty;
     QueryResultsTable        = null;
     if (Store == null)
     {
         Messages = "No target store selected.";
         return;
     }
     ThreadPool.QueueUserWorkItem(RunSparqlQuery);
 }
        private void ExecuteSparql()
        {
            SummaryMessage = "Running query...";
            ResultColumnNames.Clear();
            SparqlQueryResultsString = String.Empty;
            QueryResultsTable        = null;
            if (Store == null)
            {
                Messages = "No target store selected.";
                return;
            }
            XDocument sparqlResults;

            try
            {
                var timer = new Stopwatch();
                timer.Start();
                sparqlResults =
                    Store.ExecuteSparql(
                        String.IsNullOrEmpty(SelectedQueryString) ? SparqlQueryString : SelectedQueryString,
                        TargetCommitPoint);
                timer.Stop();
                ErrorOccurred = false;
                TimeTaken     = timer.ElapsedMilliseconds;
            }
            catch (RdfParseException parseException)
            {
                SummaryMessage = "Parse error - see message tab for details.";
                Messages       = "Parse error: " + parseException.Message;
                return;
            }
            catch (SparqlQueryException sqe)
            {
                SummaryMessage = "Query error - see message tab for details.";
                Messages       = sqe.ToString();
                return;
            }
            catch (Exception ex)
            {
                SummaryMessage = "Query error - see message tab for details.";
                Messages       = "Error running SPARQL query. " + ex.Message;
                return;
            }

            if (sparqlResults != null)
            {
                var resultsTable = new DataTable();
                SparqlQueryResultsString = sparqlResults.ToString();
                RowsReturned             = sparqlResults.SparqlResultRows().Count();
                if (sparqlResults.Root != null)
                {
                    XElement sparqlQueryResults = sparqlResults.Root.Element(SparqlResultsNs + "results");
                    XElement head = sparqlResults.Root.Element(SparqlResultsNs + "head");
                    if (sparqlQueryResults != null && head != null)
                    {
                        foreach (
                            XAttribute n in
                            from v in
                            head.Elements(SparqlResultsNs + "variable")
                            select v.Attribute("name"))
                        {
                            if (n != null)
                            {
                                ResultColumnNames.Add(n.Value);
                                resultsTable.Columns.Add(n.Value);
                            }
                        }
                    }
                    if (ResultColumnNames.Count > 0)
                    {
                        int colCount = resultsTable.Columns.Count;
                        foreach (XElement row in sparqlResults.SparqlResultRows())
                        {
                            var rowData = new object[colCount];
                            for (int i = 0; i < colCount; i++)
                            {
                                rowData[i] = row.GetColumnValue(resultsTable.Columns[i].ColumnName);
                            }
                            resultsTable.Rows.Add(rowData);
                        }
                    }
                    QueryResultsTable = resultsTable;
                }
                Messages       = "Query executed successfully.";
                SummaryMessage = String.Format("Query returned {0} rows in {1}ms", RowsReturned, TimeTaken);
            }
            else
            {
                RowsReturned   = 0;
                Messages       = "Query executed successfully.";
                SummaryMessage = String.Format("Query executed in {0}ms", TimeTaken);
            }
        }