示例#1
0
        public string BuildCouchDbApiPostData()
        {
            var stringBuilder = new StringBuilder();

            if (string.IsNullOrEmpty(SelectPart) || FromParts.Count == 0)
            {
                throw new InvalidOperationException("A query must have a select part and at least one from part.");
            }

            stringBuilder.Append("{ \"map\":\"function (doc) {");

            if (WhereParts.Count > 0)
            {
                stringBuilder.AppendFormat("if ({0}) ", SeparatedStringBuilder.Build(" && ", WhereParts));
            }

            stringBuilder.AppendFormat("emit({0}, ", !String.IsNullOrEmpty(OrderBy) ? OrderBy : "doc._id");

            // TODO: use the SelectParts to only return the properties that are needed by emitting {Name: "Jeff", Title: "Awesome"}

            var select = "doc"; // return the entire thing by default

            if (!String.IsNullOrEmpty(SelectPart))
            {
                select = SelectPart;
            }

            stringBuilder.Append(select + ");}\"}");

            return(stringBuilder.ToString());
        }
        public override string ToString()
        {
            var result       = string.Format("Fetch ({0}.{1})", _relationMember.DeclaringType.Name, _relationMember.Name);
            var innerResults = InnerFetchRequests.Select(request => request.ToString());

            return(SeparatedStringBuilder.Build(".Then", new[] { result }.Concat(innerResults)));
        }
        public string BuildHQLString()
        {
            var stringBuilder = new StringBuilder();

            if (string.IsNullOrEmpty(SelectPart) || FromParts.Count == 0)
            {
                throw new InvalidOperationException("A query must have a select part and at least one from part.");
            }

            stringBuilder.AppendFormat("select {0}", SelectPart);
            stringBuilder.AppendFormat(" from {0}", SeparatedStringBuilder.Build(", ", FromParts));

            if (WhereParts.Count > 0)
            {
                stringBuilder.AppendFormat(" where {0}", SeparatedStringBuilder.Build(" and ", WhereParts));
            }

            if (OrderByParts.Count > 0)
            {
                stringBuilder.AppendFormat(" order by {0}", SeparatedStringBuilder.Build(", ", OrderByParts));
            }

            return(stringBuilder.ToString());
        }
示例#4
0
        public IEnumerable <T> Enumerable <T>()
        {
            var querystring = string.Empty;
            var resultType  = typeof(T);
            var hasFilter   = false;

            if (_queryParts.ReturnCount)
            {
                querystring += "/$count";
            }
            querystring += "?$format=json&";
            if (_queryParts.Take.HasValue)
            {
                querystring += "$top=" + _queryParts.Take.Value + "&";
            }
            if (_queryParts.Skip.HasValue)
            {
                querystring += "$skip=" + _queryParts.Skip.Value + "&";
            }
            if (!string.IsNullOrEmpty(_queryParts.OrderBy))
            {
                querystring += "$orderby=" + _queryParts.OrderBy + "&";
            }
            var filter = SeparatedStringBuilder.Build(" and ", _queryParts.WhereParts);

            if (!string.IsNullOrEmpty(filter))
            {
                querystring += "$filter=" + filter + "&";
            }
            if (!string.IsNullOrEmpty(_queryParts.SelectPart))
            {
                querystring += "$select=" + _queryParts.SelectPart + "&";
            }
            var fullUrl = _url + "/" + _collectionName + querystring;
            var json    = UrlHelper.Get(fullUrl);

            // Netflix retuns a separate array inside d when a filter is used for some reason, so hard-coded check for now during tests
            hasFilter = !querystring.EndsWith("$format=json&");
            //var json = ODataRequest.Execute(fullUrl, "POST", _queryParts.BuildODataApiPostData(), "application/json");
            JObject res;

            // check for Count() [Int32] and LongCOunt() [Int64]
            if (_queryParts.ReturnCount && (resultType == typeof(int) || resultType == typeof(long)))
            {
                var results = new List <T>();
                res = JObject.Parse(json);
                results.Add(res["total_rows"].ToObject <T>());
                return(results);
            }
            // get the rows property and deserialize that
            var jobject = JsonConvert.DeserializeObject(json) as JObject;
            var rows    = jobject["d"];

            if (hasFilter)
            {
                rows = rows["results"];
            }
            var items = rows.Select(row => row.ToObject <T>());

            return(items);
        }
        public override string ToString()
        {
            var result = "orderby " + SeparatedStringBuilder.Build(", ", Orderings);

            return(result);
        }
 public void AddOrderByPart(IEnumerable <string> orderings)
 {
     OrderByParts.Insert(0, SeparatedStringBuilder.Build(", ", orderings));
 }