summaryrefslogtreecommitdiff
path: root/Modules/_sqlite
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2017-02-09 16:08:17 +0100
committerNick Coghlan <ncoghlan@gmail.com>2017-02-09 16:08:17 +0100
commitc6180bb73c8c7c7f9d8ea9816487b710597b6fc1 (patch)
treefb4a5c18886537b4b7df46ed3b2aa579747ff507 /Modules/_sqlite
parent5e0114a832a903518c4af6983161c0c2a8942a24 (diff)
parent819a21a3a4aac38f32e1ba4f68bcef45591fa3f0 (diff)
downloadcpython-c6180bb73c8c7c7f9d8ea9816487b710597b6fc1.tar.gz
Merge issue #26355 fix from Python 3.5
Diffstat (limited to 'Modules/_sqlite')
-rw-r--r--Modules/_sqlite/cache.c3
-rw-r--r--Modules/_sqlite/connection.c95
-rw-r--r--Modules/_sqlite/connection.h4
-rw-r--r--Modules/_sqlite/cursor.c154
-rw-r--r--Modules/_sqlite/cursor.h6
-rw-r--r--Modules/_sqlite/module.c15
-rw-r--r--Modules/_sqlite/row.c4
-rw-r--r--Modules/_sqlite/statement.c24
-rw-r--r--Modules/_sqlite/statement.h1
-rw-r--r--Modules/_sqlite/util.c21
10 files changed, 102 insertions, 225 deletions
diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c
index 3689a4e387..62c58931fa 100644
--- a/Modules/_sqlite/cache.c
+++ b/Modules/_sqlite/cache.c
@@ -244,8 +244,7 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
ptr = ptr->next;
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef cache_methods[] = {
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 70d0995f83..1c6aa54c22 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -165,7 +165,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
self->statement_cache->decref_factory = 0;
Py_DECREF(self);
- self->inTransaction = 0;
self->detect_types = detect_types;
self->timeout = timeout;
(void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
@@ -202,26 +201,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
return 0;
}
-/* Empty the entire statement cache of this connection */
-void pysqlite_flush_statement_cache(pysqlite_Connection* self)
-{
- pysqlite_Node* node;
- pysqlite_Statement* statement;
-
- node = self->statement_cache->first;
-
- while (node) {
- statement = (pysqlite_Statement*)(node->data);
- (void)pysqlite_statement_finalize(statement);
- node = node->next;
- }
-
- Py_SETREF(self->statement_cache,
- (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
- Py_DECREF(self);
- self->statement_cache->decref_factory = 0;
-}
-
/* action in (ACTION_RESET, ACTION_FINALIZE) */
void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
{
@@ -366,8 +345,7 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
}
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
/*
@@ -406,9 +384,7 @@ PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
}
rc = pysqlite_step(statement, self);
- if (rc == SQLITE_DONE) {
- self->inTransaction = 1;
- } else {
+ if (rc != SQLITE_DONE) {
_pysqlite_seterror(self->db, statement);
}
@@ -439,7 +415,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
return NULL;
}
- if (self->inTransaction) {
+ if (!sqlite3_get_autocommit(self->db)) {
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
@@ -450,9 +426,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
}
rc = pysqlite_step(statement, self);
- if (rc == SQLITE_DONE) {
- self->inTransaction = 0;
- } else {
+ if (rc != SQLITE_DONE) {
_pysqlite_seterror(self->db, statement);
}
@@ -484,7 +458,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
return NULL;
}
- if (self->inTransaction) {
+ if (!sqlite3_get_autocommit(self->db)) {
pysqlite_do_all_statements(self, ACTION_RESET, 1);
Py_BEGIN_ALLOW_THREADS
@@ -496,9 +470,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
}
rc = pysqlite_step(statement, self);
- if (rc == SQLITE_DONE) {
- self->inTransaction = 0;
- } else {
+ if (rc != SQLITE_DONE) {
_pysqlite_seterror(self->db, statement);
}
@@ -533,7 +505,7 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
} else if (PyFloat_Check(py_val)) {
sqlite3_result_double(context, PyFloat_AsDouble(py_val));
} else if (PyUnicode_Check(py_val)) {
- const char *str = _PyUnicode_AsString(py_val);
+ const char *str = PyUnicode_AsUTF8(py_val);
if (str == NULL)
return -1;
sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
@@ -673,7 +645,7 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
if (*aggregate_instance == 0) {
- *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
+ *aggregate_instance = PyObject_CallFunction(aggregate_class, NULL);
if (PyErr_Occurred()) {
*aggregate_instance = 0;
@@ -745,7 +717,7 @@ void _pysqlite_final_callback(sqlite3_context* context)
PyErr_Fetch(&exception, &value, &tb);
restore = 1;
- function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
+ function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
Py_DECREF(*aggregate_instance);
@@ -874,8 +846,7 @@ PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObjec
if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -906,8 +877,7 @@ PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObje
if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -963,7 +933,7 @@ static int _progress_handler(void* user_arg)
gilstate = PyGILState_Ensure();
#endif
- ret = PyObject_CallFunction((PyObject*)user_arg, "");
+ ret = PyObject_CallFunction((PyObject*)user_arg, NULL);
if (!ret) {
if (_enable_callback_tracebacks) {
@@ -1042,8 +1012,7 @@ static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, P
if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -1072,8 +1041,7 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
return NULL;
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
@@ -1100,8 +1068,7 @@ static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* sel
sqlite3_trace(self->db, _trace_callback, trace_callback);
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
#ifdef HAVE_LOAD_EXTENSION
@@ -1124,8 +1091,7 @@ static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObj
PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
return NULL;
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -1148,8 +1114,7 @@ static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* ar
PyErr_SetString(pysqlite_OperationalError, errmsg);
return NULL;
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
#endif
@@ -1186,6 +1151,17 @@ static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self
}
}
+static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
+{
+ if (!pysqlite_check_connection(self)) {
+ return NULL;
+ }
+ if (!sqlite3_get_autocommit(self->db)) {
+ Py_RETURN_TRUE;
+ }
+ Py_RETURN_FALSE;
+}
+
static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
{
if (isolation_level == Py_None) {
@@ -1196,7 +1172,6 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
Py_DECREF(res);
self->begin_statement = NULL;
- self->inTransaction = 0;
} else {
const char * const *candidate;
PyObject *uppercase_level;
@@ -1299,7 +1274,7 @@ PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
PyObject* result = 0;
PyObject* method = 0;
- cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
+ cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
if (!cursor) {
goto error;
}
@@ -1328,7 +1303,7 @@ PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* a
PyObject* result = 0;
PyObject* method = 0;
- cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
+ cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
if (!cursor) {
goto error;
}
@@ -1357,7 +1332,7 @@ PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject*
PyObject* result = 0;
PyObject* method = 0;
- cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
+ cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
if (!cursor) {
goto error;
}
@@ -1552,7 +1527,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
}
}
- uppercase_name_str = _PyUnicode_AsString(uppercase_name);
+ uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
if (!uppercase_name_str)
goto finally;
@@ -1621,7 +1596,7 @@ pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
method_name = "rollback";
}
- result = PyObject_CallMethod((PyObject*)self, method_name, "");
+ result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
if (!result) {
return NULL;
}
@@ -1630,12 +1605,13 @@ pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
Py_RETURN_FALSE;
}
-static char connection_doc[] =
+static const char connection_doc[] =
PyDoc_STR("SQLite database connection object.");
static PyGetSetDef connection_getset[] = {
{"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
{"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
+ {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
{NULL}
};
@@ -1697,7 +1673,6 @@ static struct PyMemberDef connection_members[] =
{"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
{"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
{"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
- {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
{NULL}
};
diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h
index adbfb54523..2860a0c6f9 100644
--- a/Modules/_sqlite/connection.h
+++ b/Modules/_sqlite/connection.h
@@ -37,10 +37,6 @@ typedef struct
PyObject_HEAD
sqlite3* db;
- /* 1 if we are currently within a transaction, i. e. if a BEGIN has been
- * issued */
- char inTransaction;
-
/* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
* bitwise combination thereof makes sense */
int detect_types;
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index e1676de907..39f7a6508c 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -27,45 +27,7 @@
PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
-static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
-
-static pysqlite_StatementKind detect_statement_type(const char* statement)
-{
- char buf[20];
- const char* src;
- char* dst;
-
- src = statement;
- /* skip over whitepace */
- while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
- src++;
- }
-
- if (*src == 0)
- return STATEMENT_INVALID;
-
- dst = buf;
- *dst = 0;
- while (Py_ISALPHA(*src) && (dst - buf) < ((Py_ssize_t)sizeof(buf) - 2)) {
- *dst++ = Py_TOLOWER(*src++);
- }
-
- *dst = 0;
-
- if (!strcmp(buf, "select")) {
- return STATEMENT_SELECT;
- } else if (!strcmp(buf, "insert")) {
- return STATEMENT_INSERT;
- } else if (!strcmp(buf, "update")) {
- return STATEMENT_UPDATE;
- } else if (!strcmp(buf, "delete")) {
- return STATEMENT_DELETE;
- } else if (!strcmp(buf, "replace")) {
- return STATEMENT_REPLACE;
- } else {
- return STATEMENT_OTHER;
- }
-}
+static const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
{
@@ -143,7 +105,7 @@ PyObject* _pysqlite_get_converter(PyObject* key)
PyObject* retval;
_Py_IDENTIFIER(upper);
- upcase_key = _PyObject_CallMethodId(key, &PyId_upper, "");
+ upcase_key = _PyObject_CallMethodId(key, &PyId_upper, NULL);
if (!upcase_key) {
return NULL;
}
@@ -242,8 +204,7 @@ PyObject* _pysqlite_build_column_name(const char* colname)
const char* pos;
if (!colname) {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
for (pos = colname;; pos++) {
@@ -428,9 +389,9 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
PyObject* func_args;
PyObject* result;
int numcols;
- int statement_type;
PyObject* descriptor;
PyObject* second_argument = NULL;
+ sqlite_int64 lastrowid;
if (!check_cursor(self)) {
goto error;
@@ -504,14 +465,14 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
pysqlite_statement_reset(self->statement);
}
- operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
+ operation_cstr = PyUnicode_AsUTF8AndSize(operation, &operation_len);
if (operation_cstr == NULL)
goto error;
/* reset description and rowcount */
Py_INCREF(Py_None);
Py_SETREF(self->description, Py_None);
- self->rowcount = -1L;
+ self->rowcount = 0L;
func_args = PyTuple_New(1);
if (!func_args) {
@@ -550,43 +511,19 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
pysqlite_statement_reset(self->statement);
pysqlite_statement_mark_dirty(self->statement);
- statement_type = detect_statement_type(operation_cstr);
- if (self->connection->begin_statement) {
- switch (statement_type) {
- case STATEMENT_UPDATE:
- case STATEMENT_DELETE:
- case STATEMENT_INSERT:
- case STATEMENT_REPLACE:
- if (!self->connection->inTransaction) {
- result = _pysqlite_connection_begin(self->connection);
- if (!result) {
- goto error;
- }
- Py_DECREF(result);
- }
- break;
- case STATEMENT_OTHER:
- /* it's a DDL statement or something similar
- - we better COMMIT first so it works for all cases */
- if (self->connection->inTransaction) {
- result = pysqlite_connection_commit(self->connection, NULL);
- if (!result) {
- goto error;
- }
- Py_DECREF(result);
- }
- break;
- case STATEMENT_SELECT:
- if (multiple) {
- PyErr_SetString(pysqlite_ProgrammingError,
- "You cannot execute SELECT statements in executemany().");
- goto error;
- }
- break;
+ /* For backwards compatibility reasons, do not start a transaction if a
+ DDL statement is encountered. If anybody wants transactional DDL,
+ they can issue a BEGIN statement manually. */
+ if (self->connection->begin_statement && !sqlite3_stmt_readonly(self->statement->st) && !self->statement->is_ddl) {
+ if (sqlite3_get_autocommit(self->connection->db)) {
+ result = _pysqlite_connection_begin(self->connection);
+ if (!result) {
+ goto error;
+ }
+ Py_DECREF(result);
}
}
-
while (1) {
parameters = PyIter_Next(parameters_iter);
if (!parameters) {
@@ -672,6 +609,20 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
}
}
+ if (!sqlite3_stmt_readonly(self->statement->st)) {
+ self->rowcount += (long)sqlite3_changes(self->connection->db);
+ } else {
+ self->rowcount= -1L;
+ }
+
+ if (!multiple) {
+ Py_DECREF(self->lastrowid);
+ Py_BEGIN_ALLOW_THREADS
+ lastrowid = sqlite3_last_insert_rowid(self->connection->db);
+ Py_END_ALLOW_THREADS
+ self->lastrowid = _pysqlite_long_from_int64(lastrowid);
+ }
+
if (rc == SQLITE_ROW) {
if (multiple) {
PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
@@ -686,29 +637,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
Py_CLEAR(self->statement);
}
- switch (statement_type) {
- case STATEMENT_UPDATE:
- case STATEMENT_DELETE:
- case STATEMENT_INSERT:
- case STATEMENT_REPLACE:
- if (self->rowcount == -1L) {
- self->rowcount = 0L;
- }
- self->rowcount += (long)sqlite3_changes(self->connection->db);
- }
-
- Py_DECREF(self->lastrowid);
- if (!multiple && statement_type == STATEMENT_INSERT) {
- sqlite_int64 lastrowid;
- Py_BEGIN_ALLOW_THREADS
- lastrowid = sqlite3_last_insert_rowid(self->connection->db);
- Py_END_ALLOW_THREADS
- self->lastrowid = _pysqlite_long_from_int64(lastrowid);
- } else {
- Py_INCREF(Py_None);
- self->lastrowid = Py_None;
- }
-
if (multiple) {
pysqlite_statement_reset(self->statement);
}
@@ -716,15 +644,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
}
error:
- /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
- * ROLLBACK could have happened */
- #ifdef SQLITE_VERSION_NUMBER
- #if SQLITE_VERSION_NUMBER >= 3002002
- if (self->connection && self->connection->db)
- self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
- #endif
- #endif
-
Py_XDECREF(parameters);
Py_XDECREF(parameters_iter);
Py_XDECREF(parameters_list);
@@ -770,7 +689,7 @@ PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
self->reset = 0;
if (PyUnicode_Check(script_obj)) {
- script_cstr = _PyUnicode_AsString(script_obj);
+ script_cstr = PyUnicode_AsUTF8(script_obj);
if (!script_cstr) {
return NULL;
}
@@ -913,8 +832,7 @@ PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
row = pysqlite_cursor_iternext(self);
if (!row && !PyErr_Occurred()) {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
return row;
@@ -995,8 +913,7 @@ PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
{
/* don't care, return None */
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
@@ -1012,8 +929,7 @@ PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
self->closed = 1;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef cursor_methods[] = {
@@ -1049,7 +965,7 @@ static struct PyMemberDef cursor_members[] =
{NULL}
};
-static char cursor_doc[] =
+static const char cursor_doc[] =
PyDoc_STR("SQLite database cursor class.");
PyTypeObject pysqlite_CursorType = {
diff --git a/Modules/_sqlite/cursor.h b/Modules/_sqlite/cursor.h
index 118ba388a4..28bbd5f911 100644
--- a/Modules/_sqlite/cursor.h
+++ b/Modules/_sqlite/cursor.h
@@ -51,12 +51,6 @@ typedef struct
PyObject* in_weakreflist; /* List of weak references */
} pysqlite_Cursor;
-typedef enum {
- STATEMENT_INVALID, STATEMENT_INSERT, STATEMENT_DELETE,
- STATEMENT_UPDATE, STATEMENT_REPLACE, STATEMENT_SELECT,
- STATEMENT_OTHER
-} pysqlite_StatementKind;
-
extern PyTypeObject pysqlite_CursorType;
PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args);
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 7a7e86040a..cc45331ab5 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -139,8 +139,7 @@ static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyOb
PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
return NULL;
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -172,8 +171,7 @@ static PyObject* module_register_adapter(PyObject* self, PyObject* args)
if (rc == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(module_register_adapter_doc,
@@ -194,7 +192,7 @@ static PyObject* module_register_converter(PyObject* self, PyObject* args)
}
/* convert the name to upper case */
- name = _PyObject_CallMethodId(orig_name, &PyId_upper, "");
+ name = _PyObject_CallMethodId(orig_name, &PyId_upper, NULL);
if (!name) {
goto error;
}
@@ -221,8 +219,7 @@ static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
return NULL;
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(enable_callback_tracebacks_doc,
@@ -261,13 +258,13 @@ static PyMethodDef module_methods[] = {
};
struct _IntConstantPair {
- char* constant_name;
+ const char *constant_name;
int constant_value;
};
typedef struct _IntConstantPair IntConstantPair;
-static IntConstantPair _int_constants[] = {
+static const IntConstantPair _int_constants[] = {
{"PARSE_DECLTYPES", PARSE_DECLTYPES},
{"PARSE_COLNAMES", PARSE_COLNAMES},
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
index 07584e30e5..53342f3444 100644
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -98,7 +98,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
Py_XINCREF(item);
return item;
} else if (PyUnicode_Check(idx)) {
- key = _PyUnicode_AsString(idx);
+ key = PyUnicode_AsUTF8(idx);
if (key == NULL)
return NULL;
@@ -108,7 +108,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
PyObject *obj;
obj = PyTuple_GET_ITEM(self->description, i);
obj = PyTuple_GET_ITEM(obj, 0);
- compare_key = _PyUnicode_AsString(obj);
+ compare_key = PyUnicode_AsUTF8(obj);
if (!compare_key) {
return NULL;
}
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index e87063341d..0df661b9c7 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -54,11 +54,12 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
int rc;
const char* sql_cstr;
Py_ssize_t sql_cstr_len;
+ const char* p;
self->st = NULL;
self->in_use = 0;
- sql_cstr = _PyUnicode_AsStringAndSize(sql, &sql_cstr_len);
+ sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len);
if (sql_cstr == NULL) {
rc = PYSQLITE_SQL_WRONG_TYPE;
return rc;
@@ -72,6 +73,23 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
Py_INCREF(sql);
self->sql = sql;
+ /* determine if the statement is a DDL statement */
+ self->is_ddl = 0;
+ for (p = sql_cstr; *p != 0; p++) {
+ switch (*p) {
+ case ' ':
+ case '\r':
+ case '\n':
+ case '\t':
+ continue;
+ }
+
+ self->is_ddl = (PyOS_strnicmp(p, "create ", 7) == 0)
+ || (PyOS_strnicmp(p, "drop ", 5) == 0)
+ || (PyOS_strnicmp(p, "reindex ", 8) == 0);
+ break;
+ }
+
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(connection->db,
sql_cstr,
@@ -134,7 +152,7 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
break;
case TYPE_UNICODE:
- string = _PyUnicode_AsStringAndSize(parameter, &buflen);
+ string = PyUnicode_AsUTF8AndSize(parameter, &buflen);
if (string == NULL)
return -1;
if (buflen > INT_MAX) {
@@ -307,7 +325,7 @@ int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Py_ssize_t sql_len;
sqlite3_stmt* new_st;
- sql_cstr = _PyUnicode_AsStringAndSize(self->sql, &sql_len);
+ sql_cstr = PyUnicode_AsUTF8AndSize(self->sql, &sql_len);
if (sql_cstr == NULL) {
rc = PYSQLITE_SQL_WRONG_TYPE;
return rc;
diff --git a/Modules/_sqlite/statement.h b/Modules/_sqlite/statement.h
index 4681443e76..6eef16857f 100644
--- a/Modules/_sqlite/statement.h
+++ b/Modules/_sqlite/statement.h
@@ -38,6 +38,7 @@ typedef struct
sqlite3_stmt* st;
PyObject* sql;
int in_use;
+ int is_ddl;
PyObject* in_weakreflist; /* List of weak references */
} pysqlite_Statement;
diff --git a/Modules/_sqlite/util.c b/Modules/_sqlite/util.c
index 312fe3be11..351b1b47a4 100644
--- a/Modules/_sqlite/util.c
+++ b/Modules/_sqlite/util.c
@@ -113,7 +113,6 @@ int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st)
PyObject *
_pysqlite_long_from_int64(sqlite_int64 value)
{
-#ifdef HAVE_LONG_LONG
# if SIZEOF_LONG_LONG < 8
if (value > PY_LLONG_MAX || value < PY_LLONG_MIN) {
return _PyLong_FromByteArray(&value, sizeof(value),
@@ -124,14 +123,6 @@ _pysqlite_long_from_int64(sqlite_int64 value)
if (value > LONG_MAX || value < LONG_MIN)
return PyLong_FromLongLong(value);
# endif
-#else
-# if SIZEOF_LONG < 8
- if (value > LONG_MAX || value < LONG_MIN) {
- return _PyLong_FromByteArray(&value, sizeof(value),
- IS_LITTLE_ENDIAN, 1 /* signed */);
- }
-# endif
-#endif
return PyLong_FromLong(Py_SAFE_DOWNCAST(value, sqlite_int64, long));
}
@@ -139,23 +130,13 @@ sqlite_int64
_pysqlite_long_as_int64(PyObject * py_val)
{
int overflow;
-#ifdef HAVE_LONG_LONG
- PY_LONG_LONG value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
-#else
- long value = PyLong_AsLongAndOverflow(py_val, &overflow);
-#endif
+ long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
if (value == -1 && PyErr_Occurred())
return -1;
if (!overflow) {
-#ifdef HAVE_LONG_LONG
# if SIZEOF_LONG_LONG > 8
if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
# endif
-#else
-# if SIZEOF_LONG > 8
- if (-0x8000000000000000L <= value && value <= 0x7FFFFFFFFFFFFFFFL)
-# endif
-#endif
return value;
}
else if (sizeof(value) < sizeof(sqlite_int64)) {