MySQL · Execute

Progress messages from a procedure

The messages a procedure sends, in two scenarios. A MySQL procedure reports with a warning: SIGNAL SQLSTATE '01000' (class 01 is a warning, not an error, and the procedure goes on). MySQL keeps only the warnings of the procedure's last statement, so a procedure reports one note when it ends rather than progress while it runs. It comes back in Messages, in order, for the application to show or log as it chooses (ARX never writes them to a log); CollectMessages = false leaves them out.

Set up for this page

The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleCatalog.cs (the tables' ids, keys, lookups and display names).

1 The note the procedure ends with

An import of 1,000 rows: the note the procedure ends with, read with SHOW WARNINGS in the same round trip.

Already in scope: service (ArxMySqlDbService)

C#
var result = await service.ExecuteAsync(Import(1_000));

foreach (var message in result.Messages)
{
    Console.WriteLine($"{message.Severity,-7} {message.Text}");
}
Used above

Defined once in this sample.

private static ArxProcedureExpression Import(int rows)
{
    var procedure = new ArxProcedureExpression("sp_import_progress");
    procedure.AddParameter("p_rows", rows);
    return procedure;
}

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SELECT routine_name, routine_type
FROM information_schema.routines
WHERE routine_schema = DATABASE() AND routine_name = 'sp_import_progress';

Expect: sp_import_progress, PROCEDURE (the messages themselves are only seen by the session that ran the call).

2 Not collected

The same call without collecting them.

Already in scope: service (ArxMySqlDbService)

C#
var result = await service.ExecuteAsync(Import(1_000), new ArxExecuteOptions { CollectMessages = false });
Console.WriteLine($"{result.Messages.Count} messages");
Used above

Defined once in this sample.

private static ArxProcedureExpression Import(int rows)
{
    var procedure = new ArxProcedureExpression("sp_import_progress");
    procedure.AddParameter("p_rows", rows);
    return procedure;
}

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SELECT COUNT(*)
FROM information_schema.routines
WHERE routine_schema = DATABASE() AND routine_type = 'PROCEDURE' AND routine_name = 'sp_import_progress';

Expect: 1.

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.MySQL --version 1.0.0-rc.1
  4. Database: a MySQL 8.4 (tested with 8.4.13) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_MYSQL to its connection string. Nothing is hard-coded.
  6. Source code: add the files of the sample application this sample uses (SampleContext.cs, SampleOutput.cs, SampleContext.Application.cs, SampleCatalog.cs), then replace Program.cs and add the other file below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment.

using ArxSamples.MySQL;
using ArxSamples.MySQL.Operations.Execute.RealCases;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_MYSQL")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_MYSQL environment variable to your MySQL connection string.");

await new RealCase6_ProgressMessages().RunAsync(new SampleContext(connectionString));
RealCase6_ProgressMessages.cs

The sample, as it is in the repository.

using ARX.SDK.Query;
using ARX.SDK.Services;
using static ArxSamples.MySQL.SampleOutput;

namespace ArxSamples.MySQL.Operations.Execute.RealCases;

/// <summary>
/// The messages a procedure sends, in two scenarios. A MySQL procedure reports with a warning: <c>SIGNAL SQLSTATE
/// '01000'</c> (class 01 is a warning, not an error, and the procedure goes on). MySQL keeps only the warnings of the
/// procedure's last statement, so a procedure reports one note when it ends rather than progress while it runs. It comes
/// back in <see cref="ArxExecuteResult.Messages"/>, in order, for the application to show or log as it chooses (ARX never
/// writes them to a log); <c>CollectMessages = false</c> leaves them out.
/// </summary>
public sealed class RealCase6_ProgressMessages : ISample
{
    public string Name => "execute-real-6-progress-messages";

    public string Title => "Progress messages from a procedure";

    public async Task RunAsync(SampleContext dbContext)
    {
        await Scenario1_TheMessagesInOrderAsync(dbContext);

        await Scenario2_NotCollectedAsync(dbContext);
    }

    private static ArxProcedureExpression Import(int rows)
    {
        var procedure = new ArxProcedureExpression("sp_import_progress");
        procedure.AddParameter("p_rows", rows);
        return procedure;
    }

    /// <summary>1. An import of 1,000 rows: the note the procedure ends with, read with SHOW WARNINGS in the same round trip.</summary>
    private static async Task Scenario1_TheMessagesInOrderAsync(SampleContext dbContext)
    {
        Step("1. The note the procedure ends with");

        var result = await dbContext.Service.ExecuteAsync(Import(1_000));

        foreach (var message in result.Messages)
        {
            Show($"{message.Severity,-7} {message.Text}");
        }

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT routine_name, routine_type
         * FROM information_schema.routines
         * WHERE routine_schema = DATABASE() AND routine_name = 'sp_import_progress';
         * -- Expect: sp_import_progress, PROCEDURE (the messages themselves are only seen by the session that ran the call).
         * ===========================================================
         */
    }

    /// <summary>2. The same call without collecting them.</summary>
    private static async Task Scenario2_NotCollectedAsync(SampleContext dbContext)
    {
        Step("2. Not collected");

        var result = await dbContext.Service.ExecuteAsync(Import(1_000), new ArxExecuteOptions { CollectMessages = false });
        Show($"{result.Messages.Count} messages");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT COUNT(*)
         * FROM information_schema.routines
         * WHERE routine_schema = DATABASE() AND routine_type = 'PROCEDURE' AND routine_name = 'sp_import_progress';
         * -- Expect: 1.
         * ===========================================================
         */
    }
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. The note the procedure ends with
      Warning imported 1000 rows in 4 steps of 250: a large import
  > 2. Not collected
      0 messages