prepare v2.2.3
[m6w6/ext-pq] / src / php_pq_misc.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18 #include <ext/date/php_date.h>
19 #include <ext/standard/php_string.h>
20
21 #include <Zend/zend_interfaces.h>
22
23 #include <libpq/libpq-fs.h>
24
25 #include "php_pq.h"
26 #include "php_pqexc.h"
27 #include "php_pq_misc.h"
28 #include "php_pqconn_event.h"
29 #undef PHP_PQ_TYPE
30 #include "php_pq_type.h"
31
32
33 /* clear result object associated with a result handle */
34 void php_pqres_clear(PGresult *r) {
35 php_pq_object_t *o = PQresultInstanceData(r, php_pqconn_event);
36
37 if (o) {
38 php_pq_object_delref(o);
39 } else {
40 PQclear(r);
41 }
42 }
43
44 /* clear any asynchronous results */
45 void php_pqconn_clear(PGconn *conn) {
46 PGresult *r;
47 php_pqconn_event_data_t *evdata = PQinstanceData(conn, php_pqconn_event);
48
49 while ((r = PQgetResult(conn))) {
50 php_pqres_clear(r);
51 }
52
53 if (evdata && evdata->obj) {
54 if (php_pq_callback_is_enabled(&evdata->obj->intern->onevent)) {
55 if (php_pq_callback_is_locked(&evdata->obj->intern->onevent)) {
56 php_pq_callback_disable(&evdata->obj->intern->onevent);
57 } else {
58 php_pq_callback_dtor(&evdata->obj->intern->onevent);
59 }
60 }
61 }
62 }
63
64 /* safe wrappers to clear any asynchronous wrappers before querying synchronously */
65 PGresult *php_pq_exec(PGconn *conn, const char *query) {
66 php_pqconn_clear(conn);
67 return PQexec(conn, query);
68 }
69 PGresult *php_pq_exec_params(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const * paramValues, const int *paramLengths, const int *paramFormats, int resultFormat) {
70 php_pqconn_clear(conn);
71 return PQexecParams(conn, command, nParams, paramTypes, paramValues, paramLengths, paramFormats, resultFormat);
72 }
73 PGresult *php_pq_prepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes) {
74 php_pqconn_clear(conn);
75 return PQprepare(conn, stmtName, query, nParams, paramTypes);
76 }
77 PGresult *php_pq_exec_prepared(PGconn *conn, const char *stmtName, int nParams, const char *const * paramValues, const int *paramLengths, const int *paramFormats, int resultFormat) {
78 php_pqconn_clear(conn);
79 return PQexecPrepared(conn, stmtName, nParams, paramValues, paramLengths, paramFormats, resultFormat);
80 }
81
82 char *php_pq_rtrim(char *e)
83 {
84 size_t l = strlen(e);
85
86 while (l-- > 0 && e[l] == '\n') {
87 e[l] = '\0';
88 }
89 return e;
90 }
91
92 const char *php_pq_strmode(long mode)
93 {
94 switch (mode & (INV_READ|INV_WRITE)) {
95 case INV_READ|INV_WRITE:
96 return "rw";
97 case INV_READ:
98 return "r";
99 case INV_WRITE:
100 return "w";
101 default:
102 return "-";
103 }
104 }
105
106 static inline int compare_index(zend_ulong l, zend_ulong r)
107 {
108 if (l < r) {
109 return -1;
110 }
111 if (l > r) {
112 return 1;
113 }
114 return 0;
115 }
116 #if PHP_VERSION_ID >= 80000
117 int php_pq_compare_index(Bucket *lptr, Bucket *rptr)
118 {
119 return compare_index(lptr->h, rptr->h);
120 }
121 #else
122 int php_pq_compare_index(const void *lptr, const void *rptr) {
123 return compare_index(((const Bucket *) lptr)->h, ((const Bucket *) rptr)->h);
124 }
125 #endif
126
127 void php_pq_hash_ptr_dtor(zval *p)
128 {
129 efree(Z_PTR_P(p));
130 }
131
132 zend_class_entry *php_pqdt_class_entry;
133
134 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string, 0, 0, 0)
135 ZEND_END_ARG_INFO();
136 static PHP_METHOD(pqdt, __toString)
137 {
138 zval rv, tmp;
139
140 ZVAL_NULL(&rv);
141 php_pq_call_method(getThis(), "format", 1, &rv, php_pq_read_property(getThis(), "format", &tmp));
142 RETVAL_ZVAL(&rv, 1, 1);
143 }
144
145 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_create_from_format, 0, 0, 2)
146 ZEND_ARG_INFO(0, format)
147 ZEND_ARG_INFO(0, datetime)
148 #if PHP_VERSION_ID >= 70200
149 ZEND_ARG_OBJ_INFO(0, object, DateTimeZone, 1)
150 #else
151 ZEND_ARG_INFO(0, timezone)
152 #endif
153 ZEND_END_ARG_INFO();
154 static PHP_METHOD(pqdt, createFromFormat)
155 {
156 zend_error_handling zeh;
157 char *fmt_str, *dt_str;
158 size_t fmt_len, dt_len;
159 zval *ztz = NULL;
160 ZEND_RESULT_CODE rv;
161
162 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
163 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O", &fmt_str, &fmt_len, &dt_str, &dt_len, &ztz, php_date_get_timezone_ce());
164 zend_restore_error_handling(&zeh);
165
166 if (SUCCESS == rv) {
167 php_pqdt_from_string(return_value, fmt_str, dt_str, dt_len, "Y-m-d H:i:s.uO", ztz);
168 }
169 }
170
171 static zend_function_entry php_pqdt_methods[] = {
172 PHP_ME(pqdt, createFromFormat, ai_pqdt_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
173 PHP_ME(pqdt, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
174 PHP_MALIAS(pqdt, jsonSerialize, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
175 {0}
176 };
177
178 zval *php_pqdt_from_string(zval *zv, char *input_fmt, char *dt_str, size_t dt_len, const char *output_fmt, zval *ztimezone)
179 {
180 php_date_obj *dobj;
181
182 php_date_instantiate(php_pqdt_class_entry, zv);
183 dobj = php_date_obj_from_obj(Z_OBJ_P(zv));
184 if (!php_date_initialize(dobj, dt_str, dt_len, input_fmt, ztimezone, 1)) {
185 zval_dtor(zv);
186 ZVAL_NULL(zv);
187 } else if (output_fmt) {
188 zval fmt;
189 ZVAL_STRING(&fmt, output_fmt);
190 php_pq_update_property(zv, "format", &fmt);
191 zval_ptr_dtor(&fmt);
192 }
193
194 return zv;
195 }
196
197 zend_string *php_pqdt_to_string(zval *zdt, const char *format)
198 {
199 zval rv;
200
201 ZVAL_NULL(&rv);
202
203 if (php_pq_cast_object(zdt, IS_STRING, &rv)) {
204 return Z_STR(rv);
205 } else if (instanceof_function(Z_OBJCE_P(zdt), php_date_get_date_ce())) {
206 zval rv, zfmt;
207
208 ZVAL_NULL(&rv);
209 ZVAL_STRING(&zfmt, format);
210 php_pq_call_method(zdt, "format", 1, &rv, &zfmt);
211 zval_ptr_dtor(&zfmt);
212
213 if (Z_TYPE(rv) == IS_STRING) {
214 return Z_STR(rv);
215 }
216 zval_ptr_dtor(&rv);
217 }
218
219 return NULL;
220 }
221
222 zend_class_entry *php_pqconv_class_entry;
223
224 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_types, 0, 0, 0)
225 ZEND_END_ARG_INFO();
226
227 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_from_string, 0, 0, 2)
228 ZEND_ARG_INFO(0, data)
229 ZEND_ARG_INFO(0, type)
230 ZEND_END_ARG_INFO();
231
232 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_to_string, 0, 0, 2)
233 ZEND_ARG_INFO(0, data)
234 ZEND_ARG_INFO(0, type)
235 ZEND_END_ARG_INFO();
236
237 zend_function_entry php_pqconv_methods[] = {
238 PHP_ABSTRACT_ME(pqconv, convertTypes, ai_pqconv_convert_types)
239 PHP_ABSTRACT_ME(pqconv, convertFromString, ai_pqconv_convert_from_string)
240 PHP_ABSTRACT_ME(pqconv, convertToString, ai_pqconv_convert_to_string)
241 {0}
242 };
243
244
245 PHP_MINIT_FUNCTION(pq_misc)
246 {
247 zend_class_entry *json, ce = {0};
248
249 INIT_NS_CLASS_ENTRY(ce, "pq", "Converter", php_pqconv_methods);
250 php_pqconv_class_entry = zend_register_internal_interface(&ce);
251
252 memset(&ce, 0, sizeof(ce));
253 INIT_NS_CLASS_ENTRY(ce ,"pq", "DateTime", php_pqdt_methods);
254 php_pqdt_class_entry = zend_register_internal_class_ex(&ce, php_date_get_date_ce());
255
256 zend_declare_property_stringl(php_pqdt_class_entry, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.uO"), ZEND_ACC_PUBLIC);
257
258 /* stop reading this file right here! */
259 if ((json = zend_hash_str_find_ptr(CG(class_table), ZEND_STRL("jsonserializable")))) {
260 zend_class_implements(php_pqdt_class_entry, 1, json);
261 }
262
263 return SUCCESS;
264 }
265
266 typedef struct _HashTableList {
267 zval arr;
268 struct _HashTableList *parent;
269 } HashTableList;
270
271 typedef struct _ArrayParserState {
272 const char *ptr, *end;
273 HashTableList *list;
274 php_pqres_t *res;
275 Oid typ;
276 unsigned quotes:1;
277 unsigned escaped:1;
278 } ArrayParserState;
279
280 static char caa(ArrayParserState *a, const char *any, unsigned advance)
281 {
282 const char *p = any;
283
284 do {
285 if (*p == *a->ptr) {
286 a->ptr += advance;
287 return *p;
288 }
289 } while (*++p);
290
291 php_error_docref(NULL, E_WARNING, "Failed to parse array: expected one of '%s', got '%c'", any, *a->ptr); \
292 return 0;
293 }
294
295 static ZEND_RESULT_CODE add_element(ArrayParserState *a, const char *start)
296 {
297 zval zelem;
298 zend_string *zstr = zend_string_init(start, a->ptr - start, 0);
299
300 if (a->quotes) {
301 php_stripslashes(zstr);
302 ZVAL_STR(&zelem, zstr);
303 } else if (!zend_string_equals_literal(zstr, "NULL")) {
304 ZVAL_STR(&zelem, zstr);
305 } else {
306 zend_string_release(zstr);
307 ZVAL_NULL(&zelem);
308 }
309
310 if (!ZVAL_IS_NULL(&zelem)) {
311 php_pqres_typed_zval(a->res, a->typ, &zelem);
312 }
313
314 add_next_index_zval(&a->list->arr, &zelem);
315 return SUCCESS;
316 }
317
318 static ZEND_RESULT_CODE parse_array(ArrayParserState *a);
319
320 static ZEND_RESULT_CODE parse_element(ArrayParserState *a, char delim)
321 {
322 const char *el;
323
324 switch (*a->ptr) {
325 case '{':
326 return parse_array(a);
327
328 case '}':
329 return SUCCESS;
330
331 case '"':
332 a->quotes = 1;
333 ++a->ptr;
334 break;
335 }
336
337 for (el = a->ptr; a->ptr < a->end; ++a->ptr) {
338 switch (*a->ptr) {
339 case '\\':
340 a->escaped = !a->escaped;
341 break;
342
343 case '"':
344 if (a->escaped) {
345 a->escaped = 0;
346 } else if (a->quotes) {
347 if (SUCCESS != add_element(a, el)) {
348 return FAILURE;
349 }
350 a->quotes = 0;
351 ++a->ptr;
352 return SUCCESS;
353 } else {
354 php_error_docref(NULL, E_WARNING, "Failed to parse element, unexpected quote: '%.*s'", (int) (a->ptr - el), el);
355 return FAILURE;
356 }
357 break;
358
359 default:
360 if (delim != *a->ptr) {
361 a->escaped = 0;
362 break;
363 }
364 /* no break */
365 case '}':
366 if (!a->quotes) {
367 return add_element(a, el);
368 }
369 break;
370
371 }
372 }
373
374 php_error_docref(NULL, E_WARNING, "Failed to parse element, reached end of input");
375 return FAILURE;
376 }
377
378 static ZEND_RESULT_CODE parse_elements(ArrayParserState *a)
379 {
380 char delims[] = {'}', (char) PHP_PQ_DELIM_OF_ARRAY(a->typ), 0};
381
382 while (SUCCESS == parse_element(a, delims[1])) {
383 switch (caa(a, delims, 0)) {
384 case 0:
385 return FAILURE;
386
387 case '}':
388 return SUCCESS;
389
390 default:
391 if (!*++a->ptr) {
392 php_error_docref(NULL, E_WARNING, "Failed to parse elements, reached end of input");
393 return FAILURE;
394 }
395 break;
396 }
397 }
398
399 return FAILURE;
400 }
401
402 static ZEND_RESULT_CODE parse_array(ArrayParserState *a)
403 {
404 HashTableList *list;
405
406 if (!caa(a, "{", 1)) {
407 return FAILURE;
408 }
409
410 list = ecalloc(1, sizeof(*list));
411 array_init(&list->arr);
412
413 if (a->list) {
414 add_next_index_zval(&a->list->arr, &list->arr);
415 list->parent = a->list;
416 }
417 a->list = list;
418
419 if (SUCCESS != parse_elements(a)) {
420 return FAILURE;
421 }
422
423 if (!caa(a, "}", 1)) {
424 return FAILURE;
425 }
426
427 /* step one level back up */
428 if (a->list->parent) {
429 HashTableList *l = a->list->parent;
430
431 efree(a->list);
432 a->list = l;
433 }
434
435 return SUCCESS;
436 }
437
438 HashTable *php_pq_parse_array(php_pqres_t *res, const char *val_str, size_t val_len, Oid typ)
439 {
440 HashTable *ht = NULL;
441 ArrayParserState a = {0};
442
443 a.typ = typ;
444 a.ptr = val_str;
445 a.end = val_str + val_len;
446 a.res = res;
447
448 if (SUCCESS != parse_array(&a)) {
449 while (a.list) {
450 HashTableList *l = a.list->parent;
451
452 zval_dtor(&a.list->arr);
453 efree(a.list);
454 a.list = l;
455 }
456 return ht;
457 }
458
459 if (*a.ptr) {
460 php_error_docref(NULL, E_NOTICE, "Trailing input: '%s'", a.ptr);
461 }
462
463 while (a.list) {
464 HashTableList *l = a.list->parent;
465
466 ht = Z_ARRVAL(a.list->arr);
467 efree(a.list);
468 a.list = l;
469 }
470
471 return ht;
472 }
473
474
475 /*
476 * Local variables:
477 * tab-width: 4
478 * c-basic-offset: 4
479 * End:
480 * vim600: noet sw=4 ts=4 fdm=marker
481 * vim<600: noet sw=4 ts=4
482 */