1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-01-17 22:28:46 +01:00

Merge branch 'develop-olympus' into develop

* develop-olympus:
  [ticket/10653] Call get_row_count of base class in mysql get_estimated_row_count
  [ticket/9813] Only get posts table row count if we detected a fulltext index.
  [ticket/9813] Also use estimated row count of posts table for fulltext mysql.
  [ticket/10653] Fix parameter to substr() in unit tests. Should be 1, not -1.
  [ticket/10653] Unit tests for get_row_count() and get_estimated_row_count().
  [ticket/10653] Add ability to count table rows to database abstraction layer.
  [ticket/9813] Use table status row count only if greater than 100000 or exact.
  [ticket/9813] Use SHOW TABLE STATUS to get search stats for native on MySQL.
This commit is contained in:
Oleg Pudeyev 2012-03-08 08:44:32 -05:00
commit 35309de0b7
6 changed files with 203 additions and 16 deletions

View File

@ -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;
}
}
/**

View File

@ -317,6 +317,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 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

View File

@ -314,6 +314,76 @@ class dbal_mysqli extends dbal
return @mysqli_real_escape_string($this->db_connect_id, $msg);
}
/**
* 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

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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."
);
}
}