PHP-8.2 compat with __toString
[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 #if PHP_VERSION_ID >= 80100
135 ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(ai_pqdt_jsonserialize, 0, 0, IS_MIXED, 0)
136 ZEND_END_ARG_INFO()
137 #else
138 #define ai_pqdt_jsonserialize ai_pqdt_to_string
139 #endif
140
141 #if PHP_VERSION_ID >= 80200
142 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ai_pqdt_to_string, 0, 0, IS_STRING, 0)
143 #else
144 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string, 0, 0, 0)
145 #endif
146 ZEND_END_ARG_INFO();
147 static PHP_METHOD(pqdt, __toString)
148 {
149 zval rv, tmp;
150
151 ZVAL_NULL(&rv);
152 php_pq_call_method(getThis(), "format", 1, &rv, php_pq_read_property(getThis(), "format", &tmp));
153 RETVAL_ZVAL(&rv, 1, 1);
154 }
155
156 #if PHP_VERSION_ID >= 80100
157 ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(ai_pqdt_create_from_format, 0, 2, DateTime, MAY_BE_FALSE)
158 #else
159 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_create_from_format, 0, 0, 2)
160 #endif
161 ZEND_ARG_INFO(0, format)
162 ZEND_ARG_INFO(0, datetime)
163 #if PHP_VERSION_ID >= 70200
164 ZEND_ARG_OBJ_INFO(0, object, DateTimeZone, 1)
165 #else
166 ZEND_ARG_INFO(0, timezone)
167 #endif
168 ZEND_END_ARG_INFO();
169 static PHP_METHOD(pqdt, createFromFormat)
170 {
171 zend_error_handling zeh;
172 char *fmt_str, *dt_str;
173 size_t fmt_len, dt_len;
174 zval *ztz = NULL;
175 ZEND_RESULT_CODE rv;
176
177 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
178 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O", &fmt_str, &fmt_len, &dt_str, &dt_len, &ztz, php_date_get_timezone_ce());
179 zend_restore_error_handling(&zeh);
180
181 if (SUCCESS == rv) {
182 php_pqdt_from_string(return_value, fmt_str, dt_str, dt_len, "Y-m-d H:i:s.uO", ztz);
183 }
184 }
185
186 static zend_function_entry php_pqdt_methods[] = {
187 PHP_ME(pqdt, createFromFormat, ai_pqdt_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
188 PHP_ME(pqdt, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
189 PHP_MALIAS(pqdt, jsonSerialize, __toString, ai_pqdt_jsonserialize, ZEND_ACC_PUBLIC)
190 {0}
191 };
192
193 zval *php_pqdt_from_string(zval *zv, char *input_fmt, char *dt_str, size_t dt_len, const char *output_fmt, zval *ztimezone)
194 {
195 php_date_obj *dobj;
196
197 php_date_instantiate(php_pqdt_class_entry, zv);
198 dobj = php_date_obj_from_obj(Z_OBJ_P(zv));
199 if (!php_date_initialize(dobj, dt_str, dt_len, input_fmt, ztimezone, 1)) {
200 zval_dtor(zv);
201 ZVAL_NULL(zv);
202 } else if (output_fmt) {
203 zval fmt;
204 ZVAL_STRING(&fmt, output_fmt);
205 php_pq_update_property(zv, "format", &fmt);
206 zval_ptr_dtor(&fmt);
207 }
208
209 return zv;
210 }
211
212 zend_string *php_pqdt_to_string(zval *zdt, const char *format)
213 {
214 zval rv;
215
216 ZVAL_NULL(&rv);
217
218 if (php_pq_cast_object(zdt, IS_STRING, &rv)) {
219 return Z_STR(rv);
220 } else if (instanceof_function(Z_OBJCE_P(zdt), php_date_get_date_ce())) {
221 zval rv, zfmt;
222
223 ZVAL_NULL(&rv);
224 ZVAL_STRING(&zfmt, format);
225 php_pq_call_method(zdt, "format", 1, &rv, &zfmt);
226 zval_ptr_dtor(&zfmt);
227
228 if (Z_TYPE(rv) == IS_STRING) {
229 return Z_STR(rv);
230 }
231 zval_ptr_dtor(&rv);
232 }
233
234 return NULL;
235 }
236
237 zend_class_entry *php_pqconv_class_entry;
238
239 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_types, 0, 0, 0)
240 ZEND_END_ARG_INFO();
241
242 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_from_string, 0, 0, 2)
243 ZEND_ARG_INFO(0, data)
244 ZEND_ARG_INFO(0, type)
245 ZEND_END_ARG_INFO();
246
247 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_to_string, 0, 0, 2)
248 ZEND_ARG_INFO(0, data)
249 ZEND_ARG_INFO(0, type)
250 ZEND_END_ARG_INFO();
251
252 zend_function_entry php_pqconv_methods[] = {
253 PHP_ABSTRACT_ME(pqconv, convertTypes, ai_pqconv_convert_types)
254 PHP_ABSTRACT_ME(pqconv, convertFromString, ai_pqconv_convert_from_string)
255 PHP_ABSTRACT_ME(pqconv, convertToString, ai_pqconv_convert_to_string)
256 {0}
257 };
258
259
260 PHP_MINIT_FUNCTION(pq_misc)
261 {
262 zend_class_entry *json, ce = {0};
263
264 INIT_NS_CLASS_ENTRY(ce, "pq", "Converter", php_pqconv_methods);
265 php_pqconv_class_entry = zend_register_internal_interface(&ce);
266
267 memset(&ce, 0, sizeof(ce));
268 INIT_NS_CLASS_ENTRY(ce ,"pq", "DateTime", php_pqdt_methods);
269 php_pqdt_class_entry = zend_register_internal_class_ex(&ce, php_date_get_date_ce());
270
271 zend_declare_property_stringl(php_pqdt_class_entry, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.uO"), ZEND_ACC_PUBLIC);
272
273 /* stop reading this file right here! */
274 if ((json = zend_hash_str_find_ptr(CG(class_table), ZEND_STRL("jsonserializable")))) {
275 zend_class_implements(php_pqdt_class_entry, 1, json);
276 }
277
278 return SUCCESS;
279 }
280
281 typedef struct _HashTableList {
282 zval arr;
283 struct _HashTableList *parent;
284 } HashTableList;
285
286 typedef struct _ArrayParserState {
287 const char *ptr, *end;
288 HashTableList *list;
289 php_pqres_t *res;
290 Oid typ;
291 unsigned quotes:1;
292 unsigned escaped:1;
293 } ArrayParserState;
294
295 static char caa(ArrayParserState *a, const char *any, unsigned advance)
296 {
297 const char *p = any;
298
299 do {
300 if (*p == *a->ptr) {
301 a->ptr += advance;
302 return *p;
303 }
304 } while (*++p);
305
306 php_error_docref(NULL, E_WARNING, "Failed to parse array: expected one of '%s', got '%c'", any, *a->ptr); \
307 return 0;
308 }
309
310 static ZEND_RESULT_CODE add_element(ArrayParserState *a, const char *start)
311 {
312 zval zelem;
313 zend_string *zstr = zend_string_init(start, a->ptr - start, 0);
314
315 if (a->quotes) {
316 php_stripslashes(zstr);
317 ZVAL_STR(&zelem, zstr);
318 } else if (!zend_string_equals_literal(zstr, "NULL")) {
319 ZVAL_STR(&zelem, zstr);
320 } else {
321 zend_string_release(zstr);
322 ZVAL_NULL(&zelem);
323 }
324
325 if (!ZVAL_IS_NULL(&zelem)) {
326 php_pqres_typed_zval(a->res, a->typ, &zelem);
327 }
328
329 add_next_index_zval(&a->list->arr, &zelem);
330 return SUCCESS;
331 }
332
333 static ZEND_RESULT_CODE parse_array(ArrayParserState *a);
334
335 static ZEND_RESULT_CODE parse_element(ArrayParserState *a, char delim)
336 {
337 const char *el;
338
339 switch (*a->ptr) {
340 case '{':
341 return parse_array(a);
342
343 case '}':
344 return SUCCESS;
345
346 case '"':
347 a->quotes = 1;
348 ++a->ptr;
349 break;
350 }
351
352 for (el = a->ptr; a->ptr < a->end; ++a->ptr) {
353 switch (*a->ptr) {
354 case '\\':
355 a->escaped = !a->escaped;
356 break;
357
358 case '"':
359 if (a->escaped) {
360 a->escaped = 0;
361 } else if (a->quotes) {
362 if (SUCCESS != add_element(a, el)) {
363 return FAILURE;
364 }
365 a->quotes = 0;
366 ++a->ptr;
367 return SUCCESS;
368 } else {
369 php_error_docref(NULL, E_WARNING, "Failed to parse element, unexpected quote: '%.*s'", (int) (a->ptr - el), el);
370 return FAILURE;
371 }
372 break;
373
374 default:
375 if (delim != *a->ptr) {
376 a->escaped = 0;
377 break;
378 }
379 /* no break */
380 case '}':
381 if (!a->quotes) {
382 return add_element(a, el);
383 }
384 break;
385
386 }
387 }
388
389 php_error_docref(NULL, E_WARNING, "Failed to parse element, reached end of input");
390 return FAILURE;
391 }
392
393 static ZEND_RESULT_CODE parse_elements(ArrayParserState *a)
394 {
395 char delims[] = {'}', (char) PHP_PQ_DELIM_OF_ARRAY(a->typ), 0};
396
397 while (SUCCESS == parse_element(a, delims[1])) {
398 switch (caa(a, delims, 0)) {
399 case 0:
400 return FAILURE;
401
402 case '}':
403 return SUCCESS;
404
405 default:
406 if (!*++a->ptr) {
407 php_error_docref(NULL, E_WARNING, "Failed to parse elements, reached end of input");
408 return FAILURE;
409 }
410 break;
411 }
412 }
413
414 return FAILURE;
415 }
416
417 static ZEND_RESULT_CODE parse_array(ArrayParserState *a)
418 {
419 HashTableList *list;
420
421 if (!caa(a, "{", 1)) {
422 return FAILURE;
423 }
424
425 list = ecalloc(1, sizeof(*list));
426 array_init(&list->arr);
427
428 if (a->list) {
429 add_next_index_zval(&a->list->arr, &list->arr);
430 list->parent = a->list;
431 }
432 a->list = list;
433
434 if (SUCCESS != parse_elements(a)) {
435 return FAILURE;
436 }
437
438 if (!caa(a, "}", 1)) {
439 return FAILURE;
440 }
441
442 /* step one level back up */
443 if (a->list->parent) {
444 HashTableList *l = a->list->parent;
445
446 efree(a->list);
447 a->list = l;
448 }
449
450 return SUCCESS;
451 }
452
453 HashTable *php_pq_parse_array(php_pqres_t *res, const char *val_str, size_t val_len, Oid typ)
454 {
455 HashTable *ht = NULL;
456 ArrayParserState a = {0};
457
458 a.typ = typ;
459 a.ptr = val_str;
460 a.end = val_str + val_len;
461 a.res = res;
462
463 if (SUCCESS != parse_array(&a)) {
464 while (a.list) {
465 HashTableList *l = a.list->parent;
466
467 zval_dtor(&a.list->arr);
468 efree(a.list);
469 a.list = l;
470 }
471 return ht;
472 }
473
474 if (*a.ptr) {
475 php_error_docref(NULL, E_NOTICE, "Trailing input: '%s'", a.ptr);
476 }
477
478 while (a.list) {
479 HashTableList *l = a.list->parent;
480
481 ht = Z_ARRVAL(a.list->arr);
482 efree(a.list);
483 a.list = l;
484 }
485
486 return ht;
487 }
488
489
490 /*
491 * Local variables:
492 * tab-width: 4
493 * c-basic-offset: 4
494 * End:
495 * vim600: noet sw=4 ts=4 fdm=marker
496 * vim<600: noet sw=4 ts=4
497 */