From f9953fc3395e6b206c0789d40f89f9d2463348e7 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 18 Feb 2012 19:29:32 +0100 Subject: [ticket/10653] Add ability to count table rows to database abstraction layer. PHPBB3-10653 --- phpBB/includes/db/mysql.php | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'phpBB/includes/db/mysql.php') diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index 1e24c79577..87413e808d 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -318,6 +318,76 @@ class dbal_mysql extends dbal return @mysql_real_escape_string($msg, $this->db_connect_id); } + /** + * Gets the estimated number of rows in a specified table. + * + * @param string $table_name Table name + * + * @return string Number of rows in $table_name. + * Prefixed with ~ if estimated (otherwise exact). + * + * @access public + */ + function get_estimated_row_count($table_name) + { + $table_status = $this->get_table_status($table_name); + + if (isset($table_status['Engine'])) + { + if ($table_status['Engine'] === 'MyISAM') + { + return $table_status['Rows']; + } + else if ($table_status['Engine'] === 'InnoDB' && $table_status['Rows'] > 100000) + { + return '~' . $table_status['Rows']; + } + } + + return $this->get_row_count($table_name); + } + + /** + * Gets the exact number of rows in a specified table. + * + * @param string $table_name Table name + * + * @return string Exact number of rows in $table_name. + * + * @access public + */ + function get_row_count($table_name) + { + $table_status = $this->get_table_status($table_name); + + if (isset($table_status['Engine']) && $table_status['Engine'] === 'MyISAM') + { + return $table_status['Rows']; + } + + return parent::get_row_count($table_name); + } + + /** + * Gets some information about the specified table. + * + * @param string $table_name Table name + * + * @return array + * + * @access protected + */ + function get_table_status($table_name) + { + $sql = "SHOW TABLE STATUS + LIKE '" . $this->sql_escape($table_name) . "'"; + $result = $this->sql_query($sql); + $table_status = $this->sql_fetchrow($result); + $this->sql_freeresult($result); + + return $table_status; + } + /** * Build LIKE expression * @access private -- cgit v1.2.1 From c11607bdd1ad4209a7697f0dc8d46ea81795b14c Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 7 Mar 2012 12:11:18 +0100 Subject: [ticket/10653] Call get_row_count of base class in mysql get_estimated_row_count There is no point in fetching the table status again. PHPBB3-10653 --- phpBB/includes/db/mysql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/db/mysql.php') diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index 87413e808d..1ccb785150 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -344,7 +344,7 @@ class dbal_mysql extends dbal } } - return $this->get_row_count($table_name); + return parent::get_row_count($table_name); } /** -- cgit v1.2.1