-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9394 from Tishj/python_execute_multi_statements
[Python] Throw explicit error for misuse of `execute`
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
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
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,38 @@ | ||
import duckdb | ||
import pytest | ||
|
||
|
||
class TestDuckDBExecute(object): | ||
def test_execute_basic(self, duckdb_cursor): | ||
duckdb_cursor.execute('create table t as select 5') | ||
res = duckdb_cursor.table('t').fetchall() | ||
assert res == [(5,)] | ||
|
||
def test_execute_many_basic(self, duckdb_cursor): | ||
duckdb_cursor.execute("create table t(x int);") | ||
|
||
# This works because prepared parameter is only present in the last statement | ||
duckdb_cursor.execute( | ||
""" | ||
delete from t where x=5; | ||
insert into t(x) values($1); | ||
""", | ||
(99,), | ||
) | ||
res = duckdb_cursor.table('t').fetchall() | ||
assert res == [(99,)] | ||
|
||
def test_execute_many_error(self, duckdb_cursor): | ||
duckdb_cursor.execute("create table t(x int);") | ||
|
||
# Prepared parameter used in a statement that is not the last | ||
with pytest.raises( | ||
duckdb.NotImplementedException, match='Prepared parameters are only supported for the last statement' | ||
): | ||
duckdb_cursor.execute( | ||
""" | ||
delete from t where x=$1; | ||
insert into t(x) values($1); | ||
""", | ||
(99,), | ||
) |