2006-04-21 07:40:58 -03:00
|
|
|
/* statement.c - the statement type
|
|
|
|
*
|
2010-09-03 20:47:32 -03:00
|
|
|
* Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
|
2006-04-21 07:40:58 -03:00
|
|
|
*
|
|
|
|
* This file is part of pysqlite.
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "connection.h"
|
2022-05-03 17:00:39 -03:00
|
|
|
#include "statement.h"
|
2013-02-07 11:01:47 -04:00
|
|
|
#include "util.h"
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
/* prototypes */
|
2022-06-14 08:56:36 -03:00
|
|
|
static const char *lstrip_sql(const char *sql);
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2021-06-01 07:47:37 -03:00
|
|
|
pysqlite_Statement *
|
|
|
|
pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
2021-07-29 06:21:45 -03:00
|
|
|
pysqlite_state *state = connection->state;
|
2019-06-25 22:16:24 -03:00
|
|
|
assert(PyUnicode_Check(sql));
|
2021-06-08 12:00:56 -03:00
|
|
|
Py_ssize_t size;
|
|
|
|
const char *sql_cstr = PyUnicode_AsUTF8AndSize(sql, &size);
|
2007-08-29 00:34:29 -03:00
|
|
|
if (sql_cstr == NULL) {
|
2021-06-01 07:47:37 -03:00
|
|
|
return NULL;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
2021-06-02 09:26:06 -03:00
|
|
|
|
2021-06-08 12:00:56 -03:00
|
|
|
sqlite3 *db = connection->db;
|
2021-11-10 14:46:11 -04:00
|
|
|
int max_length = sqlite3_limit(db, SQLITE_LIMIT_SQL_LENGTH, -1);
|
|
|
|
if (size > max_length) {
|
2021-07-14 08:26:44 -03:00
|
|
|
PyErr_SetString(connection->DataError,
|
|
|
|
"query string is too large");
|
2021-06-02 10:22:15 -03:00
|
|
|
return NULL;
|
2021-06-02 09:26:06 -03:00
|
|
|
}
|
2021-06-08 12:00:56 -03:00
|
|
|
if (strlen(sql_cstr) != (size_t)size) {
|
2022-03-17 02:58:25 -03:00
|
|
|
PyErr_SetString(connection->ProgrammingError,
|
2021-06-01 07:47:37 -03:00
|
|
|
"the query contains a null character");
|
|
|
|
return NULL;
|
2014-09-11 07:29:05 -03:00
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2021-06-08 12:00:56 -03:00
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
const char *tail;
|
|
|
|
int rc;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
rc = sqlite3_prepare_v2(db, sql_cstr, (int)size + 1, &stmt, &tail);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
2021-07-29 06:21:45 -03:00
|
|
|
_pysqlite_seterror(state, db);
|
2021-06-01 07:47:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-14 08:56:36 -03:00
|
|
|
if (lstrip_sql(tail) != NULL) {
|
2022-03-17 02:58:25 -03:00
|
|
|
PyErr_SetString(connection->ProgrammingError,
|
2021-07-14 08:26:44 -03:00
|
|
|
"You can only execute one statement at a time.");
|
2021-06-08 12:00:56 -03:00
|
|
|
goto error;
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2017-02-26 11:22:38 -04:00
|
|
|
/* Determine if the statement is a DML statement.
|
|
|
|
SELECT is the only exception. See #9924. */
|
2021-06-08 12:00:56 -03:00
|
|
|
int is_dml = 0;
|
2022-06-14 08:56:36 -03:00
|
|
|
const char *p = lstrip_sql(sql_cstr);
|
|
|
|
if (p != NULL) {
|
2021-06-08 12:00:56 -03:00
|
|
|
is_dml = (PyOS_strnicmp(p, "insert", 6) == 0)
|
|
|
|
|| (PyOS_strnicmp(p, "update", 6) == 0)
|
|
|
|
|| (PyOS_strnicmp(p, "delete", 6) == 0)
|
|
|
|
|| (PyOS_strnicmp(p, "replace", 7) == 0);
|
2016-09-11 06:57:15 -03:00
|
|
|
}
|
|
|
|
|
2021-06-08 12:00:56 -03:00
|
|
|
pysqlite_Statement *self = PyObject_GC_New(pysqlite_Statement,
|
2021-06-15 09:47:34 -03:00
|
|
|
state->StatementType);
|
2021-06-08 12:00:56 -03:00
|
|
|
if (self == NULL) {
|
2021-06-01 07:47:37 -03:00
|
|
|
goto error;
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2021-06-08 12:00:56 -03:00
|
|
|
self->st = stmt;
|
|
|
|
self->is_dml = is_dml;
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2021-06-08 12:00:56 -03:00
|
|
|
PyObject_GC_Track(self);
|
2021-06-01 07:47:37 -03:00
|
|
|
return self;
|
|
|
|
|
|
|
|
error:
|
2021-06-08 12:00:56 -03:00
|
|
|
(void)sqlite3_finalize(stmt);
|
2021-06-01 07:47:37 -03:00
|
|
|
return NULL;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
2021-02-20 20:29:19 -04:00
|
|
|
static void
|
2021-05-25 14:43:56 -03:00
|
|
|
stmt_dealloc(pysqlite_Statement *self)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
2020-10-01 10:24:31 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
2021-05-25 14:43:56 -03:00
|
|
|
PyObject_GC_UnTrack(self);
|
2021-06-04 23:50:39 -03:00
|
|
|
if (self->st) {
|
2021-06-05 19:41:11 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2021-06-04 23:50:39 -03:00
|
|
|
sqlite3_finalize(self->st);
|
2021-06-05 19:41:11 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2021-06-04 23:50:39 -03:00
|
|
|
self->st = 0;
|
|
|
|
}
|
2021-05-25 14:43:56 -03:00
|
|
|
tp->tp_free(self);
|
|
|
|
Py_DECREF(tp);
|
|
|
|
}
|
2020-10-01 10:24:31 -03:00
|
|
|
|
2021-05-25 14:43:56 -03:00
|
|
|
static int
|
|
|
|
stmt_traverse(pysqlite_Statement *self, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(Py_TYPE(self));
|
|
|
|
return 0;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-06-14 08:56:36 -03:00
|
|
|
* Strip leading whitespace and comments from incoming SQL (null terminated C
|
|
|
|
* string) and return a pointer to the first non-whitespace, non-comment
|
|
|
|
* character.
|
2006-04-21 07:40:58 -03:00
|
|
|
*
|
2022-06-14 08:56:36 -03:00
|
|
|
* This is used to check if somebody tries to execute more than one SQL query
|
|
|
|
* with one execute()/executemany() command, which the DB-API don't allow.
|
|
|
|
*
|
|
|
|
* It is also used to harden DML query detection.
|
2006-04-21 07:40:58 -03:00
|
|
|
*/
|
2022-06-14 08:56:36 -03:00
|
|
|
static inline const char *
|
|
|
|
lstrip_sql(const char *sql)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
2022-06-14 08:56:36 -03:00
|
|
|
// This loop is borrowed from the SQLite source code.
|
|
|
|
for (const char *pos = sql; *pos; pos++) {
|
2006-04-21 07:40:58 -03:00
|
|
|
switch (*pos) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2022-06-14 08:56:36 -03:00
|
|
|
case '\f':
|
2006-04-21 07:40:58 -03:00
|
|
|
case '\n':
|
2022-06-14 08:56:36 -03:00
|
|
|
case '\r':
|
|
|
|
// Skip whitespace.
|
2006-04-21 07:40:58 -03:00
|
|
|
break;
|
2022-06-14 08:56:36 -03:00
|
|
|
case '-':
|
|
|
|
// Skip line comments.
|
|
|
|
if (pos[1] == '-') {
|
|
|
|
pos += 2;
|
|
|
|
while (pos[0] && pos[0] != '\n') {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (pos[0] == '\0') {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
continue;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
2022-06-14 08:56:36 -03:00
|
|
|
return pos;
|
|
|
|
case '/':
|
|
|
|
// Skip C style comments.
|
|
|
|
if (pos[1] == '*') {
|
|
|
|
pos += 2;
|
|
|
|
while (pos[0] && (pos[0] != '*' || pos[1] != '/')) {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (pos[0] == '\0') {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
continue;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
2022-06-14 08:56:36 -03:00
|
|
|
return pos;
|
2006-04-21 07:40:58 -03:00
|
|
|
default:
|
2022-06-14 08:56:36 -03:00
|
|
|
return pos;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 08:56:36 -03:00
|
|
|
return NULL;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
2020-10-01 10:24:31 -03:00
|
|
|
static PyType_Slot stmt_slots[] = {
|
2021-05-25 14:43:56 -03:00
|
|
|
{Py_tp_dealloc, stmt_dealloc},
|
|
|
|
{Py_tp_traverse, stmt_traverse},
|
2020-10-01 10:24:31 -03:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec stmt_spec = {
|
|
|
|
.name = MODULE_NAME ".Statement",
|
|
|
|
.basicsize = sizeof(pysqlite_Statement),
|
2021-06-17 07:06:09 -03:00
|
|
|
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2021-06-20 16:24:32 -03:00
|
|
|
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
|
2020-10-01 10:24:31 -03:00
|
|
|
.slots = stmt_slots,
|
2006-04-21 07:40:58 -03:00
|
|
|
};
|
|
|
|
|
2021-02-21 06:07:49 -04:00
|
|
|
int
|
|
|
|
pysqlite_statement_setup_types(PyObject *module)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
2021-06-15 09:47:34 -03:00
|
|
|
PyObject *type = PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
|
|
|
|
if (type == NULL) {
|
2020-10-01 10:24:31 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2021-06-15 09:47:34 -03:00
|
|
|
pysqlite_state *state = pysqlite_get_state(module);
|
|
|
|
state->StatementType = (PyTypeObject *)type;
|
2020-10-01 10:24:31 -03:00
|
|
|
return 0;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|