:::info Spice is built on Apache DataFusion (opens in a new tab) and uses the PostgreSQL dialect, even when querying datasources with different SQL dialects. :::
Prepared statements bind parameter values to a query when it runs. Each parameter is referenced by its position in the statement, as $1, $2, and so on, or as ?. Spice supports two ways to use them:
PREPARE and EXECUTE. The statement is named with PREPARE and run later with EXECUTE. It is stored in a Flight SQL session, so it is only available to later requests in the same session.Pass the parameter values with the query:
To create a prepared statement named greater_than with two parameters:
To execute the prepared statement with integer and double arguments:
A prepared statement belongs to the session that created it. A Flight SQL client starts a session with a Flight Handshake call. The runtime returns a session ID in the response and in the x-session-id response header. The client then sends that ID in the x-session-id header on each later request. Sessions expire after one hour of inactivity.
A request that carries no session ID, including every HTTP /v1/sql (opens in a new tab) request, runs in a new context that does not keep the statement. Running PREPARE and EXECUTE as two separate requests without a session fails on the EXECUTE:
To run a parameterized query without managing a session, use Flight SQL parameter binding (opens in a new tab).
:::warning[Limitations]
date keyword to construct a date value, like date $1. Specify the date value in the query instead: l_shipdate > date '1995-01-01'.:::