MySQL · Execute
Place an order through a procedure
Placing an order through a procedure, in two scenarios: sp_place_order writes the order and its lines (from the products' current prices) in one call and returns the new id and the total; the id is then used like any other (Retrieve reads the order back). The application doesn't repeat the pricing rule the procedure owns.
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).
Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:
var account = new ArxEntity("account");
account["name"] = "Order Oasis";
account["account_number"] = "ACC-E501";
var accountId = await service.CreateAsync(account);
foreach (var (sku, price) in new[] { ("SKU-E501", 12m), ("SKU-E502", 3.25m), ("SKU-E503", 99m) })
{
var product = new ArxEntity("product");
product["sku"] = sku;
product["name"] = $"Product {sku}";
product["unit_price"] = price;
await service.CreateAsync(product);
}1 Place the order
Two products, three of each: the procedure prices them and returns the order's id and total.
Already in scope: service (ArxMySqlDbService), accountId (ArxRecordId)
var place = new ArxProcedureExpression("sp_place_order");
place.AddParameter("p_account_id", accountId.AsInt64());
place.AddParameter("p_order_number", "SO-E501");
place.AddParameter("p_skus", "SKU-E501,SKU-E502");
place.AddParameter("p_quantity", 3);
place.AddOutParameter<long>("p_order_id");
place.AddOutParameter<decimal>("p_total");
var outputs = (await service.ExecuteAsync(place)).Outputs;
Console.WriteLine($"order {outputs["p_order_id"]} placed, total {outputs["p_total"]:N2}");
return (long)outputs["p_order_id"]!;Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT o.total, count(l.id) AS line_count, sum(l.quantity) AS units
FROM sales_order o
JOIN order_line l ON l.order_id = o.id
WHERE o.order_number = 'SO-E501'
GROUP BY o.total;Expect: 45.75, 2, 6.
2 Read it back by the returned id
The returned id is the order's id: Retrieve reads the order by it.
Already in scope: service (ArxMySqlDbService), orderId (long)
var order = await service.RetrieveAsync("sales_order", orderId, new ArxColumnSet("order_number", "total"));
Console.WriteLine($"{order!["order_number"]}: {order["total"]:N2}");Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT order_number, total
FROM sales_order
WHERE order_number = 'SO-E501';Expect: SO-E501, 45.75.
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
- .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target
net10.0). - Create the project:
Terminal dotnet new console -n ArxSample -f net10.0 cd ArxSample - Install the packages:
Terminal dotnet add package ARX.SDK.MySQL --version 1.0.0-rc.1 - Database: a MySQL 8.4 (tested with 8.4.13) database with the sample schema.
- Configuration: set
ARX_SAMPLES_MYSQLto its connection string. Nothing is hard-coded. - Source code: add the files of the sample application this sample uses (
SampleContext.cs,SampleOutput.cs,SampleContext.Application.cs,SampleCatalog.cs), then replaceProgram.csand add the other file below, with the file names shown. - Run it:
Terminal dotnet run - Expected output: below.
The files
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 RealCase1_PlaceAnOrder().RunAsync(new SampleContext(connectionString));
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>
/// Placing an order through a procedure, in two scenarios: <c>sp_place_order</c> writes the order and its lines (from
/// the products' current prices) in one call and returns the new id and the total; the id is then used like any other
/// (Retrieve reads the order back). The application doesn't repeat the pricing rule the procedure owns.
/// </summary>
public sealed class RealCase1_PlaceAnOrder : ISample
{
public string Name => "execute-real-1-place-an-order";
public string Title => "Place an order through a procedure";
public async Task RunAsync(SampleContext dbContext)
{
var account = new ArxEntity("account");
account["name"] = "Order Oasis";
account["account_number"] = "ACC-E501";
var accountId = await dbContext.Service.CreateAsync(account);
foreach (var (sku, price) in new[] { ("SKU-E501", 12m), ("SKU-E502", 3.25m), ("SKU-E503", 99m) })
{
var product = new ArxEntity("product");
product["sku"] = sku;
product["name"] = $"Product {sku}";
product["unit_price"] = price;
await dbContext.Service.CreateAsync(product);
}
var orderId = await Scenario1_PlaceItAsync(dbContext, accountId);
await Scenario2_ReadItBackAsync(dbContext, orderId);
}
/// <summary>1. Two products, three of each: the procedure prices them and returns the order's id and total.</summary>
private static async Task<long> Scenario1_PlaceItAsync(SampleContext dbContext, ArxRecordId accountId)
{
Step("1. Place the order");
var place = new ArxProcedureExpression("sp_place_order");
place.AddParameter("p_account_id", accountId.AsInt64());
place.AddParameter("p_order_number", "SO-E501");
place.AddParameter("p_skus", "SKU-E501,SKU-E502");
place.AddParameter("p_quantity", 3);
place.AddOutParameter<long>("p_order_id");
place.AddOutParameter<decimal>("p_total");
var outputs = (await dbContext.Service.ExecuteAsync(place)).Outputs;
Show($"order {outputs["p_order_id"]} placed, total {outputs["p_total"]:N2}");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT o.total, count(l.id) AS line_count, sum(l.quantity) AS units
* FROM sales_order o
* JOIN order_line l ON l.order_id = o.id
* WHERE o.order_number = 'SO-E501'
* GROUP BY o.total;
* -- Expect: 45.75, 2, 6.
* ===========================================================
*/
return (long)outputs["p_order_id"]!;
}
/// <summary>2. The returned id is the order's id: Retrieve reads the order by it.</summary>
private static async Task Scenario2_ReadItBackAsync(SampleContext dbContext, long orderId)
{
Step("2. Read it back by the returned id");
var order = await dbContext.Service.RetrieveAsync("sales_order", orderId, new ArxColumnSet("order_number", "total"));
Show($"{order!["order_number"]}: {order["total"]:N2}");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT order_number, total
* FROM sales_order
* WHERE order_number = 'SO-E501';
* -- Expect: SO-E501, 45.75.
* ===========================================================
*/
}
}
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.
> 1. Place the order
order 1 placed, total 45.75
> 2. Read it back by the returned id
SO-E501: 45.75