-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation on using PREPARE / EXEC
It has been supported since 1.6.4 but it wasn't documented See also #116
- Loading branch information
1 parent
52f5dfb
commit 43f1195
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ Contents | |
introduction | ||
examples | ||
filetransfers | ||
prepared_statements | ||
batchsize | ||
api | ||
development | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Prepared Statements | ||
=================== | ||
|
||
MonetDB offers a PREPARE_ statement, which precompiles a SQL statement for | ||
later execution. If the statement is going to be executed frequently, | ||
precompiling will safe time. | ||
|
||
The PREPARE statement yields a numeric prepared-sql-id which can then be passed | ||
to the EXECUTE statement to execute it. For example, | ||
|
||
:: | ||
|
||
> sql>PREPARE SELECT ? + 42; | ||
> execute prepared statement using: EXEC 0(...) | ||
> ... | ||
> sql>execute 0(100); | ||
> +------+ | ||
> | %2 | | ||
> +======+ | ||
> | 142 | | ||
> +------+ | ||
> 1 tuple | ||
> sql> | ||
|
||
When PREPARE is called from pymonetdb, the prepared-sql-id will be made available | ||
in the `lastrowid` attribute of the cursor. For example, | ||
|
||
:: | ||
|
||
with pymonetdb.connect('demo') as conn, conn.cursor() as c: | ||
c.execute("PREPARE SELECT ? + 42") | ||
exec_id = c.lastrowid | ||
c.execute("EXECUTE %s(%s)", [exec_id, 100]) | ||
result = c.fetchone()[0] | ||
assert result == 142 | ||
|
||
Note: MonetDB versions older than Dec2023 (11.49.x) drop all prepared statements whenever | ||
the transaction fails. From Dec2023 onward, this has been corrected. | ||
|
||
|
||
.. _PREPARE: https://www.monetdb.org/documentation/user-guide/sql-manual/data-manipulation/prepare-statement/ |