blob: 2fd9ba3ca801ad7b49861923b8f63b9543ebafcf [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* statement.c - the statement type
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "statement.h"
25#include "cursor.h"
26#include "connection.h"
27#include "microprotocols.h"
28#include "prepare_protocol.h"
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +020029#include "util.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030
31/* prototypes */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000032static int pysqlite_check_remaining_sql(const char* tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033
34typedef enum {
35 LINECOMMENT_1,
36 IN_LINECOMMENT,
37 COMMENTSTART_1,
38 IN_COMMENT,
39 COMMENTEND_1,
40 NORMAL
41} parse_remaining_sql_state;
42
Gerhard Häringe7ea7452008-03-29 00:45:29 +000043typedef enum {
44 TYPE_LONG,
45 TYPE_FLOAT,
Gerhard Häringe7ea7452008-03-29 00:45:29 +000046 TYPE_UNICODE,
47 TYPE_BUFFER,
48 TYPE_UNKNOWN
49} parameter_type;
50
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +020051pysqlite_Statement *
52pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053{
54 const char* tail;
55 int rc;
Guido van Rossum83857e32007-05-09 23:37:01 +000056 const char* sql_cstr;
57 Py_ssize_t sql_cstr_len;
Berker Peksagab994ed2016-09-11 12:57:15 +030058 const char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059
Victor Stinnerc6a23202019-06-26 03:16:24 +020060 assert(PyUnicode_Check(sql));
61
Serhiy Storchaka06515832016-11-20 09:13:07 +020062 sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +000063 if (sql_cstr == NULL) {
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +020064 PyErr_Format(pysqlite_Warning,
65 "SQL is of wrong type ('%s'). Must be string.",
66 Py_TYPE(sql)->tp_name);
67 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 }
Serhiy Storchaka42d67af2014-09-11 13:29:05 +030069 if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +020070 PyErr_SetString(PyExc_ValueError,
71 "the query contains a null character");
72 return NULL;
Serhiy Storchaka42d67af2014-09-11 13:29:05 +030073 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +020075 pysqlite_Statement *self = PyObject_GC_New(pysqlite_Statement,
76 pysqlite_StatementType);
77 if (self == NULL) {
78 return NULL;
79 }
80
81 self->db = connection->db;
82 self->st = NULL;
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +010083 self->sql = Py_NewRef(sql);
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +020084 self->in_use = 0;
85 self->is_dml = 0;
86 self->in_weakreflist = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087
Berker Peksag4a926ca2017-02-26 18:22:38 +030088 /* Determine if the statement is a DML statement.
89 SELECT is the only exception. See #9924. */
Berker Peksagab994ed2016-09-11 12:57:15 +030090 for (p = sql_cstr; *p != 0; p++) {
91 switch (*p) {
92 case ' ':
93 case '\r':
94 case '\n':
95 case '\t':
96 continue;
97 }
98
Berker Peksag8d1e1902018-09-20 14:10:49 +030099 self->is_dml = (PyOS_strnicmp(p, "insert", 6) == 0)
100 || (PyOS_strnicmp(p, "update", 6) == 0)
101 || (PyOS_strnicmp(p, "delete", 6) == 0)
102 || (PyOS_strnicmp(p, "replace", 7) == 0);
Berker Peksagab994ed2016-09-11 12:57:15 +0300103 break;
104 }
105
Benjamin Petersond7b03282008-09-13 15:58:53 +0000106 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +0200107 rc = sqlite3_prepare_v2(self->db,
Benjamin Peterson52526942017-09-20 07:36:18 -0700108 sql_cstr,
109 -1,
110 &self->st,
111 &tail);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000112 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +0200114 PyObject_GC_Track(self);
115
116 if (rc != SQLITE_OK) {
117 _pysqlite_seterror(self->db, NULL);
118 goto error;
119 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000121 if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122 (void)sqlite3_finalize(self->st);
123 self->st = NULL;
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +0200124 PyErr_SetString(pysqlite_Warning,
125 "You can only execute one statement at a time.");
126 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 }
128
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +0200129 return self;
130
131error:
132 Py_DECREF(self);
133 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134}
135
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200136int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137{
138 int rc = SQLITE_OK;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200139 const char *string;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140 Py_ssize_t buflen;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000141 parameter_type paramtype;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142
143 if (parameter == Py_None) {
144 rc = sqlite3_bind_null(self->st, pos);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000145 goto final;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 }
147
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000148 if (PyLong_CheckExact(parameter)) {
149 paramtype = TYPE_LONG;
150 } else if (PyFloat_CheckExact(parameter)) {
151 paramtype = TYPE_FLOAT;
152 } else if (PyUnicode_CheckExact(parameter)) {
153 paramtype = TYPE_UNICODE;
154 } else if (PyLong_Check(parameter)) {
155 paramtype = TYPE_LONG;
156 } else if (PyFloat_Check(parameter)) {
157 paramtype = TYPE_FLOAT;
158 } else if (PyUnicode_Check(parameter)) {
Gerhard Häring6117f422008-09-22 06:04:51 +0000159 paramtype = TYPE_UNICODE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000160 } else if (PyObject_CheckBuffer(parameter)) {
161 paramtype = TYPE_BUFFER;
162 } else {
163 paramtype = TYPE_UNKNOWN;
164 }
165
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000166 switch (paramtype) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200167 case TYPE_LONG: {
168 sqlite_int64 value = _pysqlite_long_as_int64(parameter);
169 if (value == -1 && PyErr_Occurred())
170 rc = -1;
171 else
172 rc = sqlite3_bind_int64(self->st, pos, value);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000173 break;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200174 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000175 case TYPE_FLOAT:
176 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
177 break;
178 case TYPE_UNICODE:
Serhiy Storchaka06515832016-11-20 09:13:07 +0200179 string = PyUnicode_AsUTF8AndSize(parameter, &buflen);
Victor Stinner3f658be2013-11-18 01:36:29 +0100180 if (string == NULL)
181 return -1;
182 if (buflen > INT_MAX) {
183 PyErr_SetString(PyExc_OverflowError,
184 "string longer than INT_MAX bytes");
185 return -1;
186 }
187 rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000188 break;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200189 case TYPE_BUFFER: {
190 Py_buffer view;
191 if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000192 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Victor Stinner3f658be2013-11-18 01:36:29 +0100193 return -1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000194 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200195 if (view.len > INT_MAX) {
Victor Stinner3f658be2013-11-18 01:36:29 +0100196 PyErr_SetString(PyExc_OverflowError,
197 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200198 PyBuffer_Release(&view);
Victor Stinner3f658be2013-11-18 01:36:29 +0100199 return -1;
200 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200201 rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT);
202 PyBuffer_Release(&view);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000203 break;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200204 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000205 case TYPE_UNKNOWN:
206 rc = -1;
207 }
208
209final:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210 return rc;
211}
212
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000213/* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
214static int _need_adapt(PyObject* obj)
215{
216 if (pysqlite_BaseTypeAdapted) {
217 return 1;
218 }
219
220 if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj)
Christian Heimes9c4756e2008-05-26 13:22:05 +0000221 || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000222 return 0;
223 } else {
224 return 1;
225 }
226}
227
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200228void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229{
230 PyObject* current_param;
231 PyObject* adapted;
232 const char* binding_name;
233 int i;
234 int rc;
235 int num_params_needed;
Victor Stinnerafccb0a2013-11-18 02:07:29 +0100236 Py_ssize_t num_params;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237
238 Py_BEGIN_ALLOW_THREADS
239 num_params_needed = sqlite3_bind_parameter_count(self->st);
240 Py_END_ALLOW_THREADS
241
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000242 if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) {
243 /* parameters passed as sequence */
244 if (PyTuple_CheckExact(parameters)) {
245 num_params = PyTuple_GET_SIZE(parameters);
246 } else if (PyList_CheckExact(parameters)) {
247 num_params = PyList_GET_SIZE(parameters);
248 } else {
249 num_params = PySequence_Size(parameters);
Serhiy Storchaka0b419b72020-09-17 10:35:44 +0300250 if (num_params == -1) {
251 return;
252 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000253 }
254 if (num_params != num_params_needed) {
Victor Stinnerafccb0a2013-11-18 02:07:29 +0100255 PyErr_Format(pysqlite_ProgrammingError,
256 "Incorrect number of bindings supplied. The current "
257 "statement uses %d, and there are %zd supplied.",
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000258 num_params_needed, num_params);
259 return;
260 }
261 for (i = 0; i < num_params; i++) {
262 if (PyTuple_CheckExact(parameters)) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100263 PyObject *item = PyTuple_GET_ITEM(parameters, i);
264 current_param = Py_NewRef(item);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000265 } else if (PyList_CheckExact(parameters)) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100266 PyObject *item = PyList_GetItem(parameters, i);
267 current_param = Py_XNewRef(item);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000268 } else {
269 current_param = PySequence_GetItem(parameters, i);
270 }
271 if (!current_param) {
272 return;
273 }
274
275 if (!_need_adapt(current_param)) {
276 adapted = current_param;
277 } else {
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200278 adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)pysqlite_PrepareProtocolType, current_param);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200279 Py_DECREF(current_param);
280 if (!adapted) {
281 return;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000282 }
283 }
284
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200285 rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000286 Py_DECREF(adapted);
287
288 if (rc != SQLITE_OK) {
289 if (!PyErr_Occurred()) {
290 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
291 }
292 return;
293 }
294 }
295 } else if (PyDict_Check(parameters)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296 /* parameters passed as dictionary */
297 for (i = 1; i <= num_params_needed; i++) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200298 PyObject *binding_name_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299 Py_BEGIN_ALLOW_THREADS
300 binding_name = sqlite3_bind_parameter_name(self->st, i);
301 Py_END_ALLOW_THREADS
302 if (!binding_name) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000303 PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 return;
305 }
306
307 binding_name++; /* skip first char (the colon) */
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200308 binding_name_obj = PyUnicode_FromString(binding_name);
309 if (!binding_name_obj) {
310 return;
311 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000312 if (PyDict_CheckExact(parameters)) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100313 PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj);
314 current_param = Py_XNewRef(item);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000315 } else {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200316 current_param = PyObject_GetItem(parameters, binding_name_obj);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000317 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200318 Py_DECREF(binding_name_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 if (!current_param) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200320 if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) {
Serhiy Storchaka81715802020-09-04 20:55:41 +0300321 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding parameter :%s.", binding_name);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200322 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 return;
324 }
325
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000326 if (!_need_adapt(current_param)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 adapted = current_param;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000328 } else {
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200329 adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)pysqlite_PrepareProtocolType, current_param);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200330 Py_DECREF(current_param);
331 if (!adapted) {
332 return;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000333 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334 }
335
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200336 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337 Py_DECREF(adapted);
338
339 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000340 if (!PyErr_Occurred()) {
341 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
342 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 return;
344 }
345 }
346 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000347 PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000348 }
349}
350
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000351int pysqlite_statement_finalize(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352{
353 int rc;
354
355 rc = SQLITE_OK;
356 if (self->st) {
357 Py_BEGIN_ALLOW_THREADS
358 rc = sqlite3_finalize(self->st);
359 Py_END_ALLOW_THREADS
360 self->st = NULL;
361 }
362
363 self->in_use = 0;
364
365 return rc;
366}
367
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000368int pysqlite_statement_reset(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369{
370 int rc;
371
372 rc = SQLITE_OK;
373
374 if (self->in_use && self->st) {
375 Py_BEGIN_ALLOW_THREADS
376 rc = sqlite3_reset(self->st);
377 Py_END_ALLOW_THREADS
378
379 if (rc == SQLITE_OK) {
380 self->in_use = 0;
381 }
382 }
383
384 return rc;
385}
386
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000387void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388{
389 self->in_use = 1;
390}
391
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100392static void
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700393stmt_dealloc(pysqlite_Statement *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394{
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200395 PyTypeObject *tp = Py_TYPE(self);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700396 PyObject_GC_UnTrack(self);
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700397 if (self->in_weakreflist != NULL) {
398 PyObject_ClearWeakRefs((PyObject*)self);
399 }
Miss Islington (bot)ad2f3b72021-06-04 20:09:40 -0700400 if (self->st) {
Miss Islington (bot)317e9ed2021-06-05 16:13:27 -0700401 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)ad2f3b72021-06-04 20:09:40 -0700402 sqlite3_finalize(self->st);
Miss Islington (bot)317e9ed2021-06-05 16:13:27 -0700403 Py_END_ALLOW_THREADS
Miss Islington (bot)ad2f3b72021-06-04 20:09:40 -0700404 self->st = 0;
405 }
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700406 tp->tp_clear((PyObject *)self);
407 tp->tp_free(self);
408 Py_DECREF(tp);
409}
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200410
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700411static int
412stmt_clear(pysqlite_Statement *self)
413{
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700414 Py_CLEAR(self->sql);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700415 return 0;
416}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000417
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700418static int
419stmt_traverse(pysqlite_Statement *self, visitproc visit, void *arg)
420{
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700421 Py_VISIT(Py_TYPE(self));
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700422 Py_VISIT(self->sql);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700423 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424}
425
426/*
427 * Checks if there is anything left in an SQL string after SQLite compiled it.
428 * This is used to check if somebody tried to execute more than one SQL command
429 * with one execute()/executemany() command, which the DB-API and we don't
430 * allow.
431 *
432 * Returns 1 if there is more left than should be. 0 if ok.
433 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000434static int pysqlite_check_remaining_sql(const char* tail)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435{
436 const char* pos = tail;
437
438 parse_remaining_sql_state state = NORMAL;
439
440 for (;;) {
441 switch (*pos) {
442 case 0:
443 return 0;
444 case '-':
445 if (state == NORMAL) {
446 state = LINECOMMENT_1;
447 } else if (state == LINECOMMENT_1) {
448 state = IN_LINECOMMENT;
449 }
450 break;
451 case ' ':
452 case '\t':
453 break;
454 case '\n':
455 case 13:
456 if (state == IN_LINECOMMENT) {
457 state = NORMAL;
458 }
459 break;
460 case '/':
461 if (state == NORMAL) {
462 state = COMMENTSTART_1;
463 } else if (state == COMMENTEND_1) {
464 state = NORMAL;
465 } else if (state == COMMENTSTART_1) {
466 return 1;
467 }
468 break;
469 case '*':
470 if (state == NORMAL) {
471 return 1;
472 } else if (state == LINECOMMENT_1) {
473 return 1;
474 } else if (state == COMMENTSTART_1) {
475 state = IN_COMMENT;
476 } else if (state == IN_COMMENT) {
477 state = COMMENTEND_1;
478 }
479 break;
480 default:
481 if (state == COMMENTEND_1) {
482 state = IN_COMMENT;
483 } else if (state == IN_LINECOMMENT) {
484 } else if (state == IN_COMMENT) {
485 } else {
486 return 1;
487 }
488 }
489
490 pos++;
491 }
492
493 return 0;
494}
495
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200496static PyMemberDef stmt_members[] = {
497 {"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Statement, in_weakreflist), READONLY},
498 {NULL},
499};
500static PyType_Slot stmt_slots[] = {
501 {Py_tp_members, stmt_members},
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700502 {Py_tp_dealloc, stmt_dealloc},
503 {Py_tp_traverse, stmt_traverse},
504 {Py_tp_clear, stmt_clear},
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200505 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000506};
507
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200508static PyType_Spec stmt_spec = {
509 .name = MODULE_NAME ".Statement",
510 .basicsize = sizeof(pysqlite_Statement),
Miss Islington (bot)7297d742021-06-17 03:19:44 -0700511 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
512 Py_TPFLAGS_IMMUTABLETYPE),
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200513 .slots = stmt_slots,
514};
515PyTypeObject *pysqlite_StatementType = NULL;
516
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +0100517int
518pysqlite_statement_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519{
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200520 pysqlite_StatementType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
521 if (pysqlite_StatementType == NULL) {
522 return -1;
523 }
524 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525}