/* * Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Extensions.Data * File: IDbQueryLookup.cs * * IDbQueryLookup.cs is part of VNLib.Plugins.Extensions.Data which is part * of the larger VNLib collection of libraries and utilities. * * VNLib.Plugins.Extensions.Data is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * VNLib.Plugins.Extensions.Data is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ using System.Linq; namespace VNLib.Plugins.Extensions.Data.Abstractions { /// /// Represents a collection of queries that can be used to execute operations against a a database /// /// public interface IDbQueryLookup where T : class, IDbModel { /// /// Builds a query that attempts to get a single entry from the /// store based on the specified record if it does not have a /// valid property /// /// The active context to query /// The record to search for /// A query that yields a single record if it exists in the store virtual IQueryable AddOrUpdateQueryBuilder(IDbContextHandle context, T record) { //default to get single of the specific record return GetSingleQueryBuilder(context, record); } /// /// Builds a query that attempts to get a single entry from the /// store to update based on the specified record /// /// The active context to query /// The record to search for /// A query that yields a single record to update if it exists in the store virtual IQueryable UpdateQueryBuilder(IDbContextHandle context, T record) { //default to get single of the specific record return GetSingleQueryBuilder(context, record); } /// /// Builds a query that results in a single entry to delete from the /// constraint arguments /// /// The active context /// A variable length parameter array of query constraints /// A query that yields a single record (or no record) to delete virtual IQueryable DeleteQueryBuilder(IDbContextHandle context, params string[] constraints) { //default use the get-single method, as the implementation is usually identical return GetSingleQueryBuilder(context, constraints); } /// /// Builds a query to get a count of records constrained by the specifier /// /// The active context to run the query on /// The specifier constrain /// A query that can be counted virtual IQueryable GetCollectionQueryBuilder(IDbContextHandle context, string specifier) { return GetCollectionQueryBuilder(context, new string[] { specifier }); } /// /// Builds a query to get a count of records constrained by the specifier /// /// The active context to run the query on /// The specifier constrain /// A query that can be counted virtual IQueryable GetCountQueryBuilder(IDbContextHandle context, string specifier) { //Default use the get collection and just call the count method return GetCollectionQueryBuilder(context, specifier); } /// /// /// Builds a query to get a single record from the specified record. /// /// /// Unless overridden, performs an ID based query for a single entry /// /// /// The context to execute query against /// A record to referrence the lookup /// A query that yields a single record virtual IQueryable GetSingleQueryBuilder(IDbContextHandle context, T record) { return from entry in context.Set() where entry.Id == record.Id select entry; } /// /// Builds a query to get a collection of records based on an variable length array of parameters /// /// The active context to run the query on /// An arguments array to constrain the results of the query /// A query that returns a paginated collection of records from the store virtual IQueryable GetPageQueryBuilder(IDbContextHandle context, params string[] constraints) { //Default to getting the entire collection and just selecting a single page return GetCollectionQueryBuilder(context, constraints); } /// /// Builds a query to get a single record from the variable length parameter arguments /// /// The context to execute query against /// Arguments to constrain the results of the query to a single record /// A query that yields a single record IQueryable GetSingleQueryBuilder(IDbContextHandle context, params string[] constraints); /// /// Builds a query to get a collection of records based on an variable length array of parameters /// /// The active context to run the query on /// An arguments array to constrain the results of the query /// A query that returns a collection of records from the store IQueryable GetCollectionQueryBuilder(IDbContextHandle context, params string[] constraints); } }