gh-91602: Add iterdump() support for filtering database objects (#114501)

Add optional 'filter' parameter to iterdump() that allows a "LIKE"
pattern for filtering database objects to dump.

Co-authored-by: Erlend E. Aasland <erlend@python.org>
This commit is contained in:
Mariusz Felisiak 2024-02-06 12:34:56 +01:00 committed by GitHub
parent 4bf41879d0
commit 1a10437a14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 176 additions and 17 deletions

View file

@ -1204,21 +1204,67 @@ pysqlite_connection_interrupt(pysqlite_Connection *self, PyObject *Py_UNUSED(ign
}
PyDoc_STRVAR(pysqlite_connection_iterdump__doc__,
"iterdump($self, /)\n"
"iterdump($self, /, *, filter=None)\n"
"--\n"
"\n"
"Returns iterator to the dump of the database in an SQL text format.");
"Returns iterator to the dump of the database in an SQL text format.\n"
"\n"
" filter\n"
" An optional LIKE pattern for database objects to dump");
#define PYSQLITE_CONNECTION_ITERDUMP_METHODDEF \
{"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS, pysqlite_connection_iterdump__doc__},
{"iterdump", _PyCFunction_CAST(pysqlite_connection_iterdump), METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_iterdump__doc__},
static PyObject *
pysqlite_connection_iterdump_impl(pysqlite_Connection *self);
pysqlite_connection_iterdump_impl(pysqlite_Connection *self,
PyObject *filter);
static PyObject *
pysqlite_connection_iterdump(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored))
pysqlite_connection_iterdump(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
return pysqlite_connection_iterdump_impl(self);
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 1
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(filter), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"filter", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "iterdump",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
PyObject *filter = Py_None;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
if (!args) {
goto exit;
}
if (!noptargs) {
goto skip_optional_kwonly;
}
filter = args[0];
skip_optional_kwonly:
return_value = pysqlite_connection_iterdump_impl(self, filter);
exit:
return return_value;
}
PyDoc_STRVAR(pysqlite_connection_backup__doc__,
@ -1820,4 +1866,4 @@ exit:
#ifndef DESERIALIZE_METHODDEF
#define DESERIALIZE_METHODDEF
#endif /* !defined(DESERIALIZE_METHODDEF) */
/*[clinic end generated code: output=99299d3ee2c247ab input=a9049054013a1b77]*/
/*[clinic end generated code: output=3c6d0b748fac016f input=a9049054013a1b77]*/

View file

@ -1979,12 +1979,17 @@ finally:
/*[clinic input]
_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
*
filter: object = None
An optional LIKE pattern for database objects to dump
Returns iterator to the dump of the database in an SQL text format.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=586997aaf9808768 input=1911ca756066da89]*/
pysqlite_connection_iterdump_impl(pysqlite_Connection *self,
PyObject *filter)
/*[clinic end generated code: output=fd81069c4bdeb6b0 input=4ae6d9a898f108df]*/
{
if (!pysqlite_check_connection(self)) {
return NULL;
@ -1998,9 +2003,16 @@ pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
}
return NULL;
}
PyObject *retval = PyObject_CallOneArg(iterdump, (PyObject *)self);
PyObject *args[3] = {NULL, (PyObject *)self, filter};
PyObject *kwnames = Py_BuildValue("(s)", "filter");
if (!kwnames) {
Py_DECREF(iterdump);
return NULL;
}
Py_ssize_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
PyObject *retval = PyObject_Vectorcall(iterdump, args + 1, nargsf, kwnames);
Py_DECREF(iterdump);
Py_DECREF(kwnames);
return retval;
}