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