1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Edin Kadribasic <edink@emini.dk> |
14 | Ilia Alshanestsky <ilia@prohost.org> |
15 | Wez Furlong <wez@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "ext/standard/php_string.h"
27 #include "main/php_network.h"
28 #include "pdo/php_pdo.h"
29 #include "pdo/php_pdo_driver.h"
30 #include "pdo/php_pdo_error.h"
31 #include "ext/standard/file.h"
32 #undef SIZEOF_OFF_T
33 #include "php_pdo_pgsql.h"
34 #include "php_pdo_pgsql_int.h"
35 #include "zend_exceptions.h"
36 #include "pgsql_driver_arginfo.h"
37
38 static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh);
39
_pdo_pgsql_trim_message(const char * message,int persistent)40 static char * _pdo_pgsql_trim_message(const char *message, int persistent)
41 {
42 size_t i = strlen(message)-1;
43 char *tmp;
44
45 if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
46 --i;
47 }
48 while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
49 --i;
50 }
51 ++i;
52 tmp = pemalloc(i + 1, persistent);
53 memcpy(tmp, message, i);
54 tmp[i] = '\0';
55
56 return tmp;
57 }
58
_pdo_pgsql_escape_credentials(char * str)59 static zend_string* _pdo_pgsql_escape_credentials(char *str)
60 {
61 if (str) {
62 return php_addcslashes_str(str, strlen(str), "\\'", sizeof("\\'"));
63 }
64
65 return NULL;
66 }
67
_pdo_pgsql_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,int errcode,const char * sqlstate,const char * msg,const char * file,int line)68 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) /* {{{ */
69 {
70 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
71 pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
72 pdo_pgsql_error_info *einfo = &H->einfo;
73 char *errmsg = PQerrorMessage(H->server);
74
75 einfo->errcode = errcode;
76 einfo->file = file;
77 einfo->line = line;
78
79 if (einfo->errmsg) {
80 pefree(einfo->errmsg, dbh->is_persistent);
81 einfo->errmsg = NULL;
82 }
83
84 if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
85 strcpy(*pdo_err, "HY000");
86 }
87 else {
88 strcpy(*pdo_err, sqlstate);
89 }
90
91 if (msg) {
92 einfo->errmsg = pestrdup(msg, dbh->is_persistent);
93 }
94 else if (errmsg) {
95 einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
96 }
97
98 if (!dbh->methods) {
99 pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
100 }
101
102 return errcode;
103 }
104 /* }}} */
105
_pdo_pgsql_notice(pdo_dbh_t * dbh,const char * message)106 static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
107 {
108 /* pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
109 }
110 /* }}} */
111
pdo_pgsql_fetch_error_func(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)112 static void pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
113 {
114 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
115 pdo_pgsql_error_info *einfo = &H->einfo;
116
117 if (einfo->errcode) {
118 add_next_index_long(info, einfo->errcode);
119 } else {
120 /* Add null to respect expected info array structure */
121 add_next_index_null(info);
122 }
123 if (einfo->errmsg) {
124 add_next_index_string(info, einfo->errmsg);
125 }
126 }
127 /* }}} */
128
129 /* {{{ pdo_pgsql_create_lob_stream */
pgsql_lob_write(php_stream * stream,const char * buf,size_t count)130 static ssize_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count)
131 {
132 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
133 return lo_write(self->conn, self->lfd, (char*)buf, count);
134 }
135
pgsql_lob_read(php_stream * stream,char * buf,size_t count)136 static ssize_t pgsql_lob_read(php_stream *stream, char *buf, size_t count)
137 {
138 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
139 return lo_read(self->conn, self->lfd, buf, count);
140 }
141
pgsql_lob_close(php_stream * stream,int close_handle)142 static int pgsql_lob_close(php_stream *stream, int close_handle)
143 {
144 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
145 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(&self->dbh))->driver_data;
146
147 if (close_handle) {
148 lo_close(self->conn, self->lfd);
149 }
150 zend_hash_index_del(H->lob_streams, php_stream_get_resource_id(stream));
151 zval_ptr_dtor(&self->dbh);
152 efree(self);
153 return 0;
154 }
155
pgsql_lob_flush(php_stream * stream)156 static int pgsql_lob_flush(php_stream *stream)
157 {
158 return 0;
159 }
160
pgsql_lob_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffset)161 static int pgsql_lob_seek(php_stream *stream, zend_off_t offset, int whence,
162 zend_off_t *newoffset)
163 {
164 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
165 #if defined(HAVE_PG_LO64) && defined(ZEND_ENABLE_ZVAL_LONG64)
166 zend_off_t pos = lo_lseek64(self->conn, self->lfd, offset, whence);
167 #else
168 zend_off_t pos = lo_lseek(self->conn, self->lfd, offset, whence);
169 #endif
170 *newoffset = pos;
171 return pos >= 0 ? 0 : -1;
172 }
173
174 const php_stream_ops pdo_pgsql_lob_stream_ops = {
175 pgsql_lob_write,
176 pgsql_lob_read,
177 pgsql_lob_close,
178 pgsql_lob_flush,
179 "pdo_pgsql lob stream",
180 pgsql_lob_seek,
181 NULL,
182 NULL,
183 NULL
184 };
185
pdo_pgsql_create_lob_stream(zval * dbh,int lfd,Oid oid)186 php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid)
187 {
188 php_stream *stm;
189 struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
190 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data;
191
192 ZVAL_COPY_VALUE(&self->dbh, dbh);
193 self->lfd = lfd;
194 self->oid = oid;
195 self->conn = H->server;
196
197 stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
198
199 if (stm) {
200 Z_ADDREF_P(dbh);
201 zend_hash_index_add_ptr(H->lob_streams, php_stream_get_resource_id(stm), stm->res);
202 return stm;
203 }
204
205 efree(self);
206 return NULL;
207 }
208 /* }}} */
209
pdo_pgsql_close_lob_streams(pdo_dbh_t * dbh)210 void pdo_pgsql_close_lob_streams(pdo_dbh_t *dbh)
211 {
212 zend_resource *res;
213 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
214 if (H->lob_streams) {
215 ZEND_HASH_REVERSE_FOREACH_PTR(H->lob_streams, res) {
216 if (res->type >= 0) {
217 zend_list_close(res);
218 }
219 } ZEND_HASH_FOREACH_END();
220 }
221 }
222
pgsql_handle_closer(pdo_dbh_t * dbh)223 static void pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
224 {
225 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
226 if (H) {
227 if (H->lob_streams) {
228 pdo_pgsql_close_lob_streams(dbh);
229 zend_hash_destroy(H->lob_streams);
230 pefree(H->lob_streams, dbh->is_persistent);
231 H->lob_streams = NULL;
232 }
233 if (H->server) {
234 PQfinish(H->server);
235 H->server = NULL;
236 }
237 if (H->einfo.errmsg) {
238 pefree(H->einfo.errmsg, dbh->is_persistent);
239 H->einfo.errmsg = NULL;
240 }
241 pefree(H, dbh->is_persistent);
242 dbh->driver_data = NULL;
243 }
244 }
245 /* }}} */
246
pgsql_handle_preparer(pdo_dbh_t * dbh,zend_string * sql,pdo_stmt_t * stmt,zval * driver_options)247 static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
248 {
249 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
250 pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
251 int scrollable;
252 int ret;
253 zend_string *nsql = NULL;
254 int emulate = 0;
255 int execute_only = 0;
256
257 S->H = H;
258 stmt->driver_data = S;
259 stmt->methods = &pgsql_stmt_methods;
260
261 scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
262 PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL;
263
264 if (scrollable) {
265 if (S->cursor_name) {
266 efree(S->cursor_name);
267 }
268 spprintf(&S->cursor_name, 0, "pdo_crsr_%08x", ++H->stmt_counter);
269 emulate = 1;
270 } else if (driver_options) {
271 if (pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepares) == 1) {
272 emulate = 1;
273 }
274 if (pdo_attr_lval(driver_options, PDO_PGSQL_ATTR_DISABLE_PREPARES, H->disable_prepares) == 1) {
275 execute_only = 1;
276 }
277 } else {
278 emulate = H->disable_native_prepares || H->emulate_prepares;
279 execute_only = H->disable_prepares;
280 }
281
282 if (!emulate && PQprotocolVersion(H->server) <= 2) {
283 emulate = 1;
284 }
285
286 if (emulate) {
287 stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
288 } else {
289 stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
290 stmt->named_rewrite_template = "$%d";
291 }
292
293 ret = pdo_parse_params(stmt, sql, &nsql);
294
295 if (ret == -1) {
296 /* couldn't grok it */
297 strcpy(dbh->error_code, stmt->error_code);
298 return false;
299 } else if (ret == 1) {
300 /* query was re-written */
301 S->query = nsql;
302 } else {
303 S->query = zend_string_copy(sql);
304 }
305
306 if (!emulate && !execute_only) {
307 /* prepared query: set the query name and defer the
308 actual prepare until the first execute call */
309 spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
310 }
311
312 return true;
313 }
314
pgsql_handle_doer(pdo_dbh_t * dbh,const zend_string * sql)315 static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
316 {
317 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
318 PGresult *res;
319 zend_long ret = 1;
320 ExecStatusType qs;
321
322 bool in_trans = pgsql_handle_in_transaction(dbh);
323
324 if (!(res = PQexec(H->server, ZSTR_VAL(sql)))) {
325 /* fatal error */
326 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
327 return -1;
328 }
329 qs = PQresultStatus(res);
330 if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
331 pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
332 PQclear(res);
333 return -1;
334 }
335 H->pgoid = PQoidValue(res);
336 if (qs == PGRES_COMMAND_OK) {
337 ret = ZEND_ATOL(PQcmdTuples(res));
338 } else {
339 ret = Z_L(0);
340 }
341 PQclear(res);
342 if (in_trans && !pgsql_handle_in_transaction(dbh)) {
343 pdo_pgsql_close_lob_streams(dbh);
344 }
345
346 return ret;
347 }
348
pgsql_handle_quoter(pdo_dbh_t * dbh,const zend_string * unquoted,enum pdo_param_type paramtype)349 static zend_string* pgsql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
350 {
351 unsigned char *escaped;
352 char *quoted;
353 size_t quotedlen;
354 zend_string *quoted_str;
355 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
356 size_t tmp_len;
357
358 switch (paramtype) {
359 case PDO_PARAM_LOB:
360 /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
361 escaped = PQescapeByteaConn(H->server, (unsigned char *)ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), &tmp_len);
362 quotedlen = tmp_len + 1;
363 quoted = emalloc(quotedlen + 1);
364 memcpy(quoted+1, escaped, quotedlen-2);
365 quoted[0] = '\'';
366 quoted[quotedlen-1] = '\'';
367 quoted[quotedlen] = '\0';
368 PQfreemem(escaped);
369 break;
370 default:
371 quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3);
372 quoted[0] = '\'';
373 quotedlen = PQescapeStringConn(H->server, quoted + 1, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), NULL);
374 quoted[quotedlen + 1] = '\'';
375 quoted[quotedlen + 2] = '\0';
376 quotedlen += 2;
377 }
378
379 quoted_str = zend_string_init(quoted, quotedlen, 0);
380 efree(quoted);
381 return quoted_str;
382 }
383
pdo_pgsql_last_insert_id(pdo_dbh_t * dbh,const zend_string * name)384 static zend_string *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
385 {
386 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
387 zend_string *id = NULL;
388 PGresult *res;
389 ExecStatusType status;
390
391 if (name == NULL) {
392 res = PQexec(H->server, "SELECT LASTVAL()");
393 } else {
394 const char *q[1];
395 q[0] = ZSTR_VAL(name);
396
397 res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
398 }
399 status = PQresultStatus(res);
400
401 if (res && (status == PGRES_TUPLES_OK)) {
402 id = zend_string_init((char *)PQgetvalue(res, 0, 0), PQgetlength(res, 0, 0), 0);
403 } else {
404 pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
405 }
406
407 if (res) {
408 PQclear(res);
409 }
410
411 return id;
412 }
413
pdo_libpq_version(char * buf,size_t len)414 void pdo_libpq_version(char *buf, size_t len)
415 {
416 int version = PQlibVersion();
417 int major = version / 10000;
418 if (major >= 10) {
419 int minor = version % 10000;
420 snprintf(buf, len, "%d.%d", major, minor);
421 } else {
422 int minor = version / 100 % 100;
423 int revision = version % 100;
424 snprintf(buf, len, "%d.%d.%d", major, minor, revision);
425 }
426 }
427
pdo_pgsql_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)428 static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
429 {
430 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
431
432 switch (attr) {
433 case PDO_ATTR_EMULATE_PREPARES:
434 ZVAL_BOOL(return_value, H->emulate_prepares);
435 break;
436
437 case PDO_PGSQL_ATTR_DISABLE_PREPARES:
438 ZVAL_BOOL(return_value, H->disable_prepares);
439 break;
440
441 case PDO_ATTR_CLIENT_VERSION: {
442 char buf[16];
443 pdo_libpq_version(buf, sizeof(buf));
444 ZVAL_STRING(return_value, buf);
445 break;
446 }
447
448 case PDO_ATTR_SERVER_VERSION:
449 if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
450 ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"));
451 } else /* emulate above via a query */
452 {
453 PGresult *res = PQexec(H->server, "SELECT VERSION()");
454 if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
455 ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0));
456 }
457
458 if (res) {
459 PQclear(res);
460 }
461 }
462 break;
463
464 case PDO_ATTR_CONNECTION_STATUS:
465 switch (PQstatus(H->server)) {
466 case CONNECTION_STARTED:
467 ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1);
468 break;
469
470 case CONNECTION_MADE:
471 case CONNECTION_OK:
472 ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1);
473 break;
474
475 case CONNECTION_AWAITING_RESPONSE:
476 ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1);
477 break;
478
479 case CONNECTION_AUTH_OK:
480 ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1);
481 break;
482 #ifdef CONNECTION_SSL_STARTUP
483 case CONNECTION_SSL_STARTUP:
484 ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1);
485 break;
486 #endif
487 case CONNECTION_SETENV:
488 ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1);
489 break;
490
491 case CONNECTION_BAD:
492 default:
493 ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1);
494 break;
495 }
496 break;
497
498 case PDO_ATTR_SERVER_INFO: {
499 int spid = PQbackendPID(H->server);
500
501
502 zend_string *str_info =
503 strpprintf(0,
504 "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
505 spid,
506 (char*)PQparameterStatus(H->server, "client_encoding"),
507 (char*)PQparameterStatus(H->server, "is_superuser"),
508 (char*)PQparameterStatus(H->server, "session_authorization"),
509 (char*)PQparameterStatus(H->server, "DateStyle"));
510
511 ZVAL_STR(return_value, str_info);
512 break;
513 }
514
515 default:
516 return 0;
517 }
518
519 return 1;
520 }
521
522 /* {{{ */
pdo_pgsql_check_liveness(pdo_dbh_t * dbh)523 static zend_result pdo_pgsql_check_liveness(pdo_dbh_t *dbh)
524 {
525 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
526 if (!PQconsumeInput(H->server) || PQstatus(H->server) == CONNECTION_BAD) {
527 PQreset(H->server);
528 }
529 return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
530 }
531 /* }}} */
532
pgsql_handle_in_transaction(pdo_dbh_t * dbh)533 static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh)
534 {
535 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
536
537 return PQtransactionStatus(H->server) > PQTRANS_IDLE;
538 }
539
pdo_pgsql_transaction_cmd(const char * cmd,pdo_dbh_t * dbh)540 static bool pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
541 {
542 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
543 PGresult *res;
544 bool ret = true;
545
546 res = PQexec(H->server, cmd);
547
548 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
549 pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
550 ret = false;
551 }
552
553 PQclear(res);
554 return ret;
555 }
556
pgsql_handle_begin(pdo_dbh_t * dbh)557 static bool pgsql_handle_begin(pdo_dbh_t *dbh)
558 {
559 return pdo_pgsql_transaction_cmd("BEGIN", dbh);
560 }
561
pgsql_handle_commit(pdo_dbh_t * dbh)562 static bool pgsql_handle_commit(pdo_dbh_t *dbh)
563 {
564 bool ret = pdo_pgsql_transaction_cmd("COMMIT", dbh);
565
566 /* When deferred constraints are used the commit could
567 fail, and a ROLLBACK implicitly ran. See bug #67462 */
568 if (ret) {
569 pdo_pgsql_close_lob_streams(dbh);
570 } else {
571 dbh->in_txn = pgsql_handle_in_transaction(dbh);
572 }
573
574 return ret;
575 }
576
pgsql_handle_rollback(pdo_dbh_t * dbh)577 static bool pgsql_handle_rollback(pdo_dbh_t *dbh)
578 {
579 int ret = pdo_pgsql_transaction_cmd("ROLLBACK", dbh);
580
581 if (ret) {
582 pdo_pgsql_close_lob_streams(dbh);
583 }
584
585 return ret;
586 }
587
588 /* {{{ Returns true if the copy worked fine or false if error */
PHP_METHOD(PDO_PGSql_Ext,pgsqlCopyFromArray)589 PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyFromArray)
590 {
591 pdo_dbh_t *dbh;
592 pdo_pgsql_db_handle *H;
593
594 zval *pg_rows;
595
596 char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
597 size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
598 char *query;
599
600 PGresult *pgsql_result;
601 ExecStatusType status;
602
603 if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa|sss!",
604 &table_name, &table_name_len, &pg_rows,
605 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
606 RETURN_THROWS();
607 }
608
609 if (!zend_hash_num_elements(Z_ARRVAL_P(pg_rows))) {
610 zend_argument_value_error(2, "cannot be empty");
611 RETURN_THROWS();
612 }
613
614 dbh = Z_PDO_DBH_P(ZEND_THIS);
615 PDO_CONSTRUCT_CHECK;
616 PDO_DBH_CLEAR_ERR();
617
618 /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
619 if (pg_fields) {
620 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"));
621 } else {
622 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"));
623 }
624
625 /* Obtain db Handle */
626 H = (pdo_pgsql_db_handle *)dbh->driver_data;
627
628 while ((pgsql_result = PQgetResult(H->server))) {
629 PQclear(pgsql_result);
630 }
631 pgsql_result = PQexec(H->server, query);
632
633 efree(query);
634 query = NULL;
635
636 if (pgsql_result) {
637 status = PQresultStatus(pgsql_result);
638 } else {
639 status = (ExecStatusType) PQstatus(H->server);
640 }
641
642 if (status == PGRES_COPY_IN && pgsql_result) {
643 int command_failed = 0;
644 size_t buffer_len = 0;
645 zval *tmp;
646
647 PQclear(pgsql_result);
648 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) {
649 size_t query_len;
650 if (!try_convert_to_string(tmp)) {
651 efree(query);
652 RETURN_THROWS();
653 }
654
655 if (buffer_len < Z_STRLEN_P(tmp)) {
656 buffer_len = Z_STRLEN_P(tmp);
657 query = erealloc(query, buffer_len + 2); /* room for \n\0 */
658 }
659 memcpy(query, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
660 query_len = Z_STRLEN_P(tmp);
661 if (query[query_len - 1] != '\n') {
662 query[query_len++] = '\n';
663 }
664 query[query_len] = '\0';
665 if (PQputCopyData(H->server, query, query_len) != 1) {
666 efree(query);
667 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
668 PDO_HANDLE_DBH_ERR();
669 RETURN_FALSE;
670 }
671 } ZEND_HASH_FOREACH_END();
672 if (query) {
673 efree(query);
674 }
675
676 if (PQputCopyEnd(H->server, NULL) != 1) {
677 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
678 PDO_HANDLE_DBH_ERR();
679 RETURN_FALSE;
680 }
681
682 while ((pgsql_result = PQgetResult(H->server))) {
683 if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
684 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
685 command_failed = 1;
686 }
687 PQclear(pgsql_result);
688 }
689
690 PDO_HANDLE_DBH_ERR();
691 RETURN_BOOL(!command_failed);
692 } else {
693 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
694 PQclear(pgsql_result);
695 PDO_HANDLE_DBH_ERR();
696 RETURN_FALSE;
697 }
698 }
699 /* }}} */
700
701 /* {{{ Returns true if the copy worked fine or false if error */
PHP_METHOD(PDO_PGSql_Ext,pgsqlCopyFromFile)702 PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyFromFile)
703 {
704 pdo_dbh_t *dbh;
705 pdo_pgsql_db_handle *H;
706
707 char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
708 size_t table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
709 char *query;
710 PGresult *pgsql_result;
711 ExecStatusType status;
712 php_stream *stream;
713
714 if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss!",
715 &table_name, &table_name_len, &filename, &filename_len,
716 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
717 RETURN_THROWS();
718 }
719
720 /* Obtain db Handler */
721 dbh = Z_PDO_DBH_P(ZEND_THIS);
722 PDO_CONSTRUCT_CHECK;
723 PDO_DBH_CLEAR_ERR();
724
725 stream = php_stream_open_wrapper_ex(filename, "rb", 0, NULL, FG(default_context));
726 if (!stream) {
727 pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
728 PDO_HANDLE_DBH_ERR();
729 RETURN_FALSE;
730 }
731
732 /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
733 if (pg_fields) {
734 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"));
735 } else {
736 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"));
737 }
738
739 H = (pdo_pgsql_db_handle *)dbh->driver_data;
740
741 while ((pgsql_result = PQgetResult(H->server))) {
742 PQclear(pgsql_result);
743 }
744 pgsql_result = PQexec(H->server, query);
745
746 efree(query);
747
748 if (pgsql_result) {
749 status = PQresultStatus(pgsql_result);
750 } else {
751 status = (ExecStatusType) PQstatus(H->server);
752 }
753
754 if (status == PGRES_COPY_IN && pgsql_result) {
755 char *buf;
756 int command_failed = 0;
757 size_t line_len = 0;
758
759 PQclear(pgsql_result);
760 while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
761 if (PQputCopyData(H->server, buf, line_len) != 1) {
762 efree(buf);
763 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
764 php_stream_close(stream);
765 PDO_HANDLE_DBH_ERR();
766 RETURN_FALSE;
767 }
768 efree(buf);
769 }
770 php_stream_close(stream);
771
772 if (PQputCopyEnd(H->server, NULL) != 1) {
773 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
774 PDO_HANDLE_DBH_ERR();
775 RETURN_FALSE;
776 }
777
778 while ((pgsql_result = PQgetResult(H->server))) {
779 if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
780 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
781 command_failed = 1;
782 }
783 PQclear(pgsql_result);
784 }
785
786 PDO_HANDLE_DBH_ERR();
787 RETURN_BOOL(!command_failed);
788 } else {
789 php_stream_close(stream);
790 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
791 PQclear(pgsql_result);
792 PDO_HANDLE_DBH_ERR();
793 RETURN_FALSE;
794 }
795 }
796 /* }}} */
797
798
799 /* {{{ Returns true if the copy worked fine or false if error */
PHP_METHOD(PDO_PGSql_Ext,pgsqlCopyToFile)800 PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyToFile)
801 {
802 pdo_dbh_t *dbh;
803 pdo_pgsql_db_handle *H;
804
805 char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
806 size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
807 char *query;
808
809 PGresult *pgsql_result;
810 ExecStatusType status;
811
812 php_stream *stream;
813
814 if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss!",
815 &table_name, &table_name_len, &filename, &filename_len,
816 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
817 RETURN_THROWS();
818 }
819
820 dbh = Z_PDO_DBH_P(ZEND_THIS);
821 PDO_CONSTRUCT_CHECK;
822 PDO_DBH_CLEAR_ERR();
823
824 H = (pdo_pgsql_db_handle *)dbh->driver_data;
825
826 stream = php_stream_open_wrapper_ex(filename, "wb", 0, NULL, FG(default_context));
827 if (!stream) {
828 pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
829 PDO_HANDLE_DBH_ERR();
830 RETURN_FALSE;
831 }
832
833 while ((pgsql_result = PQgetResult(H->server))) {
834 PQclear(pgsql_result);
835 }
836
837 /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
838 if (pg_fields) {
839 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"));
840 } else {
841 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"));
842 }
843 pgsql_result = PQexec(H->server, query);
844 efree(query);
845
846 if (pgsql_result) {
847 status = PQresultStatus(pgsql_result);
848 } else {
849 status = (ExecStatusType) PQstatus(H->server);
850 }
851
852 if (status == PGRES_COPY_OUT && pgsql_result) {
853 PQclear(pgsql_result);
854 while (1) {
855 char *csv = NULL;
856 int ret = PQgetCopyData(H->server, &csv, 0);
857
858 if (ret == -1) {
859 break; /* done */
860 } else if (ret > 0) {
861 if (php_stream_write(stream, csv, ret) != (size_t)ret) {
862 pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
863 PQfreemem(csv);
864 php_stream_close(stream);
865 PDO_HANDLE_DBH_ERR();
866 RETURN_FALSE;
867 } else {
868 PQfreemem(csv);
869 }
870 } else {
871 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
872 php_stream_close(stream);
873 PDO_HANDLE_DBH_ERR();
874 RETURN_FALSE;
875 }
876 }
877 php_stream_close(stream);
878
879 while ((pgsql_result = PQgetResult(H->server))) {
880 PQclear(pgsql_result);
881 }
882 RETURN_TRUE;
883 } else {
884 php_stream_close(stream);
885 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
886 PQclear(pgsql_result);
887 PDO_HANDLE_DBH_ERR();
888 RETURN_FALSE;
889 }
890 }
891 /* }}} */
892
893 /* {{{ Returns true if the copy worked fine or false if error */
PHP_METHOD(PDO_PGSql_Ext,pgsqlCopyToArray)894 PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyToArray)
895 {
896 pdo_dbh_t *dbh;
897 pdo_pgsql_db_handle *H;
898
899 char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
900 size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
901 char *query;
902
903 PGresult *pgsql_result;
904 ExecStatusType status;
905
906 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sss!",
907 &table_name, &table_name_len,
908 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
909 RETURN_THROWS();
910 }
911
912 dbh = Z_PDO_DBH_P(ZEND_THIS);
913 PDO_CONSTRUCT_CHECK;
914 PDO_DBH_CLEAR_ERR();
915
916 H = (pdo_pgsql_db_handle *)dbh->driver_data;
917
918 while ((pgsql_result = PQgetResult(H->server))) {
919 PQclear(pgsql_result);
920 }
921
922 /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
923 if (pg_fields) {
924 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"));
925 } else {
926 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"));
927 }
928 pgsql_result = PQexec(H->server, query);
929 efree(query);
930
931 if (pgsql_result) {
932 status = PQresultStatus(pgsql_result);
933 } else {
934 status = (ExecStatusType) PQstatus(H->server);
935 }
936
937 if (status == PGRES_COPY_OUT && pgsql_result) {
938 PQclear(pgsql_result);
939 array_init(return_value);
940
941 while (1) {
942 char *csv = NULL;
943 int ret = PQgetCopyData(H->server, &csv, 0);
944 if (ret == -1) {
945 break; /* copy done */
946 } else if (ret > 0) {
947 add_next_index_stringl(return_value, csv, ret);
948 PQfreemem(csv);
949 } else {
950 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
951 PDO_HANDLE_DBH_ERR();
952 RETURN_FALSE;
953 }
954 }
955
956 while ((pgsql_result = PQgetResult(H->server))) {
957 PQclear(pgsql_result);
958 }
959 } else {
960 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
961 PQclear(pgsql_result);
962 PDO_HANDLE_DBH_ERR();
963 RETURN_FALSE;
964 }
965 }
966 /* }}} */
967
968
969 /* {{{ Creates a new large object, returning its identifier. Must be called inside a transaction. */
PHP_METHOD(PDO_PGSql_Ext,pgsqlLOBCreate)970 PHP_METHOD(PDO_PGSql_Ext, pgsqlLOBCreate)
971 {
972 pdo_dbh_t *dbh;
973 pdo_pgsql_db_handle *H;
974 Oid lfd;
975
976 ZEND_PARSE_PARAMETERS_NONE();
977
978 dbh = Z_PDO_DBH_P(ZEND_THIS);
979 PDO_CONSTRUCT_CHECK;
980 PDO_DBH_CLEAR_ERR();
981
982 H = (pdo_pgsql_db_handle *)dbh->driver_data;
983 lfd = lo_creat(H->server, INV_READ|INV_WRITE);
984
985 if (lfd != InvalidOid) {
986 zend_string *buf = strpprintf(0, ZEND_ULONG_FMT, (zend_long) lfd);
987
988 RETURN_STR(buf);
989 }
990
991 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
992 PDO_HANDLE_DBH_ERR();
993 RETURN_FALSE;
994 }
995 /* }}} */
996
997 /* {{{ Opens an existing large object stream. Must be called inside a transaction. */
PHP_METHOD(PDO_PGSql_Ext,pgsqlLOBOpen)998 PHP_METHOD(PDO_PGSql_Ext, pgsqlLOBOpen)
999 {
1000 pdo_dbh_t *dbh;
1001 pdo_pgsql_db_handle *H;
1002 Oid oid;
1003 int lfd;
1004 char *oidstr;
1005 size_t oidstrlen;
1006 char *modestr = "rb";
1007 size_t modestrlen;
1008 int mode = INV_READ;
1009 char *end_ptr;
1010
1011 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s",
1012 &oidstr, &oidstrlen, &modestr, &modestrlen)) {
1013 RETURN_THROWS();
1014 }
1015
1016 oid = (Oid)strtoul(oidstr, &end_ptr, 10);
1017 if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
1018 RETURN_FALSE;
1019 }
1020
1021 if (strpbrk(modestr, "+w")) {
1022 mode = INV_READ|INV_WRITE;
1023 }
1024
1025 dbh = Z_PDO_DBH_P(ZEND_THIS);
1026 PDO_CONSTRUCT_CHECK;
1027 PDO_DBH_CLEAR_ERR();
1028
1029 H = (pdo_pgsql_db_handle *)dbh->driver_data;
1030
1031 lfd = lo_open(H->server, oid, mode);
1032
1033 if (lfd >= 0) {
1034 php_stream *stream = pdo_pgsql_create_lob_stream(ZEND_THIS, lfd, oid);
1035 if (stream) {
1036 php_stream_to_zval(stream, return_value);
1037 return;
1038 }
1039 } else {
1040 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
1041 }
1042
1043 PDO_HANDLE_DBH_ERR();
1044 RETURN_FALSE;
1045 }
1046 /* }}} */
1047
1048 /* {{{ Deletes the large object identified by oid. Must be called inside a transaction. */
PHP_METHOD(PDO_PGSql_Ext,pgsqlLOBUnlink)1049 PHP_METHOD(PDO_PGSql_Ext, pgsqlLOBUnlink)
1050 {
1051 pdo_dbh_t *dbh;
1052 pdo_pgsql_db_handle *H;
1053 Oid oid;
1054 char *oidstr, *end_ptr;
1055 size_t oidlen;
1056
1057 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s",
1058 &oidstr, &oidlen)) {
1059 RETURN_THROWS();
1060 }
1061
1062 oid = (Oid)strtoul(oidstr, &end_ptr, 10);
1063 if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
1064 RETURN_FALSE;
1065 }
1066
1067 dbh = Z_PDO_DBH_P(ZEND_THIS);
1068 PDO_CONSTRUCT_CHECK;
1069 PDO_DBH_CLEAR_ERR();
1070
1071 H = (pdo_pgsql_db_handle *)dbh->driver_data;
1072
1073 if (1 == lo_unlink(H->server, oid)) {
1074 RETURN_TRUE;
1075 }
1076
1077 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
1078 PDO_HANDLE_DBH_ERR();
1079 RETURN_FALSE;
1080 }
1081 /* }}} */
1082
1083 /* {{{ Get asynchronous notification */
PHP_METHOD(PDO_PGSql_Ext,pgsqlGetNotify)1084 PHP_METHOD(PDO_PGSql_Ext, pgsqlGetNotify)
1085 {
1086 pdo_dbh_t *dbh;
1087 pdo_pgsql_db_handle *H;
1088 zend_long result_type = PDO_FETCH_USE_DEFAULT;
1089 zend_long ms_timeout = 0;
1090 PGnotify *pgsql_notify;
1091
1092 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|ll",
1093 &result_type, &ms_timeout)) {
1094 RETURN_THROWS();
1095 }
1096
1097 dbh = Z_PDO_DBH_P(ZEND_THIS);
1098 PDO_CONSTRUCT_CHECK;
1099
1100 if (result_type == PDO_FETCH_USE_DEFAULT) {
1101 result_type = dbh->default_fetch_type;
1102 }
1103
1104 if (result_type != PDO_FETCH_BOTH && result_type != PDO_FETCH_ASSOC && result_type != PDO_FETCH_NUM) {
1105 zend_argument_value_error(1, "must be one of PDO::FETCH_BOTH, PDO::FETCH_ASSOC, or PDO::FETCH_NUM");
1106 RETURN_THROWS();
1107 }
1108
1109 if (ms_timeout < 0) {
1110 zend_argument_value_error(2, "must be greater than or equal to 0");
1111 RETURN_THROWS();
1112 #ifdef ZEND_ENABLE_ZVAL_LONG64
1113 } else if (ms_timeout > INT_MAX) {
1114 php_error_docref(NULL, E_WARNING, "Timeout was shrunk to %d", INT_MAX);
1115 ms_timeout = INT_MAX;
1116 #endif
1117 }
1118
1119 H = (pdo_pgsql_db_handle *)dbh->driver_data;
1120
1121 if (!PQconsumeInput(H->server)) {
1122 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
1123 PDO_HANDLE_DBH_ERR();
1124 RETURN_FALSE;
1125 }
1126 pgsql_notify = PQnotifies(H->server);
1127
1128 if (ms_timeout && !pgsql_notify) {
1129 php_pollfd_for_ms(PQsocket(H->server), PHP_POLLREADABLE, (int)ms_timeout);
1130
1131 if (!PQconsumeInput(H->server)) {
1132 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
1133 PDO_HANDLE_DBH_ERR();
1134 RETURN_FALSE;
1135 }
1136 pgsql_notify = PQnotifies(H->server);
1137 }
1138
1139 if (!pgsql_notify) {
1140 RETURN_FALSE;
1141 }
1142
1143 array_init(return_value);
1144 if (result_type == PDO_FETCH_NUM || result_type == PDO_FETCH_BOTH) {
1145 add_index_string(return_value, 0, pgsql_notify->relname);
1146 add_index_long(return_value, 1, pgsql_notify->be_pid);
1147 if (pgsql_notify->extra && pgsql_notify->extra[0]) {
1148 add_index_string(return_value, 2, pgsql_notify->extra);
1149 }
1150 }
1151 if (result_type == PDO_FETCH_ASSOC || result_type == PDO_FETCH_BOTH) {
1152 add_assoc_string(return_value, "message", pgsql_notify->relname);
1153 add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
1154 if (pgsql_notify->extra && pgsql_notify->extra[0]) {
1155 add_assoc_string(return_value, "payload", pgsql_notify->extra);
1156 }
1157 }
1158
1159 PQfreemem(pgsql_notify);
1160 }
1161 /* }}} */
1162
1163 /* {{{ Get backend(server) pid */
PHP_METHOD(PDO_PGSql_Ext,pgsqlGetPid)1164 PHP_METHOD(PDO_PGSql_Ext, pgsqlGetPid)
1165 {
1166 pdo_dbh_t *dbh;
1167 pdo_pgsql_db_handle *H;
1168
1169 ZEND_PARSE_PARAMETERS_NONE();
1170
1171 dbh = Z_PDO_DBH_P(ZEND_THIS);
1172 PDO_CONSTRUCT_CHECK;
1173
1174 H = (pdo_pgsql_db_handle *)dbh->driver_data;
1175
1176 RETURN_LONG(PQbackendPID(H->server));
1177 }
1178 /* }}} */
1179
pdo_pgsql_get_driver_methods(pdo_dbh_t * dbh,int kind)1180 static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind)
1181 {
1182 switch (kind) {
1183 case PDO_DBH_DRIVER_METHOD_KIND_DBH:
1184 return class_PDO_PGSql_Ext_methods;
1185 default:
1186 return NULL;
1187 }
1188 }
1189
pdo_pgsql_set_attr(pdo_dbh_t * dbh,zend_long attr,zval * val)1190 static bool pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
1191 {
1192 bool bval;
1193 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
1194
1195 switch (attr) {
1196 case PDO_ATTR_EMULATE_PREPARES:
1197 if (!pdo_get_bool_param(&bval, val)) {
1198 return false;
1199 }
1200 H->emulate_prepares = bval;
1201 return true;
1202 case PDO_PGSQL_ATTR_DISABLE_PREPARES:
1203 if (!pdo_get_bool_param(&bval, val)) {
1204 return false;
1205 }
1206 H->disable_prepares = bval;
1207 return true;
1208 default:
1209 return false;
1210 }
1211 }
1212
1213 static const struct pdo_dbh_methods pgsql_methods = {
1214 pgsql_handle_closer,
1215 pgsql_handle_preparer,
1216 pgsql_handle_doer,
1217 pgsql_handle_quoter,
1218 pgsql_handle_begin,
1219 pgsql_handle_commit,
1220 pgsql_handle_rollback,
1221 pdo_pgsql_set_attr,
1222 pdo_pgsql_last_insert_id,
1223 pdo_pgsql_fetch_error_func,
1224 pdo_pgsql_get_attribute,
1225 pdo_pgsql_check_liveness, /* check_liveness */
1226 pdo_pgsql_get_driver_methods, /* get_driver_methods */
1227 NULL,
1228 pgsql_handle_in_transaction,
1229 NULL /* get_gc */
1230 };
1231
pdo_pgsql_handle_factory(pdo_dbh_t * dbh,zval * driver_options)1232 static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
1233 {
1234 pdo_pgsql_db_handle *H;
1235 int ret = 0;
1236 char *conn_str, *p, *e;
1237 zend_string *tmp_user, *tmp_pass;
1238 zend_long connect_timeout = 30;
1239
1240 H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
1241 dbh->driver_data = H;
1242
1243 dbh->skip_param_evt =
1244 1 << PDO_PARAM_EVT_EXEC_POST |
1245 1 << PDO_PARAM_EVT_FETCH_PRE |
1246 1 << PDO_PARAM_EVT_FETCH_POST;
1247
1248 H->einfo.errcode = 0;
1249 H->einfo.errmsg = NULL;
1250
1251 /* PostgreSQL wants params in the connect string to be separated by spaces,
1252 * if the PDO standard semicolons are used, we convert them to spaces
1253 */
1254 e = (char *) dbh->data_source + strlen(dbh->data_source);
1255 p = (char *) dbh->data_source;
1256 while ((p = memchr(p, ';', (e - p)))) {
1257 *p = ' ';
1258 }
1259
1260 if (driver_options) {
1261 connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
1262 }
1263
1264 /* escape username and password, if provided */
1265 tmp_user = _pdo_pgsql_escape_credentials(dbh->username);
1266 tmp_pass = _pdo_pgsql_escape_credentials(dbh->password);
1267
1268 /* support both full connection string & connection string + login and/or password */
1269 if (tmp_user && tmp_pass) {
1270 spprintf(&conn_str, 0, "%s user='%s' password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), ZSTR_VAL(tmp_pass), connect_timeout);
1271 } else if (tmp_user) {
1272 spprintf(&conn_str, 0, "%s user='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), connect_timeout);
1273 } else if (tmp_pass) {
1274 spprintf(&conn_str, 0, "%s password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_pass), connect_timeout);
1275 } else {
1276 spprintf(&conn_str, 0, "%s connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, connect_timeout);
1277 }
1278
1279 H->server = PQconnectdb(conn_str);
1280 H->lob_streams = (HashTable *) pemalloc(sizeof(HashTable), dbh->is_persistent);
1281 zend_hash_init(H->lob_streams, 0, NULL, NULL, 1);
1282
1283 if (tmp_user) {
1284 zend_string_release_ex(tmp_user, 0);
1285 }
1286 if (tmp_pass) {
1287 zend_string_release_ex(tmp_pass, 0);
1288 }
1289
1290 efree(conn_str);
1291
1292 if (PQstatus(H->server) != CONNECTION_OK) {
1293 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
1294 goto cleanup;
1295 }
1296
1297 PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
1298
1299 H->attached = 1;
1300 H->pgoid = -1;
1301
1302 dbh->methods = &pgsql_methods;
1303 dbh->alloc_own_columns = 1;
1304 dbh->max_escaped_char_length = 2;
1305
1306 ret = 1;
1307
1308 cleanup:
1309 dbh->methods = &pgsql_methods;
1310 if (!ret) {
1311 pgsql_handle_closer(dbh);
1312 }
1313
1314 return ret;
1315 }
1316 /* }}} */
1317
1318 const pdo_driver_t pdo_pgsql_driver = {
1319 PDO_DRIVER_HEADER(pgsql),
1320 pdo_pgsql_handle_factory
1321 };
1322