-
Notifications
You must be signed in to change notification settings - Fork 2
/
php_midgard_query_executors.c
330 lines (246 loc) · 11.4 KB
/
php_midgard_query_executors.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Copyright (C) 2010 Alexey Zakhlestin <indeyets@gmail.com>
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "php_midgard.h"
#include "php_midgard_query.h"
#include "php_midgard_gobject.h"
#include "php_midgard__helpers.h"
#include <midgard/midgard_executable.h>
zend_class_entry *php_midgard_query_executor_class = NULL;
zend_class_entry *php_midgard_query_select_class;
// midgard_query_executor
static PHP_METHOD(midgard_query_executor, set_constraint)
{
zval *z_constraint = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_constraint, php_midgard_query_constraint_simple_class) == FAILURE) {
return;
}
MidgardQueryExecutor *executor = MIDGARD_QUERY_EXECUTOR(__php_gobject_ptr(getThis()));
MidgardQueryConstraintSimple *constraint = MIDGARD_QUERY_CONSTRAINT_SIMPLE(__php_gobject_ptr(z_constraint));
zend_bool result = midgard_query_executor_set_constraint(executor, constraint);
RETURN_BOOL(result);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_set_constraint, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, property, midgard_query_constraint_simple, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_executor, set_offset)
{
long offset = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &offset) == FAILURE) {
return;
}
if (offset < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "offset can't be negative");
return;
}
MidgardQueryExecutor *executor = MIDGARD_QUERY_EXECUTOR(__php_gobject_ptr(getThis()));
zend_bool result = midgard_query_executor_set_offset(executor, offset);
RETURN_BOOL(result);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_set_offset, 0, 0, 1)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_executor, set_limit)
{
long limit = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &limit) == FAILURE) {
return;
}
if (limit < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "limit can't be negative");
return;
}
MidgardQueryExecutor *executor = MIDGARD_QUERY_EXECUTOR(__php_gobject_ptr(getThis()));
zend_bool result = midgard_query_executor_set_limit(executor, limit);
RETURN_BOOL(result);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_set_limit, 0, 0, 1)
ZEND_ARG_INFO(0, limit)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_executor, add_order)
{
zval *z_property = NULL;
long direction = PHP_SORT_ASC;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|l", &z_property, php_midgard_query_holder_class, &direction) == FAILURE) {
return;
}
if (direction != PHP_SORT_ASC && direction != PHP_SORT_DESC) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "direction should be either SORT_ASC or SORT_DESC");
return;
}
MidgardQueryExecutor *executor = MIDGARD_QUERY_EXECUTOR(__php_gobject_ptr(getThis()));
MidgardQueryHolder *holder = MIDGARD_QUERY_HOLDER(__php_gobject_ptr(z_property));
const gchar *g_direction = (direction == PHP_SORT_ASC) ? "ASC" : "DESC";
zend_bool result = midgard_query_executor_add_order(executor, holder, g_direction);
RETURN_BOOL(result);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_add_order, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, property, midgard_query_holder, 0)
ZEND_ARG_INFO(0, direction)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_executor, add_join)
{
char *join_type = NULL;
int join_type_len = 0;
zval *z_property_left = NULL;
zval *z_property_right = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sOO",
&join_type, &join_type_len,
&z_property_left, php_midgard_query_holder_class,
&z_property_right, php_midgard_query_holder_class
) == FAILURE
) {
return;
}
MidgardQueryExecutor *executor = MIDGARD_QUERY_EXECUTOR(__php_gobject_ptr(getThis()));
MidgardQueryHolder *left_property = MIDGARD_QUERY_HOLDER(__php_gobject_ptr(z_property_left));
MidgardQueryHolder *right_property = MIDGARD_QUERY_HOLDER(__php_gobject_ptr(z_property_right));
zend_bool result = midgard_query_executor_add_join(executor, join_type, MIDGARD_QUERY_HOLDER(left_property), MIDGARD_QUERY_HOLDER(right_property));
RETURN_BOOL(result);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_add_join, 0, 0, 3)
ZEND_ARG_INFO(0, join_type)
ZEND_ARG_OBJ_INFO(0, left_property, midgard_query_holder, 0)
ZEND_ARG_OBJ_INFO(0, right_property, midgard_query_holder, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_executor, execute)
{
if (zend_parse_parameters_none() == FAILURE)
return;
MidgardExecutable *executor = MIDGARD_EXECUTABLE(__php_gobject_ptr(getThis()));
GError *error = NULL;
midgard_executable_execute(executor, &error);
if (error) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed to execute statement: %s", error->message);
g_error_free(error);
}
RETURN_TRUE;
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_execute, 0, 0, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_executor, get_results_count)
{
if (zend_parse_parameters_none() == FAILURE)
return;
MidgardQueryExecutor *executor = MIDGARD_QUERY_EXECUTOR(__php_gobject_ptr(getThis()));
guint count = midgard_query_executor_get_results_count(executor);
RETURN_LONG(count);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_executor_get_results_count, 0, 0, 0)
ZEND_END_ARG_INFO()
// midgard_query_select
static PHP_METHOD(midgard_query_select, __construct)
{
zval *z_storage = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_storage, php_midgard_query_storage_class) == FAILURE) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed to create Select executor");
return;
}
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
if (!mgd) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "midgard_query_select object requires active midgard_connection");
return;
}
MidgardQueryStorage *storage = MIDGARD_QUERY_STORAGE(__php_gobject_ptr(z_storage));
MidgardQuerySelect *executor = midgard_query_select_new(mgd, storage);
if (!executor) {
zend_throw_exception_ex(ce_midgard_error_exception, 0 TSRMLS_CC, "Failed to create Select executor");
return;
}
MGD_PHP_SET_GOBJECT(getThis(), executor);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_select___construct, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, storage, midgard_query_storage, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_select, toggle_readonly)
{
zend_bool readonly;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &readonly) == FAILURE) {
return;
}
MidgardQuerySelect *select = MIDGARD_QUERY_SELECT(__php_gobject_ptr(getThis()));
midgard_query_select_toggle_read_only(select, readonly);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_select_toggle_readonly, 0, 0, 1)
ZEND_ARG_INFO(0, read_only)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_select, include_deleted)
{
zend_bool include_deleted;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &include_deleted) == FAILURE) {
return;
}
MidgardQuerySelect *select = MIDGARD_QUERY_SELECT(__php_gobject_ptr(getThis()));
midgard_query_select_include_deleted(select, include_deleted);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_select_include_deleted, 0, 0, 1)
ZEND_ARG_INFO(0, include_deleted)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_select, list_objects)
{
if (zend_parse_parameters_none() == FAILURE)
return;
MidgardQuerySelect *select = MIDGARD_QUERY_SELECT(__php_gobject_ptr(getThis()));
guint n_objects;
MidgardDBObject **objects = midgard_query_select_list_objects(select, &n_objects);
array_init(return_value);
php_midgard_array_from_unknown_objects((GObject **)objects, n_objects, return_value TSRMLS_CC);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_select_list_objects, 0, 0, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_query_select, connect)
{
php_midgard_gobject_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_query_select_connect, 0, 0, 2)
ZEND_ARG_INFO(0, signal)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(0, user_data)
ZEND_END_ARG_INFO()
PHP_MINIT_FUNCTION(midgard2_query_executors)
{
static zend_function_entry midgard_query_executor_methods[] = {
PHP_ME(midgard_query_executor, set_constraint, arginfo_midgard_query_executor_set_constraint, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_executor, set_offset, arginfo_midgard_query_executor_set_offset, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_executor, set_limit, arginfo_midgard_query_executor_set_limit, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_executor, add_order, arginfo_midgard_query_executor_add_order, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_executor, add_join, arginfo_midgard_query_executor_add_join, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_executor, execute, arginfo_midgard_query_executor_execute, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_executor, get_results_count, arginfo_midgard_query_executor_get_results_count, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
static zend_class_entry php_midgard_query_executor_class_entry;
INIT_CLASS_ENTRY(php_midgard_query_executor_class_entry, "MidgardQueryExecutor", midgard_query_executor_methods);
php_midgard_query_executor_class = zend_register_internal_class(&php_midgard_query_executor_class_entry TSRMLS_CC);
php_midgard_query_executor_class->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
php_midgard_query_executor_class->create_object = php_midgard_gobject_new;
CLASS_SET_DOC_COMMENT(php_midgard_query_executor_class, strdup("Base, abstract class for queries executions"));
_FIXME_zend_register_class_alias("midgard_query_executor", php_midgard_query_executor_class);
static zend_function_entry midgard_query_select_methods[] = {
PHP_ME(midgard_query_select, __construct, arginfo_midgard_query_select___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(midgard_query_select, toggle_readonly, arginfo_midgard_query_select_toggle_readonly, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_select, include_deleted, arginfo_midgard_query_select_include_deleted, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_select, list_objects, arginfo_midgard_query_select_list_objects, ZEND_ACC_PUBLIC)
PHP_ME(midgard_query_select, connect, arginfo_midgard_query_select_connect, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
static zend_class_entry php_midgard_query_select_class_entry;
INIT_CLASS_ENTRY(php_midgard_query_select_class_entry, "MidgardQuerySelect", midgard_query_select_methods);
php_midgard_query_select_class = zend_register_internal_class_ex(&php_midgard_query_select_class_entry, php_midgard_query_executor_class, "midgard_query_executor" TSRMLS_CC);
php_midgard_query_select_class->create_object = php_midgard_gobject_new;
CLASS_SET_DOC_COMMENT(php_midgard_query_select_class, strdup("SQL SELECT queries generator and executor"));
_FIXME_zend_register_class_alias("midgard_query_select", php_midgard_query_select_class);
return SUCCESS;
}