deferred/
lib.rs

1// Copyright 2024 Heath Stewart.
2// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3
4use msica::prelude::*;
5
6#[no_mangle]
7pub extern "C" fn DeferredExampleCustomAction(session: Session) -> CustomActionResult {
8    let database = session.database();
9    let view = database
10        .open_view("SELECT `Cardinal`, `Ordinal` FROM `DeferredExample` ORDER BY `Cardinal`")?;
11
12    // Add another row.
13    view.modify(
14        ModifyMode::InsertTemporary,
15        &Record::with_fields(
16            None,
17            vec![
18                Field::IntegerData(100),
19                Field::StringData("last".to_string()),
20            ],
21        )?,
22    )?;
23
24    // Schedule custom actions for each row.
25    view.execute(None)?;
26    for record in view {
27        let data = format!(
28            "{}\t{}",
29            record.integer_data(1).unwrap_or(0),
30            record.string_data(2)?,
31        );
32        session.do_deferred_action("DeferredExampleCustomActionDeferred", &data)?;
33    }
34    Success
35}
36
37#[no_mangle]
38pub extern "C" fn DeferredExampleCustomActionDeferred(session: Session) -> CustomActionResult {
39    // Process the custom action data passed by the immediate custom action.
40    // This data is always made available in a property named "CustomActionData".
41    let data = session.property("CustomActionData")?;
42    let fields: Vec<&str> = data.split('\t').collect();
43    let record = Record::with_fields(
44        Some("Running the [2] ([1]) deferred custom action"),
45        vec![
46            Field::StringData(fields[0].to_string()),
47            Field::StringData(fields[1].to_string()),
48        ],
49    )?;
50    session.message(MessageType::Info, &record);
51    Success
52}