global::mysql

fn connect

fn connect(parameters: Map) -> EvalAltResult>>
details

Open a pool of connections to a MySQL database.

Args

  • parameters - a map of the following parameters:
    • url - a string url to connect to the database.
    • timeout - time allowed between each query to the database. (default: 30s)
    • connections - Number of connections to open to the database. (default: 4)

Return

A service used to query the database pointed by the url parameter.

Error

  • The service failed to connect to the database.

Example

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_mysql" as mysql;

export const database = mysql::connect(#{
    // Connect to a database on the system with the 'greylist-manager' user and 'my-password' password.
    url: "mysql://localhost/?user=greylist-manager&password=my-password",
    timeout: "1m",
    connections: 1,
});

fn query

fn query(database: MySQL, query: SharedObject) -> EvalAltResult>>
fn query(database: MySQL, query: String) -> EvalAltResult>>
details

Query the database.

Args

  • query - The query to execute.

Return

A list of records.

Example

Build a service in services/database.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_mysql" as mysql;

export const database = mysql::connect(#{
    // Connect to a database on the system with the 'greylist-manager' user and 'my-password' password.
    url: "mysql://localhost/?user=greylist-manager&password=my-password",
    timeout: "1m",
    connections: 1,
});

Query the database during filtering.

import "services/database" as srv;

#{
    connect: [
        action "get records from my database" || {
            // For the sake of this example, we assume that there is a populated
            // table called 'my_table' in the database.
            const records = srv::database.query("SELECT * FROM my_table");

            // `records` is an array, we can run a for loop and print all records.
            log("info", "fetching mysql records ...");
            for record in records {
                log("info", ` -> ${record}`);
            }
        }
    ],
}
```    #[allow(clippy::needless_pass_by_value)]
</details>

</div>
</br>