SQL
Query
typescriptCopied1export default airplane.task(2{3slug: "find_user_by_name",4// Attach SQL resource to task5resources: ["my_sql_db"],6parameters: { name: "shorttext" },7},8async (params) => {9const run = await airplane.sql.query<{ id: string; name: string; email: string }>(10// The slug of the SQL resource to query11"my_sql_db",12// SQL query to execute13"select * from users where name ilike :name",14// Query arguments15{ args: { name: `%${params.name}%` } },16);17return run.output.Q1;18},19);
name:sqlCopied1-- Query2select * from users3where name ilike :name;4-- Query Args5{ "name": "%colin%" }
Slug of SQL resource to use. See Resources.
The SQL query to execute. Can include multiple queries separated by semicolons which will be executed within a single transaction.
Values that can be safely referenced from a query.
By default, Airplane will parse the query and choose the most relevant kind of transaction. If readOnly, write operations will fail. If none, queries will be applied one-at-a-time without a transaction.
Output
query returns rows (e.g. a SELECT or INSERT... RETURNING statement) then sql.query will
return:List of rows returned. Each row is an object mapping column headers to column values.
The number of rows that inserted, updated, or deleted by the query.
query includes multiple SQL statements, the response will include a response for each query.
Each query's response will be identified by a field with its query index (Q1, Q2, Q3, ...).pythonCopied1import airplane23@airplane.task(4resources=[5# Attach SQL resource to task6airplane.Resource("my_sql_db"),7]8)9def find_user_by_name(name: str):10run = airplane.sql.query(11# The slug of the SQL resource to query12sql_resource="my_sql_db",13# SQL query to execute14query="select * from users where name ilike :name",15# Query arguments16query_args={"name": f"%{name}%"},17)18return run.output["Q1"]
name:sqlCopied1-- Query2select * from users3where name ilike :name;4-- Query Args5{ "name": "%colin%" }
Slug of SQL resource to use. See Resources.
The query to run on the SQL resource. Multiple queries can be separated by semicolons.
Optional map of query arg names to values to insert into the query.
Optional transaction mode with which to run the query. It has the following options: AUTO, READ_ONLY, READ_WRITE, NONE.
Whether or not to omit leading whitespace from query.
Output
id, task id, param values, status and outputs of the executed run.pythonCopied1result = sql.query(2"my_db",3'''4SELECT email FROM users;5SELECT name FROM teams;6''',7)
-
result.outputwill be a dictionary with keysQ1andQ2. -
result.output["Q1"]will contain a dictionary of{"email": ...}from theSELECT email FROM users;query. -
result.output["Q2"]will contain a dictionary of{"name": ...}from theSELECT name FROM teams;query.
RETURNING clause, the output will be a
dictionary with key rows_affected containing the number of rows affected by the query.If the prompt cannot be created properly.
If the run fails or is cancelled.