Named queries
A named query is SQL kept on the gateway with the parameters it takes, the database it runs on and who may run it. Whoever runs it names the query and gives values; the SQL never leaves the gateway and the values are never pasted into it. This page is for engineers who write queries and administrators who make them public. The routes are in Named queries (API).
Why keep the SQL on the gateway
- A page or a program that only sends a name and values can't send a different statement. What it can do is what the query says.
- The database's address and password stay in the connection, not in the page.
- The check on each value (its type, its length, whether it is needed) happens on the gateway, for every caller alike.
- A change to the statement is one change in one place.
The Named queries page
Open Connections, then Named queries. Engineers and administrators see it; the list shows each query's name, kind, access, database, whether it is on, how many times it ran and failed, when it last ran and how many runs there were in the last minute. The counts are since the gateway started. Selecting a query, or New query, opens the editor in a panel beside the list.
- Under Query, give the Name (lowercase letters, digits,
-and_), an optional Description and the Database it runs on. - Choose the Kind: a query that reads, or an update.
- Under SQL, write the statement with each value as
@name. Tables and columns lists what the database holds, and a click puts a name into the SQL. - Under Parameters, list each value the caller gives: its name, kind (text, integer, number, boolean or time), whether it is needed, a longest length for text and a default. Add parameters used in the SQL lists the
@namesit finds in the statement. - Under Who may run it, choose the Access. Public is open to an administrator for any query, and to an engineer for an update; for a query that reads data an engineer sees it switched off with the reason, and sees such a public query read-only. For a public query, Each address and All callers are the calls a minute allowed from one client address and from all clients together. Most rows limits what a reading query returns.
- On switches the query on or off. Click Create query or Save.
The saved query has three more blocks. Activity shows its counts, last run and last error. Test has one input for each parameter and runs the query as it stands in the form, saved or not; an update runs in a rolled-back transaction and reports how many rows would change. Call it gives the query's address and a curl command to copy. A parameter you filled in under Test is in the command; one you left empty shows its kind. Delete query removes it.
Two kinds
| Kind | Statement | Answers with |
|---|---|---|
query | One SELECT, or WITH ... SELECT, run in a read-only transaction where the server has them | The rows, as columns with types, and whether more rows were left out |
update | One INSERT, UPDATE or DELETE | How many rows it changed. It is committed when it succeeds |
Write each parameter in the SQL as @name and list it with a type: text, integer, number, boolean or time. A value that doesn't fit its type or length, or a value for a parameter the query doesn't have, is refused before the database is reached. The gateway asks the database to bind each value as a value, so text such as '); DROP TABLE t; -- is stored as text.
INSERT INTO contact_messages (received_at, name, email, company, message, page)
VALUES (UTC_TIMESTAMP(3), @name, @email, @company, @message, @page)
The statement is checked when it is saved and again each time it runs, with the same guard as a dashboard's SQL: no second statement, and none of the words that change the database itself (DROP, ALTER, CREATE, TRUNCATE, GRANT, EXEC and the like). The guard stops mistakes and the obvious misuse; it is not a sandbox. Give the connection a database user that may do only what its queries do.
Who may run a query
| Access | Who |
|---|---|
viewer, operator, engineer | Someone signed in with that role or a higher one, or an API key with it |
public | Anyone who can reach the gateway, without signing in |
Engineers write queries, and an engineer (or an API key with the engineer role) may make an update public: what a stranger can do with it is limited to the statement's own INSERT, UPDATE or DELETE of values it checks, at a limited rate. Only an administrator makes a query that reads data public, or changes one that is, because everyone on the network would then see what it returns. An engineer can still delete a public query.
Public queries
A public query is what a page or a program outside the plant uses when there is nobody to sign in: the contact form on a website, the check-in of a gateway. It runs at its own address, /api/v1/public/queries/{name}/run, which answers only for public queries and tells a stranger as little as it can:
- A query that is not public, is off, or does not exist gets the same
404. - A failure in the database is answered with a plain sentence. The database's own message, and where it lives, go to the gateway's log.
- A body over 32 KB is refused.
- Each public query allows so many calls a minute from one client address and so many from all clients together, and answers
429past either. The defaults are 10 and 120. The total is the limit that holds however callers present themselves.
A page on another site needs its origin in AUTH_TRUSTED_ORIGINS for the browser to read the answer, and sends the call without cookies.
const response = await fetch("https://app.scadex.io/api/v1/public/queries/contact/run", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "omit",
body: JSON.stringify({ params: { name, email, company, message, page } }),
});
Running a query from a script
system.db.runNamedQuery(name, params) runs a query from a script with the role the script runs as. See system.db.
var rows = system.db.runNamedQuery('contact-messages');
Testing
While you write a query you can run it as it stands, saved or not, with values for its parameters. A query runs. An update runs inside a transaction that is rolled back, so the test tells you how many rows it would change and changes none.
What the gateway keeps about a query
Since the gateway started, and in memory only: how many times it ran, how many of those failed, when it last ran and whether that worked, the last error, how long the last run took, and the runs in the last minute. A restart starts the counts again.
The collecting gateway
The gateway at app.scadex.io uses named queries for two things: the website's contact form (contact) and the hourly check-in of every installation (call-home). Its tables, queries and the script that puts them on a gateway are in deploy/collector of the platform repository.