Skip to content

Commit

Permalink
Add documentation on using PREPARE / EXEC
Browse files Browse the repository at this point in the history
It has been supported since 1.6.4 but it wasn't documented

See also #116
  • Loading branch information
joerivanruth committed Aug 26, 2024
1 parent 52f5dfb commit 43f1195
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Contents
introduction
examples
filetransfers
prepared_statements
batchsize
api
development
Expand Down
41 changes: 41 additions & 0 deletions doc/prepared_statements.rst
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/

0 comments on commit 43f1195

Please sign in to comment.