From d69a7c620a08d1866a24033ef43646a4bbfc9925 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 23 Feb 2011 20:12:12 -0500 Subject: [ticket/10057] Report postgres db connection errors. Addresses two issues: 1. When pgsql extension is missing, @pg_connect would silently abort execution. Check for pg_connect existence before calling it, same with pg_pconnect. 2. When connection fails, the error reported by php is discarded. User is shown the failure message without the reason for failure, making debugging difficult. Collect the error (if any) via a temporarily installed error handler, and display it if connection failed. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 4360c790a1..29e15143bc 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -81,13 +81,25 @@ class dbal_postgres extends dbal if ($this->persistency) { + if (!function_exists('pg_pconnect')) + { + return $this->sql_error('pg_pconnect does not exist'); + } + phpbb_start_error_collection(); $this->db_connect_id = (!$new_link) ? @pg_pconnect($connect_string) : @pg_pconnect($connect_string, PGSQL_CONNECT_FORCE_NEW); } else { + if (!function_exists('pg_pconnect')) + { + return $this->sql_error('pg_connect does not exist'); + } + phpbb_start_error_collection(); $this->db_connect_id = (!$new_link) ? @pg_connect($connect_string) : @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW); } + $errors = phpbb_stop_error_collection(); + if ($this->db_connect_id) { if (version_compare($this->sql_server_info(true), '8.2', '>=')) @@ -102,7 +114,8 @@ class dbal_postgres extends dbal return $this->db_connect_id; } - return $this->sql_error(''); + $errors = phpbb_format_collected_errors($errors); + return $this->sql_error($errors); } /** -- cgit v1.2.1 From a4100fe7094aaa5377065d2f25ca8d0fa2ff6bf8 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 23 Feb 2011 22:54:25 -0500 Subject: [ticket/10057] More informative error messages in postgres dbal. When pg_connect/pg_pconnect do not exist, mention that they come with pgsql extension. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 29e15143bc..c1dc7f7e2b 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -83,7 +83,7 @@ class dbal_postgres extends dbal { if (!function_exists('pg_pconnect')) { - return $this->sql_error('pg_pconnect does not exist'); + return $this->sql_error('pg_pconnect function does not exist, is pgsql extension installed?'); } phpbb_start_error_collection(); $this->db_connect_id = (!$new_link) ? @pg_pconnect($connect_string) : @pg_pconnect($connect_string, PGSQL_CONNECT_FORCE_NEW); @@ -92,7 +92,7 @@ class dbal_postgres extends dbal { if (!function_exists('pg_pconnect')) { - return $this->sql_error('pg_connect does not exist'); + return $this->sql_error('pg_connect function does not exist, is pgsql extension installed?'); } phpbb_start_error_collection(); $this->db_connect_id = (!$new_link) ? @pg_connect($connect_string) : @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW); -- cgit v1.2.1 From 17693c2802c92297251f5cd94cc5452f5e54eb0b Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 2 Mar 2011 06:35:25 -0500 Subject: [ticket/10057] Use a class for error collection. Replaced error collection functions with a class for a cleaner implementation. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index c1dc7f7e2b..a8dc3dd8ee 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -85,7 +85,8 @@ class dbal_postgres extends dbal { return $this->sql_error('pg_pconnect function does not exist, is pgsql extension installed?'); } - phpbb_start_error_collection(); + $collector = new phpbb_error_collector; + $collector->install(); $this->db_connect_id = (!$new_link) ? @pg_pconnect($connect_string) : @pg_pconnect($connect_string, PGSQL_CONNECT_FORCE_NEW); } else @@ -94,11 +95,12 @@ class dbal_postgres extends dbal { return $this->sql_error('pg_connect function does not exist, is pgsql extension installed?'); } - phpbb_start_error_collection(); + $collector = new phpbb_error_collector; + $collector->install(); $this->db_connect_id = (!$new_link) ? @pg_connect($connect_string) : @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW); } - $errors = phpbb_stop_error_collection(); + $collector->uninstall(); if ($this->db_connect_id) { @@ -114,7 +116,7 @@ class dbal_postgres extends dbal return $this->db_connect_id; } - $errors = phpbb_format_collected_errors($errors); + $errors = $collector->format_errors(); return $this->sql_error($errors); } -- cgit v1.2.1 From 22004fa7d65b19e8bfe96a97042a614be1adf444 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 6 Mar 2011 20:51:09 -0500 Subject: [ticket/10057] Include error collector class file in postgres dbal. This change is in its own commit because it will be reverted for 3.1. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index a8dc3dd8ee..d703f5b567 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -18,6 +18,11 @@ if (!defined('IN_PHPBB')) include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx); +if (!class_exists('phpbb_error_collector')) +{ + include($phpbb_root_path . 'includes/error_collector.' . $phpEx); +} + /** * PostgreSQL Database Abstraction Layer * Minimum Requirement is Version 7.3+ -- cgit v1.2.1 From 98388b29212cf94c443e0b4f626508efc937715f Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 6 Mar 2011 23:17:01 -0500 Subject: [ticket/10057] Fixed wrong usage of sql_error in postgres dbal. pg_last_error does not work if no connection was ever established. Therefore we must keep track of connection errors in postgres dbal ourselves. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index d703f5b567..78b6a75750 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -31,6 +31,7 @@ if (!class_exists('phpbb_error_collector')) class dbal_postgres extends dbal { var $last_query_text = ''; + var $connect_error = ''; /** * Connect to server @@ -121,8 +122,8 @@ class dbal_postgres extends dbal return $this->db_connect_id; } - $errors = $collector->format_errors(); - return $this->sql_error($errors); + $this->connect_error = $collector->format_errors(); + return $this->sql_error(''); } /** @@ -391,8 +392,19 @@ class dbal_postgres extends dbal */ function _sql_error() { + // pg_last_error only works when there is an established connection. + // Connection errors have to be tracked by us manually. + if ($this->db_connect_id) + { + $message = @pg_last_error($this->db_connect_id); + } + else + { + $message = $this->connect_error; + } + return array( - 'message' => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id), + 'message' => $message, 'code' => '' ); } -- cgit v1.2.1 From e5aa2c9ac112156b56db9c4b1d8fc2b6f6f79265 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 7 Mar 2011 08:52:59 -0500 Subject: [ticket/10057] Fixed usage of sql_error again. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 78b6a75750..69f605fc47 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -89,7 +89,8 @@ class dbal_postgres extends dbal { if (!function_exists('pg_pconnect')) { - return $this->sql_error('pg_pconnect function does not exist, is pgsql extension installed?'); + $this->connect_error = 'pg_pconnect function does not exist, is pgsql extension installed?'; + return $this->sql_error(''); } $collector = new phpbb_error_collector; $collector->install(); @@ -99,7 +100,8 @@ class dbal_postgres extends dbal { if (!function_exists('pg_pconnect')) { - return $this->sql_error('pg_connect function does not exist, is pgsql extension installed?'); + $this->connect_error = 'pg_connect function does not exist, is pgsql extension installed?'; + return $this->sql_error(''); } $collector = new phpbb_error_collector; $collector->install(); -- cgit v1.2.1 From 87e3560c30365d757280f6ef6f067c29c1f9c5f0 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 8 Mar 2011 19:48:56 -0500 Subject: [ticket/10057] Fixes for a bunch of small problems. PHPBB3-10057 --- phpBB/includes/db/postgres.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/db/postgres.php') diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 69f605fc47..bb116e0763 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -98,7 +98,7 @@ class dbal_postgres extends dbal } else { - if (!function_exists('pg_pconnect')) + if (!function_exists('pg_connect')) { $this->connect_error = 'pg_connect function does not exist, is pgsql extension installed?'; return $this->sql_error(''); -- cgit v1.2.1