summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/cextension/utils.c
blob: 5928c41030b43e81ec95c5fb4b5f333496150cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
utils.c
Copyright (C) 2012-2013 the SQLAlchemy authors and contributors <see AUTHORS file>

This module is part of SQLAlchemy and is released under
the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

#include <Python.h>

/*
    Given arguments from the calling form *multiparams, **params,
    return a list of bind parameter structures, usually a list of
    dictionaries.

    In the case of 'raw' execution which accepts positional parameters,
    it may be a list of tuples or lists.

 */
static PyObject *
distill_params(PyObject *self, PyObject *args)
{
	PyObject *multiparams, *params;
	PyObject *enclosing_list, *double_enclosing_list;
	PyObject *zero_element, *zero_element_item;
	Py_ssize_t multiparam_size, zero_element_length;

	if (!PyArg_UnpackTuple(args, "_distill_params", 2, 2, &multiparams, &params)) {
		return NULL;
	}

	if (multiparams != Py_None) {
		multiparam_size = PyTuple_Size(multiparams);
		if (multiparam_size < 0) {
			return NULL;
		}
	}
	else {
		multiparam_size = 0;
	}

	if (multiparam_size == 0) {
		if (params != Py_None && PyDict_Size(params) != 0) {
			enclosing_list = PyList_New(1);
			if (enclosing_list == NULL) {
				return NULL;
			}
			Py_INCREF(params);
			if (PyList_SetItem(enclosing_list, 0, params) == -1) {
				Py_DECREF(params);
				Py_DECREF(enclosing_list);
				return NULL;
			}
		}
		else {
			enclosing_list = PyList_New(0);
			if (enclosing_list == NULL) {
				return NULL;
			}
		}
		return enclosing_list;
	}
	else if (multiparam_size == 1) {
		zero_element = PyTuple_GetItem(multiparams, 0);
		if (PyTuple_Check(zero_element) || PyList_Check(zero_element)) {
			zero_element_length = PySequence_Length(zero_element);

			if (zero_element_length != 0) {
				zero_element_item = PySequence_GetItem(zero_element, 0);
				if (zero_element_item == NULL) {
					return NULL;
				}
			}
			else {
				zero_element_item = NULL;
			}

			if (zero_element_length == 0 ||
					(
						PyObject_HasAttrString(zero_element_item, "__iter__") &&
						!PyObject_HasAttrString(zero_element_item, "strip")
					)
				) {
				/*
				 * execute(stmt, [{}, {}, {}, ...])
        		 * execute(stmt, [(), (), (), ...])
				 */
				Py_XDECREF(zero_element_item);
				Py_INCREF(zero_element);
				return zero_element;
			}
			else {
				/*
				 * execute(stmt, ("value", "value"))
				 */
				Py_XDECREF(zero_element_item);
				enclosing_list = PyList_New(1);
				if (enclosing_list == NULL) {
					return NULL;
				}
				Py_INCREF(zero_element);
				if (PyList_SetItem(enclosing_list, 0, zero_element) == -1) {
					Py_DECREF(zero_element);
					Py_DECREF(enclosing_list);
					return NULL;
				}
				return enclosing_list;
			}
		}
		else if (PyObject_HasAttrString(zero_element, "keys")) {
			/*
			 * execute(stmt, {"key":"value"})
			 */
			enclosing_list = PyList_New(1);
			if (enclosing_list ==  NULL) {
				return NULL;
			}
			Py_INCREF(zero_element);
			if (PyList_SetItem(enclosing_list, 0, zero_element) == -1) {
				Py_DECREF(zero_element);
				Py_DECREF(enclosing_list);
				return NULL;
			}
			return enclosing_list;
		} else {
			enclosing_list = PyList_New(1);
			if (enclosing_list ==  NULL) {
				return NULL;
			}
			double_enclosing_list = PyList_New(1);
			if (double_enclosing_list == NULL) {
				Py_DECREF(enclosing_list);
				return NULL;
			}
			Py_INCREF(zero_element);
			if (PyList_SetItem(enclosing_list, 0, zero_element) == -1) {
				Py_DECREF(zero_element);
				Py_DECREF(enclosing_list);
				Py_DECREF(double_enclosing_list);
				return NULL;
			}
			if (PyList_SetItem(double_enclosing_list, 0, enclosing_list) == -1) {
				Py_DECREF(zero_element);
				Py_DECREF(enclosing_list);
				Py_DECREF(double_enclosing_list);
				return NULL;
			}
			return double_enclosing_list;
		}
	}
	else {
		zero_element = PyTuple_GetItem(multiparams, 0);
		if (PyObject_HasAttrString(zero_element, "__iter__") &&
				!PyObject_HasAttrString(zero_element, "strip")
			) {
			Py_INCREF(multiparams);
			return multiparams;
		}
		else {
			enclosing_list = PyList_New(1);
			if (enclosing_list ==  NULL) {
				return NULL;
			}
			Py_INCREF(multiparams);
			if (PyList_SetItem(enclosing_list, 0, multiparams) == -1) {
				Py_DECREF(multiparams);
				Py_DECREF(enclosing_list);
				return NULL;
			}
			return enclosing_list;
		}
	}
}

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif


static PyMethodDef module_methods[] = {
    {"_distill_params", distill_params, METH_VARARGS,
     "Distill an execute() parameter structure."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initcutils(void)
{
    PyObject *m;

    m = Py_InitModule3("cutils", module_methods,
                       "Internal utility functions.");
    if (m == NULL)
        return;

}