public static IEnumerable <QueryResult <T> > Query <T>( this IDocumentStore store, DocumentTable table, out QueryStats stats, bool top1 = false, string select = null, string where = "", Window window = null, string orderby = "", bool includeDeleted = false, object parameters = null) => store.Query <T>(table, null, out stats, top1, select, where, window, orderby, includeDeleted, parameters);
IEnumerable <T> InternalQuery <T>(ManagedConnection connection, SqlBuilder sql, object parameters, bool hasTotalsQuery, out QueryStats stats) { var normalizedParameters = new FastDynamicParameters( parameters as IEnumerable <Parameter> ?? ConvertToParameters <T>(parameters)); if (hasTotalsQuery) { using (var reader = connection.Connection.QueryMultiple(sql.ToString(), normalizedParameters)) { stats = reader.Read <QueryStats>(buffered: true).Single(); return(reader.Read <T, object, T>((first, second) => first, "RowNumber", buffered: true)); } } using (var reader = connection.Connection.QueryMultiple(sql.ToString(), normalizedParameters)) { List <T> rows = reader.Read <T, object, T>((first, second) => first, "RowNumber", buffered: true).ToList(); stats = new QueryStats { TotalResults = rows.Count }; return(rows); } }
public static IEnumerable<IDictionary<string, object>> Query( this IDocumentStore store, DocumentTable table, out QueryStats stats, string select = null, string where = "", int skip = 0, int take = 0, string orderby = "", object parameters = null) { return store.Query<object>(table, out stats, @select, @where, skip, take, @orderby, parameters).Select(x => (IDictionary<string, object>) x.Data); }
public IEnumerable <TProjection> Query <TProjection>( DocumentTable table, out QueryStats stats, string select = null, string where = "", int skip = 0, int take = 0, string orderby = "", object parameters = null) { if (select.IsNullOrEmpty() || select == "*") { select = ""; } var projectToDictionary = typeof(TProjection).IsA <IDictionary <string, object> >(); if (!projectToDictionary) { select = MatchSelectedColumnsWithProjectedType <TProjection>(select); } var timer = Stopwatch.StartNew(); using (var connection = Database.Connect()) { var sql = new SqlBuilder(); var isWindowed = skip > 0 || take > 0; if (isWindowed) { sql.Append("select count(*) as TotalResults") .Append("from {0}", Database.FormatTableNameAndEscape(table.Name)) .Append(!string.IsNullOrEmpty(@where), "where {0}", @where) .Append(";"); sql.Append(@"with temp as (select *") .Append(", row_number() over(ORDER BY {0}) as RowNumber", string.IsNullOrEmpty(@orderby) ? "CURRENT_TIMESTAMP" : @orderby) .Append("from {0}", Database.FormatTableNameAndEscape(table.Name)) .Append(!string.IsNullOrEmpty(@where), "where {0}", @where) .Append(")") .Append("select {0} from temp", select.IsNullOrEmpty() ? "*" : select + ", RowNumber") .Append("where RowNumber >= {0}", skip + 1) .Append(take > 0, "and RowNumber <= {0}", skip + take) .Append("order by RowNumber"); } else { sql.Append(@"with temp as (select *") .Append(", 0 as RowNumber") .Append("from {0}", Database.FormatTableNameAndEscape(table.Name)) .Append(!string.IsNullOrEmpty(@where), "where {0}", @where) .Append(")") .Append("select {0} from temp", select.IsNullOrEmpty() ? "*" : select + ", RowNumber") .Append(!string.IsNullOrEmpty(orderby), "order by {0}", orderby); } IEnumerable <TProjection> result; if (projectToDictionary) { result = (IEnumerable <TProjection>) InternalQuery <object>(connection, sql, parameters, isWindowed, out stats) .Cast <IDictionary <string, object> >(); } else { result = InternalQuery <TProjection>(connection, sql, parameters, isWindowed, out stats); } stats.QueryDurationInMilliseconds = timer.ElapsedMilliseconds; if (isWindowed) { var potential = stats.TotalResults - skip; if (potential < 0) { potential = 0; } stats.RetrievedResults = take > 0 && potential > take ? take : potential; } else { stats.RetrievedResults = stats.TotalResults; } Interlocked.Increment(ref numberOfRequests); Logger.Information("Retrieved {0} of {1} in {2}ms", stats.RetrievedResults, stats.TotalResults, stats.QueryDurationInMilliseconds); connection.Complete(); return(result); } }
public static IEnumerable <IDictionary <string, object> > Query(this IDocumentStore store, DocumentTable table, out QueryStats stats, string select = null, string where = "", int skip = 0, int take = 0, string orderby = "", object parameters = null) { return(store.Query <IDictionary <string, object> >(table, out stats, @select, @where, skip, take, @orderby, parameters)); }