3
Copyright (C) 2012-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
5
This module is part of SQLAlchemy and is released under
6
the MIT License: http://www.opensource.org/licenses/mit-license.php
12
Given arguments from the calling form *multiparams, **params,
13
return a list of bind parameter structures, usually a list of
16
In the case of 'raw' execution which accepts positional parameters,
17
it may be a list of tuples or lists.
21
distill_params(PyObject *self, PyObject *args)
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;
28
if (!PyArg_UnpackTuple(args, "_distill_params", 2, 2, &multiparams, ¶ms)) {
32
if (multiparams != Py_None) {
33
multiparam_size = PyTuple_Size(multiparams);
34
if (multiparam_size < 0) {
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) {
49
if (PyList_SetItem(enclosing_list, 0, params) == -1) {
51
Py_DECREF(enclosing_list);
56
enclosing_list = PyList_New(0);
57
if (enclosing_list == NULL) {
61
return enclosing_list;
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);
68
if (zero_element_length != 0) {
69
zero_element_item = PySequence_GetItem(zero_element, 0);
70
if (zero_element_item == NULL) {
75
zero_element_item = NULL;
78
if (zero_element_length == 0 ||
80
PyObject_HasAttrString(zero_element_item, "__iter__") &&
81
!PyObject_HasAttrString(zero_element_item, "strip")
85
* execute(stmt, [{}, {}, {}, ...])
86
* execute(stmt, [(), (), (), ...])
88
Py_XDECREF(zero_element_item);
89
Py_INCREF(zero_element);
94
* execute(stmt, ("value", "value"))
96
Py_XDECREF(zero_element_item);
97
enclosing_list = PyList_New(1);
98
if (enclosing_list == NULL) {
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);
107
return enclosing_list;
110
else if (PyObject_HasAttrString(zero_element, "keys")) {
112
* execute(stmt, {"key":"value"})
114
enclosing_list = PyList_New(1);
115
if (enclosing_list == NULL) {
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);
124
return enclosing_list;
126
enclosing_list = PyList_New(1);
127
if (enclosing_list == NULL) {
130
double_enclosing_list = PyList_New(1);
131
if (double_enclosing_list == NULL) {
132
Py_DECREF(enclosing_list);
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);
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);
148
return double_enclosing_list;
152
zero_element = PyTuple_GetItem(multiparams, 0);
153
if (PyObject_HasAttrString(zero_element, "__iter__") &&
154
!PyObject_HasAttrString(zero_element, "strip")
156
Py_INCREF(multiparams);
160
enclosing_list = PyList_New(1);
161
if (enclosing_list == NULL) {
164
Py_INCREF(multiparams);
165
if (PyList_SetItem(enclosing_list, 0, multiparams) == -1) {
166
Py_DECREF(multiparams);
167
Py_DECREF(enclosing_list);
170
return enclosing_list;
175
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
176
#define PyMODINIT_FUNC void
180
static PyMethodDef module_methods[] = {
181
{"_distill_params", distill_params, METH_VARARGS,
182
"Distill an execute() parameter structure."},
183
{NULL, NULL, 0, NULL} /* Sentinel */
191
m = Py_InitModule3("cutils", module_methods,
192
"Internal utility functions.");