summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/cextension
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-04-21 12:51:13 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-05-01 16:09:24 -0400
commitaded39f68c29e44a50c85be1ddb370d3d1affe9d (patch)
tree0855ecfe2ecf5092f1e350c33f460571f495f1b8 /lib/sqlalchemy/cextension
parent18ce4f9937c2d6753acbb054b4990c7da298a5d7 (diff)
downloadsqlalchemy-aded39f68c29e44a50c85be1ddb370d3d1affe9d.tar.gz
Propose Result as immediate replacement for ResultProxy
As progress is made on the _future.Result, including breaking it out such that DBAPI behaviors are local to specific implementations, it becomes apparent that the Result object is a functional superset of ResultProxy and that basic operations like fetchone(), fetchall(), and fetchmany() behave pretty much exactly the same way on the new object. Reorganize things so that ResultProxy is now referred to as LegacyCursorResult, which subclasses CursorResult that represents the DBAPI-cursor version of Result, making use of a multiple inheritance pattern so that the functionality of Result is also available in non-DBAPI contexts, as will be necessary for some ORM patterns. Additionally propose the composition system for Result that will form the basis for ORM-alternative result systems such as horizontal sharding and dogpile cache. As ORM results will soon be coming directly from instances of Result, these extensions will instead build their own ResultFetchStrategies that perform the special steps to create composed or cached result sets. Also considering at the moment not emitting deprecation warnings for fetchXYZ() methods; the immediate issue is Keystone tests are calling upon it, but as the implementations here are proving to be not in any kind of conflict with how Result works, there's not too much issue leaving them around and deprecating at some later point. References: #5087 References: #4395 Fixes: #4959 Change-Id: I8091919d45421e3f53029b8660427f844fee0228
Diffstat (limited to 'lib/sqlalchemy/cextension')
-rw-r--r--lib/sqlalchemy/cextension/resultproxy.c84
1 files changed, 64 insertions, 20 deletions
diff --git a/lib/sqlalchemy/cextension/resultproxy.c b/lib/sqlalchemy/cextension/resultproxy.c
index b105038bc..d5a6ea0c8 100644
--- a/lib/sqlalchemy/cextension/resultproxy.c
+++ b/lib/sqlalchemy/cextension/resultproxy.c
@@ -47,6 +47,10 @@ typedef struct {
PyObject *keymap;
} BaseRow;
+
+static PyObject *sqlalchemy_engine_row = NULL;
+static PyObject *sqlalchemy_engine_result = NULL;
+
/****************
* BaseRow *
****************/
@@ -103,7 +107,7 @@ BaseRow_init(BaseRow *self, PyObject *args, PyObject *kwds)
return -1;
num_values = PySequence_Length(values_fastseq);
- num_processors = PyList_Size(processors);
+ num_processors = PySequence_Size(processors);
if (num_values != num_processors) {
PyErr_Format(PyExc_RuntimeError,
"number of values in row (%d) differ from number of column "
@@ -172,12 +176,14 @@ BaseRow_reduce(PyObject *self)
if (state == NULL)
return NULL;
- module = PyImport_ImportModule("sqlalchemy.engine.result");
- if (module == NULL)
- return NULL;
+ if (sqlalchemy_engine_row == NULL) {
+ module = PyImport_ImportModule("sqlalchemy.engine.row");
+ if (module == NULL)
+ return NULL;
+ sqlalchemy_engine_row = module;
+ }
- reconstructor = PyObject_GetAttrString(module, "rowproxy_reconstructor");
- Py_DECREF(module);
+ reconstructor = PyObject_GetAttrString(sqlalchemy_engine_row, "rowproxy_reconstructor");
if (reconstructor == NULL) {
Py_DECREF(state);
return NULL;
@@ -193,6 +199,33 @@ BaseRow_reduce(PyObject *self)
return Py_BuildValue("(N(NN))", reconstructor, cls, state);
}
+static PyObject *
+BaseRow_filter_on_values(BaseRow *self, PyObject *filters)
+{
+ PyObject *module, *row_class, *new_obj;
+
+ if (sqlalchemy_engine_row == NULL) {
+ module = PyImport_ImportModule("sqlalchemy.engine.row");
+ if (module == NULL)
+ return NULL;
+ sqlalchemy_engine_row = module;
+ }
+
+ // TODO: do we want to get self.__class__ instead here? I'm not sure
+ // how to use METH_VARARGS and then also get the BaseRow struct
+ // at the same time
+ row_class = PyObject_GetAttrString(sqlalchemy_engine_row, "Row");
+
+ new_obj = PyObject_CallFunction(row_class, "OOOO", self->parent, filters, self->keymap, self->row);
+ Py_DECREF(row_class);
+ if (new_obj == NULL) {
+ return NULL;
+ }
+
+ return new_obj;
+
+}
+
static void
BaseRow_dealloc(BaseRow *self)
{
@@ -449,12 +482,14 @@ BaseRow_setparent(BaseRow *self, PyObject *value, void *closure)
return -1;
}
- module = PyImport_ImportModule("sqlalchemy.engine.result");
- if (module == NULL)
- return -1;
+ if (sqlalchemy_engine_result == NULL) {
+ module = PyImport_ImportModule("sqlalchemy.engine.result");
+ if (module == NULL)
+ return -1;
+ sqlalchemy_engine_result = module;
+ }
- cls = PyObject_GetAttrString(module, "ResultMetaData");
- Py_DECREF(module);
+ cls = PyObject_GetAttrString(sqlalchemy_engine_result, "ResultMetaData");
if (cls == NULL)
return -1;
@@ -557,6 +592,9 @@ static PyMethodDef BaseRow_methods[] = {
"implement mapping-like getitem as well as sequence getitem"},
{"_get_by_key_impl_mapping", (PyCFunction)BaseRow_subscript_mapping, METH_O,
"implement mapping-like getitem as well as sequence getitem"},
+ {"_filter_on_values", (PyCFunction)BaseRow_filter_on_values, METH_O,
+ "return a new Row with per-value filters applied to columns"},
+
{NULL} /* Sentinel */
};
@@ -681,14 +719,18 @@ tuplegetter_traverse(tuplegetterobject *tg, visitproc visit, void *arg)
static PyObject *
tuplegetter_call(tuplegetterobject *tg, PyObject *args, PyObject *kw)
{
- PyObject *row, *result;
+ PyObject *row_or_tuple, *result;
Py_ssize_t i, nitems=tg->nitems;
+ int has_row_method;
assert(PyTuple_CheckExact(args));
- // this is normally a BaseRow subclass but we are not doing
- // strict checking at the moment
- row = PyTuple_GET_ITEM(args, 0);
+ // this is a tuple, however if its a BaseRow subclass we want to
+ // call specific methods to bypass the pure python LegacyRow.__getitem__
+ // method for now
+ row_or_tuple = PyTuple_GET_ITEM(args, 0);
+
+ has_row_method = PyObject_HasAttrString(row_or_tuple, "_get_by_key_impl_mapping");
assert(PyTuple_Check(tg->item));
assert(PyTuple_GET_SIZE(tg->item) == nitems);
@@ -701,11 +743,13 @@ tuplegetter_call(tuplegetterobject *tg, PyObject *args, PyObject *kw)
PyObject *item, *val;
item = PyTuple_GET_ITEM(tg->item, i);
- val = PyObject_CallMethod(row, "_get_by_key_impl_mapping", "O", item);
+ if (has_row_method) {
+ val = PyObject_CallMethod(row_or_tuple, "_get_by_key_impl_mapping", "O", item);
+ }
+ else {
+ val = PyObject_GetItem(row_or_tuple, item);
+ }
- // generic itemgetter version; if BaseRow __getitem__ is implemented
- // in C directly then we can use that
- //val = PyObject_GetItem(row, item);
if (val == NULL) {
Py_DECREF(result);
return NULL;
@@ -756,7 +800,7 @@ and returns them as a tuple.\n");
static PyTypeObject tuplegetter_type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "sqlalchemy.engine.util..tuplegetter", /* tp_name */
+ "sqlalchemy.engine.util.tuplegetter", /* tp_name */
sizeof(tuplegetterobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */