-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
pgsql.c
5881 lines (5122 loc) · 156 KB
/
pgsql.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Zeev Suraski <zeev@php.net> |
| Jouni Ahto <jouni.ahto@exdec.fi> |
| Yasuo Ohgaki <yohgaki@php.net> |
| Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*) |
| Chris Kings-Lynne <chriskl@php.net> (v3 protocol) |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#define PHP_PGSQL_PRIVATE 1
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define SMART_STR_PREALLOC 512
#include "php.h"
#include "php_ini.h"
#include "ext/standard/php_standard.h"
#include "zend_smart_str.h"
#include "ext/pcre/php_pcre.h"
#ifdef PHP_WIN32
# include "win32/time.h"
#endif
#include "php_pgsql.h"
#include "php_globals.h"
#include "zend_exceptions.h"
#ifdef HAVE_PGSQL
#ifndef InvalidOid
#define InvalidOid ((Oid) 0)
#endif
#define PGSQL_ASSOC 1<<0
#define PGSQL_NUM 1<<1
#define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM)
#define PGSQL_NOTICE_LAST 1 /* Get the last notice */
#define PGSQL_NOTICE_ALL 2 /* Get all notices */
#define PGSQL_NOTICE_CLEAR 3 /* Remove notices */
#define PGSQL_STATUS_LONG 1
#define PGSQL_STATUS_STRING 2
#define PGSQL_MAX_LENGTH_OF_LONG 30
#define PGSQL_MAX_LENGTH_OF_DOUBLE 60
char pgsql_libpq_version[16];
#include "pgsql_arginfo.h"
#if ZEND_LONG_MAX < UINT_MAX
#define PGSQL_RETURN_OID(oid) do { \
if (oid > ZEND_LONG_MAX) { \
RETURN_STR(zend_ulong_to_str(oid)); \
} \
RETURN_LONG((zend_long)oid); \
} while(0)
#else
#define PGSQL_RETURN_OID(oid) RETURN_LONG((zend_long)oid)
#endif
#define CHECK_DEFAULT_LINK(x) \
if ((x) == NULL) { \
zend_throw_error(NULL, "No PostgreSQL connection opened yet"); \
RETURN_THROWS(); \
}
/* This is a bit hacky as the macro usage is "link = FETCH_DEFAULT_LINK();" */
#define FETCH_DEFAULT_LINK() \
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL); \
php_error_docref(NULL, E_DEPRECATED, "Automatic fetching of PostgreSQL connection is deprecated")
/* Used only when creating a connection */
#define FETCH_DEFAULT_LINK_NO_WARNING() \
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)
#define CHECK_PGSQL_LINK(link_handle) \
if (link_handle->conn == NULL) { \
zend_throw_error(NULL, "PostgreSQL connection has already been closed"); \
RETURN_THROWS(); \
}
#define CHECK_PGSQL_RESULT(result_handle) \
if (result_handle->result == NULL) { \
zend_throw_error(NULL, "PostgreSQL result has already been closed"); \
RETURN_THROWS(); \
}
#define CHECK_PGSQL_LOB(lob) \
if (lob->conn == NULL) { \
zend_throw_error(NULL, "PostgreSQL large object has already been closed"); \
RETURN_THROWS(); \
}
#ifndef HAVE_PQFREEMEM
#define PQfreemem free
#endif
#if PG_VERSION_NUM < 120000
#define PQERRORS_SQLSTATE 0
#endif
ZEND_DECLARE_MODULE_GLOBALS(pgsql)
static PHP_GINIT_FUNCTION(pgsql);
/* {{{ pgsql_module_entry */
zend_module_entry pgsql_module_entry = {
STANDARD_MODULE_HEADER,
"pgsql",
ext_functions,
PHP_MINIT(pgsql),
PHP_MSHUTDOWN(pgsql),
PHP_RINIT(pgsql),
PHP_RSHUTDOWN(pgsql),
PHP_MINFO(pgsql),
PHP_PGSQL_VERSION,
PHP_MODULE_GLOBALS(pgsql),
PHP_GINIT(pgsql),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_PGSQL
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(pgsql)
#endif
static int le_plink;
static zend_class_entry *pgsql_link_ce, *pgsql_result_ce, *pgsql_lob_ce;
static zend_object_handlers pgsql_link_object_handlers, pgsql_result_object_handlers, pgsql_lob_object_handlers;
static inline pgsql_link_handle *pgsql_link_from_obj(zend_object *obj) {
return (pgsql_link_handle *)((char *)(obj) - XtOffsetOf(pgsql_link_handle, std));
}
#define Z_PGSQL_LINK_P(zv) pgsql_link_from_obj(Z_OBJ_P(zv))
static zend_object *pgsql_link_create_object(zend_class_entry *class_type) {
pgsql_link_handle *intern = zend_object_alloc(sizeof(pgsql_link_handle), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *pgsql_link_get_constructor(zend_object *object) {
zend_throw_error(NULL, "Cannot directly construct PgSql\\Connection, use pg_connect() or pg_pconnect() instead");
return NULL;
}
static void pgsql_link_free(pgsql_link_handle *link)
{
PGresult *res;
while ((res = PQgetResult(link->conn))) {
PQclear(res);
}
if (!link->persistent) {
PQuntrace(link->conn);
PQfinish(link->conn);
}
PGG(num_links)--;
zend_hash_del(&PGG(connections), link->hash);
link->conn = NULL;
zend_string_release(link->hash);
if (link->notices) {
zend_hash_destroy(link->notices);
FREE_HASHTABLE(link->notices);
link->notices = NULL;
}
}
static void pgsql_link_free_obj(zend_object *obj)
{
pgsql_link_handle *link = pgsql_link_from_obj(obj);
if (link->conn) {
pgsql_link_free(link);
}
zend_object_std_dtor(&link->std);
}
static inline pgsql_result_handle *pgsql_result_from_obj(zend_object *obj) {
return (pgsql_result_handle *)((char *)(obj) - XtOffsetOf(pgsql_result_handle, std));
}
#define Z_PGSQL_RESULT_P(zv) pgsql_result_from_obj(Z_OBJ_P(zv))
static zend_object *pgsql_result_create_object(zend_class_entry *class_type) {
pgsql_result_handle *intern = zend_object_alloc(sizeof(pgsql_result_handle), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *pgsql_result_get_constructor(zend_object *object) {
zend_throw_error(NULL, "Cannot directly construct PgSql\\Result, use a dedicated function instead");
return NULL;
}
static void pgsql_result_free(pgsql_result_handle *pg_result)
{
PQclear(pg_result->result);
pg_result->result = NULL;
}
static void pgsql_result_free_obj(zend_object *obj)
{
pgsql_result_handle *pg_result = pgsql_result_from_obj(obj);
if (pg_result->result) {
pgsql_result_free(pg_result);
}
zend_object_std_dtor(&pg_result->std);
}
static inline pgLofp *pgsql_lob_from_obj(zend_object *obj) {
return (pgLofp *)((char *)(obj) - XtOffsetOf(pgLofp, std));
}
#define Z_PGSQL_LOB_P(zv) pgsql_lob_from_obj(Z_OBJ_P(zv))
static zend_object *pgsql_lob_create_object(zend_class_entry *class_type) {
pgLofp *intern = zend_object_alloc(sizeof(pgLofp), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *pgsql_lob_get_constructor(zend_object *object) {
zend_throw_error(NULL, "Cannot directly construct PgSql\\Lob, use pg_lo_open() instead");
return NULL;
}
static void pgsql_lob_free_obj(zend_object *obj)
{
pgLofp *lofp = pgsql_lob_from_obj(obj);
zend_object_std_dtor(&lofp->std);
}
/* Compatibility definitions */
#ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
#define pg_encoding_to_char(x) "SQL_ASCII"
#endif
static zend_string *_php_pgsql_trim_message(const char *message)
{
size_t i = strlen(message);
if (i>2 && (message[i-2] == '\r' || message[i-2] == '\n') && message[i-1] == '.') {
--i;
}
while (i>1 && (message[i-1] == '\r' || message[i-1] == '\n')) {
--i;
}
return zend_string_init(message, i, 0);
}
#define PHP_PQ_ERROR(text, pgsql) { \
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); \
php_error_docref(NULL, E_WARNING, text, ZSTR_VAL(msgbuf)); \
zend_string_release(msgbuf); \
} \
static void php_pgsql_set_default_link(zend_object *obj)
{
GC_ADDREF(obj);
if (PGG(default_link) != NULL) {
zend_object_release(PGG(default_link));
}
PGG(default_link) = obj;
}
static void _close_pgsql_plink(zend_resource *rsrc)
{
PGconn *link = (PGconn *)rsrc->ptr;
PGresult *res;
while ((res = PQgetResult(link))) {
PQclear(res);
}
PQfinish(link);
PGG(num_persistent)--;
PGG(num_links)--;
rsrc->ptr = NULL;
}
static void _php_pgsql_notice_handler(void *l, const char *message)
{
if (PGG(ignore_notices)) {
return;
}
zval tmp;
pgsql_link_handle *link = (pgsql_link_handle *) l;
if (!link->notices) {
link->notices = zend_new_array(1);
}
zend_string *trimmed_message = _php_pgsql_trim_message(message);
if (PGG(log_notices)) {
php_error_docref(NULL, E_NOTICE, "%s", ZSTR_VAL(trimmed_message));
}
ZVAL_STR(&tmp, trimmed_message);
zend_hash_next_index_insert(link->notices, &tmp);
}
static int _rollback_transactions(zval *el)
{
PGconn *link;
PGresult *res;
zend_resource *rsrc = Z_RES_P(el);
if (rsrc->type != le_plink) {
return ZEND_HASH_APPLY_KEEP;
}
link = (PGconn *) rsrc->ptr;
if (PQsetnonblocking(link, 0)) {
php_error_docref("ref.pgsql", E_NOTICE, "Cannot set connection to blocking mode");
return -1;
}
while ((res = PQgetResult(link))) {
PQclear(res);
}
if ((PQprotocolVersion(link) >= 3 && PQtransactionStatus(link) != PQTRANS_IDLE) || PQprotocolVersion(link) < 3) {
int orig = PGG(ignore_notices);
PGG(ignore_notices) = 1;
res = PQexec(link,"ROLLBACK;");
PQclear(res);
PGG(ignore_notices) = orig;
}
return ZEND_HASH_APPLY_KEEP;
}
static void release_string(zval *zv)
{
zend_string_release((zend_string *) Z_PTR_P(zv));
}
static bool _php_pgsql_identifier_is_escaped(const char *identifier, size_t len) /* {{{ */
{
/* Handle edge case. Cannot be a escaped string */
if (len <= 2) {
return false;
}
/* Detect double quotes */
if (identifier[0] == '"' && identifier[len-1] == '"') {
size_t i;
/* Detect wrong format of " inside of escaped string */
for (i = 1; i < len-1; i++) {
if (identifier[i] == '"' && (identifier[++i] != '"' || i == len-1)) {
return false;
}
}
} else {
return false;
}
/* Escaped properly */
return true;
}
/* {{{ PHP_INI */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN( "pgsql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_persistent, zend_pgsql_globals, pgsql_globals)
STD_PHP_INI_ENTRY_EX("pgsql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_pgsql_globals, pgsql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("pgsql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_pgsql_globals, pgsql_globals, display_link_numbers)
STD_PHP_INI_BOOLEAN( "pgsql.auto_reset_persistent", "0", PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, zend_pgsql_globals, pgsql_globals)
STD_PHP_INI_BOOLEAN( "pgsql.ignore_notice", "0", PHP_INI_ALL, OnUpdateBool, ignore_notices, zend_pgsql_globals, pgsql_globals)
STD_PHP_INI_BOOLEAN( "pgsql.log_notice", "0", PHP_INI_ALL, OnUpdateBool, log_notices, zend_pgsql_globals, pgsql_globals)
PHP_INI_END()
static PHP_GINIT_FUNCTION(pgsql)
{
#if defined(COMPILE_DL_PGSQL) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
zend_hash_init(&pgsql_globals->connections, 0, NULL, NULL, 1);
}
static void php_libpq_version(char *buf, size_t len)
{
int version = PQlibVersion();
int major = version / 10000;
if (major >= 10) {
int minor = version % 10000;
snprintf(buf, len, "%d.%d", major, minor);
} else {
int minor = version / 100 % 100;
int revision = version % 100;
snprintf(buf, len, "%d.%d.%d", major, minor, revision);
}
}
PHP_MINIT_FUNCTION(pgsql)
{
REGISTER_INI_ENTRIES();
le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
pgsql_link_ce = register_class_PgSql_Connection();
pgsql_link_ce->create_object = pgsql_link_create_object;
pgsql_link_ce->default_object_handlers = &pgsql_link_object_handlers;
memcpy(&pgsql_link_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
pgsql_link_object_handlers.offset = XtOffsetOf(pgsql_link_handle, std);
pgsql_link_object_handlers.free_obj = pgsql_link_free_obj;
pgsql_link_object_handlers.get_constructor = pgsql_link_get_constructor;
pgsql_link_object_handlers.clone_obj = NULL;
pgsql_link_object_handlers.compare = zend_objects_not_comparable;
pgsql_result_ce = register_class_PgSql_Result();
pgsql_result_ce->create_object = pgsql_result_create_object;
pgsql_result_ce->default_object_handlers = &pgsql_result_object_handlers;
memcpy(&pgsql_result_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
pgsql_result_object_handlers.offset = XtOffsetOf(pgsql_result_handle, std);
pgsql_result_object_handlers.free_obj = pgsql_result_free_obj;
pgsql_result_object_handlers.get_constructor = pgsql_result_get_constructor;
pgsql_result_object_handlers.clone_obj = NULL;
pgsql_result_object_handlers.compare = zend_objects_not_comparable;
pgsql_lob_ce = register_class_PgSql_Lob();
pgsql_lob_ce->create_object = pgsql_lob_create_object;
pgsql_lob_ce->default_object_handlers = &pgsql_lob_object_handlers;
memcpy(&pgsql_lob_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
pgsql_lob_object_handlers.offset = XtOffsetOf(pgLofp, std);
pgsql_lob_object_handlers.free_obj = pgsql_lob_free_obj;
pgsql_lob_object_handlers.get_constructor = pgsql_lob_get_constructor;
pgsql_lob_object_handlers.clone_obj = NULL;
pgsql_lob_object_handlers.compare = zend_objects_not_comparable;
/* libpq version */
php_libpq_version(pgsql_libpq_version, sizeof(pgsql_libpq_version));
register_pgsql_symbols(module_number);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(pgsql)
{
UNREGISTER_INI_ENTRIES();
zend_hash_destroy(&PGG(connections));
return SUCCESS;
}
PHP_RINIT_FUNCTION(pgsql)
{
PGG(default_link) = NULL;
PGG(num_links) = PGG(num_persistent);
zend_hash_init(&PGG(field_oids), 0, NULL, release_string, 0);
zend_hash_init(&PGG(table_oids), 0, NULL, release_string, 0);
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(pgsql)
{
if (PGG(default_link)) {
zend_object_release(PGG(default_link));
PGG(default_link) = NULL;
}
zend_hash_destroy(&PGG(field_oids));
zend_hash_destroy(&PGG(table_oids));
/* clean up persistent connection */
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
return SUCCESS;
}
PHP_MINFO_FUNCTION(pgsql)
{
char buf[256];
php_info_print_table_start();
php_info_print_table_row(2, "PostgreSQL Support", "enabled");
php_info_print_table_row(2, "PostgreSQL (libpq) Version", pgsql_libpq_version);
#ifdef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
php_info_print_table_row(2, "Multibyte character support", "enabled");
#else
php_info_print_table_row(2, "Multibyte character support", "disabled");
#endif
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, PGG(num_persistent));
php_info_print_table_row(2, "Active Persistent Links", buf);
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, PGG(num_links));
php_info_print_table_row(2, "Active Links", buf);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *connstring;
size_t connstring_len;
pgsql_link_handle *link;
PGconn *pgsql;
smart_str str = {0};
zend_long connect_type = 0;
PGresult *pg_result;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &connstring, &connstring_len, &connect_type) == FAILURE) {
RETURN_THROWS();
}
smart_str_appends(&str, "pgsql");
smart_str_appendl(&str, connstring, connstring_len);
smart_str_appendc(&str, '_');
/* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent
* connections can re-use this connection. See bug #39979. */
smart_str_append_long(&str, connect_type & ~PGSQL_CONNECT_FORCE_NEW);
smart_str_0(&str);
if (persistent && PGG(allow_persistent)) {
zend_resource *le;
/* try to find if we already have this link in our persistent list */
if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
php_error_docref(NULL, E_WARNING,
"Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
goto err;
}
if (PGG(max_persistent) != -1 && PGG(num_persistent) >= PGG(max_persistent)) {
php_error_docref(NULL, E_WARNING,
"Cannot create new link. Too many open persistent links (" ZEND_LONG_FMT ")", PGG(num_persistent));
goto err;
}
/* create the link */
pgsql = PQconnectdb(connstring);
if (pgsql == NULL || PQstatus(pgsql) == CONNECTION_BAD) {
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql)
if (pgsql) {
PQfinish(pgsql);
}
goto err;
}
/* hash it up */
if (zend_register_persistent_resource(ZSTR_VAL(str.s), ZSTR_LEN(str.s), pgsql, le_plink) == NULL) {
goto err;
}
PGG(num_links)++;
PGG(num_persistent)++;
} else { /* we do */
if (le->type != le_plink) {
goto err;
}
/* ensure that the link did not die */
if (PGG(auto_reset_persistent) & 1) {
/* need to send & get something from backend to
make sure we catch CONNECTION_BAD every time */
PGresult *pg_result;
pg_result = PQexec(le->ptr, "select 1");
PQclear(pg_result);
}
if (PQstatus(le->ptr) == CONNECTION_BAD) { /* the link died */
if (le->ptr == NULL) {
le->ptr = PQconnectdb(connstring);
}
else {
PQreset(le->ptr);
}
if (le->ptr == NULL || PQstatus(le->ptr) == CONNECTION_BAD) {
php_error_docref(NULL, E_WARNING,"PostgreSQL connection lost, unable to reconnect");
zend_hash_del(&EG(persistent_list), str.s);
goto err;
}
}
pgsql = (PGconn *) le->ptr;
/* consider to use php_version_compare() here */
if (PQprotocolVersion(pgsql) >= 3 && zend_strtod(PQparameterStatus(pgsql, "server_version"), NULL) >= 7.2) {
pg_result = PQexec(pgsql, "RESET ALL;");
PQclear(pg_result);
}
}
object_init_ex(return_value, pgsql_link_ce);
link = Z_PGSQL_LINK_P(return_value);
link->conn = pgsql;
link->hash = zend_string_copy(str.s);
link->notices = NULL;
link->persistent = 1;
} else { /* Non persistent connection */
zval *index_ptr;
/* first we check the hash for the hashed_details key. If it exists,
* it should point us to the right offset where the actual pgsql link sits.
* if it doesn't, open a new pgsql link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
&& (index_ptr = zend_hash_find(&PGG(connections), str.s)) != NULL) {
php_pgsql_set_default_link(Z_OBJ_P(index_ptr));
ZVAL_COPY(return_value, index_ptr);
goto cleanup;
}
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
php_error_docref(NULL, E_WARNING, "Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
goto err;
}
/* Non-blocking connect */
if (connect_type & PGSQL_CONNECT_ASYNC) {
pgsql = PQconnectStart(connstring);
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
if (pgsql) {
PQfinish(pgsql);
}
goto err;
}
} else {
pgsql = PQconnectdb(connstring);
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
if (pgsql) {
PQfinish(pgsql);
}
goto err;
}
}
object_init_ex(return_value, pgsql_link_ce);
link = Z_PGSQL_LINK_P(return_value);
link->conn = pgsql;
link->hash = zend_string_copy(str.s);
link->notices = NULL;
link->persistent = 0;
/* add it to the hash */
zend_hash_update(&PGG(connections), str.s, return_value);
/* Keep track of link => hash mapping, so we can remove the hash entry from connections
* when the connection is closed. This uses the address of the connection rather than the
* zend_resource, because the resource destructor is passed a stack copy of the resource
* structure. */
PGG(num_links)++;
}
/* set notice processor */
if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, link);
}
php_pgsql_set_default_link(Z_OBJ_P(return_value));
cleanup:
smart_str_free(&str);
return;
err:
smart_str_free(&str);
RETURN_FALSE;
}
/* }}} */
/* {{{ Open a PostgreSQL connection */
PHP_FUNCTION(pg_connect)
{
php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
}
/* }}} */
/* {{{ Poll the status of an in-progress async PostgreSQL connection attempt*/
PHP_FUNCTION(pg_connect_poll)
{
zval *pgsql_link;
pgsql_link_handle *pgsql_handle;
PGconn *pgsql;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
RETURN_THROWS();
}
pgsql_handle = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(pgsql_handle);
pgsql = pgsql_handle->conn;
ret = PQconnectPoll(pgsql);
RETURN_LONG(ret);
}
/* }}} */
/* {{{ Open a persistent PostgreSQL connection */
PHP_FUNCTION(pg_pconnect)
{
php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
}
/* }}} */
/* {{{ Close a PostgreSQL connection */
PHP_FUNCTION(pg_close)
{
zval *pgsql_link = NULL;
pgsql_link_handle *link;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
RETURN_THROWS();
}
if (!pgsql_link) {
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
zend_object_release(PGG(default_link));
PGG(default_link) = NULL;
RETURN_TRUE;
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
if (link == FETCH_DEFAULT_LINK_NO_WARNING()) {
GC_DELREF(PGG(default_link));
PGG(default_link) = NULL;
}
pgsql_link_free(link);
RETURN_TRUE;
}
/* }}} */
#define PHP_PG_DBNAME 1
#define PHP_PG_ERROR_MESSAGE 2
#define PHP_PG_OPTIONS 3
#define PHP_PG_PORT 4
#define PHP_PG_TTY 5
#define PHP_PG_HOST 6
#define PHP_PG_VERSION 7
/* php_pgsql_get_link_info */
static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
{
pgsql_link_handle *link;
zval *pgsql_link = NULL;
PGconn *pgsql;
char *result;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
RETURN_THROWS();
}
if (!pgsql_link) {
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
}
pgsql = link->conn;
switch(entry_type) {
case PHP_PG_DBNAME:
result = PQdb(pgsql);
break;
case PHP_PG_ERROR_MESSAGE:
RETURN_STR(_php_pgsql_trim_message(PQerrorMessage(pgsql)));
case PHP_PG_OPTIONS:
result = PQoptions(pgsql);
break;
case PHP_PG_PORT:
result = PQport(pgsql);
break;
case PHP_PG_TTY:
result = PQtty(pgsql);
break;
case PHP_PG_HOST:
result = PQhost(pgsql);
break;
case PHP_PG_VERSION:
array_init(return_value);
add_assoc_string(return_value, "client", pgsql_libpq_version);
add_assoc_long(return_value, "protocol", PQprotocolVersion(pgsql));
if (PQprotocolVersion(pgsql) >= 3) {
/* 8.0 or grater supports protorol version 3 */
char *tmp;
add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version"));
#define PHP_PQ_COPY_PARAM(_x) tmp = (char*)PQparameterStatus(pgsql, _x); \
if(tmp) add_assoc_string(return_value, _x, tmp); \
else add_assoc_null(return_value, _x);
PHP_PQ_COPY_PARAM("server_encoding");
PHP_PQ_COPY_PARAM("client_encoding");
PHP_PQ_COPY_PARAM("is_superuser");
PHP_PQ_COPY_PARAM("session_authorization");
PHP_PQ_COPY_PARAM("DateStyle");
PHP_PQ_COPY_PARAM("IntervalStyle");
PHP_PQ_COPY_PARAM("TimeZone");
PHP_PQ_COPY_PARAM("integer_datetimes");
PHP_PQ_COPY_PARAM("standard_conforming_strings");
PHP_PQ_COPY_PARAM("application_name");
}
return;
EMPTY_SWITCH_DEFAULT_CASE()
}
if (result) {
RETURN_STRING(result);
} else {
RETURN_EMPTY_STRING();
}
}
/* Get the database name */
PHP_FUNCTION(pg_dbname)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_DBNAME);
}
/* Get the error message string */
PHP_FUNCTION(pg_last_error)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE);
}
/* Get the options associated with the connection */
PHP_FUNCTION(pg_options)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_OPTIONS);
}
/* Return the port number associated with the connection */
PHP_FUNCTION(pg_port)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_PORT);
}
/* Return the tty name associated with the connection */
PHP_FUNCTION(pg_tty)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_TTY);
}
/* Returns the host name associated with the connection */
PHP_FUNCTION(pg_host)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_HOST);
}
/* Returns an array with client, protocol and server version (when available) */
PHP_FUNCTION(pg_version)
{
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_VERSION);
}
/* Returns the value of a server parameter */
PHP_FUNCTION(pg_parameter_status)
{
zval *pgsql_link = NULL;
pgsql_link_handle *link;
PGconn *pgsql;
char *param;
size_t len;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, ¶m, &len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", ¶m, &len) == FAILURE) {
RETURN_THROWS();
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
}
pgsql = link->conn;
param = (char*)PQparameterStatus(pgsql, param);
if (param) {
RETURN_STRING(param);
} else {
RETURN_FALSE;
}
}
/* Ping database. If connection is bad, try to reconnect. */
PHP_FUNCTION(pg_ping)
{
zval *pgsql_link = NULL;
PGconn *pgsql;
PGresult *res;
pgsql_link_handle *link;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
RETURN_THROWS();
}
if (pgsql_link == NULL) {
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
}
pgsql = link->conn;
/* ping connection */
res = PQexec(pgsql, "SELECT 1;");
PQclear(res);
/* check status. */
if (PQstatus(pgsql) == CONNECTION_OK)
RETURN_TRUE;
/* reset connection if it's broken */
PQreset(pgsql);
if (PQstatus(pgsql) == CONNECTION_OK) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* Execute a query */
PHP_FUNCTION(pg_query)
{
zval *pgsql_link = NULL;
char *query;
size_t query_len;
int leftover = 0;
pgsql_link_handle *link;
PGconn *pgsql;
PGresult *pgsql_result;
ExecStatusType status;
if (ZEND_NUM_ARGS() == 1) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
RETURN_THROWS();
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &query, &query_len) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
}
pgsql = link->conn;
if (PQsetnonblocking(pgsql, 0)) {
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
RETURN_FALSE;
}
while ((pgsql_result = PQgetResult(pgsql))) {
PQclear(pgsql_result);
leftover = 1;
}
if (leftover) {
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");