Database step
The Query a database step runs one query on your own database (PostgreSQL, MySQL / MariaDB or Microsoft SQL Server) and gives the result to the steps after it. Use it to look a caller up in your customer database, check an order, count open tickets, or record the outcome of a call.
It is an automation step (an event flow). A call flow hands the work to an automation with an after-call hand-off.
Before you start
- Save a database login. Open Admin, Saved credentials and choose New credential. Pick the type of your database (PostgreSQL database, MySQL / MariaDB database or Microsoft SQL Server database) and fill in the host, the port (empty: the usual one), the database, the user name, the password and the encryption. The password is write-only: it is stored sealed and never shown again, and a flow never contains it. You can also use + Create new in the step itself.
- Use a read-only login. Give the step a database user that can only read the tables it needs. This is the strongest protection there is, and the only one SQL Server offers (see What is checked).
- Make the database reachable. A database on a public address works as it is. A database on a private address (your own network) can only be reached when the person who runs the installation lists it, see Private addresses.
The step
| Setting | What it does |
|---|---|
| Database login | One of your saved logins (the list shows only the database types; + Create new saves one in place). |
| Query | One statement, written as it is. Values go in as :name parameters, never inside the text. |
| Parameters | The value of each :name of the query: a template such as {{trigger.caller}} or {{vars.order}}. Each :name of the query gets a row; choose how it is bound: Text (default), Whole number, Number or Yes / no. |
| Allow changes to rows | Off by default: only reads run, in a read-only transaction. On: INSERT, UPDATE, DELETE and MERGE may run. Saving a flow with this on needs the administrator permission to change settings (settings:write). |
| Time limit (ms) | How long the statement may run (100 to 30 000, default 5000). |
| Most rows | How many rows are kept (1 to 1000, default 100). The rest are dropped and truncated is true. |
An example, to find the customer of a caller:
SELECT name, tier FROM customers WHERE phone = :phone LIMIT 1
with the parameter phone set to {{trigger.caller}}.
Outputs. count (rows kept), row (the first row as an object, for example {{nodes.find.row.name}}), rows (the rows as a list), columns (the column names), truncated and affected (rows changed by a statement that changes rows). A step's result is limited in size: when the rows are too big, the last ones are dropped and truncated is true, so select only the columns you need. Save outputs into variables with save, as for any step.
A database that is down, a wrong login, a refused query or a time-out takes the step's error exit (and can be retried in an automation). A read that fails is safe to retry. A change that may have been made when the database stopped answering is not repeated: the step fails with MPBX-FLOW-SEND-UNCERTAIN, as a message does.
Try it before you publish
In the step's form, Try it has two buttons. Test connection connects with the login and runs SELECT 1. Test query (read-only) runs the query with sample values that you type for its parameters and shows the first rows. The test never changes rows, even when the step allows it. The sample values are bound as parameters, as in the real run.
What is checked
- Values are parameters, never text. The query text is the one you wrote. A flow value can never become part of it, so a caller's name or a form field cannot change what the query does. A query that tries to build text from values (
'{{...}}') is refused when you publish: the query is not a template. - One statement. A second statement, a comment and an unclosed quote are refused.
- Reads only, unless you allow changes. Without Allow changes to rows only statements that read (
SELECT,WITH,SHOW,VALUES) are accepted, and PostgreSQL and MySQL / MariaDB run them in a read-only transaction that is rolled back. SQL Server has no such transaction, so there the check of the statement is the guard: use a read-only login. - Never allowed, in either mode: creating, changing or dropping objects, granting rights, running programs or procedures, reading or writing files, and reaching other servers.
- Limits. A time limit for connecting and for the statement, a row limit, a size limit for each value, and small connection pools per login.
- Your company only. The login is looked up in your company's saved credentials; another company's login is not reachable. Publishing checks that the login exists and the query is valid for its database type, that every
:namehas a value, and that no value is left over.
Private addresses
Your server will not connect to a private address (your local network, localhost, a cloud metadata address) by default. If the database is on a private address, the person who runs the installation must approve it in the server configuration, as a database=host:port entry in MPBX_PRIVATE_EGRESS (entries are separated by commas, for example database=db.corp.lan:5432). A host approved for databases is not approved for anything else, and a company cannot add to the list. Link-local addresses (such as a cloud metadata address) are never reachable. A name is resolved once, every address is checked, and the connection goes to the address that was checked.
Limits worth knowing
- A step's result is at most a few kilobytes: use
LIMIT, aggregates and short column lists. - PostgreSQL and MySQL / MariaDB keep up to two connections per saved login; SQL Server connects for each query.
- A number or true/false value that you compare with a numeric column needs the parameter type Whole number or Number.
- Dates, times and timestamps come back as text in ISO form; decimal numbers come back as text, to keep their digits.