mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-83638: Add sqlite3.Connection.autocommit for PEP 249 compliant behaviour (#93823)
Introduce the autocommit attribute to Connection and the autocommit parameter to connect() for PEP 249-compliant transaction handling. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM> Co-authored-by: Géry Ogam <gery.ogam@gmail.com>
This commit is contained in:
parent
99972dc745
commit
c95f554a40
14 changed files with 537 additions and 62 deletions
34
Modules/_sqlite/clinic/connection.c.h
generated
34
Modules/_sqlite/clinic/connection.c.h
generated
|
@ -13,7 +13,8 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
|
|||
double timeout, int detect_types,
|
||||
const char *isolation_level,
|
||||
int check_same_thread, PyObject *factory,
|
||||
int cache_size, int uri);
|
||||
int cache_size, int uri,
|
||||
enum autocommit_mode autocommit);
|
||||
|
||||
static int
|
||||
pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
|
@ -21,14 +22,14 @@ pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
int return_value = -1;
|
||||
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
|
||||
|
||||
#define NUM_KEYWORDS 8
|
||||
#define NUM_KEYWORDS 9
|
||||
static struct {
|
||||
PyGC_Head _this_is_not_used;
|
||||
PyObject_VAR_HEAD
|
||||
PyObject *ob_item[NUM_KEYWORDS];
|
||||
} _kwtuple = {
|
||||
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
|
||||
.ob_item = { &_Py_ID(database), &_Py_ID(timeout), &_Py_ID(detect_types), &_Py_ID(isolation_level), &_Py_ID(check_same_thread), &_Py_ID(factory), &_Py_ID(cached_statements), &_Py_ID(uri), },
|
||||
.ob_item = { &_Py_ID(database), &_Py_ID(timeout), &_Py_ID(detect_types), &_Py_ID(isolation_level), &_Py_ID(check_same_thread), &_Py_ID(factory), &_Py_ID(cached_statements), &_Py_ID(uri), &_Py_ID(autocommit), },
|
||||
};
|
||||
#undef NUM_KEYWORDS
|
||||
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
|
||||
|
@ -37,14 +38,14 @@ pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
# define KWTUPLE NULL
|
||||
#endif // !Py_BUILD_CORE
|
||||
|
||||
static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", NULL};
|
||||
static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", "autocommit", NULL};
|
||||
static _PyArg_Parser _parser = {
|
||||
.keywords = _keywords,
|
||||
.fname = "Connection",
|
||||
.kwtuple = KWTUPLE,
|
||||
};
|
||||
#undef KWTUPLE
|
||||
PyObject *argsbuf[8];
|
||||
PyObject *argsbuf[9];
|
||||
PyObject * const *fastargs;
|
||||
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
|
||||
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
|
||||
|
@ -56,6 +57,7 @@ pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
PyObject *factory = (PyObject*)clinic_state()->ConnectionType;
|
||||
int cache_size = 128;
|
||||
int uri = 0;
|
||||
enum autocommit_mode autocommit = LEGACY_TRANSACTION_CONTROL;
|
||||
|
||||
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 8, 0, argsbuf);
|
||||
if (!fastargs) {
|
||||
|
@ -121,12 +123,24 @@ pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
goto skip_optional_pos;
|
||||
}
|
||||
}
|
||||
uri = PyObject_IsTrue(fastargs[7]);
|
||||
if (uri < 0) {
|
||||
goto exit;
|
||||
if (fastargs[7]) {
|
||||
uri = PyObject_IsTrue(fastargs[7]);
|
||||
if (uri < 0) {
|
||||
goto exit;
|
||||
}
|
||||
if (!--noptargs) {
|
||||
goto skip_optional_pos;
|
||||
}
|
||||
}
|
||||
skip_optional_pos:
|
||||
return_value = pysqlite_connection_init_impl((pysqlite_Connection *)self, database, timeout, detect_types, isolation_level, check_same_thread, factory, cache_size, uri);
|
||||
if (!noptargs) {
|
||||
goto skip_optional_kwonly;
|
||||
}
|
||||
if (!autocommit_converter(fastargs[8], &autocommit)) {
|
||||
goto exit;
|
||||
}
|
||||
skip_optional_kwonly:
|
||||
return_value = pysqlite_connection_init_impl((pysqlite_Connection *)self, database, timeout, detect_types, isolation_level, check_same_thread, factory, cache_size, uri, autocommit);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
|
@ -1518,4 +1532,4 @@ exit:
|
|||
#ifndef DESERIALIZE_METHODDEF
|
||||
#define DESERIALIZE_METHODDEF
|
||||
#endif /* !defined(DESERIALIZE_METHODDEF) */
|
||||
/*[clinic end generated code: output=beef3eac690a1f88 input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=20e929a7a7d62a01 input=a9049054013a1b77]*/
|
||||
|
|
|
@ -92,6 +92,30 @@ isolation_level_converter(PyObject *str_or_none, const char **result)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
autocommit_converter(PyObject *val, enum autocommit_mode *result)
|
||||
{
|
||||
if (Py_IsTrue(val)) {
|
||||
*result = AUTOCOMMIT_ENABLED;
|
||||
return 1;
|
||||
}
|
||||
if (Py_IsFalse(val)) {
|
||||
*result = AUTOCOMMIT_DISABLED;
|
||||
return 1;
|
||||
}
|
||||
if (PyLong_Check(val) &&
|
||||
PyLong_AsLong(val) == LEGACY_TRANSACTION_CONTROL)
|
||||
{
|
||||
*result = AUTOCOMMIT_LEGACY;
|
||||
return 1;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"autocommit must be True, False, or "
|
||||
"sqlite3.LEGACY_TRANSACTION_CONTROL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
|
||||
#include "clinic/connection.c.h"
|
||||
#undef clinic_state
|
||||
|
@ -132,13 +156,38 @@ new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
|
|||
return res;
|
||||
}
|
||||
|
||||
static inline int
|
||||
connection_exec_stmt(pysqlite_Connection *self, const char *sql)
|
||||
{
|
||||
int rc;
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
int len = (int)strlen(sql) + 1;
|
||||
sqlite3_stmt *stmt;
|
||||
rc = sqlite3_prepare_v2(self->db, sql, len, &stmt, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
(void)sqlite3_step(stmt);
|
||||
rc = sqlite3_finalize(stmt);
|
||||
}
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
(void)_pysqlite_seterror(self->state, self->db);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*[python input]
|
||||
class IsolationLevel_converter(CConverter):
|
||||
type = "const char *"
|
||||
converter = "isolation_level_converter"
|
||||
|
||||
class Autocommit_converter(CConverter):
|
||||
type = "enum autocommit_mode"
|
||||
converter = "autocommit_converter"
|
||||
|
||||
[python start generated code]*/
|
||||
/*[python end generated code: output=da39a3ee5e6b4b0d input=cbcfe85b253061c2]*/
|
||||
/*[python end generated code: output=da39a3ee5e6b4b0d input=bc2aa6c7ba0c5f8f]*/
|
||||
|
||||
// NB: This needs to be in sync with the sqlite3.connect docstring
|
||||
/*[clinic input]
|
||||
|
@ -152,6 +201,8 @@ _sqlite3.Connection.__init__ as pysqlite_connection_init
|
|||
factory: object(c_default='(PyObject*)clinic_state()->ConnectionType') = ConnectionType
|
||||
cached_statements as cache_size: int = 128
|
||||
uri: bool = False
|
||||
*
|
||||
autocommit: Autocommit(c_default='LEGACY_TRANSACTION_CONTROL') = sqlite3.LEGACY_TRANSACTION_CONTROL
|
||||
[clinic start generated code]*/
|
||||
|
||||
static int
|
||||
|
@ -159,8 +210,9 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
|
|||
double timeout, int detect_types,
|
||||
const char *isolation_level,
|
||||
int check_same_thread, PyObject *factory,
|
||||
int cache_size, int uri)
|
||||
/*[clinic end generated code: output=839eb2fee4293bda input=b8ce63dc6f70a383]*/
|
||||
int cache_size, int uri,
|
||||
enum autocommit_mode autocommit)
|
||||
/*[clinic end generated code: output=cba057313ea7712f input=b21abce28ebcd304]*/
|
||||
{
|
||||
if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
|
||||
return -1;
|
||||
|
@ -227,6 +279,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
|
|||
self->state = state;
|
||||
self->detect_types = detect_types;
|
||||
self->isolation_level = isolation_level;
|
||||
self->autocommit = autocommit;
|
||||
self->check_same_thread = check_same_thread;
|
||||
self->thread_ident = PyThread_get_thread_ident();
|
||||
self->statement_cache = statement_cache;
|
||||
|
@ -256,6 +309,10 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
|
|||
}
|
||||
|
||||
self->initialized = 1;
|
||||
|
||||
if (autocommit == AUTOCOMMIT_DISABLED) {
|
||||
(void)connection_exec_stmt(self, "BEGIN");
|
||||
}
|
||||
return 0;
|
||||
|
||||
error:
|
||||
|
@ -321,10 +378,33 @@ free_callback_contexts(pysqlite_Connection *self)
|
|||
set_callback_context(&self->authorizer_ctx, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
remove_callbacks(sqlite3 *db)
|
||||
{
|
||||
#ifdef HAVE_TRACE_V2
|
||||
sqlite3_trace_v2(db, SQLITE_TRACE_STMT, 0, 0);
|
||||
#else
|
||||
sqlite3_trace(db, 0, (void*)0);
|
||||
#endif
|
||||
sqlite3_progress_handler(db, 0, 0, (void *)0);
|
||||
(void)sqlite3_set_authorizer(db, NULL, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
connection_close(pysqlite_Connection *self)
|
||||
{
|
||||
if (self->db) {
|
||||
if (self->autocommit == AUTOCOMMIT_DISABLED &&
|
||||
!sqlite3_get_autocommit(self->db))
|
||||
{
|
||||
/* If close is implicitly called as a result of interpreter
|
||||
* tear-down, we must not call back into Python. */
|
||||
if (_Py_IsFinalizing()) {
|
||||
remove_callbacks(self->db);
|
||||
}
|
||||
(void)connection_exec_stmt(self, "ROLLBACK");
|
||||
}
|
||||
|
||||
free_callback_contexts(self);
|
||||
|
||||
sqlite3 *db = self->db;
|
||||
|
@ -538,24 +618,21 @@ pysqlite_connection_commit_impl(pysqlite_Connection *self)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (!sqlite3_get_autocommit(self->db)) {
|
||||
int rc;
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
sqlite3_stmt *statement;
|
||||
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
(void)sqlite3_step(statement);
|
||||
rc = sqlite3_finalize(statement);
|
||||
if (self->autocommit == AUTOCOMMIT_LEGACY) {
|
||||
if (!sqlite3_get_autocommit(self->db)) {
|
||||
if (connection_exec_stmt(self, "COMMIT") < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
(void)_pysqlite_seterror(self->state, self->db);
|
||||
}
|
||||
else if (self->autocommit == AUTOCOMMIT_DISABLED) {
|
||||
if (connection_exec_stmt(self, "COMMIT") < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (connection_exec_stmt(self, "BEGIN") < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
@ -575,25 +652,21 @@ pysqlite_connection_rollback_impl(pysqlite_Connection *self)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (!sqlite3_get_autocommit(self->db)) {
|
||||
int rc;
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
sqlite3_stmt *statement;
|
||||
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
(void)sqlite3_step(statement);
|
||||
rc = sqlite3_finalize(statement);
|
||||
if (self->autocommit == AUTOCOMMIT_LEGACY) {
|
||||
if (!sqlite3_get_autocommit(self->db)) {
|
||||
if (connection_exec_stmt(self, "ROLLBACK") < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
(void)_pysqlite_seterror(self->state, self->db);
|
||||
}
|
||||
else if (self->autocommit == AUTOCOMMIT_DISABLED) {
|
||||
if (connection_exec_stmt(self, "ROLLBACK") < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (connection_exec_stmt(self, "BEGIN") < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
@ -2270,6 +2343,48 @@ getlimit_impl(pysqlite_Connection *self, int category)
|
|||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
|
||||
{
|
||||
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
||||
return NULL;
|
||||
}
|
||||
if (self->autocommit == AUTOCOMMIT_ENABLED) {
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
if (self->autocommit == AUTOCOMMIT_DISABLED) {
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
return PyLong_FromLong(LEGACY_TRANSACTION_CONTROL);
|
||||
}
|
||||
|
||||
static int
|
||||
set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
|
||||
{
|
||||
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
||||
return -1;
|
||||
}
|
||||
if (!autocommit_converter(val, &self->autocommit)) {
|
||||
return -1;
|
||||
}
|
||||
if (self->autocommit == AUTOCOMMIT_ENABLED) {
|
||||
if (!sqlite3_get_autocommit(self->db)) {
|
||||
if (connection_exec_stmt(self, "COMMIT") < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (self->autocommit == AUTOCOMMIT_DISABLED) {
|
||||
if (sqlite3_get_autocommit(self->db)) {
|
||||
if (connection_exec_stmt(self, "BEGIN") < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const char connection_doc[] =
|
||||
PyDoc_STR("SQLite database connection object.");
|
||||
|
||||
|
@ -2277,6 +2392,7 @@ 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},
|
||||
{"autocommit", (getter)get_autocommit, (setter)set_autocommit},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
|
|
@ -39,6 +39,12 @@ typedef struct _callback_context
|
|||
pysqlite_state *state;
|
||||
} callback_context;
|
||||
|
||||
enum autocommit_mode {
|
||||
AUTOCOMMIT_LEGACY = LEGACY_TRANSACTION_CONTROL,
|
||||
AUTOCOMMIT_ENABLED = 1,
|
||||
AUTOCOMMIT_DISABLED = 0,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PyObject_HEAD
|
||||
|
@ -51,6 +57,7 @@ typedef struct
|
|||
|
||||
/* NULL for autocommit, otherwise a string with the isolation level */
|
||||
const char *isolation_level;
|
||||
enum autocommit_mode autocommit;
|
||||
|
||||
/* 1 if a check should be performed for each API call if the connection is
|
||||
* used from the same thread it was created in */
|
||||
|
|
|
@ -855,7 +855,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
|
|||
|
||||
/* We start a transaction implicitly before a DML statement.
|
||||
SELECT is the only exception. See #9924. */
|
||||
if (self->connection->isolation_level
|
||||
if (self->connection->autocommit == AUTOCOMMIT_LEGACY
|
||||
&& self->connection->isolation_level
|
||||
&& self->statement->is_dml
|
||||
&& sqlite3_get_autocommit(self->connection->db))
|
||||
{
|
||||
|
@ -1033,7 +1034,9 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
|
|||
|
||||
// Commit if needed
|
||||
sqlite3 *db = self->connection->db;
|
||||
if (!sqlite3_get_autocommit(db)) {
|
||||
if (self->connection->autocommit == AUTOCOMMIT_LEGACY
|
||||
&& !sqlite3_get_autocommit(db))
|
||||
{
|
||||
int rc = SQLITE_OK;
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
|
|
|
@ -46,7 +46,8 @@ module _sqlite3
|
|||
PyDoc_STRVAR(module_connect_doc,
|
||||
"connect($module, /, database, timeout=5.0, detect_types=0,\n"
|
||||
" isolation_level='', check_same_thread=True,\n"
|
||||
" factory=ConnectionType, cached_statements=128, uri=False)\n"
|
||||
" factory=ConnectionType, cached_statements=128, uri=False, *,\n"
|
||||
" autocommit=sqlite3.LEGACY_TRANSACTION_CONTROL)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Opens a connection to the SQLite database file database.\n"
|
||||
|
@ -706,6 +707,10 @@ module_exec(PyObject *module)
|
|||
goto error;
|
||||
}
|
||||
|
||||
if (PyModule_AddIntMacro(module, LEGACY_TRANSACTION_CONTROL) < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
int threadsafety = get_threadsafety(state);
|
||||
if (threadsafety < 0) {
|
||||
goto error;
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
#define PY_SSIZE_T_CLEAN
|
||||
#include "Python.h"
|
||||
|
||||
#define LEGACY_TRANSACTION_CONTROL -1
|
||||
|
||||
#define PYSQLITE_VERSION "2.6.0"
|
||||
#define MODULE_NAME "sqlite3"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue