Skip to content

Commit a6a728d

Browse files
committed
Add support for binding arrays by index
Signed-off-by: Salil Chandra <[email protected]>
1 parent 6bba605 commit a6a728d

File tree

6 files changed

+23
-43
lines changed

6 files changed

+23
-43
lines changed

comdb2/_ccdb2.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,13 @@ cdef class Handle(object):
421421
cval.type, cval.data, cval.size)
422422
else:
423423
if bind_by_index:
424-
raise ValueError(
425-
"Binding arrays by index is currently unsupported."
426-
" You must bind by name when binding arrays."
427-
)
428-
rc = lib.cdb2_bind_array(self.hndl, <char*>ckey,
429-
cval.type, cval.data,
430-
cval.list_size, cval.size)
424+
rc = lib.cdb2_bind_array_index(self.hndl, ckey,
425+
cval.type, cval.data,
426+
cval.list_size, cval.size)
427+
else:
428+
rc = lib.cdb2_bind_array(self.hndl, <char*>ckey,
429+
cval.type, cval.data,
430+
cval.list_size, cval.size)
431431
_errchk(rc, self.hndl)
432432

433433
with nogil:

comdb2/_cdb2api.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ cdef extern from "cdb2api.h" nogil:
7878
int cdb2_bind_param(cdb2_hndl_tp *hndl, const char *name, int type, const void *varaddr, int length) except +
7979
int cdb2_bind_index(cdb2_hndl_tp *hndl, int index, int type, const void *varaddr, int length) except +
8080
int cdb2_bind_array(cdb2_hndl_tp *hndl, const char *name, cdb2_coltype, const void *varaddr, size_t count, size_t typelen) except +
81+
int cdb2_bind_array_index(cdb2_hndl_tp *hndl, int index, cdb2_coltype, const void *varaddr, size_t count, size_t typelen) except +
8182
int cdb2_clearbindings(cdb2_hndl_tp *hndl) except +
8283
int cdb2_clear_ack(cdb2_hndl_tp *hndl) except +

comdb2/cdb2.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@
107107
>>> print(list(hndl.execute(query, [20, 42])))
108108
[[1]]
109109
110-
Note:
111-
112-
Arrays can currently only be bound by name, not positionally.
113-
114110
Types
115111
-----
116112
@@ -400,7 +396,6 @@ def execute(
400396
you must pass a mapping from parameter name to value.
401397
If the SQL statement has ``?`` style placeholders, you must
402398
instead pass an ordered sequence of parameter values.
403-
Note that arrays can currently only be bound by name.
404399
column_types (Sequence[int]): An optional sequence of types (values
405400
of the `ColumnType` enumeration) which the columns of the
406401
result set will be coerced to.

comdb2/dbapi2.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@
137137
In this example, we execute the query with the first ``?`` bound to 20 and the
138138
second ``?`` bound to 42, so a ``1`` is returned like in the previous example.
139139
140-
Note:
141-
142-
Arrays can currently only be bound by name, not positionally.
143-
144140
Types
145141
-----
146142
@@ -1057,7 +1053,6 @@ def execute(
10571053
you must pass a mapping from parameter name to value.
10581054
If the SQL statement has ``?`` style placeholders, you must
10591055
instead pass an ordered sequence of parameter values.
1060-
Note that arrays can currently only be bound by name.
10611056
column_types (Sequence[int]): An optional sequence of types (values
10621057
of the `ColumnType` enumeration) which the columns of the
10631058
result set will be coerced to.

tests/test_cdb2.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,6 @@ def test_parameter_name_in_binding_errors_noexception():
307307
)
308308

309309

310-
def test_binding_array_by_index():
311-
hndl = cdb2.Handle("mattdb", "dev")
312-
313-
with pytest.raises(Exception) as exc:
314-
hndl.execute("select * from carray(?)", [[1, 2, 3]])
315-
316-
assert exc.value.args[0] == (
317-
"Binding arrays by index is currently unsupported. "
318-
"You must bind by name when binding arrays."
319-
)
320-
321-
322310
def test_specifying_column_types():
323311
# GIVEN
324312
hndl = cdb2.Handle("mattdb", "dev")

tests/test_dbapi2.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,13 @@ def test_parameter_binding_arrays(values):
990990
cursor.execute("select * from carray(%(values)s)", dict(values=values))
991991
results = cursor.fetchall()
992992

993+
# THEN
994+
assert results == [[v] for v in values]
995+
996+
# WHEN
997+
cursor.execute("select * from carray(?)", [values])
998+
results = cursor.fetchall()
999+
9931000
# THEN
9941001
assert results == [[v] for v in values]
9951002
conn.close()
@@ -1000,27 +1007,27 @@ def test_parameter_binding_arrays(values):
10001007
[
10011008
(
10021009
[],
1003-
"Can't bind list value [] for parameter 'values': "
1010+
"Can't bind list value [] for parameter {}: "
10041011
+ "ValueError: empty lists cannot be bound",
10051012
),
10061013
(
10071014
(),
1008-
"Can't bind tuple value () for parameter 'values': "
1015+
"Can't bind tuple value () for parameter {}: "
10091016
+ "ValueError: empty tuples cannot be bound",
10101017
),
10111018
(
10121019
[1, "hello"],
1013-
"Can't bind list value [1, 'hello'] for parameter 'values': "
1020+
"Can't bind list value [1, 'hello'] for parameter {}: "
10141021
+ "ValueError: all list elements must be the same type",
10151022
),
10161023
(
10171024
(1j,),
1018-
"Can't bind tuple value (1j,) for parameter 'values': "
1025+
"Can't bind tuple value (1j,) for parameter {}: "
10191026
+ "ValueError: Cannot bind a tuple of complex",
10201027
),
10211028
(
10221029
((1, 2, 3),),
1023-
"Can't bind tuple value ((1, 2, 3),) for parameter 'values': "
1030+
"Can't bind tuple value ((1, 2, 3),) for parameter {}: "
10241031
+ "ValueError: Cannot bind a tuple of tuple",
10251032
),
10261033
],
@@ -1031,18 +1038,12 @@ def test_parameter_binding_invalid_arrays(values, exc_msg):
10311038
cursor = conn.cursor()
10321039

10331040
# WHEN/THEN
1034-
with pytest.raises(DataError, match=re.escape(exc_msg)):
1041+
with pytest.raises(DataError, match=re.escape(exc_msg.format("'values'"))):
10351042
cursor.execute("select * from carray(%(values)s)", dict(values=values))
10361043

1037-
1038-
def test_parameter_binding_arrays_by_index():
1039-
# GIVEN
1040-
conn = connect("mattdb", "dev")
1041-
cursor = conn.cursor()
1042-
10431044
# WHEN/THEN
1044-
with pytest.raises(ValueError) as exc:
1045-
cursor.execute("select * from carray(?)", [[1, 2, 3]])
1045+
with pytest.raises(DataError, match=re.escape(exc_msg.format("1"))):
1046+
cursor.execute("select * from carray(?)", [values])
10461047

10471048

10481049
def test_specifying_column_types():

0 commit comments

Comments
 (0)