|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Raven.Database.Data;
namespace Raven.Client.Document
{
public abstract class AbstractDocumentQuery<T> : IDocumentQuery<T>
{
protected string indexName;
protected string query = "";
protected string[] orderByFields = new string[0];
protected int start;
protected int pageSize = 128;
protected bool waitForNonStaleResults;
protected TimeSpan timeout;
protected string[] projectionFields;
private QueryResult queryResult;
public IDocumentQuery<T> WaitForNonStaleResults(TimeSpan waitTimeout)
{
waitForNonStaleResults = true;
timeout = waitTimeout;
return this;
}
public abstract IDocumentQuery<TProjection> Select<TProjection>(Func<T, TProjection> projectionExpression);
public QueryResult QueryResult
{
get { return queryResult ?? (queryResult = GetQueryResult()); }
}
public IEnumerator<T> GetEnumerator()
{
var jsonSerializer = new JsonSerializer();
return QueryResult.Results
.Select(j => (T)jsonSerializer.Deserialize(new JsonTokenReader(j), typeof(T)))
.GetEnumerator();
}
protected abstract QueryResult GetQueryResult();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IDocumentQuery<T> Take(int count)
{
pageSize = count;
return this;
}
public IDocumentQuery<T> Skip(int count)
{
start = count;
return this;
}
public IDocumentQuery<T> Where(string whereClause)
{
if (string.IsNullOrEmpty(query))
query = whereClause;
else
query += " " + whereClause;
return this;
}
public IDocumentQuery<T> OrderBy(params string[] fields)
{
orderByFields = orderByFields.Concat(fields).ToArray();
return this;
}
public IDocumentQuery<T> WaitForNonStaleResults()
{
waitForNonStaleResults = true;
timeout = TimeSpan.FromSeconds(15);
return this;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.