Beispiel #1
0
 public void AddOrdering(SparqlOrdering ordering)
 {
     _ordering.Add(ordering);
 }
        private string GetSparqlQuery(bool withDatasetDescription, bool projectSortVariables = false)
        {
            var queryStringBuilder = new StringBuilder();

            if (IsDistinct)
            {
                queryStringBuilder.Append("DISTINCT ");
            }
            foreach (var sv in _selectVars)
            {
                queryStringBuilder.AppendFormat("?{0} ", sv);
            }
            foreach (var ag in _aggregates)
            {
                queryStringBuilder.AppendFormat("({0} AS ?{1}) ", ag.Item2, ag.Item1);
            }
            if (projectSortVariables)
            {
                if (IsDistinct && _ordering.Count > 0)
                {
                    // Eagerly loaded queries with DISTINCT and ordering need to be grouped by
                    // the select variable so that we can then use MIN and MAX aggregates to project the sort variables exactly once
                    _groupByExpressions.Add("?" + _selectVars[0]);
                }
                for (int i = 0; i < _ordering.Count; i++)
                {
                    if (IsDistinct)
                    {
                        // Project MIN or MAX of the sort variable for consistency
                        queryStringBuilder.AppendFormat("({0}(?{1}_sort{2}) AS ?{1}_dsort{2}) ",
                                                        _ordering[i].OrderingDirection == OrderingDirection.Asc
                                                            ? "MAX"
                                                            : "MIN",
                                                        _selectVars[0], i);
                    }
                    else
                    {
                        queryStringBuilder.AppendFormat("?{0}_sort{1} ", _selectVars[0], i);
                    }
                }
            }
            if (withDatasetDescription)
            {
                AppendFromClause(queryStringBuilder);
            }
            queryStringBuilder
            .Append("WHERE {")
            .Append(_graphPatternBuilder.ToString());

            if (projectSortVariables)
            {
                // Bind the expressions to variables
                for (int i = 0; i < _ordering.Count; i++)
                {
                    queryStringBuilder.AppendFormat(" BIND ({0} AS ?{1}_sort{2}) .",
                                                    _ordering[i].SelectorExpression, _selectVars[0], i);
                }
            }
            queryStringBuilder.Append("}");
            if (projectSortVariables && IsDistinct && IsOrdered)
            {
                // The ordering needs to be changed to use MIN and MAX expressions too
                for (int i = 0; i < _ordering.Count; i++)
                {
                    _ordering[i] = new SparqlOrdering(String.Format("{0}(?{1}_sort{2})",
                                                                    _ordering[i].OrderingDirection ==
                                                                    OrderingDirection.Asc
                                                                            ? "MAX"
                                                                            : "MIN",
                                                                    _selectVars[0], i),
                                                      _ordering[i].OrderingDirection);
                }
            }
            AppendModifiers(queryStringBuilder);
            var sparqlString = queryStringBuilder.ToString();

            return(ReplaceFixedVariables(sparqlString));
        }
 public void AddOrdering(SparqlOrdering ordering)
 {
     _ordering.Add(ordering);
 }
        private string GetSparqlQuery(bool withDatasetDescription, bool projectSortVariables =false)
        {
            var queryStringBuilder = new StringBuilder();
            if (IsDistinct) queryStringBuilder.Append("DISTINCT ");
            foreach(var sv in _selectVars)
            {
                queryStringBuilder.AppendFormat("?{0} ", sv);
            }
            foreach(var ag in _aggregates)
            {
                queryStringBuilder.AppendFormat("({0} AS ?{1}) ", ag.Item2, ag.Item1);
            }
            if (projectSortVariables)
            {
                if (IsDistinct && _ordering.Count > 0)
                {
                    // Eagerly loaded queries with DISTINCT and ordering need to be grouped by 
                    // the select variable so that we can then use MIN and MAX aggregates to project the sort variables exactly once
                    _groupByExpressions.Add("?" + _selectVars[0]);
                }
                for (int i = 0; i < _ordering.Count; i++)
                {
                    if (IsDistinct)
                    {
                        // Project MIN or MAX of the sort variable for consistency
                        queryStringBuilder.AppendFormat("({0}(?{1}_sort{2}) AS ?{1}_dsort{2}) ",
                                                        _ordering[i].OrderingDirection == OrderingDirection.Asc
                                                            ? "MAX"
                                                            : "MIN",
                                                        _selectVars[0], i);
                    }
                    else
                    {
                        queryStringBuilder.AppendFormat("?{0}_sort{1} ", _selectVars[0], i);
                    }
                }
            }
            if (withDatasetDescription) AppendFromClause(queryStringBuilder);
            queryStringBuilder
                .Append("WHERE {")
                .Append(_graphPatternBuilder);

            if (projectSortVariables)
            {
                // Bind the expressions to variables
                for (int i = 0; i < _ordering.Count; i++)
                {
                    queryStringBuilder.AppendFormat(" BIND ({0} AS ?{1}_sort{2}) .",
                                                    _ordering[i].SelectorExpression, _selectVars[0], i);
                }
            }
            queryStringBuilder.Append("}");
            if (projectSortVariables && IsDistinct && IsOrdered)
            {
                    // The ordering needs to be changed to use MIN and MAX expressions too
                    for (int i = 0; i < _ordering.Count; i++)
                    {
                        _ordering[i] = new SparqlOrdering(String.Format("{0}(?{1}_sort{2})",
                                                                        _ordering[i].OrderingDirection ==
                                                                        OrderingDirection.Asc
                                                                            ? "MAX"
                                                                            : "MIN",
                                                                        _selectVars[0], i),
                                                          _ordering[i].OrderingDirection);
                    }
            }
            AppendModifiers(queryStringBuilder);
            var sparqlString = queryStringBuilder.ToString();
            return ReplaceFixedVariables(sparqlString);
        }