public void SqlBuffer_Batching_HasCorrectValue() { PageDescriptor page = this.locator.FindPage("../Commands/Batching/BatchedCommand.cssql", typeof(LocatorAccessor)); ProcFactory factory = this.engine.Proc(page, new ProcArgs(typeof(object), typeof(object))); IProcResult result = factory(null); ISqlSerializer <CommandData> serializer = result.Buffer as ISqlSerializer <CommandData>; IList <CommandData> batchedBySql = serializer.Serialize(new SqlOptions() { MaxSql = 1 }).ToList(); IList <CommandData> batchedByParams = serializer.Serialize(new SqlOptions() { MaxParameters = 2 }).ToList(); IList <CommandData> notBatched = serializer.Serialize(new SqlOptions()).ToList(); batchedBySql.ShouldNotBeNull(); batchedByParams.ShouldNotBeNull(); notBatched.ShouldNotBeNull(); batchedBySql.Count.ShouldBe(20); batchedByParams.Count.ShouldBe(10); notBatched.Count.ShouldBe(1); string joinedSql = string.Join("", batchedBySql.Select(d => d.CommandText)); string joinedParams = string.Join("", batchedByParams.Select(d => d.CommandText)); notBatched.First().CommandText.ShouldBe(joinedSql); notBatched.First().CommandText.ShouldBe(joinedParams); }
/// <summary> /// Executes a Razor SQL query with a specified model and returns a single, continuous, unbuffered list from its result sets. /// </summary> /// <typeparam name="TItem">The resulting type of each item in the list.</typeparam> /// <param name="model">The concrete model to initialize to the Razor page with.</param> /// <param name="configure">A method for configuring query options.</param> /// <param name="queryName">The query name to locate the Razor page by. Defaults to the name of the calling method.</param> /// <returns>An unbuffered list of <typeparamref name="TItem"/> items.</returns> protected IEnumerable <TItem> Enumerate <TItem>(object model = null, Action <SqlOptions> configure = null, [CallerMemberName] string queryName = null) { IProcResult result = this.ExecuteAndGetResult(queryName, model, new ProcArgs() { ModelType = model?.GetType() ?? typeof(object), ResultType = typeof(IList <TItem>), }); SqlOptions options = this.GetQueryOptions(result.Domain); configure?.Invoke(options); ISqlSerializer <QueryData> serializer = result.Buffer as ISqlSerializer <QueryData>; IEnumerable <QueryData> queries = serializer?.Serialize(options); QueryOptions queryOptions = new QueryOptions() { ConnectionFactory = result.Domain.ConnectionFactory, Schemas = result.Domain.Schemas, Filters = options.Filters, }; QueryHandler handler = new QueryHandler(queryOptions); return(handler.Enumerate <TItem>(queries)); }
/// <summary> /// Executes an asynchronous Razor SQL command with the specified model and updates the model from its resulting data set. /// </summary> /// <typeparam name="TModel">Type of the concrete parameter model.</typeparam> /// <param name="model">A concrete model containing parameter values for the command.</param> /// <param name="configure">A method for configuring command options.</param> /// <param name="commandName">The command name to locate the Razor page by. Defaults to the name of the calling method.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> protected async Task ExecuteAsync <TModel>(TModel model = default, Action <SqlOptions> configure = null, [CallerMemberName] string commandName = null, CancellationToken cancellationToken = default) { IProcResult result = this.ExecuteAndGetResult(commandName, model, new ProcArgs() { ModelType = typeof(TModel) == typeof(object) ? model?.GetType() : typeof(TModel), ResultType = typeof(object), }); SqlOptions options = this.GetCommandOptions(result.Domain); configure?.Invoke(options); ISqlSerializer <CommandData> serializer = result.Buffer as ISqlSerializer <CommandData>; IEnumerable <CommandData> commands = serializer.Serialize(options); CommandOptions commandOptions = new CommandOptions() { ConnectionFactory = result.Domain.ConnectionFactory, Filters = options.Filters, }; CommandHandler handler = new CommandHandler(commandOptions); await handler.ExecuteAsync(commands, cancellationToken).ConfigureAwait(false); }
/// <summary> /// Executes an asynchronous Razor SQL query with a specified model and returns an unbuffered list of <see cref="QueryReader"/> instances each of which can enumerate items in its corresponding result set. /// </summary> /// <param name="model">The concrete model to initialize to the Razor page with.</param> /// <param name="configure">A method for configuring query options.</param> /// <param name="queryName">The query name to locate the Razor page by. Defaults to the name of the calling method.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>An enumerable providing a <see cref="QueryReader"/> instance for every data set in the result.</returns> protected IAsyncEnumerable <QueryReader> EnumerateAsync(object model = null, Action <SqlOptions> configure = null, [CallerMemberName] string queryName = null, CancellationToken cancellationToken = default) { IProcResult result = this.ExecuteAndGetResult(queryName, model, new ProcArgs() { ModelType = model?.GetType() ?? typeof(object), ResultType = typeof(IList <object>), }); SqlOptions options = this.GetQueryOptions(result.Domain); configure?.Invoke(options); ISqlSerializer <Query> serializer = result.Buffer as ISqlSerializer <Query>; IEnumerable <Query> queries = serializer?.Serialize(options); QueryOptions queryOptions = new QueryOptions() { ConnectionFactory = result.Domain.ConnectionFactory, Store = result.Domain.Schemas, Filters = options.Filters, }; QueryEngine engine = new QueryEngine(queryOptions); return(engine.EnumerateAsync(queries, cancellationToken)); }
/// <summary> /// Executes an asynchronous Razor SQL query with a specified model and returns a single, buffered list from the product of its result sets. /// </summary> /// <typeparam name="T">The resulting type of each item in the list.</typeparam> /// <param name="model">A concrete model containing parameter values for the query.</param> /// <param name="configure">A method for configuring query options.</param> /// <param name="queryName">The query name to locate the Razor page by. Defaults to the name of the calling method.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>An buffered list of <typeparamref name="T"/> items.</returns> protected async Task <T> AggregateAsync <T>(object model = null, Action <SqlOptions> configure = null, [CallerMemberName] string queryName = null, CancellationToken cancellationToken = default) { IProcResult result = this.ExecuteAndGetResult(queryName, model, new ProcArgs() { ModelType = model?.GetType() ?? typeof(object), ResultType = typeof(IList <T>), }); SqlOptions options = this.GetQueryOptions(result.Domain); configure?.Invoke(options); ISqlSerializer <Query> serializer = result.Buffer as ISqlSerializer <Query>; IEnumerable <Query> queries = serializer?.Serialize(options); QueryOptions queryOptions = new QueryOptions() { ConnectionFactory = result.Domain.ConnectionFactory, Store = result.Domain.Schemas, Filters = options.Filters, }; QueryEngine engine = new QueryEngine(queryOptions); return((await engine.ExecuteAsync <IList <T> >(queries, QueryType.Aggregate, cancellationToken).ConfigureAwait(false)).FirstOrDefault() ?? default); }
/// <summary> /// Executes a Razor SQL command with the specified model and updates the model from its resulting data set. /// </summary> /// <typeparam name="TModel">Type of the concrete parameter model.</typeparam> /// <param name="model">A concrete model containing parameter values for the command.</param> /// <param name="configure">A method for configuring command options.</param> /// <param name="commandName">The command name to locate the Razor page by. Defaults to the name of the calling method.</param> protected void Execute <TModel>(TModel model = default, Action <SqlOptions> configure = null, [CallerMemberName] string commandName = null) { IProcResult result = this.ExecuteAndGetResult(commandName, model, new ProcArgs() { ModelType = typeof(TModel) == typeof(object) ? model?.GetType() : typeof(TModel), ResultType = typeof(object), }); SqlOptions options = this.GetCommandOptions(result.Domain); configure?.Invoke(options); ISqlSerializer <Command> serializer = result.Buffer as ISqlSerializer <Command>; IEnumerable <Command> commands = serializer.Serialize(options); CommandOptions commandOptions = new CommandOptions() { ConnectionFactory = result.Domain.ConnectionFactory, Filters = options.Filters, }; CommandEngine engine = new CommandEngine(commandOptions); engine.Execute(commands); }