sql
Runs an arbitrary SQL query against a database and (optionally) returns the result as an array of objects, one for each row returned.
For each message the configured query is executed against the target database using the selected driver and data source name. Query arguments can be supplied from the message contents through a Bloblang mapping, and when the query returns rows the result can be written back into the message.
# Config fields, showing default values
processor:
label: ""
sql:
driver: "" # No default (required)
data_source_name: "" # No default (required)
query: "" # No default (required)
unsafe_dynamic_query: false
args_mapping: ""
result_codec: none
Examples
- Insert rows
- Parameterised read
Insert rows into a MySQL table, populating the columns with values extracted from each message. MySQL uses question mark placeholders.
processor:
sql:
driver: mysql
data_source_name: "foouser:foopassword@tcp(localhost:3306)/foodb"
query: "INSERT INTO footable (foo, bar, baz) VALUES (?, ?, ?);"
args_mapping: root = [ this.foo, this.bar, this.baz ]
Run a parameterised query against PostgreSQL, supplying the placeholder argument from the message. PostgreSQL uses incrementing dollar sign placeholders.
processor:
sql:
driver: postgres
data_source_name: postgres://foouser:foopass@localhost:5432/foodb?sslmode=disable
query: "SELECT * FROM footable WHERE id = $1;"
args_mapping: root = [ this.id ]
Fields
driver
A database driver to use. Supported values include mysql, postgres, pgx, clickhouse, mssql, sqlite, oracle, snowflake, trino, gocosmos, spanner and databricks.
Type: string
data_source_name
The data source name used to connect to the database.
Type: string
query
The query to execute. The style of placeholder to use depends on the driver; some drivers require question marks (?) whereas others expect incrementing dollar signs ($1, $2, and so on) or colons (:1, :2, and so on).
Type: string
unsafe_dynamic_query
Whether to enable interpolation functions in the query. Great care should be taken to ensure your queries are defended against injection attacks.
Type: bool
Default: false
args_mapping
An optional Bloblang mapping which should evaluate to an array of values matching in size to the number of placeholder arguments in the field query.
Type: string
Default: ""
result_codec
The result codec to use when returning the query response.
Type: string
Default: none