~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/cextension/utils.c

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
utils.c
 
3
Copyright (C) 2012-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
 
4
 
 
5
This module is part of SQLAlchemy and is released under
 
6
the MIT License: http://www.opensource.org/licenses/mit-license.php
 
7
*/
 
8
 
 
9
#include <Python.h>
 
10
 
 
11
/*
 
12
    Given arguments from the calling form *multiparams, **params,
 
13
    return a list of bind parameter structures, usually a list of
 
14
    dictionaries.
 
15
 
 
16
    In the case of 'raw' execution which accepts positional parameters,
 
17
    it may be a list of tuples or lists.
 
18
 
 
19
 */
 
20
static PyObject *
 
21
distill_params(PyObject *self, PyObject *args)
 
22
{
 
23
        PyObject *multiparams, *params;
 
24
        PyObject *enclosing_list, *double_enclosing_list;
 
25
        PyObject *zero_element, *zero_element_item;
 
26
        Py_ssize_t multiparam_size, zero_element_length;
 
27
 
 
28
        if (!PyArg_UnpackTuple(args, "_distill_params", 2, 2, &multiparams, &params)) {
 
29
                return NULL;
 
30
        }
 
31
 
 
32
        if (multiparams != Py_None) {
 
33
                multiparam_size = PyTuple_Size(multiparams);
 
34
                if (multiparam_size < 0) {
 
35
                        return NULL;
 
36
                }
 
37
        }
 
38
        else {
 
39
                multiparam_size = 0;
 
40
        }
 
41
 
 
42
        if (multiparam_size == 0) {
 
43
                if (params != Py_None && PyDict_Size(params) != 0) {
 
44
                        enclosing_list = PyList_New(1);
 
45
                        if (enclosing_list == NULL) {
 
46
                                return NULL;
 
47
                        }
 
48
                        Py_INCREF(params);
 
49
                        if (PyList_SetItem(enclosing_list, 0, params) == -1) {
 
50
                                Py_DECREF(params);
 
51
                                Py_DECREF(enclosing_list);
 
52
                                return NULL;
 
53
                        }
 
54
                }
 
55
                else {
 
56
                        enclosing_list = PyList_New(0);
 
57
                        if (enclosing_list == NULL) {
 
58
                                return NULL;
 
59
                        }
 
60
                }
 
61
                return enclosing_list;
 
62
        }
 
63
        else if (multiparam_size == 1) {
 
64
                zero_element = PyTuple_GetItem(multiparams, 0);
 
65
                if (PyTuple_Check(zero_element) || PyList_Check(zero_element)) {
 
66
                        zero_element_length = PySequence_Length(zero_element);
 
67
 
 
68
                        if (zero_element_length != 0) {
 
69
                                zero_element_item = PySequence_GetItem(zero_element, 0);
 
70
                                if (zero_element_item == NULL) {
 
71
                                        return NULL;
 
72
                                }
 
73
                        }
 
74
                        else {
 
75
                                zero_element_item = NULL;
 
76
                        }
 
77
 
 
78
                        if (zero_element_length == 0 ||
 
79
                                        (
 
80
                                                PyObject_HasAttrString(zero_element_item, "__iter__") &&
 
81
                                                !PyObject_HasAttrString(zero_element_item, "strip")
 
82
                                        )
 
83
                                ) {
 
84
                                /*
 
85
                                 * execute(stmt, [{}, {}, {}, ...])
 
86
                         * execute(stmt, [(), (), (), ...])
 
87
                                 */
 
88
                                Py_XDECREF(zero_element_item);
 
89
                                Py_INCREF(zero_element);
 
90
                                return zero_element;
 
91
                        }
 
92
                        else {
 
93
                                /*
 
94
                                 * execute(stmt, ("value", "value"))
 
95
                                 */
 
96
                                Py_XDECREF(zero_element_item);
 
97
                                enclosing_list = PyList_New(1);
 
98
                                if (enclosing_list == NULL) {
 
99
                                        return NULL;
 
100
                                }
 
101
                                Py_INCREF(zero_element);
 
102
                                if (PyList_SetItem(enclosing_list, 0, zero_element) == -1) {
 
103
                                        Py_DECREF(zero_element);
 
104
                                        Py_DECREF(enclosing_list);
 
105
                                        return NULL;
 
106
                                }
 
107
                                return enclosing_list;
 
108
                        }
 
109
                }
 
110
                else if (PyObject_HasAttrString(zero_element, "keys")) {
 
111
                        /*
 
112
                         * execute(stmt, {"key":"value"})
 
113
                         */
 
114
                        enclosing_list = PyList_New(1);
 
115
                        if (enclosing_list ==  NULL) {
 
116
                                return NULL;
 
117
                        }
 
118
                        Py_INCREF(zero_element);
 
119
                        if (PyList_SetItem(enclosing_list, 0, zero_element) == -1) {
 
120
                                Py_DECREF(zero_element);
 
121
                                Py_DECREF(enclosing_list);
 
122
                                return NULL;
 
123
                        }
 
124
                        return enclosing_list;
 
125
                } else {
 
126
                        enclosing_list = PyList_New(1);
 
127
                        if (enclosing_list ==  NULL) {
 
128
                                return NULL;
 
129
                        }
 
130
                        double_enclosing_list = PyList_New(1);
 
131
                        if (double_enclosing_list == NULL) {
 
132
                                Py_DECREF(enclosing_list);
 
133
                                return NULL;
 
134
                        }
 
135
                        Py_INCREF(zero_element);
 
136
                        if (PyList_SetItem(enclosing_list, 0, zero_element) == -1) {
 
137
                                Py_DECREF(zero_element);
 
138
                                Py_DECREF(enclosing_list);
 
139
                                Py_DECREF(double_enclosing_list);
 
140
                                return NULL;
 
141
                        }
 
142
                        if (PyList_SetItem(double_enclosing_list, 0, enclosing_list) == -1) {
 
143
                                Py_DECREF(zero_element);
 
144
                                Py_DECREF(enclosing_list);
 
145
                                Py_DECREF(double_enclosing_list);
 
146
                                return NULL;
 
147
                        }
 
148
                        return double_enclosing_list;
 
149
                }
 
150
        }
 
151
        else {
 
152
                zero_element = PyTuple_GetItem(multiparams, 0);
 
153
                if (PyObject_HasAttrString(zero_element, "__iter__") &&
 
154
                                !PyObject_HasAttrString(zero_element, "strip")
 
155
                        ) {
 
156
                        Py_INCREF(multiparams);
 
157
                        return multiparams;
 
158
                }
 
159
                else {
 
160
                        enclosing_list = PyList_New(1);
 
161
                        if (enclosing_list ==  NULL) {
 
162
                                return NULL;
 
163
                        }
 
164
                        Py_INCREF(multiparams);
 
165
                        if (PyList_SetItem(enclosing_list, 0, multiparams) == -1) {
 
166
                                Py_DECREF(multiparams);
 
167
                                Py_DECREF(enclosing_list);
 
168
                                return NULL;
 
169
                        }
 
170
                        return enclosing_list;
 
171
                }
 
172
        }
 
173
}
 
174
 
 
175
#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
 
176
#define PyMODINIT_FUNC void
 
177
#endif
 
178
 
 
179
 
 
180
static PyMethodDef module_methods[] = {
 
181
    {"_distill_params", distill_params, METH_VARARGS,
 
182
     "Distill an execute() parameter structure."},
 
183
    {NULL, NULL, 0, NULL}        /* Sentinel */
 
184
};
 
185
 
 
186
PyMODINIT_FUNC
 
187
initcutils(void)
 
188
{
 
189
    PyObject *m;
 
190
 
 
191
    m = Py_InitModule3("cutils", module_methods,
 
192
                       "Internal utility functions.");
 
193
    if (m == NULL)
 
194
        return;
 
195
 
 
196
}
 
197