Path: redb/redb.Core/Query/QueryContext.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Query/QueryContext.cs
----
using System.Collections.Generic;
using redb.Core.Query.QueryExpressions;
namespace redb.Core.Query;
///
/// Query context - contains all information about LINQ query
///
public class QueryContext where TProps : class, new()
{
public long SchemeId { get; init; }
public long? UserId { get; init; }
public bool CheckPermissions { get; init; }
// SYNC WITH RUSLAN VERSION: Add tree query support
public long? ParentId { get; init; } // For single parent
public long[]? ParentIds { get; set; } // For batch operations
public int? MaxDepth { get; init; } // Maximum search depth
public FilterExpression? Filter { get; set; }
public List Orderings { get; set; } = new();
///
/// DistinctBy / DistinctByRedb: the ordering by the key goes first, once, in the direction the query gave it
/// (ascending when it gave none). DISTINCT ON needs the key first, and the providers keep one row per key in that
/// order. The key used to be inserted ascending in front of the query's own ordering by it, so
/// OrderByDescending(key).DistinctBy(key) held both: the Free providers took the first and came back ascending.
///
internal void PutDistinctKeyFirst(OrderingExpression key)
{
bool SameKey(OrderingExpression o) => !o.HasExpression
&& o.Property.IsBaseField == key.Property.IsBaseField
&& o.Property.Name == key.Property.Name;
var given = Orderings.LastOrDefault(SameKey);
Orderings.RemoveAll(SameKey);
Orderings.Insert(0, given ?? key);
}
public int? Limit { get; set; }
public int? Offset { get; set; }
public bool IsDistinct { get; set; }
///
/// DISTINCT by base IRedbObject fields (Name, ValueLong, ParentId, etc.) excluding Id.
/// Used for finding duplicates by base fields.
///
public bool IsDistinctRedb { get; set; }
///
/// Field for DISTINCT ON (field) - one object per each unique field value
///
public OrderingExpression? DistinctByField { get; set; }
///
/// true = DistinctByField is a base IRedbObject field
/// false = DistinctByField is a Props field
///
public bool DistinctByIsBaseField { get; set; }
public int? MaxRecursionDepth { get; set; }
///
/// NEW FLAG: Indicates that query should return empty result
/// Used for Where(x => false) and similar cases
///
public bool IsEmpty { get; set; }
///
/// V4 (LAZY Л2): per-query override of EnableLazyReferences. null = use the global
/// option; the executor toggles the session flag of the context's connection around the
/// query when the override differs from it.
///
public bool? LazyReferences { get; set; }
///
/// PROJECTION: Structure IDs for optimized Props loading
/// null = load ALL Props fields
/// HashSet = load ONLY specified structure_ids
///
public HashSet? ProjectedStructureIds { get; set; }
///
/// PROJECTION: Text field paths for SQL function search_objects_with_projection_by_paths
/// Format: ["Name", "AddressBook[home].City", "Items[0].Price"]
/// Used instead of ProjectedStructureIds for human-readable queries
///
public List? ProjectedFieldPaths { get; set; }
///
/// PROJECTION: Skip ALL Props post-processing in MaterializeResultsFromJson.
/// When true, neither LoadPropsForManyAsync (eager) nor lazy loader setup will run.
/// Used by projections: SQL projection function already returns partial Props in JSON,
/// deserialized via Props setter. Re-loading would overwrite them with full object data.
///
public bool SkipPropsLoading { get; set; }
///
/// PROJECTION: the selector was read completely and touches no Props field (base fields only),
/// so no Props has to be loaded at all. Not the same as being
/// null, which means "load every field". A provider that does not know this flag loads everything,
/// which is slower and still correct.
///
public bool ProjectionReadsNoProps { get; set; }
///
/// Maximum depth for loading nested RedbObject in Props.
/// Controls recursion when Props contain references to other RedbObjects via _values._Object.
/// null = use global DefaultMaxTreeDepth from config.
///
public int? PropsDepth { get; set; }
public QueryContext(long schemeId, long? userId = null, bool checkPermissions = false, long? parentId = null, int? maxDepth = null)
{
SchemeId = schemeId;
UserId = userId;
CheckPermissions = checkPermissions;
ParentId = parentId; // SYNC: add initialization
MaxDepth = maxDepth; // SYNC: add initialization
}
///
/// Create a copy of context
///
public QueryContext Clone()
{
return new QueryContext(SchemeId, UserId, CheckPermissions, ParentId, MaxDepth)
{
ParentIds = ParentIds, // SYNC: copy batch array
Filter = Filter,
Orderings = new List(Orderings),
Limit = Limit,
Offset = Offset,
IsDistinct = IsDistinct,
IsDistinctRedb = IsDistinctRedb,
DistinctByField = DistinctByField,
DistinctByIsBaseField = DistinctByIsBaseField,
MaxRecursionDepth = MaxRecursionDepth,
IsEmpty = IsEmpty, // FIX: copy IsEmpty flag
LazyReferences = LazyReferences,
ProjectedStructureIds = ProjectedStructureIds,
ProjectedFieldPaths = ProjectedFieldPaths,
SkipPropsLoading = SkipPropsLoading,
ProjectionReadsNoProps = ProjectionReadsNoProps,
PropsDepth = PropsDepth
};
}
}