|
5 | 5 | # the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0 |
6 | 6 |
|
7 | 7 |
|
| 8 | +from asyncpg import exceptions |
| 9 | + |
| 10 | + |
8 | 11 | @cython.final |
9 | 12 | cdef class PreparedStatementState: |
10 | 13 |
|
@@ -92,18 +95,34 @@ cdef class PreparedStatementState: |
92 | 95 | Codec codec |
93 | 96 |
|
94 | 97 | if len(args) > 32767: |
95 | | - raise ValueError('number of arguments cannot exceed 32767') |
| 98 | + raise exceptions.InterfaceError( |
| 99 | + 'the number of query arguments cannot exceed 32767') |
96 | 100 |
|
97 | 101 | self._ensure_args_encoder() |
98 | 102 | self._ensure_rows_decoder() |
99 | 103 |
|
100 | 104 | writer = WriteBuffer.new() |
101 | 105 |
|
102 | | - if self.args_num != len(args): |
103 | | - raise ValueError( |
104 | | - 'number of arguments ({}) does not match ' |
105 | | - 'number of parameters ({})'.format( |
106 | | - len(args), self.args_num)) |
| 106 | + num_args_passed = len(args) |
| 107 | + if self.args_num != num_args_passed: |
| 108 | + hint = 'Check the query against the passed list of arguments.' |
| 109 | + |
| 110 | + if self.args_num == 0: |
| 111 | + # If the server was expecting zero arguments, it is likely |
| 112 | + # that the user tried to parametrize a statement that does |
| 113 | + # not support parameters. |
| 114 | + hint += (r' Note that parameters are supported only in' |
| 115 | + r' SELECT, INSERT, UPDATE, DELETE, and VALUES' |
| 116 | + r' statements, and will *not* work in statements ' |
| 117 | + r' like CREATE VIEW or DECLARE CURSOR.') |
| 118 | + |
| 119 | + raise exceptions.InterfaceError( |
| 120 | + 'the server expects {x} argument{s} for this query, ' |
| 121 | + '{y} {w} passed'.format( |
| 122 | + x=self.args_num, s='s' if self.args_num != 1 else '', |
| 123 | + y=num_args_passed, |
| 124 | + w='was' if num_args_passed == 1 else 'were'), |
| 125 | + hint=hint) |
107 | 126 |
|
108 | 127 | if self.have_text_args: |
109 | 128 | writer.write_int16(self.args_num) |
|
0 commit comments