Skip to content
Reference

SQL database adapter

Read from, write to and look up rows in a PostgreSQL, SQL Server, MySQL or Oracle database from a workflow, the way Mirth's Database Reader and Database Writer do.

On this page

The SQL database adapter lets a workflow talk to a relational database the way Mirth's Database Reader and Database Writer and Rhapsody's Database communication point do. Healthcare organizations keep a lot of integration traffic in staging tables, reporting databases and crosswalk tables, and this adapter is how MedHook reaches them.

It supports PostgreSQL, SQL Server, MySQL (and MariaDB) and Oracle. The drivers are pure JavaScript: Oracle runs in thin mode, so the engine needs no Oracle client install. Set the profile's dialect, and see Dialect notes for what differs between them.

The three uses

UseWhereWhat it does
ReaderA polling trigger with the poll actionRuns a SELECT on a schedule and starts one execution per returned row. An optional post-process statement marks each row.
WriterAn adapter step with the execute actionRuns one INSERT, UPDATE or DELETE with values from the message, and returns rowCount.
LookupA transform step of type sql-lookupRuns a one-row SELECT in the middle of a workflow and writes the answer onto the message, for MRN or facility crosswalks.

A query action also runs any single statement from an adapter step and returns { rows, rowCount, truncated }. It runs READ ONLY unless the step sets readOnly: false, and returns at most maxRows rows (default 1000).

Values are always bound

Name each value in the SQL as :name, whatever the database. The engine turns each name into the database's own placeholder ($n on PostgreSQL, @pn on SQL Server, ? on MySQL, :pn on Oracle) and sends the statement and the values separately, so a value is never pasted into the SQL text.

sql
SELECT org_id FROM facility_map WHERE facility = :facility

The engine refuses a statement before it reaches the database when:

  • it holds more than one statement (SELECT 1; DROP TABLE t). One trailing ; is fine.
  • it uses a placeholder written by hand: $n (PostgreSQL, SQL Server), ? (MySQL) or :n with a number (Oracle). Name the value instead.
  • on MySQL, it holds an executable comment (/*! ... */), which MySQL runs as SQL.
  • it contains a {{...}} template. For the poll cursor, write :cursor.

A polling trigger's SELECT and post-process statement are checked the same way when the workflow is saved, and the poll never pastes its cursor into SQL text: the cursor only ever reaches the database as the bound :cursor value.

A :name inside a string literal, a quoted identifier, a comment or a :: cast is left alone. The engine reads literals and comments the way each database does: [brackets] on SQL Server, backslash escapes, backticks and # comments on MySQL, q'[...]' literals on Oracle, dollar quotes on PostgreSQL.

PostgreSQL (the extended query protocol) and MySQL (server-side prepared statements) also refuse a second statement on their own. SQL Server does not need a ; between statements, so the engine cannot always see a second one there.

Read-only is a safeguard, not a guarantee

A read-only call (the poll, a lookup, a query step) asks the database not to write, or rolls back what it did, but some statements get past that: DDL (CREATE, ALTER, DROP, TRUNCATE) commits on its own on MySQL and Oracle, DELETE FROM t COMMIT commits inside the call on SQL Server, and xp_cmdshell, OPENROWSET(BULK ...), UTL_HTTP and other UTL_/DBMS_ packages act outside the transaction altogether. As in Mirth, the engine runs them, but it warns when a workflow is saved or validated and a read-only statement uses COMMIT, ROLLBACK, DDL, EXEC, CALL, SET GLOBAL, xp_, sp_configure, OPENROWSET, OPENDATASOURCE, BULK, UTL_, DBMS_ or LOAD DATA outside literals and comments. If a SQL Server read-only call cannot be rolled back, the step fails with an uncertain outcome instead of succeeding silently.

The real control is the database user: connect with a least-privilege user that can only SELECT the tables a reader or lookup needs, and give a writer's user only INSERT/UPDATE (or EXECUTE on its procedure). Use separate profiles for reading and writing.

The adapter profile

SettingWhat it does
dialectpostgres (default), sqlserver, mysql (MySQL or MariaDB) or oracle.
host, portThe database server. The port defaults to 5432, 1433, 3306 or 1521 for the dialect.
database, userThe database and the user to connect as. On Oracle, database is the service name. Give the user only the rights it needs.
oracleSidOracle only: database is a SID rather than a service name.
passwordEncrypted at rest with the other adapter credentials, masked in the UI and the API, and never written to a log.
tls.modeverify-full (default) checks the certificate and the host name. require encrypts without checking. disable sends no TLS.
tls.caPemThe CA certificate the server's certificate must chain to, for a private CA. Not for Oracle: see Dialect notes.
tls.serverCertDnOracle only: the certificate subject DN verify-full matches. Default CN=<host>.
poolSizeConnections kept open at most, 1 to 20. Default 5.
statementTimeoutMsHow long one statement may run, 1 to 300 seconds. Default 30 seconds.

The full list is under Adapter settings.

A host on a private network, such as 10.20.1.40 or a name that resolves to one, is refused unless your operator adds it to SSRF_ALLOWLIST. The REST and FHIR adapters follow the same rule. tls.mode: disable is accepted only for a host on that allowlist, such as a database reached over an IPSec tunnel. The engine connects to the address it checked, so a second DNS answer cannot redirect it.

Reader: poll a table

Set the trigger's adapter profile to the SQL database profile and its action to poll, with these parameters:

ParameterWhat it does
sqlThe SELECT. :cursor binds the stored cursor and :limit binds limit.
cursorSet to {{cursor}}. The trigger fills in the last cursor value it stored.
cursorInitialThe value :cursor binds before the first cursor is stored, such as 1970-01-01 or 0.
limitRows one poll reads at most. Default 100.
postProcessOptional { sql, bind }. Runs after each row's workflow, with each bind column taken from the row. It also runs for a row the inbound filter rejects.

On the trigger, set Items path to rows, Cursor field to the cursor column and ID field to the primary key. For example:

json
{
  "sql": "SELECT id, mrn, result, updated_at FROM lab_inbox WHERE updated_at >= :cursor ORDER BY updated_at LIMIT :limit",
  "cursor": "{{cursor}}",
  "cursorInitial": "1970-01-01",
  "postProcess": {
    "sql": "UPDATE lab_inbox SET processed = true WHERE id = :id",
    "bind": ["id"]
  }
}

The poll's SELECT always runs read-only (see Dialect notes for how each database enforces it). Timestamps come back as text, so the stored cursor binds back as the same value. Each row reaches the workflow as input.item.

Each database writes the page limit its own way:

DialectOne page of new rows
PostgreSQL... WHERE updated_at >= :cursor ORDER BY updated_at LIMIT :limit
MySQL... WHERE updated_at >= :cursor ORDER BY updated_at LIMIT :limit
SQL ServerSELECT TOP (:limit) ... WHERE updated_at >= :cursor ORDER BY updated_at
Oracle... WHERE updated_at >= :cursor ORDER BY updated_at FETCH FIRST :limit ROWS ONLY

Nothing lost, nothing sent twice

The cursor lives in the engine's database and the row lives in yours, so the two cannot share one transaction. The engine orders the work so that a crash at any point is safe:

  1. The row runs through the inbound duplicate check, so one row and cursor value start one execution, even across a restart.
  2. The post-process statement runs in its own transaction on your database.
  3. Only then does the cursor move past the row.

If the engine stops after the first step, the row is read again, the duplicate check finds the execution that already ran, and only the post-process runs. If the post-process fails, the poll stops before that row and records the error, and the next poll tries again. So the post-process must be idempotent: UPDATE ... SET processed = true WHERE id = :id is, an INSERT into an audit table is not. Keep the duplicate window (INBOUND_DEDUP_TTL_HOURS) longer than the poll interval.

A row the trigger's inbound filter rejects starts no execution, but its post-process still runs, as in a Mirth Database Reader. A reader that selects WHERE processed = false with no cursor column therefore stops returning rejected rows, and they never crowd out new ones.

Writer: run a statement per message

An adapter step with the execute action runs its sql in one transaction and returns { rowCount }. The step's config holds the SQL. The values come from the step input's params, so put a transform before it that builds them:

json
{ "params": { "mrn": "TEST123", "result": "NEGATIVE" } }
sql
INSERT INTO results_archive (mrn, result) VALUES (:mrn, :result)

A stored procedure is one statement too: CALL archive_result(:mrn) on PostgreSQL, MySQL and Oracle, EXEC dbo.archive_result @mrn = :mrn on SQL Server. An Oracle PL/SQL block (BEGIN ... END;) holds several statements and is refused; call the procedure with CALL.

A statement the database rejects, such as a constraint violation, a syntax error or a missing table, fails the same way on every attempt and is not retried. Conditions that clear up on their own are retried, because the transaction rolled back and nothing was written: a deadlock or serialization failure, too many connections or another resource shortage, a server that is shutting down, restarting or failing over, and a lock that was not available. A connection lost before the commit is safe to retry for the same reason. A connection lost during the commit is never retried, because the row may already be written.

Lookup: read one value mid-workflow

A transform step of type sql-lookup runs a one-row SELECT against an SQL database profile of the workflow's workspace and writes the answer onto a copy of the message.

SettingWhat it does
config.adapterProfileIdThe SQL database profile.
config.sqlThe SELECT, with :name values.
config.paramsEach :name and the input path it reads, such as { "facility": "visit.facility" }. A missing value binds NULL.
config.targetThe path the answer is written to, such as visit.orgId.
config.pickvalue (default) writes the first column of the first row. row writes the whole row.
config.onMissingfail (default) fails the step when no row matches. null writes null and continues.

The lookup always runs read-only (see Read-only is a safeguard, not a guarantee). The same lookup with the same values runs once per execution: a loop over 200 lab results that share a facility queries the database once. The next execution reads fresh rows.

Errors and logs

A database error keeps its code (the SQLSTATE on PostgreSQL and MySQL, the error number on SQL Server, the ORA- code on Oracle) and its message, and drops the rest. The detail, hint and bound values that PostgreSQL adds (Key (mrn)=(TEST123) already exists) never reach the execution history, the UI or the log. SQL Server and MySQL quote values in the message itself (Duplicate entry 'TEST123'), so every quoted part of their messages is masked. A data error, which quotes the bad value, keeps only its code, and a login failure says only that authentication failed. Query results are treated as PHI like every other message: they are scrubbed from logs and kept only as long as execution data is kept.

Dialect notes

DialectDriverRead-only callsTLS
PostgreSQLpgBEGIN READ ONLYverify-full checks the certificate against tls.caPem or the public CAs.
SQL Servermssql (tedious)Run in a transaction that is rolled backencrypt on; require sets trustServerCertificate. SQL Server's own self-signed certificate needs require or a tls.caPem.
MySQLmysql2START TRANSACTION READ ONLY, then rolled backverify-full checks the certificate and the host name. MySQL's auto-generated certificate needs require or a tls.caPem. LOCAL INFILE is off, so a server cannot ask the engine for a file.
Oracleoracledb, thin modeRun in a transaction that is rolled backTCPS. Thin mode always checks the certificate chain against the CAs Node trusts, so a private CA goes in NODE_EXTRA_CA_CERTS on the engine, not in tls.caPem. verify-full also matches the certificate subject: tls.serverCertDn, or CN=<host> when it is not set; require does not.

The statement timeout is enforced by each driver, which cancels the statement or drops the connection, and by the server on PostgreSQL. Every dialect connects to the address the host guard checked, so a second DNS answer cannot redirect it. Oracle thin mode can match a certificate only to the address it dialled, so verify-full for a host name matches the subject DN instead.

A SQL Server can answer the login with a routing redirect (Azure SQL, Always On read-only routing); the engine checks the target with the same host guard before it connects there, so a redirect to a private address needs SSRF_ALLOWLIST like the profile's own host. An Oracle listener can also redirect the connection (RAC, shared servers, Connection Manager); the engine checks that target the same way, connects to the address it checked, ignores an HTTPS proxy named in the redirect, and with TLS on refuses a redirect to a target without TCPS. With TLS on, a SQL Server that answers the PRELOGIN without agreeing to encrypt is refused before the login is sent, so the password and rows never travel in clear.

A query step or lookup never holds more than maxRows + 1 rows on SQL Server, MySQL or Oracle: the driver stops reading (SQL Server, Oracle) or drops the rest of the stream (MySQL). A MySQL statement that hits the timeout is also killed on the server.

On Oracle, column names that Oracle upper-cases (unquoted identifiers) come back in lower case, as Mirth's Database Reader gives them, and true and false bind as 1 and 0. Oracle refuses a trailing ; on a SQL statement, so the engine drops it. Thin mode reads dates through a JavaScript Date, so an Oracle timestamp cursor keeps millisecond precision: use >= so a row at the cursor is read again rather than missed.

Import from Mirth

The Mirth importer maps a Database Reader to a polling trigger on this adapter, with the SELECT carried over and the update statement as the post-process. Mirth's ${id} in the update becomes :id, bound from the row. A Database Writer becomes an execute step, with ${mrn} as :mrn. The findings ask you to add a cursor and to map the writer's values. See Migrate from Mirth Connect.

The JDBC URL gives the profile's dialect, host, port, database and TLS mode:

JDBC URLProfile
jdbc:postgresql://host:5432/db?sslmode=...postgres
jdbc:sqlserver://host:1433;databaseName=db;encrypt=...sqlserver; trustServerCertificate=true becomes require
jdbc:jtds:sqlserver://host:1433/db;ssl=...sqlserver; jTDS sends no TLS unless ssl is set
jdbc:mysql://host:3306/db?sslMode=..., jdbc:mariadb://...mysql
jdbc:oracle:thin:@host:1521:SID, @//host:1521/service, @tcps://... or @(DESCRIPTION=...)oracle, with oracleSid for a SID; plain TCP becomes disable

A user name or password in the URL, and Mirth's username and password fields, are never imported: the profile is created without them. A URL with TLS off keeps TLS off, which the engine accepts only for a host on SSRF_ALLOWLIST, and the finding says so. The Mirth exporter writes the profile back as a Database Reader or Database Writer with the same JDBC URL and driver class, and leaves the username and password blank for you to enter in Mirth.

Reference