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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 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: |
| http://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: Edin Kadribasic <edink@emini.dk> |
| Ilia Alshanestsky <ilia@prohost.org> |
| Wez Furlong <wez@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "main/php_network.h"
#include "pdo/php_pdo.h"
#include "pdo/php_pdo_driver.h"
#include "pdo/php_pdo_error.h"
#include "ext/standard/file.h"
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include "pg_config.h" /* needed for PG_VERSION */
#include "php_pdo_pgsql.h"
#include "php_pdo_pgsql_int.h"
#include "zend_exceptions.h"
static char * _pdo_pgsql_trim_message(const char *message, int persistent)
{
register int i = strlen(message)-1;
char *tmp;
if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
--i;
}
while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
--i;
}
++i;
tmp = pemalloc(i + 1, persistent);
memcpy(tmp, message, i);
tmp[i] = '\0';
return tmp;
}
int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *msg, const char *file, int line TSRMLS_DC) /* {{{ */
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
pdo_pgsql_error_info *einfo = &H->einfo;
char *errmsg = PQerrorMessage(H->server);
einfo->errcode = errcode;
einfo->file = file;
einfo->line = line;
if (einfo->errmsg) {
pefree(einfo->errmsg, dbh->is_persistent);
einfo->errmsg = NULL;
}
if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
strcpy(*pdo_err, "HY000");
}
else {
strcpy(*pdo_err, sqlstate);
}
if (msg) {
einfo->errmsg = estrdup(msg);
}
else if (errmsg) {
einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
}
if (!dbh->methods) {
zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
*pdo_err, einfo->errcode, einfo->errmsg);
}
return errcode;
}
/* }}} */
static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
{
/* pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
}
/* }}} */
static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
pdo_pgsql_error_info *einfo = &H->einfo;
if (einfo->errcode) {
add_next_index_long(info, einfo->errcode);
add_next_index_string(info, einfo->errmsg, 1);
}
return 1;
}
/* }}} */
/* {{{ pdo_pgsql_create_lob_stream */
static size_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
return lo_write(self->conn, self->lfd, (char*)buf, count);
}
static size_t pgsql_lob_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
return lo_read(self->conn, self->lfd, buf, count);
}
static int pgsql_lob_close(php_stream *stream, int close_handle TSRMLS_DC)
{
struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
pdo_dbh_t *dbh = self->dbh;
if (close_handle) {
lo_close(self->conn, self->lfd);
}
efree(self);
php_pdo_dbh_delref(dbh TSRMLS_CC);
return 0;
}
static int pgsql_lob_flush(php_stream *stream TSRMLS_DC)
{
return 0;
}
static int pgsql_lob_seek(php_stream *stream, off_t offset, int whence,
off_t *newoffset TSRMLS_DC)
{
struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
int pos = lo_lseek(self->conn, self->lfd, offset, whence);
*newoffset = pos;
return pos >= 0 ? 0 : -1;
}
php_stream_ops pdo_pgsql_lob_stream_ops = {
pgsql_lob_write,
pgsql_lob_read,
pgsql_lob_close,
pgsql_lob_flush,
"pdo_pgsql lob stream",
pgsql_lob_seek,
NULL,
NULL,
NULL
};
php_stream *pdo_pgsql_create_lob_stream(pdo_dbh_t *dbh, int lfd, Oid oid TSRMLS_DC)
{
php_stream *stm;
struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
self->dbh = dbh;
self->lfd = lfd;
self->oid = oid;
self->conn = H->server;
stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
if (stm) {
php_pdo_dbh_addref(dbh TSRMLS_CC);
return stm;
}
efree(self);
return NULL;
}
/* }}} */
static int pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
if (H) {
if (H->server) {
PQfinish(H->server);
H->server = NULL;
}
if (H->einfo.errmsg) {
pefree(H->einfo.errmsg, dbh->is_persistent);
H->einfo.errmsg = NULL;
}
pefree(H, dbh->is_persistent);
dbh->driver_data = NULL;
}
return 0;
}
/* }}} */
static int pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
int scrollable;
#if HAVE_PQPREPARE
int ret;
char *nsql = NULL;
int nsql_len = 0;
int emulate = 0;
#endif
S->H = H;
stmt->driver_data = S;
stmt->methods = &pgsql_stmt_methods;
scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL;
if (scrollable) {
if (S->cursor_name) {
efree(S->cursor_name);
}
spprintf(&S->cursor_name, 0, "pdo_crsr_%08x", ++H->stmt_counter);
#if HAVE_PQPREPARE
emulate = 1;
#endif
}
#if HAVE_PQPREPARE
else if (driver_options) {
if (pdo_attr_lval(driver_options, PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, H->disable_native_prepares TSRMLS_CC) == 1 ||
pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepares TSRMLS_CC) == 1) {
emulate = 1;
}
} else {
emulate = H->disable_native_prepares || H->emulate_prepares;
}
if (!emulate && PQprotocolVersion(H->server) > 2) {
stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
stmt->named_rewrite_template = "$%d";
ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
if (ret == 1) {
/* query was re-written */
sql = nsql;
} else if (ret == -1) {
/* couldn't grok it */
strcpy(dbh->error_code, stmt->error_code);
return 0;
}
spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
/* that's all for now; we'll defer the actual prepare until the first execute call */
if (nsql) {
S->query = nsql;
} else {
S->query = estrdup(sql);
}
return 1;
}
#endif
stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
return 1;
}
static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
PGresult *res;
long ret = 1;
ExecStatusType qs;
if (!(res = PQexec(H->server, sql))) {
/* fatal error */
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
return -1;
}
qs = PQresultStatus(res);
if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
PQclear(res);
return -1;
}
H->pgoid = PQoidValue(res);
ret = (qs == PGRES_COMMAND_OK) ? atol(PQcmdTuples(res)) : 0L;
PQclear(res);
return ret;
}
static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC)
{
unsigned char *escaped;
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
size_t tmp_len;
switch (paramtype) {
case PDO_PARAM_LOB:
/* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
#ifdef HAVE_PQESCAPE_BYTEA_CONN
escaped = PQescapeByteaConn(H->server, (unsigned char *)unquoted, (size_t)unquotedlen, &tmp_len);
#else
escaped = PQescapeBytea((unsigned char *)unquoted, (size_t)unquotedlen, &tmp_len);
#endif
*quotedlen = (int)tmp_len + 1;
*quoted = emalloc(*quotedlen + 1);
memcpy((*quoted)+1, escaped, *quotedlen-2);
(*quoted)[0] = '\'';
(*quoted)[*quotedlen-1] = '\'';
(*quoted)[*quotedlen] = '\0';
PQfreemem(escaped);
break;
default:
*quoted = safe_emalloc(2, unquotedlen, 3);
(*quoted)[0] = '\'';
#ifndef HAVE_PQESCAPE_CONN
*quotedlen = PQescapeString(*quoted + 1, unquoted, (size_t)unquotedlen);
#else
*quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, (size_t)unquotedlen, NULL);
#endif
(*quoted)[*quotedlen + 1] = '\'';
(*quoted)[*quotedlen + 2] = '\0';
*quotedlen += 2;
}
return 1;
}
static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned int *len TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
char *id = NULL;
if (name == NULL) {
if (H->pgoid == InvalidOid) {
return NULL;
}
*len = spprintf(&id, 0, "%ld", (long) H->pgoid);
} else {
PGresult *res;
ExecStatusType status;
const char *q[1];
q[0] = name;
res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
status = PQresultStatus(res);
if (res && (status == PGRES_TUPLES_OK)) {
id = estrdup((char *)PQgetvalue(res, 0, 0));
*len = PQgetlength(res, 0, 0);
} else {
pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
}
if (res) {
PQclear(res);
}
}
return id;
}
static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
switch (attr) {
case PDO_ATTR_CLIENT_VERSION:
ZVAL_STRING(return_value, PG_VERSION, 1);
break;
case PDO_ATTR_SERVER_VERSION:
if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1);
} else /* emulate above via a query */
{
PGresult *res = PQexec(H->server, "SELECT VERSION()");
if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1);
}
if (res) {
PQclear(res);
}
}
break;
case PDO_ATTR_CONNECTION_STATUS:
switch (PQstatus(H->server)) {
case CONNECTION_STARTED:
ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1);
break;
case CONNECTION_MADE:
case CONNECTION_OK:
ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1);
break;
case CONNECTION_AWAITING_RESPONSE:
ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1);
break;
case CONNECTION_AUTH_OK:
ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1, 1);
break;
#ifdef CONNECTION_SSL_STARTUP
case CONNECTION_SSL_STARTUP:
ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1);
break;
#endif
case CONNECTION_SETENV:
ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1);
break;
case CONNECTION_BAD:
default:
ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1);
break;
}
break;
case PDO_ATTR_SERVER_INFO: {
int spid = PQbackendPID(H->server);
char *tmp;
spprintf(&tmp, 0,
"PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
spid,
(char*)PQparameterStatus(H->server, "client_encoding"),
(char*)PQparameterStatus(H->server, "is_superuser"),
(char*)PQparameterStatus(H->server, "session_authorization"),
(char*)PQparameterStatus(H->server, "DateStyle"));
ZVAL_STRING(return_value, tmp, 0);
}
break;
default:
return 0;
}
return 1;
}
/* {{{ */
static int pdo_pgsql_check_liveness(pdo_dbh_t *dbh TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
if (PQstatus(H->server) == CONNECTION_BAD) {
PQreset(H->server);
}
return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
}
/* }}} */
static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
PGresult *res;
int ret = 1;
res = PQexec(H->server, cmd);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
ret = 0;
}
PQclear(res);
return ret;
}
static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
{
return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC);
}
static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
{
return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC);
}
static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
{
return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC);
}
static int pgsql_handle_in_transaction(pdo_dbh_t *dbh TSRMLS_DC)
{
pdo_pgsql_db_handle *H;
H = (pdo_pgsql_db_handle *)dbh->driver_data;
return PQtransactionStatus(H->server);
}
/* {{{ proto string PDO::pgsqlCopyFromArray(string $table_name , array $rows [, string $delimiter [, string $null_as ] [, string $fields])
Returns true if the copy worked fine or false if error */
static PHP_METHOD(PDO, pgsqlCopyFromArray)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
zval *pg_rows;
char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
char *query;
PGresult *pgsql_result;
ExecStatusType status;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/a|sss",
&table_name, &table_name_len, &pg_rows,
&pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
return;
}
if (!zend_hash_num_elements(Z_ARRVAL_P(pg_rows))) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot copy from an empty array");
RETURN_FALSE;
}
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
/* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
if (pg_fields) {
spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
} else {
spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
}
/* Obtain db Handle */
H = (pdo_pgsql_db_handle *)dbh->driver_data;
while ((pgsql_result = PQgetResult(H->server))) {
PQclear(pgsql_result);
}
pgsql_result = PQexec(H->server, query);
efree(query);
query = NULL;
if (pgsql_result) {
status = PQresultStatus(pgsql_result);
} else {
status = (ExecStatusType) PQstatus(H->server);
}
if (status == PGRES_COPY_IN && pgsql_result) {
int command_failed = 0;
int buffer_len = 0;
zval **tmp;
HashPosition pos;
PQclear(pgsql_result);
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
int query_len;
convert_to_string_ex(tmp);
if (buffer_len < Z_STRLEN_PP(tmp)) {
buffer_len = Z_STRLEN_PP(tmp);
query = erealloc(query, buffer_len + 2); /* room for \n\0 */
}
memcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
query_len = Z_STRLEN_PP(tmp);
if (query[query_len - 1] != '\n') {
query[query_len++] = '\n';
}
query[query_len] = '\0';
if (PQputCopyData(H->server, query, query_len) != 1) {
efree(query);
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
}
if (query) {
efree(query);
}
if (PQputCopyEnd(H->server, NULL) != 1) {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
while ((pgsql_result = PQgetResult(H->server))) {
if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
command_failed = 1;
}
PQclear(pgsql_result);
}
PDO_HANDLE_DBH_ERR();
RETURN_BOOL(!command_failed);
} else {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
PQclear(pgsql_result);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string PDO::pgsqlCopyFromFile(string $table_name , string $filename [, string $delimiter [, string $null_as ] [, string $fields])
Returns true if the copy worked fine or false if error */
static PHP_METHOD(PDO, pgsqlCopyFromFile)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
int table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
char *query;
PGresult *pgsql_result;
ExecStatusType status;
php_stream *stream;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sp|sss",
&table_name, &table_name_len, &filename, &filename_len,
&pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
return;
}
/* Obtain db Handler */
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
stream = php_stream_open_wrapper_ex(filename, "rb", ENFORCE_SAFE_MODE, NULL, FG(default_context));
if (!stream) {
pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
if (pg_fields) {
spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
} else {
spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
}
H = (pdo_pgsql_db_handle *)dbh->driver_data;
while ((pgsql_result = PQgetResult(H->server))) {
PQclear(pgsql_result);
}
pgsql_result = PQexec(H->server, query);
efree(query);
if (pgsql_result) {
status = PQresultStatus(pgsql_result);
} else {
status = (ExecStatusType) PQstatus(H->server);
}
if (status == PGRES_COPY_IN && pgsql_result) {
char *buf;
int command_failed = 0;
size_t line_len = 0;
PQclear(pgsql_result);
while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
if (PQputCopyData(H->server, buf, line_len) != 1) {
efree(buf);
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
php_stream_close(stream);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
efree(buf);
}
php_stream_close(stream);
if (PQputCopyEnd(H->server, NULL) != 1) {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
while ((pgsql_result = PQgetResult(H->server))) {
if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
command_failed = 1;
}
PQclear(pgsql_result);
}
PDO_HANDLE_DBH_ERR();
RETURN_BOOL(!command_failed);
} else {
php_stream_close(stream);
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
PQclear(pgsql_result);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string PDO::pgsqlCopyToFile(string $table_name , $filename, [string $delimiter [, string $null_as [, string $fields]]])
Returns true if the copy worked fine or false if error */
static PHP_METHOD(PDO, pgsqlCopyToFile)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
char *query;
PGresult *pgsql_result;
ExecStatusType status;
php_stream *stream;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sp|sss",
&table_name, &table_name_len, &filename, &filename_len,
&pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
return;
}
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
H = (pdo_pgsql_db_handle *)dbh->driver_data;
stream = php_stream_open_wrapper_ex(filename, "wb", ENFORCE_SAFE_MODE, NULL, FG(default_context));
if (!stream) {
pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
while ((pgsql_result = PQgetResult(H->server))) {
PQclear(pgsql_result);
}
/* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
if (pg_fields) {
spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
} else {
spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
}
pgsql_result = PQexec(H->server, query);
efree(query);
if (pgsql_result) {
status = PQresultStatus(pgsql_result);
} else {
status = (ExecStatusType) PQstatus(H->server);
}
if (status == PGRES_COPY_OUT && pgsql_result) {
PQclear(pgsql_result);
while (1) {
char *csv = NULL;
int ret = PQgetCopyData(H->server, &csv, 0);
if (ret == -1) {
break; /* done */
} else if (ret > 0) {
if (php_stream_write(stream, csv, ret) != ret) {
pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
PQfreemem(csv);
php_stream_close(stream);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
} else {
PQfreemem(csv);
}
} else {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
php_stream_close(stream);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
}
php_stream_close(stream);
while ((pgsql_result = PQgetResult(H->server))) {
PQclear(pgsql_result);
}
RETURN_TRUE;
} else {
php_stream_close(stream);
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
PQclear(pgsql_result);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string PDO::pgsqlCopyToArray(string $table_name , [string $delimiter [, string $null_as [, string $fields]]])
Returns true if the copy worked fine or false if error */
static PHP_METHOD(PDO, pgsqlCopyToArray)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
char *query;
PGresult *pgsql_result;
ExecStatusType status;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sss",
&table_name, &table_name_len,
&pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
return;
}
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
H = (pdo_pgsql_db_handle *)dbh->driver_data;
while ((pgsql_result = PQgetResult(H->server))) {
PQclear(pgsql_result);
}
/* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
if (pg_fields) {
spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
} else {
spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
}
pgsql_result = PQexec(H->server, query);
efree(query);
if (pgsql_result) {
status = PQresultStatus(pgsql_result);
} else {
status = (ExecStatusType) PQstatus(H->server);
}
if (status == PGRES_COPY_OUT && pgsql_result) {
PQclear(pgsql_result);
array_init(return_value);
while (1) {
char *csv = NULL;
int ret = PQgetCopyData(H->server, &csv, 0);
if (ret == -1) {
break; /* copy done */
} else if (ret > 0) {
add_next_index_stringl(return_value, csv, ret, 1);
PQfreemem(csv);
} else {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
}
while ((pgsql_result = PQgetResult(H->server))) {
PQclear(pgsql_result);
}
} else {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
PQclear(pgsql_result);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string PDO::pgsqlLOBCreate()
Creates a new large object, returning its identifier. Must be called inside a transaction. */
static PHP_METHOD(PDO, pgsqlLOBCreate)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
Oid lfd;
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
H = (pdo_pgsql_db_handle *)dbh->driver_data;
lfd = lo_creat(H->server, INV_READ|INV_WRITE);
if (lfd != InvalidOid) {
char *buf;
spprintf(&buf, 0, "%lu", (long) lfd);
RETURN_STRING(buf, 0);
}
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* }}} */
/* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
Opens an existing large object stream. Must be called inside a transaction. */
static PHP_METHOD(PDO, pgsqlLOBOpen)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
Oid oid;
int lfd;
char *oidstr;
int oidstrlen;
char *modestr = "rb";
int modestrlen;
int mode = INV_READ;
char *end_ptr;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
&oidstr, &oidstrlen, &modestr, &modestrlen)) {
RETURN_FALSE;
}
oid = (Oid)strtoul(oidstr, &end_ptr, 10);
if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
RETURN_FALSE;
}
if (strpbrk(modestr, "+w")) {
mode = INV_READ|INV_WRITE;
}
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
H = (pdo_pgsql_db_handle *)dbh->driver_data;
lfd = lo_open(H->server, oid, mode);
if (lfd >= 0) {
php_stream *stream = pdo_pgsql_create_lob_stream(dbh, lfd, oid TSRMLS_CC);
if (stream) {
php_stream_to_zval(stream, return_value);
return;
}
} else {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
}
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
Deletes the large object identified by oid. Must be called inside a transaction. */
static PHP_METHOD(PDO, pgsqlLOBUnlink)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
Oid oid;
char *oidstr, *end_ptr;
int oidlen;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&oidstr, &oidlen)) {
RETURN_FALSE;
}
oid = (Oid)strtoul(oidstr, &end_ptr, 10);
if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
RETURN_FALSE;
}
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
PDO_DBH_CLEAR_ERR();
H = (pdo_pgsql_db_handle *)dbh->driver_data;
if (1 == lo_unlink(H->server, oid)) {
RETURN_TRUE;
}
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* }}} */
/* {{{ proto mixed PDO::pgsqlGetNotify([ int $result_type = PDO::FETCH_USE_DEFAULT] [, int $ms_timeout = 0 ]])
Get asyncronous notification */
static PHP_METHOD(PDO, pgsqlGetNotify)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
long result_type = PDO_FETCH_USE_DEFAULT;
long ms_timeout = 0;
PGnotify *pgsql_notify;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll",
&result_type, &ms_timeout)) {
RETURN_FALSE;
}
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
if (result_type == PDO_FETCH_USE_DEFAULT) {
result_type = dbh->default_fetch_type;
}
if (result_type != PDO_FETCH_BOTH && result_type != PDO_FETCH_ASSOC && result_type != PDO_FETCH_NUM) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type");
RETURN_FALSE;
}
if (ms_timeout < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid timeout");
RETURN_FALSE;
}
H = (pdo_pgsql_db_handle *)dbh->driver_data;
PQconsumeInput(H->server);
pgsql_notify = PQnotifies(H->server);
if (ms_timeout && !pgsql_notify) {
php_pollfd_for_ms(PQsocket(H->server), PHP_POLLREADABLE, ms_timeout);
PQconsumeInput(H->server);
pgsql_notify = PQnotifies(H->server);
}
if (!pgsql_notify) {
RETURN_FALSE;
}
array_init(return_value);
if (result_type == PDO_FETCH_NUM || result_type == PDO_FETCH_BOTH) {
add_index_string(return_value, 0, pgsql_notify->relname, 1);
add_index_long(return_value, 1, pgsql_notify->be_pid);
}
if (result_type == PDO_FETCH_ASSOC || result_type == PDO_FETCH_BOTH) {
add_assoc_string(return_value, "message", pgsql_notify->relname, 1);
add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
}
PQfreemem(pgsql_notify);
}
/* }}} */
/* {{{ proto int PDO::pgsqlGetPid()
Get backend(server) pid */
static PHP_METHOD(PDO, pgsqlGetPid)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;
H = (pdo_pgsql_db_handle *)dbh->driver_data;
RETURN_LONG(PQbackendPID(H->server));
}
/* }}} */
static const zend_function_entry dbh_methods[] = {
PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlCopyFromArray, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlGetNotify, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlGetPid, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
{
switch (kind) {
case PDO_DBH_DRIVER_METHOD_KIND_DBH:
return dbh_methods;
default:
return NULL;
}
}
static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
switch (attr) {
#if HAVE_PQPREPARE
case PDO_ATTR_EMULATE_PREPARES:
H->emulate_prepares = Z_LVAL_P(val);
return 1;
case PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT:
H->disable_native_prepares = Z_LVAL_P(val);
return 1;
#endif
default:
return 0;
}
}
static struct pdo_dbh_methods pgsql_methods = {
pgsql_handle_closer,
pgsql_handle_preparer,
pgsql_handle_doer,
pgsql_handle_quoter,
pgsql_handle_begin,
pgsql_handle_commit,
pgsql_handle_rollback,
pdo_pgsql_set_attr,
pdo_pgsql_last_insert_id,
pdo_pgsql_fetch_error_func,
pdo_pgsql_get_attribute,
pdo_pgsql_check_liveness, /* check_liveness */
pdo_pgsql_get_driver_methods, /* get_driver_methods */
NULL,
pgsql_handle_in_transaction,
};
static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
{
pdo_pgsql_db_handle *H;
int ret = 0;
char *conn_str, *p, *e;
long connect_timeout = 30;
H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
dbh->driver_data = H;
H->einfo.errcode = 0;
H->einfo.errmsg = NULL;
/* PostgreSQL wants params in the connect string to be separated by spaces,
* if the PDO standard semicolons are used, we convert them to spaces
*/
e = (char *) dbh->data_source + strlen(dbh->data_source);
p = (char *) dbh->data_source;
while ((p = memchr(p, ';', (e - p)))) {
*p = ' ';
}
if (driver_options) {
connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC);
}
/* support both full connection string & connection string + login and/or password */
if (dbh->username && dbh->password) {
spprintf(&conn_str, 0, "%s user=%s password=%s connect_timeout=%ld", dbh->data_source, dbh->username, dbh->password, connect_timeout);
} else if (dbh->username) {
spprintf(&conn_str, 0, "%s user=%s connect_timeout=%ld", dbh->data_source, dbh->username, connect_timeout);
} else if (dbh->password) {
spprintf(&conn_str, 0, "%s password=%s connect_timeout=%ld", dbh->data_source, dbh->password, connect_timeout);
} else {
spprintf(&conn_str, 0, "%s connect_timeout=%ld", (char *) dbh->data_source, connect_timeout);
}
H->server = PQconnectdb(conn_str);
efree(conn_str);
if (PQstatus(H->server) != CONNECTION_OK) {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
goto cleanup;
}
PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
H->attached = 1;
H->pgoid = -1;
dbh->methods = &pgsql_methods;
dbh->alloc_own_columns = 1;
dbh->max_escaped_char_length = 2;
ret = 1;
cleanup:
dbh->methods = &pgsql_methods;
if (!ret) {
pgsql_handle_closer(dbh TSRMLS_CC);
}
return ret;
}
/* }}} */
pdo_driver_t pdo_pgsql_driver = {
PDO_DRIVER_HEADER(pgsql),
pdo_pgsql_handle_factory
};
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
|