Skip to content

Commit

Permalink
Refactor code duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Delisle <marc@infomarc.info>
  • Loading branch information
lem9 committed Nov 5, 2014
1 parent 65d4603 commit 25a591e
Showing 1 changed file with 32 additions and 37 deletions.
69 changes: 32 additions & 37 deletions libraries/navigation/Nodes/Node_Database.class.php
Expand Up @@ -90,28 +90,35 @@ public function getPresence($type = '', $searchClause = '', $singleItem = false)
}

/**
* Returns the number of tables present inside this database
* Returns the number of tables or views present inside this database
*
* @param string $which tables|views
* @param string $searchClause A string used to filter the results of
* the query
* @param boolean $singleItem Whether to get presence of a single known
* item or false in none
*
* @return int
*/
private function _getTableCount($searchClause, $singleItem)
private function _getTableOrViewCount($which, $searchClause, $singleItem)
{
$retval = 0;
$db = $this->real_name;
if ($which == 'tables') {
$condition = '=';
} else {
$condition = '!=';
}

if (! $GLOBALS['cfg']['Server']['DisableIS'] || PMA_DRIZZLE) {
$db = PMA_Util::sqlAddSlashes($db);
$query = "SELECT COUNT(*) ";
$query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
$query .= "WHERE `TABLE_SCHEMA`='$db' ";
if (PMA_DRIZZLE) {
$query .= "AND `TABLE_TYPE`='BASE' ";
$query .= "AND `TABLE_TYPE`" . $condition . "'BASE' ";
} else {
$query .= "AND `TABLE_TYPE`='BASE TABLE' ";
$query .= "AND `TABLE_TYPE`" . $condition . "'BASE TABLE' ";
}
if (! empty($searchClause)) {
$query .= "AND " . $this->_getWhereClauseForSearch(
Expand All @@ -122,7 +129,7 @@ private function _getTableCount($searchClause, $singleItem)
} else {
$query = "SHOW FULL TABLES FROM ";
$query .= PMA_Util::backquote($db);
$query .= " WHERE `Table_type`='BASE TABLE' ";
$query .= " WHERE `Table_type`" . $condition . "'BASE TABLE' ";
if (! empty($searchClause)) {
$query .= "AND " . $this->_getWhereClauseForSearch(
$searchClause, $singleItem, 'Tables_in_' . $db
Expand All @@ -135,6 +142,23 @@ private function _getTableCount($searchClause, $singleItem)
return $retval;
}

/**
* Returns the number of tables present inside this database
*
* @param string $searchClause A string used to filter the results of
* the query
* @param boolean $singleItem Whether to get presence of a single known
* item or false in none
*
* @return int
*/
private function _getTableCount($searchClause, $singleItem)
{
return $this->_getTableOrViewCount(
'tables', $searchClause, $singleItem
);
}

/**
* Returns the number of views present inside this database
*
Expand All @@ -147,38 +171,9 @@ private function _getTableCount($searchClause, $singleItem)
*/
private function _getViewCount($searchClause, $singleItem)
{
$retval = 0;
$db = $this->real_name;
if (! $GLOBALS['cfg']['Server']['DisableIS'] || PMA_DRIZZLE) {
$db = PMA_Util::sqlAddSlashes($db);
$query = "SELECT COUNT(*) ";
$query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
$query .= "WHERE `TABLE_SCHEMA`='$db' ";
if (PMA_DRIZZLE) {
$query .= "AND `TABLE_TYPE`!='BASE' ";
} else {
$query .= "AND `TABLE_TYPE`!='BASE TABLE' ";
}
if (! empty($searchClause)) {
$query .= "AND " . $this->_getWhereClauseForSearch(
$searchClause, $singleItem, 'TABLE_NAME'
);
}
$retval = (int)$GLOBALS['dbi']->fetchValue($query);
} else {
$query = "SHOW FULL TABLES FROM ";
$query .= PMA_Util::backquote($db);
$query .= " WHERE `Table_type`!='BASE TABLE' ";
if (! empty($searchClause)) {
$query .= "AND " . $this->_getWhereClauseForSearch(
$searchClause, $singleItem, 'Tables_in_' . $db
);
}
$retval = $GLOBALS['dbi']->numRows(
$GLOBALS['dbi']->tryQuery($query)
);
}
return $retval;
return $this->_getTableOrViewCount(
'views', $searchClause, $singleItem
);
}

/**
Expand Down

0 comments on commit 25a591e

Please sign in to comment.