/// <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 <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); await engine.ExecuteAsync(commands, cancellationToken).ConfigureAwait(false); }
public async Task Test_MultiInsertAsync_WithExplicitTransaction() { this.helper.CreateTable(); Command[] commands = this.GetMultiInsertCommands(); CommandEngine engine = this.helper.GetCommandEngine(new TransactionFilter()); try { await engine.ExecuteAsync(commands); } catch (DbException) { } this.helper.VerifyTransaction(); }
public async Task Test_InsertsAsync_WithTransactionScope() { this.helper.CreateTable(); Command command = this.helper.GetInsert(); CommandEngine engine = this.helper.GetCommandEngine(new TransactionScopeFilter()); try { await engine.ExecuteAsync(command); } catch (DbException) { } this.helper.VerifyTransaction(); }
public async Task Test_CommandEngine_Connection() { var connection1 = new SqliteConnection(DatabaseHelper.TestDbConnectionString); var connection2 = new SqliteConnection(DatabaseHelper.TestDbConnectionString); try { CommandOptions options1 = new CommandOptions() { ConnectionFactory = () => connection1, }; CommandOptions options2 = new CommandOptions() { ConnectionFactory = () => connection2, }; CommandEngine engine1 = new CommandEngine(options1); CommandEngine engine2 = new CommandEngine(options2); engine1.Execute(new Command() { CommandText = "SELECT 0;" }); await engine2.ExecuteAsync(new Command() { CommandText = "SELECT 0;" }); connection1.State.ShouldBe(ConnectionState.Closed); connection2.State.ShouldBe(ConnectionState.Closed); } finally { connection1.Dispose(); connection2.Dispose(); } }