diff options
| -rw-r--r-- | phpBB/includes/db/dbal.php | 35 | ||||
| -rw-r--r-- | phpBB/includes/db/mysql.php | 70 | ||||
| -rw-r--r-- | phpBB/includes/db/mysqli.php | 70 | ||||
| -rw-r--r-- | phpBB/includes/search/fulltext_mysql.php | 6 | ||||
| -rw-r--r-- | phpBB/includes/search/fulltext_native.php | 13 | ||||
| -rw-r--r-- | tests/dbal/select_test.php | 25 | 
6 files changed, 203 insertions, 16 deletions
diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index 8564cb8426..6da854b6e2 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -920,6 +920,41 @@ class dbal  		return true;  	} + +	/** +	* 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) +	{ +		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) +	{ +		$sql = 'SELECT COUNT(*) AS rows_total +			FROM ' . $this->sql_escape($table_name); +		$result = $this->sql_query($sql); +		$rows_total = $this->sql_fetchfield('rows_total'); +		$this->sql_freeresult($result); + +		return $rows_total; +	}  }  /** diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index 317b8d123d..eb38e3e913 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -318,6 +318,76 @@ class dbal_mysql extends dbal  	}  	/** +	* 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 parent::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  	*/ diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index d6b64bf7c8..4210a58002 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -315,6 +315,76 @@ class dbal_mysqli extends dbal  	}  	/** +	* 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 parent::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  	*/ diff --git a/phpBB/includes/search/fulltext_mysql.php b/phpBB/includes/search/fulltext_mysql.php index 99467da978..7c94038cc9 100644 --- a/phpBB/includes/search/fulltext_mysql.php +++ b/phpBB/includes/search/fulltext_mysql.php @@ -897,11 +897,7 @@ class phpbb_search_fulltext_mysql extends phpbb_search_base  		}  		$db->sql_freeresult($result); -		$sql = 'SELECT COUNT(post_id) as total_posts -			FROM ' . POSTS_TABLE; -		$result = $db->sql_query($sql); -		$this->stats['total_posts'] = (int) $db->sql_fetchfield('total_posts'); -		$db->sql_freeresult($result); +		$this->stats['total_posts'] = empty($this->stats) ? 0 : $db->get_estimated_row_count(POSTS_TABLE);  	}  	/** diff --git a/phpBB/includes/search/fulltext_native.php b/phpBB/includes/search/fulltext_native.php index 59d6a312b3..3e029c86d0 100644 --- a/phpBB/includes/search/fulltext_native.php +++ b/phpBB/includes/search/fulltext_native.php @@ -1462,17 +1462,8 @@ class phpbb_search_fulltext_native extends phpbb_search_base  	{  		global $db; -		$sql = 'SELECT COUNT(*) as total_words -			FROM ' . SEARCH_WORDLIST_TABLE; -		$result = $db->sql_query($sql); -		$this->stats['total_words'] = (int) $db->sql_fetchfield('total_words'); -		$db->sql_freeresult($result); - -		$sql = 'SELECT COUNT(*) as total_matches -			FROM ' . SEARCH_WORDMATCH_TABLE; -		$result = $db->sql_query($sql); -		$this->stats['total_matches'] = (int) $db->sql_fetchfield('total_matches'); -		$db->sql_freeresult($result); +		$this->stats['total_words']		= $db->get_estimated_row_count(SEARCH_WORDLIST_TABLE); +		$this->stats['total_matches']	= $db->get_estimated_row_count(SEARCH_WORDMATCH_TABLE);  	}  	/** diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php index 05b0e68e29..21b12777dc 100644 --- a/tests/dbal/select_test.php +++ b/tests/dbal/select_test.php @@ -357,4 +357,29 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$this->assertSame(false, $row);  	} + +	public function test_get_row_count() +	{ +		$this->assertSame( +			3, +			(int) $this->new_dbal()->get_row_count('phpbb_users'), +			"Failed asserting that user table has exactly 3 rows." +		); +	} + +	public function test_get_estimated_row_count() +	{ +		$actual = $this->new_dbal()->get_estimated_row_count('phpbb_users'); + +		if (is_string($actual) && isset($actual[0]) && $actual[0] === '~') +		{ +			$actual = substr($actual, 1); +		} + +		$this->assertGreaterThan( +			1, +			$actual, +			"Failed asserting that estimated row count of user table is greater than 1." +		); +	}  }  | 
