Example #1
0
        internal static async Task UpdateModelAsync(OracleConnection connection, string currentSchema, bool suppressException, CancellationToken cancellationToken, params IModelDataProvider[] updaters)
        {
            using (var command = connection.CreateCommand())
            {
                command.BindByName = true;

                OracleTransaction transaction = null;

                try
                {
                    foreach (var updater in updaters)
                    {
                        command.ResetParametersToAvoidOdacBug();
                        command.CommandText = String.Empty;
                        command.CommandType = CommandType.Text;
                        updater.InitializeCommand(command);

                        try
                        {
                            if (updater.IsValid)
                            {
                                if (connection.State == ConnectionState.Closed)
                                {
                                    await connection.OpenAsynchronous(cancellationToken);

                                    connection.ModuleName = "SQLPad backround";
                                    connection.ActionName = "Model data provider";

                                    if (!String.IsNullOrEmpty(currentSchema))
                                    {
                                        using (var setSchemaCommand = connection.CreateCommand())
                                        {
                                            await setSchemaCommand.SetCurrentSchema(currentSchema, cancellationToken);
                                        }
                                    }

                                    transaction = connection.BeginTransaction();
                                }

                                if (updater.HasScalarResult)
                                {
                                    var result = await command.ExecuteScalarAsynchronous(cancellationToken);

                                    updater.MapScalarValue(result);
                                }
                                else
                                {
                                    using (var reader = await command.ExecuteReaderAsynchronous(CommandBehavior.Default, cancellationToken))
                                    {
                                        await updater.MapReaderData(reader, cancellationToken);
                                    }
                                }
                            }
                        }
                        catch (OracleException exception)
                        {
                            if (exception.Number == (int)OracleErrorCode.UserInvokedCancellation)
                            {
                                break;
                            }

                            throw;
                        }
                    }
                }
                catch (Exception e)
                {
                    TraceLog.WriteLine($"Update model failed: {e}");

                    if (!suppressException)
                    {
                        throw;
                    }
                }
                finally
                {
                    if (transaction != null)
                    {
                        await transaction.RollbackAsynchronous();

                        transaction.Dispose();
                    }
                }
            }
        }