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