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