Skip to content

Commit d561f9a

Browse files
lukekimpeasee
andauthored
Add Parameterized Queries (#964)
* Add MCP * Add Parameterized Queries * Add prepared statements * Remove unsupported section * Update website/docs/reference/sql/prepared_statements.md Co-authored-by: peasee <98815791+peasee@users.noreply.github.com> --------- Co-authored-by: peasee <98815791+peasee@users.noreply.github.com>
1 parent 8ffdd24 commit d561f9a

6 files changed

Lines changed: 84 additions & 2 deletions

File tree

website/docs/api/adbc/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,26 @@ pyarrow.Table
7474
2.0: [[2]]
7575
'Hello, world!': [["Hello, world!"]]
7676
```
77+
78+
## Parameterized Queries
79+
80+
Spice supports parameterized queries when using ADBC clients. Parameterized queries help prevent SQL injection and improve code clarity by separating query logic from data values. The following example demonstrates how to use parameterized queries with the Python ADBC FlightSQL driver:
81+
82+
```python
83+
from adbc_driver_flightsql import DatabaseOptions
84+
from adbc_driver_flightsql.dbapi import connect
85+
86+
with connect(
87+
"grpc://127.0.0.1:50051",
88+
) as conn:
89+
with conn.cursor() as cur:
90+
cur.execute("SELECT $1 + 1 AS the_answer", parameters=(41,))
91+
table = cur.fetch_arrow_table()
92+
print(table)
93+
94+
cur.execute("SELECT 1 AS one")
95+
table = cur.fetch_arrow_table()
96+
print(table)
97+
98+
conn.close()
99+
```

website/docs/api/jdbc/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,7 @@ Replace `<enter-api-key-here>` with the API key value. The `user` and `password`
7979
In the configured application, run a sample query, such as `SELECT * FROM taxi_trips;`
8080

8181
![Query Results](https://imagedelivery.net/HyTs22ttunfIlvyd6vumhQ/0e9f3c0f-2e03-47f9-8d5e-65e078d7e900/public 'Query Results')
82+
83+
## Parameterized Queries
84+
85+
Spice supports parameterized queries with JDBC. Parameterized queries help prevent SQL injection and improve code clarity by separating query logic from data values.

website/docs/api/odbc/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@ SELECT trip_distance, total_amount FROM taxi_trips ORDER BY trip_distance DESC L
126126
```
127127

128128
<img width="800" alt="Example Query Results" src="/img/odbc/spice-odbc-example-query.png" />
129+
130+
## Parameterized Queries
131+
132+
Spice supports parameterized queries with ODBC. Parameterized queries help prevent SQL injection and improve code clarity by separating query logic from data values.

website/docs/api/overview.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 'API Overview'
33
sidebar_label: 'Overview'
44
sidebar_position: 1
5-
description: 'API Overview'
5+
description: 'Spice.ai API overview, including SQL query interfaces, OpenAI-compatible endpoints, Iceberg catalog REST APIs, and the Model Context Protocol (MCP) for integrating external tools.'
66
pagination_prev: null
77
pagination_next: null
88
---
@@ -21,3 +21,11 @@ Spice provides high-performance, industry-standard APIs:
2121
### Iceberg Catalog REST APIs
2222

2323
- **HTTP APIs**: Unified API for consuming Apache Iceberg catalogs in data lake architectures.
24+
25+
### MCP API
26+
27+
- **HTTP APIs**: The Model Context Protocol (MCP) helps integrate external tools and services into the Spice runtime. MCP tools can be accessed via HTTP APIs for tool integration and orchestration. For details, see the [MCP documentation](/docs/features/large-language-models/mcp).
28+
29+
:::note
30+
HTTP Streaming support for MCP is coming soon.
31+
:::

website/docs/reference/sql/explain.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Explain'
33
sidebar_label: 'Explain'
44
pagination_prev: 'reference/sql/subqueries'
55
pagination_next: 'reference/sql/information_schema'
6-
sidebar_position: 4
6+
sidebar_position: 5
77
---
88

99
:::info
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: 'Prepared Statements'
3+
sidebar_label: 'Prepared Statements'
4+
pagination_prev: 'reference/sql/subqueries'
5+
pagination_next: 'reference/sql/explain'
6+
sidebar_position: 4
7+
---
8+
9+
## Positional Arguments
10+
11+
Prepared statements can use positional arguments to support multiple parameters. Each parameter is referenced by its position in the statement.
12+
13+
**SQL Example**
14+
15+
To create a prepared statement named `greater_than` with two parameters:
16+
17+
```sql
18+
PREPARE greater_than(INT, DOUBLE) AS SELECT * FROM example WHERE a > $1 AND b > $2;
19+
```
20+
21+
To execute the prepared statement with integer and double arguments:
22+
23+
```sql
24+
EXECUTE greater_than(20, 23.3);
25+
```
26+
27+
**Python Example**
28+
29+
```python
30+
import adbc_driver_flightsql.dbapi
31+
32+
with adbc_driver_flightsql.dbapi.connect("grpc://localhost:50051") as conn:
33+
with conn.cursor() as cur:
34+
cur.execute("PREPARE greater_than(INT, DOUBLE) AS SELECT * FROM example WHERE a > $1 AND b > $2;")
35+
cur.execute("EXECUTE greater_than(?, ?)", (20, 23.3))
36+
result = cur.fetchall()
37+
print(result)
38+
```
39+
:::warning[Limitations]
40+
41+
* Positional arguments are not supported with the `date` keyword to construct a date value, like `date $1`. Specify the date value in the query instead: `l_shipdate > date '1995-01-01'`.
42+
43+
:::

0 commit comments

Comments
 (0)