} /// <summary> /// Gets a query filter that should uniquely identify /// the current instance. The default implementation /// compares the Id/key field to the current instance's. /// </summary> public override IQueryFilter GetUniqueFilter() { if (UniqueFilterProvider != null) { return(UniqueFilterProvider(this)); } else { var colFilter = new ParentColumns(); return(colFilter.KeyColumn == IdValue); } }
/// <summary> /// Execute a query and return the number of results /// </summary> /// <param name="where">A WhereDelegate that recieves a ParentColumns /// and returns a IQueryFilter which is the result of any comparisons /// between ParentColumns and other values /// </param> /// <param name="db"></param> public static long Count(WhereDelegate <ParentColumns> where, Database database = null) { ParentColumns c = new ParentColumns(); IQueryFilter filter = where (c); Database db = database ?? Db.For <Parent>(); QuerySet query = GetQuerySet(db); query.Count <Parent>(); query.Where(filter); query.Execute(db); return(query.Results.As <CountResult>(0).Value); }
/// <summary> /// Get one entry matching the specified filter. If none exists /// one will be created; success will depend on the nullability /// of the specified columns. /// </summary> /// <param name="where"></param> /// <param name="database"></param> public static Parent GetOneWhere(WhereDelegate <ParentColumns> where, Database database = null) { var result = OneWhere(where, database); if (result == null) { ParentColumns c = new ParentColumns(); IQueryFilter filter = where (c); result = CreateFromFilter(filter, database); } return(result); }
public static async Task BatchQuery(int batchSize, WhereDelegate <ParentColumns> where, Action <IEnumerable <Parent> > batchProcessor, Database database = null) { await Task.Run(async() => { ParentColumns columns = new ParentColumns(); var orderBy = Order.By <ParentColumns>(c => c.KeyColumn, SortOrder.Ascending); var results = Top(batchSize, where, orderBy, database); while (results.Count > 0) { await Task.Run(() => { batchProcessor(results); }); long topId = results.Select(d => d.Property <long>(columns.KeyColumn.ToString())).ToArray().Largest(); results = Top(batchSize, (ParentColumns) where (columns) && columns.KeyColumn > topId, orderBy, database); } }); }
/// <summary> /// Execute a query and return the specified number of values. This method /// will issue a sql TOP clause so only the specified number of values /// will be returned. /// </summary> /// <param name="count">The number of values to return. /// This value is used in the sql query so no more than this /// number of values will be returned by the database. /// </param> /// <param name="where">A WhereDelegate that recieves a ParentColumns /// and returns a IQueryFilter which is the result of any comparisons /// between ParentColumns and other values /// </param> /// <param name="orderBy"> /// Specifies what column and direction to order the results by /// </param> /// <param name="database"></param> public static ParentCollection Top(int count, WhereDelegate <ParentColumns> where, OrderBy <ParentColumns> orderBy, Database database = null) { ParentColumns c = new ParentColumns(); IQueryFilter filter = where (c); Database db = database ?? Db.For <Parent>(); QuerySet query = GetQuerySet(db); query.Top <Parent>(count); query.Where(filter); if (orderBy != null) { query.OrderBy <ParentColumns>(orderBy); } query.Execute(db); var results = query.Results.As <ParentCollection>(0); results.Database = db; return(results); }