Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "php_pdo_pgsql.h"
#include "php_pdo_pgsql_int.h"
#include "zend_exceptions.h"
#include "zend_smart_str.h"
#include "pgsql_driver_arginfo.h"

static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh);
Expand Down Expand Up @@ -1329,8 +1330,9 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
{
pdo_pgsql_db_handle *H;
int ret = 0;
char *conn_str, *p, *e;
char *p, *e;
zend_string *tmp_user, *tmp_pass;
smart_str conn_str = {0};
zend_long connect_timeout = 30;

H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
Expand Down Expand Up @@ -1361,18 +1363,20 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
tmp_user = !strstr((char *) dbh->data_source, "user=") ? _pdo_pgsql_escape_credentials(dbh->username) : NULL;
tmp_pass = !strstr((char *) dbh->data_source, "password=") ? _pdo_pgsql_escape_credentials(dbh->password) : NULL;

smart_str_appends(&conn_str, dbh->data_source);
smart_str_append_printf(&conn_str, " connect_timeout=" ZEND_LONG_FMT, connect_timeout);

/* support both full connection string & connection string + login and/or password */
if (tmp_user && tmp_pass) {
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);
} else if (tmp_user) {
spprintf(&conn_str, 0, "%s user='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), connect_timeout);
} else if (tmp_pass) {
spprintf(&conn_str, 0, "%s password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_pass), connect_timeout);
} else {
spprintf(&conn_str, 0, "%s connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, connect_timeout);
if (tmp_user) {
smart_str_append_printf(&conn_str, " user='%s'", ZSTR_VAL(tmp_user));
}

if (tmp_pass) {
smart_str_append_printf(&conn_str, " password='%s'", ZSTR_VAL(tmp_pass));
}
smart_str_0(&conn_str);

H->server = PQconnectdb(conn_str);
H->server = PQconnectdb(ZSTR_VAL(conn_str.s));
H->lob_streams = (HashTable *) pemalloc(sizeof(HashTable), dbh->is_persistent);
zend_hash_init(H->lob_streams, 0, NULL, NULL, 1);

Expand All @@ -1383,7 +1387,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
zend_string_release_ex(tmp_pass, 0);
}

efree(conn_str);
smart_str_free(&conn_str);

if (PQstatus(H->server) != CONNECTION_OK) {
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
Expand Down