diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -32,6 +32,12 @@ #include "pythread.h" +/*[clinic input] +module sqlite3 +class sqlite3.Connection +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + #define ACTION_FINALIZE 1 #define ACTION_RESET 2 @@ -59,7 +65,7 @@ static void _sqlite3_result_error(sqlite #endif } -int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +static int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) { static char *kwlist[] = { "database", "timeout", "detect_types", "isolation_level", @@ -243,7 +249,7 @@ void pysqlite_do_all_statements(pysqlite } } -void pysqlite_connection_dealloc(pysqlite_Connection* self) +static void pysqlite_connection_dealloc(pysqlite_Connection* self) { Py_XDECREF(self->statement_cache); @@ -294,17 +300,48 @@ error: return 0; } -PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Connection.cursor + + self: self(type="pysqlite_Connection *") + factory: object = NULL + +Return a cursor for the connection. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_cursor__doc__, +"cursor(factory=None)\n" +"Return a cursor for the connection."); + +#define SQLITE3_CONNECTION_CURSOR_METHODDEF \ + {"cursor", (PyCFunction)sqlite3_Connection_cursor, METH_VARARGS|METH_KEYWORDS, sqlite3_Connection_cursor__doc__}, + +static PyObject * +sqlite3_Connection_cursor_impl(pysqlite_Connection *self, PyObject *factory); + +static PyObject * +sqlite3_Connection_cursor(PyObject *self, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"factory", NULL, NULL}; - PyObject* factory = NULL; + PyObject *return_value = NULL; + static char *_keywords[] = {"factory", NULL}; + PyObject *factory = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:cursor", _keywords, + &factory)) + goto exit; + return_value = sqlite3_Connection_cursor_impl((pysqlite_Connection *)self, factory); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_cursor_impl(pysqlite_Connection *self, PyObject *factory) +/*[clinic end generated code: checksum=05ca34a9c607a9894e32a1def925f2eb8e6f0354]*/ +{ PyObject* cursor; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, - &factory)) { - return NULL; - } - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } @@ -326,7 +363,37 @@ PyObject* pysqlite_connection_cursor(pys return cursor; } -PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args) +/*[clinic input] +sqlite3.Connection.close + + self: self(type="pysqlite_Connection *") + +Closes the connection. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_close__doc__, +"close()\n" +"Closes the connection."); + +#define SQLITE3_CONNECTION_CLOSE_METHODDEF \ + {"close", (PyCFunction)sqlite3_Connection_close, METH_NOARGS, sqlite3_Connection_close__doc__}, + +static PyObject * +sqlite3_Connection_close_impl(pysqlite_Connection *self); + +static PyObject * +sqlite3_Connection_close(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Connection_close_impl((pysqlite_Connection *)self); + + return return_value; +} + +static PyObject * +sqlite3_Connection_close_impl(pysqlite_Connection *self) +/*[clinic end generated code: checksum=8ee9eddd376f9f2234afbea51c84862eca001c97]*/ { int rc; @@ -412,7 +479,37 @@ error: } } -PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) +/*[clinic input] +sqlite3.Connection.commit + + self: self(type="pysqlite_Connection *") + +Commit the current transaction. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_commit__doc__, +"commit()\n" +"Commit the current transaction."); + +#define SQLITE3_CONNECTION_COMMIT_METHODDEF \ + {"commit", (PyCFunction)sqlite3_Connection_commit, METH_NOARGS, sqlite3_Connection_commit__doc__}, + +static PyObject * +sqlite3_Connection_commit_impl(pysqlite_Connection *self); + +static PyObject * +sqlite3_Connection_commit(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Connection_commit_impl((pysqlite_Connection *)self); + + return return_value; +} + +static PyObject * +sqlite3_Connection_commit_impl(pysqlite_Connection *self) +/*[clinic end generated code: checksum=e74606530f3dc3d9ed1d5d7f36d5bdb20725c0ac]*/ { int rc; const char* tail; @@ -458,7 +555,42 @@ error: } } -PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args) +PyObject* pysqlite_connection_commit(pysqlite_Connection* self) +{ + return sqlite3_Connection_commit_impl(self); +} + +/*[clinic input] +sqlite3.Connection.rollback + + self: self(type="pysqlite_Connection *") + +Roll back the current transaction. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_rollback__doc__, +"rollback()\n" +"Roll back the current transaction."); + +#define SQLITE3_CONNECTION_ROLLBACK_METHODDEF \ + {"rollback", (PyCFunction)sqlite3_Connection_rollback, METH_NOARGS, sqlite3_Connection_rollback__doc__}, + +static PyObject * +sqlite3_Connection_rollback_impl(pysqlite_Connection *self); + +static PyObject * +sqlite3_Connection_rollback(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Connection_rollback_impl((pysqlite_Connection *)self); + + return return_value; +} + +static PyObject * +sqlite3_Connection_rollback_impl(pysqlite_Connection *self) +/*[clinic end generated code: checksum=84aa4a623ee33698b4ad300f8d8ab5980be5133b]*/ { int rc; const char* tail; @@ -830,25 +962,56 @@ static void _pysqlite_drop_unused_cursor self->cursors = new_list; } -PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Connection.create_function + + self: self(type="pysqlite_Connection *") + name: str + narg: int + func: object + +Creates a new function. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_create_function__doc__, +"create_function(name, narg, func)\n" +"Creates a new function. Non-standard."); + +#define SQLITE3_CONNECTION_CREATE_FUNCTION_METHODDEF \ + {"create_function", (PyCFunction)sqlite3_Connection_create_function, METH_VARARGS|METH_KEYWORDS, sqlite3_Connection_create_function__doc__}, + +static PyObject * +sqlite3_Connection_create_function_impl(pysqlite_Connection *self, const char *name, int narg, PyObject *func); + +static PyObject * +sqlite3_Connection_create_function(PyObject *self, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"name", "narg", "func", NULL, NULL}; + PyObject *return_value = NULL; + static char *_keywords[] = {"name", "narg", "func", NULL}; + const char *name; + int narg; + PyObject *func; - PyObject* func; - char* name; - int narg; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "siO:create_function", _keywords, + &name, &narg, &func)) + goto exit; + return_value = sqlite3_Connection_create_function_impl((pysqlite_Connection *)self, name, narg, func); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_create_function_impl(pysqlite_Connection *self, const char *name, int narg, PyObject *func) +/*[clinic end generated code: checksum=7f25fea24092c9e20d7228821c6fc6ae874ad09a]*/ +{ int rc; if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist, - &name, &narg, &func)) - { - return NULL; - } - rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL); if (rc != SQLITE_OK) { @@ -864,24 +1027,56 @@ PyObject* pysqlite_connection_create_fun } } -PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Connection.create_aggregate + + self: self(type="pysqlite_Connection *") + name: str + n_arg: int + aggregate_class: object + +Creates a new aggregate. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_create_aggregate__doc__, +"create_aggregate(name, n_arg, aggregate_class)\n" +"Creates a new aggregate. Non-standard."); + +#define SQLITE3_CONNECTION_CREATE_AGGREGATE_METHODDEF \ + {"create_aggregate", (PyCFunction)sqlite3_Connection_create_aggregate, METH_VARARGS|METH_KEYWORDS, sqlite3_Connection_create_aggregate__doc__}, + +static PyObject * +sqlite3_Connection_create_aggregate_impl(pysqlite_Connection *self, const char *name, int n_arg, PyObject *aggregate_class); + +static PyObject * +sqlite3_Connection_create_aggregate(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject* aggregate_class; + PyObject *return_value = NULL; + static char *_keywords[] = {"name", "n_arg", "aggregate_class", NULL}; + const char *name; + int n_arg; + PyObject *aggregate_class; - int n_arg; - char* name; - static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "siO:create_aggregate", _keywords, + &name, &n_arg, &aggregate_class)) + goto exit; + return_value = sqlite3_Connection_create_aggregate_impl((pysqlite_Connection *)self, name, n_arg, aggregate_class); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_create_aggregate_impl(pysqlite_Connection *self, const char *name, int n_arg, PyObject *aggregate_class) +/*[clinic end generated code: checksum=e11a999d314b35fbbf8f951f8a3847519b4dcfa0]*/ +{ int rc; if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate", - kwlist, &name, &n_arg, &aggregate_class)) { - return NULL; - } - rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback); if (rc != SQLITE_OK) { /* Workaround for SQLite bug: no error code or string is available here */ @@ -1002,29 +1197,59 @@ static void _trace_callback(void* user_a #endif } -static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Connection.set_authorizer + + self: self(type="pysqlite_Connection *") + authorizer_callback: object + +Sets authorizer callback. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_set_authorizer__doc__, +"set_authorizer(authorizer_callback)\n" +"Sets authorizer callback. Non-standard."); + +#define SQLITE3_CONNECTION_SET_AUTHORIZER_METHODDEF \ + {"set_authorizer", (PyCFunction)sqlite3_Connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, sqlite3_Connection_set_authorizer__doc__}, + +static PyObject * +sqlite3_Connection_set_authorizer_impl(pysqlite_Connection *self, PyObject *authorizer_callback); + +static PyObject * +sqlite3_Connection_set_authorizer(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject* authorizer_cb; + PyObject *return_value = NULL; + static char *_keywords[] = {"authorizer_callback", NULL}; + PyObject *authorizer_callback; - static char *kwlist[] = { "authorizer_callback", NULL }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O:set_authorizer", _keywords, + &authorizer_callback)) + goto exit; + return_value = sqlite3_Connection_set_authorizer_impl((pysqlite_Connection *)self, authorizer_callback); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_set_authorizer_impl(pysqlite_Connection *self, PyObject *authorizer_callback) +/*[clinic end generated code: checksum=2e83cdf8c1c2933c3c9de8db71aaa2206302b26a]*/ +{ int rc; if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer", - kwlist, &authorizer_cb)) { - return NULL; - } - - rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb); + rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_callback); if (rc != SQLITE_OK) { PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback"); return NULL; } else { - if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1) + if (PyDict_SetItem(self->function_pinboard, authorizer_callback, Py_None) == -1) return NULL; Py_INCREF(Py_None); @@ -1032,22 +1257,52 @@ static PyObject* pysqlite_connection_set } } -static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Connection.set_progress_handler + + self: self(type="pysqlite_Connection *") + progress_handler: object + n: int + +Sets progress handler callback. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_set_progress_handler__doc__, +"set_progress_handler(progress_handler, n)\n" +"Sets progress handler callback. Non-standard."); + +#define SQLITE3_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF \ + {"set_progress_handler", (PyCFunction)sqlite3_Connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS, sqlite3_Connection_set_progress_handler__doc__}, + +static PyObject * +sqlite3_Connection_set_progress_handler_impl(pysqlite_Connection *self, PyObject *progress_handler, int n); + +static PyObject * +sqlite3_Connection_set_progress_handler(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject* progress_handler; + PyObject *return_value = NULL; + static char *_keywords[] = {"progress_handler", "n", NULL}; + PyObject *progress_handler; int n; - static char *kwlist[] = { "progress_handler", "n", NULL }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "Oi:set_progress_handler", _keywords, + &progress_handler, &n)) + goto exit; + return_value = sqlite3_Connection_set_progress_handler_impl((pysqlite_Connection *)self, progress_handler, n); +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_set_progress_handler_impl(pysqlite_Connection *self, PyObject *progress_handler, int n) +/*[clinic end generated code: checksum=a81513fafdeae4f4667fbbf1250669ceffc5993a]*/ +{ if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler", - kwlist, &progress_handler, &n)) { - return NULL; - } - if (progress_handler == Py_None) { /* None clears the progress handler previously set */ sqlite3_progress_handler(self->db, 0, 0, (void*)0); @@ -1061,21 +1316,50 @@ static PyObject* pysqlite_connection_set return Py_None; } -static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Connection.set_trace_callback + + self: self(type="pysqlite_Connection *") + trace_callback: object + +Sets a trace callback called for each SQL statement (passed as unicode). Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_set_trace_callback__doc__, +"set_trace_callback(trace_callback)\n" +"Sets a trace callback called for each SQL statement (passed as unicode). Non-standard."); + +#define SQLITE3_CONNECTION_SET_TRACE_CALLBACK_METHODDEF \ + {"set_trace_callback", (PyCFunction)sqlite3_Connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS, sqlite3_Connection_set_trace_callback__doc__}, + +static PyObject * +sqlite3_Connection_set_trace_callback_impl(pysqlite_Connection *self, PyObject *trace_callback); + +static PyObject * +sqlite3_Connection_set_trace_callback(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject* trace_callback; + PyObject *return_value = NULL; + static char *_keywords[] = {"trace_callback", NULL}; + PyObject *trace_callback; - static char *kwlist[] = { "trace_callback", NULL }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O:set_trace_callback", _keywords, + &trace_callback)) + goto exit; + return_value = sqlite3_Connection_set_trace_callback_impl((pysqlite_Connection *)self, trace_callback); +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_set_trace_callback_impl(pysqlite_Connection *self, PyObject *trace_callback) +/*[clinic end generated code: checksum=e1599e641c9bc51c41a54dcd41385cafbac1eec0]*/ +{ if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback", - kwlist, &trace_callback)) { - return NULL; - } - if (trace_callback == Py_None) { /* None clears the trace callback previously set */ sqlite3_trace(self->db, 0, (void*)0); @@ -1090,19 +1374,52 @@ static PyObject* pysqlite_connection_set } #ifdef HAVE_LOAD_EXTENSION -static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args) +/*[clinic input] +sqlite3.Connection.enable_load_extension + + self: self(type="pysqlite_Connection *") + onoff: int + / + +Enable dynamic loading of SQLite extension modules. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_enable_load_extension__doc__, +"enable_load_extension(onoff)\n" +"Enable dynamic loading of SQLite extension modules. Non-standard."); + +#define SQLITE3_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF \ + {"enable_load_extension", (PyCFunction)sqlite3_Connection_enable_load_extension, METH_VARARGS, sqlite3_Connection_enable_load_extension__doc__}, + +static PyObject * +sqlite3_Connection_enable_load_extension_impl(pysqlite_Connection *self, int onoff); + +static PyObject * +sqlite3_Connection_enable_load_extension(PyObject *self, PyObject *args) +{ + PyObject *return_value = NULL; + int onoff; + + if (!PyArg_ParseTuple(args, + "i:enable_load_extension", + &onoff)) + goto exit; + return_value = sqlite3_Connection_enable_load_extension_impl((pysqlite_Connection *)self, onoff); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_enable_load_extension_impl(pysqlite_Connection *self, int onoff) +/*[clinic end generated code: checksum=876721e74a1188630181737ce6cca36bc92070a5]*/ { int rc; - int onoff; if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTuple(args, "i", &onoff)) { - return NULL; - } - rc = sqlite3_enable_load_extension(self->db, onoff); if (rc != SQLITE_OK) { @@ -1114,20 +1431,53 @@ static PyObject* pysqlite_enable_load_ex } } -static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args) +/*[clinic input] +sqlite3.Connection.load_extension + + self: self(type="pysqlite_Connection *") + extension_name: str + / + +Load SQLite extension module. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_load_extension__doc__, +"load_extension(extension_name)\n" +"Load SQLite extension module. Non-standard."); + +#define SQLITE3_CONNECTION_LOAD_EXTENSION_METHODDEF \ + {"load_extension", (PyCFunction)sqlite3_Connection_load_extension, METH_VARARGS, sqlite3_Connection_load_extension__doc__}, + +static PyObject * +sqlite3_Connection_load_extension_impl(pysqlite_Connection *self, const char *extension_name); + +static PyObject * +sqlite3_Connection_load_extension(PyObject *self, PyObject *args) +{ + PyObject *return_value = NULL; + const char *extension_name; + + if (!PyArg_ParseTuple(args, + "s:load_extension", + &extension_name)) + goto exit; + return_value = sqlite3_Connection_load_extension_impl((pysqlite_Connection *)self, extension_name); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_load_extension_impl(pysqlite_Connection *self, const char *extension_name) +/*[clinic end generated code: checksum=698f0882b8eb8979c4a7218d57e9fe4e8e74fbcc]*/ { int rc; - char* extension_name; char* errmsg; if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { return NULL; } - if (!PyArg_ParseTuple(args, "s", &extension_name)) { - return NULL; - } - rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg); if (rc != 0) { PyErr_SetString(pysqlite_OperationalError, errmsg); @@ -1188,7 +1538,7 @@ static int pysqlite_connection_set_isola Py_INCREF(Py_None); self->isolation_level = Py_None; - res = pysqlite_connection_commit(self, NULL); + res = pysqlite_connection_commit(self); if (!res) { return -1; } @@ -1433,8 +1783,37 @@ finally: return result; } +/*[clinic input] +sqlite3.Connection.interrupt + + self: self(type="pysqlite_Connection *") + +Abort any pending database operation. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_interrupt__doc__, +"interrupt()\n" +"Abort any pending database operation. Non-standard."); + +#define SQLITE3_CONNECTION_INTERRUPT_METHODDEF \ + {"interrupt", (PyCFunction)sqlite3_Connection_interrupt, METH_NOARGS, sqlite3_Connection_interrupt__doc__}, + static PyObject * -pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args) +sqlite3_Connection_interrupt_impl(pysqlite_Connection *self); + +static PyObject * +sqlite3_Connection_interrupt(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Connection_interrupt_impl((pysqlite_Connection *)self); + + return return_value; +} + +static PyObject * +sqlite3_Connection_interrupt_impl(pysqlite_Connection *self) +/*[clinic end generated code: checksum=1dac3c13e218672f8ae7c9e743dddf39ea69836e]*/ { PyObject* retval = NULL; @@ -1451,15 +1830,41 @@ finally: return retval; } -/* Function author: Paul Kippes - * Class method of Connection to call the Python function _iterdump - * of the sqlite3 module. - */ +/*[clinic input] +sqlite3.Connection.iterdump + + self: self(type="pysqlite_Connection *") + +Returns iterator to the dump of the database in an SQL text format. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_iterdump__doc__, +"iterdump()\n" +"Returns iterator to the dump of the database in an SQL text format. Non-standard."); + +#define SQLITE3_CONNECTION_ITERDUMP_METHODDEF \ + {"iterdump", (PyCFunction)sqlite3_Connection_iterdump, METH_NOARGS, sqlite3_Connection_iterdump__doc__}, + static PyObject * -pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args) +sqlite3_Connection_iterdump_impl(pysqlite_Connection *self); + +static PyObject * +sqlite3_Connection_iterdump(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Connection_iterdump_impl((pysqlite_Connection *)self); + + return return_value; +} + +static PyObject * +sqlite3_Connection_iterdump_impl(pysqlite_Connection *self) +/*[clinic end generated code: checksum=816222f6ffaf614dd918a04932c58ab15ab633b9]*/ { PyObject* retval = NULL; PyObject* module = NULL; + PyObject* args = NULL; PyObject* module_dict; PyObject* pyfn_iterdump; @@ -1497,12 +1902,49 @@ finally: return retval; } +/*[clinic input] +sqlite3.Connection.create_collation + + self: self(type="pysqlite_Connection *") + name: object(subclass_of='&PyUnicode_Type') + callable: object + / + +Creates a collation function. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection_create_collation__doc__, +"create_collation(name, callable)\n" +"Creates a collation function. Non-standard."); + +#define SQLITE3_CONNECTION_CREATE_COLLATION_METHODDEF \ + {"create_collation", (PyCFunction)sqlite3_Connection_create_collation, METH_VARARGS, sqlite3_Connection_create_collation__doc__}, + static PyObject * -pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) +sqlite3_Connection_create_collation_impl(pysqlite_Connection *self, PyObject *name, PyObject *callable); + +static PyObject * +sqlite3_Connection_create_collation(PyObject *self, PyObject *args) { - PyObject* callable; + PyObject *return_value = NULL; + PyObject *name; + PyObject *callable; + + if (!PyArg_ParseTuple(args, + "O!O:create_collation", + &PyUnicode_Type, &name, &callable)) + goto exit; + return_value = sqlite3_Connection_create_collation_impl((pysqlite_Connection *)self, name, callable); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection_create_collation_impl(pysqlite_Connection *self, PyObject *name, PyObject *callable) +/*[clinic end generated code: checksum=b4625c27a6222e70761c694ab9286137410c9496]*/ +{ PyObject* uppercase_name = 0; - PyObject* name; PyObject* retval; Py_ssize_t i, len; _Py_IDENTIFIER(upper); @@ -1515,10 +1957,6 @@ pysqlite_connection_create_collation(pys goto finally; } - if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) { - goto finally; - } - uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, ""); if (!uppercase_name) { goto finally; @@ -1583,28 +2021,89 @@ finally: return retval; } -/* Called when the connection is used as a context manager. Returns itself as a - * convenience to the caller. */ +/*[clinic input] +sqlite3.Connection.__enter__ + + self: self(type="pysqlite_Connection *") + +For context manager. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection___enter____doc__, +"__enter__()\n" +"For context manager. Non-standard."); + +#define SQLITE3_CONNECTION___ENTER___METHODDEF \ + {"__enter__", (PyCFunction)sqlite3_Connection___enter__, METH_NOARGS, sqlite3_Connection___enter____doc__}, + static PyObject * -pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args) +sqlite3_Connection___enter___impl(pysqlite_Connection *self); + +static PyObject * +sqlite3_Connection___enter__(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Connection___enter___impl((pysqlite_Connection *)self); + + return return_value; +} + +static PyObject * +sqlite3_Connection___enter___impl(pysqlite_Connection *self) +/*[clinic end generated code: checksum=383f21c4af0bda4d2781539c299828421dac7f52]*/ { Py_INCREF(self); return (PyObject*)self; } -/** Called when the connection is used as a context manager. If there was any - * exception, a rollback takes place; otherwise we commit. */ +/*[clinic input] +sqlite3.Connection.__exit__ + + self: self(type="pysqlite_Connection *") + exc_type: object + exc_value: object + exc_tb: object + / + +For context manager. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Connection___exit____doc__, +"__exit__(exc_type, exc_value, exc_tb)\n" +"For context manager. Non-standard."); + +#define SQLITE3_CONNECTION___EXIT___METHODDEF \ + {"__exit__", (PyCFunction)sqlite3_Connection___exit__, METH_VARARGS, sqlite3_Connection___exit____doc__}, + static PyObject * -pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) +sqlite3_Connection___exit___impl(pysqlite_Connection *self, PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb); + +static PyObject * +sqlite3_Connection___exit__(PyObject *self, PyObject *args) { - PyObject* exc_type, *exc_value, *exc_tb; + PyObject *return_value = NULL; + PyObject *exc_type; + PyObject *exc_value; + PyObject *exc_tb; + + if (!PyArg_ParseTuple(args, + "OOO:__exit__", + &exc_type, &exc_value, &exc_tb)) + goto exit; + return_value = sqlite3_Connection___exit___impl((pysqlite_Connection *)self, exc_type, exc_value, exc_tb); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Connection___exit___impl(pysqlite_Connection *self, PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb) +/*[clinic end generated code: checksum=f475f66a4c7baf13b6b66126ac7ff642df38fc87]*/ +{ char* method_name; PyObject* result; - if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) { - return NULL; - } - if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) { method_name = "commit"; } else { @@ -1630,46 +2129,30 @@ static PyGetSetDef connection_getset[] = }; static PyMethodDef connection_methods[] = { - {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Return a cursor for the connection.")}, - {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS, - PyDoc_STR("Closes the connection.")}, - {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS, - PyDoc_STR("Commit the current transaction.")}, - {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS, - PyDoc_STR("Roll back the current transaction.")}, - {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Creates a new function. Non-standard.")}, - {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Creates a new aggregate. Non-standard.")}, - {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Sets authorizer callback. Non-standard.")}, + SQLITE3_CONNECTION_CURSOR_METHODDEF + SQLITE3_CONNECTION_CLOSE_METHODDEF + SQLITE3_CONNECTION_COMMIT_METHODDEF + SQLITE3_CONNECTION_ROLLBACK_METHODDEF + SQLITE3_CONNECTION_CREATE_FUNCTION_METHODDEF + SQLITE3_CONNECTION_CREATE_AGGREGATE_METHODDEF + SQLITE3_CONNECTION_SET_AUTHORIZER_METHODDEF #ifdef HAVE_LOAD_EXTENSION - {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS, - PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")}, - {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS, - PyDoc_STR("Load SQLite extension module. Non-standard.")}, + SQLITE3_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF + SQLITE3_CONNECTION_LOAD_EXTENSION_METHODDEF #endif - {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Sets progress handler callback. Non-standard.")}, - {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")}, + SQLITE3_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF + SQLITE3_CONNECTION_SET_TRACE_CALLBACK_METHODDEF {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS, PyDoc_STR("Executes a SQL statement. Non-standard.")}, {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS, PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")}, {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS, PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, - {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS, - PyDoc_STR("Creates a collation function. Non-standard.")}, - {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, - PyDoc_STR("Abort any pending database operation. Non-standard.")}, - {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS, - PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")}, - {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS, - PyDoc_STR("For context manager. Non-standard.")}, - {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS, - PyDoc_STR("For context manager. Non-standard.")}, + SQLITE3_CONNECTION_CREATE_COLLATION_METHODDEF + SQLITE3_CONNECTION_INTERRUPT_METHODDEF + SQLITE3_CONNECTION_ITERDUMP_METHODDEF + SQLITE3_CONNECTION___ENTER___METHODDEF + SQLITE3_CONNECTION___EXIT___METHODDEF {NULL, NULL} }; diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h --- a/Modules/_sqlite/connection.h +++ b/Modules/_sqlite/connection.h @@ -113,15 +113,8 @@ typedef struct extern PyTypeObject pysqlite_ConnectionType; -PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware); -void pysqlite_connection_dealloc(pysqlite_Connection* self); -PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs); -PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args); PyObject* _pysqlite_connection_begin(pysqlite_Connection* self); -PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args); -PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args); -PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw); -int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs); +PyObject* pysqlite_connection_commit(pysqlite_Connection* self); int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor); int pysqlite_check_thread(pysqlite_Connection* self); diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -25,6 +25,12 @@ #include "module.h" #include "util.h" +/*[clinic input] +module sqlite3 +class sqlite3.Cursor +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + 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."; @@ -423,9 +429,8 @@ static int check_cursor(pysqlite_Cursor* return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection); } -PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) +PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject *second_argument) { - PyObject* operation; const char* operation_cstr; Py_ssize_t operation_len; PyObject* parameters_list = NULL; @@ -438,7 +443,6 @@ PyObject* _pysqlite_query_execute(pysqli int numcols; int statement_type; PyObject* descriptor; - PyObject* second_argument = NULL; if (!check_cursor(self)) { goto error; @@ -452,10 +456,6 @@ PyObject* _pysqlite_query_execute(pysqli if (multiple) { /* executemany() */ - if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { - goto error; - } - if (!PyUnicode_Check(operation)) { PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); goto error; @@ -474,10 +474,6 @@ PyObject* _pysqlite_query_execute(pysqli } } else { /* execute() */ - if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { - goto error; - } - if (!PyUnicode_Check(operation)) { PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); goto error; @@ -579,7 +575,7 @@ PyObject* _pysqlite_query_execute(pysqli /* 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); + result = pysqlite_connection_commit(self->connection); if (!result) { goto error; } @@ -746,29 +742,123 @@ error: } } -PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) +/*[clinic input] +sqlite3.Cursor.execute + + self: self(type="pysqlite_Cursor *") + operation: object + parameters: object = NULL + / + +Executes a SQL statement. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_execute__doc__, +"execute(operation, parameters=None)\n" +"Executes a SQL statement."); + +#define SQLITE3_CURSOR_EXECUTE_METHODDEF \ + {"execute", (PyCFunction)sqlite3_Cursor_execute, METH_VARARGS, sqlite3_Cursor_execute__doc__}, + +static PyObject * +sqlite3_Cursor_execute_impl(pysqlite_Cursor *self, PyObject *operation, PyObject *parameters); + +static PyObject * +sqlite3_Cursor_execute(PyObject *self, PyObject *args) { - return _pysqlite_query_execute(self, 0, args); + PyObject *return_value = NULL; + PyObject *operation; + PyObject *parameters = NULL; + + if (!PyArg_ParseTuple(args, + "O|O:execute", + &operation, ¶meters)) + goto exit; + return_value = sqlite3_Cursor_execute_impl((pysqlite_Cursor *)self, operation, parameters); + +exit: + return return_value; } -PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) +static PyObject * +sqlite3_Cursor_execute_impl(pysqlite_Cursor *self, PyObject *operation, PyObject *parameters) +/*[clinic end generated code: checksum=13d6f3b8df1277a1407503cb20d4f3c8e4b36336]*/ { - return _pysqlite_query_execute(self, 1, args); + return _pysqlite_query_execute(self, 0, operation, parameters); } -PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) +/*[clinic input] +sqlite3.Cursor.executemany + + self: self(type="pysqlite_Cursor *") + operation: object + parameters: object + / + +Repeatedly executes a SQL statement. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_executemany__doc__, +"executemany(operation, parameters)\n" +"Repeatedly executes a SQL statement."); + +#define SQLITE3_CURSOR_EXECUTEMANY_METHODDEF \ + {"executemany", (PyCFunction)sqlite3_Cursor_executemany, METH_VARARGS, sqlite3_Cursor_executemany__doc__}, + +static PyObject * +sqlite3_Cursor_executemany_impl(pysqlite_Cursor *self, PyObject *operation, PyObject *parameters); + +static PyObject * +sqlite3_Cursor_executemany(PyObject *self, PyObject *args) { - PyObject* script_obj; + PyObject *return_value = NULL; + PyObject *operation; + PyObject *parameters; + + if (!PyArg_ParseTuple(args, + "OO:executemany", + &operation, ¶meters)) + goto exit; + return_value = sqlite3_Cursor_executemany_impl((pysqlite_Cursor *)self, operation, parameters); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Cursor_executemany_impl(pysqlite_Cursor *self, PyObject *operation, PyObject *parameters) +/*[clinic end generated code: checksum=f051d1c740e2d923c4294575429370750b3bcda8]*/ +{ + return _pysqlite_query_execute(self, 1, operation, parameters); +} + +/*[clinic input] +sqlite3.Cursor.executescript + + self: self(type="pysqlite_Cursor *") + script_obj: object + / + +Executes a multiple SQL statements at once. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_executescript__doc__, +"executescript(script_obj)\n" +"Executes a multiple SQL statements at once. Non-standard."); + +#define SQLITE3_CURSOR_EXECUTESCRIPT_METHODDEF \ + {"executescript", (PyCFunction)sqlite3_Cursor_executescript, METH_O, sqlite3_Cursor_executescript__doc__}, + +static PyObject * +sqlite3_Cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj) +/*[clinic end generated code: checksum=7bde3f238281106801305207b414f543dea9eab7]*/ +{ PyObject* script_str = NULL; const char* script_cstr; sqlite3_stmt* statement; int rc; PyObject* result; - if (!PyArg_ParseTuple(args, "O", &script_obj)) { - return NULL; - } - if (!check_cursor(self)) { return NULL; } @@ -786,7 +876,7 @@ PyObject* pysqlite_cursor_executescript( } /* commit first */ - result = pysqlite_connection_commit(self->connection, NULL); + result = pysqlite_connection_commit(self->connection); if (!result) { goto error; } @@ -908,7 +998,37 @@ PyObject* pysqlite_cursor_iternext(pysql return next_row; } -PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) +/*[clinic input] +sqlite3.Cursor.fetchone + + self: self(type="pysqlite_Cursor *") + +Fetches one row from the resultset. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_fetchone__doc__, +"fetchone()\n" +"Fetches one row from the resultset."); + +#define SQLITE3_CURSOR_FETCHONE_METHODDEF \ + {"fetchone", (PyCFunction)sqlite3_Cursor_fetchone, METH_NOARGS, sqlite3_Cursor_fetchone__doc__}, + +static PyObject * +sqlite3_Cursor_fetchone_impl(pysqlite_Cursor *self); + +static PyObject * +sqlite3_Cursor_fetchone(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Cursor_fetchone_impl((pysqlite_Cursor *)self); + + return return_value; +} + +static PyObject * +sqlite3_Cursor_fetchone_impl(pysqlite_Cursor *self) +/*[clinic end generated code: checksum=f31288872b04f59b2bf054bddd6bdd7e2bfad367]*/ { PyObject* row; @@ -921,19 +1041,51 @@ PyObject* pysqlite_cursor_fetchone(pysql return row; } -PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) +/*[clinic input] +sqlite3.Cursor.fetchmany + + self: self(type="pysqlite_Cursor *") + size: int(c_default="((pysqlite_Cursor *)self)->arraysize") = 0 + +Fetches several rows from the resultset. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_fetchmany__doc__, +"fetchmany(size=0)\n" +"Fetches several rows from the resultset."); + +#define SQLITE3_CURSOR_FETCHMANY_METHODDEF \ + {"fetchmany", (PyCFunction)sqlite3_Cursor_fetchmany, METH_VARARGS|METH_KEYWORDS, sqlite3_Cursor_fetchmany__doc__}, + +static PyObject * +sqlite3_Cursor_fetchmany_impl(pysqlite_Cursor *self, int size); + +static PyObject * +sqlite3_Cursor_fetchmany(PyObject *self, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"size", NULL, NULL}; + PyObject *return_value = NULL; + static char *_keywords[] = {"size", NULL}; + int size = ((pysqlite_Cursor *)self)->arraysize; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|i:fetchmany", _keywords, + &size)) + goto exit; + return_value = sqlite3_Cursor_fetchmany_impl((pysqlite_Cursor *)self, size); + +exit: + return return_value; +} + +static PyObject * +sqlite3_Cursor_fetchmany_impl(pysqlite_Cursor *self, int size) +/*[clinic end generated code: checksum=42cd5048c6e2c10a16c9566215a06345ac11e304]*/ +{ PyObject* row; PyObject* list; - int maxrows = self->arraysize; + int maxrows = size; int counter = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) { - return NULL; - } - list = PyList_New(0); if (!list) { return NULL; @@ -964,7 +1116,37 @@ PyObject* pysqlite_cursor_fetchmany(pysq } } -PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) +/*[clinic input] +sqlite3.Cursor.fetchall + + self: self(type="pysqlite_Cursor *") + +Fetches all rows from the resultset. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_fetchall__doc__, +"fetchall()\n" +"Fetches all rows from the resultset."); + +#define SQLITE3_CURSOR_FETCHALL_METHODDEF \ + {"fetchall", (PyCFunction)sqlite3_Cursor_fetchall, METH_NOARGS, sqlite3_Cursor_fetchall__doc__}, + +static PyObject * +sqlite3_Cursor_fetchall_impl(pysqlite_Cursor *self); + +static PyObject * +sqlite3_Cursor_fetchall(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Cursor_fetchall_impl((pysqlite_Cursor *)self); + + return return_value; +} + +static PyObject * +sqlite3_Cursor_fetchall_impl(pysqlite_Cursor *self) +/*[clinic end generated code: checksum=44cb73c6b8cdda0d416c79e0a47ae0efee4626aa]*/ { PyObject* row; PyObject* list; @@ -1000,7 +1182,37 @@ PyObject* pysqlite_noop(pysqlite_Connect return Py_None; } -PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) +/*[clinic input] +sqlite3.Cursor.close + + self: self(type="pysqlite_Cursor *") + +Closes the cursor. +[clinic start generated code]*/ + +PyDoc_STRVAR(sqlite3_Cursor_close__doc__, +"close()\n" +"Closes the cursor."); + +#define SQLITE3_CURSOR_CLOSE_METHODDEF \ + {"close", (PyCFunction)sqlite3_Cursor_close, METH_NOARGS, sqlite3_Cursor_close__doc__}, + +static PyObject * +sqlite3_Cursor_close_impl(pysqlite_Cursor *self); + +static PyObject * +sqlite3_Cursor_close(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = sqlite3_Cursor_close_impl((pysqlite_Cursor *)self); + + return return_value; +} + +static PyObject * +sqlite3_Cursor_close_impl(pysqlite_Cursor *self) +/*[clinic end generated code: checksum=753a30b8d6bcffc06d41d450ab368dd4bf65ff5d]*/ { if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { return NULL; @@ -1018,20 +1230,13 @@ PyObject* pysqlite_cursor_close(pysqlite } static PyMethodDef cursor_methods[] = { - {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, - PyDoc_STR("Executes a SQL statement.")}, - {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, - PyDoc_STR("Repeatedly executes a SQL statement.")}, - {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, - PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, - {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, - PyDoc_STR("Fetches one row from the resultset.")}, - {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS, - PyDoc_STR("Fetches several rows from the resultset.")}, - {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, - PyDoc_STR("Fetches all rows from the resultset.")}, - {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, - PyDoc_STR("Closes the cursor.")}, + SQLITE3_CURSOR_EXECUTE_METHODDEF + SQLITE3_CURSOR_EXECUTEMANY_METHODDEF + SQLITE3_CURSOR_EXECUTESCRIPT_METHODDEF + SQLITE3_CURSOR_FETCHONE_METHODDEF + SQLITE3_CURSOR_FETCHMANY_METHODDEF + SQLITE3_CURSOR_FETCHALL_METHODDEF + SQLITE3_CURSOR_CLOSE_METHODDEF {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -132,15 +132,3 @@ pysqlite_microprotocols_adapt(PyObject * PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); return NULL; } - -/** module-level functions **/ - -PyObject * -pysqlite_adapt(pysqlite_Cursor *self, PyObject *args) -{ - PyObject *obj, *alt = NULL; - PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType; - - if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL; - return pysqlite_microprotocols_adapt(obj, proto, alt); -} diff --git a/Modules/_sqlite/microprotocols.h b/Modules/_sqlite/microprotocols.h --- a/Modules/_sqlite/microprotocols.h +++ b/Modules/_sqlite/microprotocols.h @@ -47,9 +47,4 @@ extern int pysqlite_microprotocols_add( extern PyObject *pysqlite_microprotocols_adapt( PyObject *obj, PyObject *proto, PyObject *alt); -extern PyObject * - pysqlite_adapt(pysqlite_Cursor* self, PyObject *args); -#define pysqlite_adapt_doc \ - "adapt(obj, protocol, alternate) -> adapt obj to given protocol. Non-standard." - #endif /* !defined(PSYCOPG_MICROPROTOCOLS_H) */ diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -33,6 +33,11 @@ #define HAVE_SHARED_CACHE #endif +/*[clinic input] +module sqlite3 +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + /* static objects at module-level */ PyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError, @@ -91,19 +96,47 @@ Opens a connection to the SQLite databas \":memory:\" to open a database connection to a database that resides in\n\ RAM instead of on disk."); -static PyObject* module_complete(PyObject* self, PyObject* args, PyObject* - kwargs) +/*[clinic input] +sqlite3.complete as module_complete + + statement: str + +Checks if a string contains a complete SQL statement. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(module_complete__doc__, +"complete(statement)\n" +"Checks if a string contains a complete SQL statement. Non-standard."); + +#define MODULE_COMPLETE_METHODDEF \ + {"complete", (PyCFunction)module_complete, METH_VARARGS|METH_KEYWORDS, module_complete__doc__}, + +static PyObject * +module_complete_impl(PyModuleDef *module, const char *statement); + +static PyObject * +module_complete(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"statement", NULL, NULL}; - char* statement; + PyObject *return_value = NULL; + static char *_keywords[] = {"statement", NULL}; + const char *statement; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "s:complete", _keywords, + &statement)) + goto exit; + return_value = module_complete_impl(module, statement); + +exit: + return return_value; +} + +static PyObject * +module_complete_impl(PyModuleDef *module, const char *statement) +/*[clinic end generated code: checksum=c43ed748e4da7c82621ba9893a3c7c496b038ee3]*/ +{ PyObject* result; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement)) - { - return NULL; - } - if (sqlite3_complete(statement)) { result = Py_True; } else { @@ -115,24 +148,48 @@ static PyObject* module_complete(PyObjec return result; } -PyDoc_STRVAR(module_complete_doc, -"complete_statement(sql)\n\ -\n\ -Checks if a string contains a complete SQL statement. Non-standard."); +#ifdef HAVE_SHARED_CACHE +/*[clinic input] +sqlite3.complete as module_enable_shared_cache -#ifdef HAVE_SHARED_CACHE -static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject* - kwargs) + do_enable: int + +Enable or disable shared cache mode for the calling thread. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(module_enable_shared_cache__doc__, +"complete(do_enable)\n" +"Enable or disable shared cache mode for the calling thread. Non-standard."); + +#define MODULE_ENABLE_SHARED_CACHE_METHODDEF \ + {"complete", (PyCFunction)module_enable_shared_cache, METH_VARARGS|METH_KEYWORDS, module_enable_shared_cache__doc__}, + +static PyObject * +module_enable_shared_cache_impl(PyModuleDef *module, int do_enable); + +static PyObject * +module_enable_shared_cache(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"do_enable", NULL, NULL}; + PyObject *return_value = NULL; + static char *_keywords[] = {"do_enable", NULL}; int do_enable; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "i:complete", _keywords, + &do_enable)) + goto exit; + return_value = module_enable_shared_cache_impl(module, do_enable); + +exit: + return return_value; +} + +static PyObject * +module_enable_shared_cache_impl(PyModuleDef *module, int do_enable) +/*[clinic end generated code: checksum=1f3c26506b3325cb3047fdf56717472dffb0de02]*/ +{ int rc; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable)) - { - return NULL; - } - rc = sqlite3_enable_shared_cache(do_enable); if (rc != SQLITE_OK) { @@ -144,31 +201,61 @@ static PyObject* module_enable_shared_ca } } -PyDoc_STRVAR(module_enable_shared_cache_doc, -"enable_shared_cache(do_enable)\n\ -\n\ -Enable or disable shared cache mode for the calling thread.\n\ -Experimental/Non-standard."); #endif /* HAVE_SHARED_CACHE */ -static PyObject* module_register_adapter(PyObject* self, PyObject* args) +/*[clinic input] +sqlite3.register_adapter as module_register_adapter + + type_obj: object(type="PyTypeObject *") + caster: object + / + +Registers an adapter with pysqlite's adapter registry. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(module_register_adapter__doc__, +"register_adapter(type_obj, caster)\n" +"Registers an adapter with pysqlite\'s adapter registry. Non-standard."); + +#define MODULE_REGISTER_ADAPTER_METHODDEF \ + {"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, module_register_adapter__doc__}, + +static PyObject * +module_register_adapter_impl(PyModuleDef *module, PyTypeObject *type_obj, PyObject *caster); + +static PyObject * +module_register_adapter(PyModuleDef *module, PyObject *args) { - PyTypeObject* type; - PyObject* caster; + PyObject *return_value = NULL; + PyTypeObject *type_obj; + PyObject *caster; + + if (!PyArg_ParseTuple(args, + "OO:register_adapter", + &type_obj, &caster)) + goto exit; + return_value = module_register_adapter_impl(module, type_obj, caster); + +exit: + return return_value; +} + +static PyObject * +module_register_adapter_impl(PyModuleDef *module, PyTypeObject *type_obj, PyObject *caster) +/*[clinic end generated code: checksum=1d00c9c09466dae973bb62fa44bd4803ac57da2a]*/ +{ int rc; - if (!PyArg_ParseTuple(args, "OO", &type, &caster)) { - return NULL; - } - /* a basic type is adapted; there's a performance optimization if that's not the case * (99 % of all usages) */ - if (type == &PyLong_Type || type == &PyFloat_Type - || type == &PyUnicode_Type || type == &PyByteArray_Type) { + if (type_obj == &PyLong_Type || type_obj == &PyFloat_Type + || type_obj == &PyUnicode_Type || type_obj == &PyByteArray_Type) { pysqlite_BaseTypeAdapted = 1; } - rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster); + rc = pysqlite_microprotocols_add(type_obj, + (PyObject*)&pysqlite_PrepareProtocolType, + caster); if (rc == -1) return NULL; @@ -176,23 +263,51 @@ static PyObject* module_register_adapter return Py_None; } -PyDoc_STRVAR(module_register_adapter_doc, -"register_adapter(type, callable)\n\ -\n\ -Registers an adapter with pysqlite's adapter registry. Non-standard."); +/*[clinic input] +sqlite3.register_converter as module_register_converter -static PyObject* module_register_converter(PyObject* self, PyObject* args) + orig_name: unicode + callable: object + / + +Registers a converter with pysqlite. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(module_register_converter__doc__, +"register_converter(orig_name, callable)\n" +"Registers a converter with pysqlite. Non-standard."); + +#define MODULE_REGISTER_CONVERTER_METHODDEF \ + {"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, module_register_converter__doc__}, + +static PyObject * +module_register_converter_impl(PyModuleDef *module, PyObject *orig_name, PyObject *callable); + +static PyObject * +module_register_converter(PyModuleDef *module, PyObject *args) { - PyObject* orig_name; + PyObject *return_value = NULL; + PyObject *orig_name; + PyObject *callable; + + if (!PyArg_ParseTuple(args, + "UO:register_converter", + &orig_name, &callable)) + goto exit; + return_value = module_register_converter_impl(module, orig_name, callable); + +exit: + return return_value; +} + +static PyObject * +module_register_converter_impl(PyModuleDef *module, PyObject *orig_name, PyObject *callable) +/*[clinic end generated code: checksum=2f6719133cbbfa6746e90d4e697bad7e15c26776]*/ +{ PyObject* name = NULL; - PyObject* callable; PyObject* retval = NULL; _Py_IDENTIFIER(upper); - if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) { - return NULL; - } - /* convert the name to upper case */ name = _PyObject_CallMethodId(orig_name, &PyId_upper, ""); if (!name) { @@ -210,26 +325,98 @@ error: return retval; } -PyDoc_STRVAR(module_register_converter_doc, -"register_converter(typename, callable)\n\ -\n\ -Registers a converter with pysqlite. Non-standard."); -static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args) +/*[clinic input] +sqlite3.adapt as module_adapt + + obj: object + proto: object(c_default="(PyObject*)&pysqlite_PrepareProtocolType") = None + alt: object = NULL + / + +Adapt obj to given protocol. Non-standard. +[clinic start generated code]*/ + +PyDoc_STRVAR(module_adapt__doc__, +"adapt(obj, proto=None, alt=None)\n" +"Adapt obj to given protocol. Non-standard."); + +#define MODULE_ADAPT_METHODDEF \ + {"adapt", (PyCFunction)module_adapt, METH_VARARGS, module_adapt__doc__}, + +static PyObject * +module_adapt_impl(PyModuleDef *module, PyObject *obj, PyObject *proto, PyObject *alt); + +static PyObject * +module_adapt(PyModuleDef *module, PyObject *args) { - if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) { - return NULL; - } + PyObject *return_value = NULL; + PyObject *obj; + PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType; + PyObject *alt = NULL; + + if (!PyArg_ParseTuple(args, + "O|OO:adapt", + &obj, &proto, &alt)) + goto exit; + return_value = module_adapt_impl(module, obj, proto, alt); + +exit: + return return_value; +} + +static PyObject * +module_adapt_impl(PyModuleDef *module, PyObject *obj, PyObject *proto, PyObject *alt) +/*[clinic end generated code: checksum=01d4c2202d36d66a4eed8b9c554b3d2ecd77fa1c]*/ +{ + return pysqlite_microprotocols_adapt(obj, proto, alt); +} + +/*[clinic input] +sqlite3.enable_callback_tracebacks as module_enable_callback_tracebacks + + flag: int + / + +Enable or disable callback functions throwing errors to stderr. +[clinic start generated code]*/ + +PyDoc_STRVAR(module_enable_callback_tracebacks__doc__, +"enable_callback_tracebacks(flag)\n" +"Enable or disable callback functions throwing errors to stderr."); + +#define MODULE_ENABLE_CALLBACK_TRACEBACKS_METHODDEF \ + {"enable_callback_tracebacks", (PyCFunction)module_enable_callback_tracebacks, METH_VARARGS, module_enable_callback_tracebacks__doc__}, + +static PyObject * +module_enable_callback_tracebacks_impl(PyModuleDef *module, int flag); + +static PyObject * +module_enable_callback_tracebacks(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int flag; + + if (!PyArg_ParseTuple(args, + "i:enable_callback_tracebacks", + &flag)) + goto exit; + return_value = module_enable_callback_tracebacks_impl(module, flag); + +exit: + return return_value; +} + +static PyObject * +module_enable_callback_tracebacks_impl(PyModuleDef *module, int flag) +/*[clinic end generated code: checksum=d89e2506c9c78b09870271f577030a4b11b85d1f]*/ +{ + _enable_callback_tracebacks = flag; Py_INCREF(Py_None); return Py_None; } -PyDoc_STRVAR(enable_callback_tracebacks_doc, -"enable_callback_tracebacks(flag)\n\ -\n\ -Enable or disable callback functions throwing errors to stderr."); - static void converters_init(PyObject* dict) { converters = PyDict_New(); @@ -243,20 +430,14 @@ static void converters_init(PyObject* di static PyMethodDef module_methods[] = { {"connect", (PyCFunction)module_connect, METH_VARARGS | METH_KEYWORDS, module_connect_doc}, - {"complete_statement", (PyCFunction)module_complete, - METH_VARARGS | METH_KEYWORDS, module_complete_doc}, + MODULE_COMPLETE_METHODDEF #ifdef HAVE_SHARED_CACHE - {"enable_shared_cache", (PyCFunction)module_enable_shared_cache, - METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc}, + MODULE_ENABLE_SHARED_CACHE_METHODDEF #endif - {"register_adapter", (PyCFunction)module_register_adapter, - METH_VARARGS, module_register_adapter_doc}, - {"register_converter", (PyCFunction)module_register_converter, - METH_VARARGS, module_register_converter_doc}, - {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS, - pysqlite_adapt_doc}, - {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, - METH_VARARGS, enable_callback_tracebacks_doc}, + MODULE_REGISTER_ADAPTER_METHODDEF + MODULE_REGISTER_CONVERTER_METHODDEF + MODULE_ADAPT_METHODDEF + MODULE_ENABLE_CALLBACK_TRACEBACKS_METHODDEF {NULL, NULL} };