Пример #1
0
static PyObject *
BaseRowProxy_iter(BaseRowProxy *self)
{
    PyObject *values, *result;

    values = BaseRowProxy_processvalues(self->row, self->processors, 1);
    if (values == NULL)
        return NULL;

    result = PyObject_GetIter(values);
    Py_DECREF(values);
    if (result == NULL)
        return NULL;

    return result;
}
Пример #2
0
static PyObject *
BaseRowProxy_subscript(BaseRowProxy *self, PyObject *key)
{
    PyObject *processors, *values;
    PyObject *processor, *value, *processed_value;
    PyObject *row, *record, *result, *indexobject;
    PyObject *exc_module, *exception;
    char *cstr_key;
    long index;
    int key_fallback = 0;
    int tuple_check = 0;
    
    if (PyInt_CheckExact(key)) {
        index = PyInt_AS_LONG(key);
    } else if (PyLong_CheckExact(key)) {
        index = PyLong_AsLong(key);
        if ((index == -1) && PyErr_Occurred())
            /* -1 can be either the actual value, or an error flag. */
            return NULL;
    } else if (PySlice_Check(key)) {
        values = PyObject_GetItem(self->row, key);
        if (values == NULL)
            return NULL;

        processors = PyObject_GetItem(self->processors, key);
        if (processors == NULL) {
            Py_DECREF(values);
            return NULL;
        }

        result = BaseRowProxy_processvalues(values, processors, 1);
        Py_DECREF(values);
        Py_DECREF(processors);
        return result;
    } else {
        record = PyDict_GetItem((PyObject *)self->keymap, key);
        if (record == NULL) {
            record = PyObject_CallMethod(self->parent, "_key_fallback",
                                         "O", key);
            if (record == NULL)
                return NULL;
            key_fallback = 1;
        }

        indexobject = PyTuple_GetItem(record, 2);
        if (indexobject == NULL)
            return NULL;

        if (key_fallback) {
            Py_DECREF(record);
        }

        if (indexobject == Py_None) {
            exc_module = PyImport_ImportModule("sqlalchemy.exc");
            if (exc_module == NULL)
                return NULL;

            exception = PyObject_GetAttrString(exc_module,
                                               "InvalidRequestError");
            Py_DECREF(exc_module);
            if (exception == NULL)
                return NULL;

            cstr_key = PyString_AsString(key);
            if (cstr_key == NULL)
                return NULL;

            PyErr_Format(exception,
                    "Ambiguous column name '%.200s' in result set! "
                    "try 'use_labels' option on select statement.", cstr_key);
            return NULL;
        }

        index = PyInt_AsLong(indexobject);
        if ((index == -1) && PyErr_Occurred())
            /* -1 can be either the actual value, or an error flag. */
            return NULL;
    }
    processor = PyList_GetItem(self->processors, index);
    if (processor == NULL)
        return NULL;

    row = self->row;
    if (PyTuple_CheckExact(row)) {
        value = PyTuple_GetItem(row, index);
        tuple_check = 1;
    }
    else {
        value = PySequence_GetItem(row, index);
        tuple_check = 0;
    }
        
    if (value == NULL)
        return NULL;

    if (processor != Py_None) {
        processed_value = PyObject_CallFunctionObjArgs(processor, value, NULL);
        if (!tuple_check) {
            Py_DECREF(value);
        }
        return processed_value;
    } else {
        if (tuple_check) {
            Py_INCREF(value);
        }
        return value;
    }
}
Пример #3
0
static PyListObject *
BaseRowProxy_values(BaseRowProxy *self)
{
    return (PyListObject *)BaseRowProxy_processvalues(self->row,
                                                      self->processors, 0);
}
static PyObject *
BaseRowProxy_subscript(BaseRowProxy *self, PyObject *key)
{
    PyObject *processors, *values;
    PyObject *processor, *value, *processed_value;
    PyObject *row, *record, *result, *indexobject;
    PyObject *exc_module, *exception, *cstr_obj;
#if PY_MAJOR_VERSION >= 3
    PyObject *bytes;
#endif
    char *cstr_key;
    long index;
    int key_fallback = 0;
    int tuple_check = 0;

#if PY_MAJOR_VERSION < 3
    if (PyInt_CheckExact(key)) {
        index = PyInt_AS_LONG(key);
    }
#endif

    if (PyLong_CheckExact(key)) {
        index = PyLong_AsLong(key);
        if ((index == -1) && PyErr_Occurred())
            /* -1 can be either the actual value, or an error flag. */
            return NULL;
    } else if (PySlice_Check(key)) {
        values = PyObject_GetItem(self->row, key);
        if (values == NULL)
            return NULL;

        processors = PyObject_GetItem(self->processors, key);
        if (processors == NULL) {
            Py_DECREF(values);
            return NULL;
        }

        result = BaseRowProxy_processvalues(values, processors, 1);
        Py_DECREF(values);
        Py_DECREF(processors);
        return result;
    } else {
        record = PyDict_GetItem((PyObject *)self->keymap, key);
        if (record == NULL) {
            record = PyObject_CallMethod(self->parent, "_key_fallback",
                                         "O", key);
            if (record == NULL)
                return NULL;
            key_fallback = 1;
        }

        indexobject = PyTuple_GetItem(record, 2);
        if (indexobject == NULL)
            return NULL;

        if (key_fallback) {
            Py_DECREF(record);
        }

        if (indexobject == Py_None) {
            exc_module = PyImport_ImportModule("sqlalchemy.exc");
            if (exc_module == NULL)
                return NULL;

            exception = PyObject_GetAttrString(exc_module,
                                               "InvalidRequestError");
            Py_DECREF(exc_module);
            if (exception == NULL)
                return NULL;

            // wow.  this seems quite excessive.
            cstr_obj = PyObject_Str(key);
            if (cstr_obj == NULL)
                return NULL;

/*
           FIXME: raise encoding error exception (in both versions below)
           if the key contains non-ascii chars, instead of an
           InvalidRequestError without any message like in the
           python version.
*/
#if PY_MAJOR_VERSION >= 3
            bytes = PyUnicode_AsASCIIString(cstr_obj);
            if (bytes == NULL)
                return NULL;
            cstr_key = PyBytes_AS_STRING(bytes);
#else
            cstr_key = PyString_AsString(cstr_obj);
#endif
            if (cstr_key == NULL) {
                Py_DECREF(cstr_obj);
                return NULL;
            }
            Py_DECREF(cstr_obj);

            PyErr_Format(exception,
                    "Ambiguous column name '%.200s' in result set! "
                    "try 'use_labels' option on select statement.", cstr_key);
            return NULL;
        }

#if PY_MAJOR_VERSION >= 3
        index = PyLong_AsLong(indexobject);
#else
        index = PyInt_AsLong(indexobject);
#endif
        if ((index == -1) && PyErr_Occurred())
            /* -1 can be either the actual value, or an error flag. */
            return NULL;
    }
    processor = PyList_GetItem(self->processors, index);
    if (processor == NULL)
        return NULL;

    row = self->row;
    if (PyTuple_CheckExact(row)) {
        value = PyTuple_GetItem(row, index);
        tuple_check = 1;
    }
    else {
        value = PySequence_GetItem(row, index);
        tuple_check = 0;
    }

    if (value == NULL)
        return NULL;

    if (processor != Py_None) {
        processed_value = PyObject_CallFunctionObjArgs(processor, value, NULL);
        if (!tuple_check) {
            Py_DECREF(value);
        }
        return processed_value;
    } else {
        if (tuple_check) {
            Py_INCREF(value);
        }
        return value;
    }
}