Skip to content

Commit

Permalink
moved PMA_generateFieldSpec(), PMA_tableIsView(), PMA_countRecords(),…
Browse files Browse the repository at this point in the history
… PMA_generateAlterTable() into class PMA_Table
  • Loading branch information
CybotTM committed Feb 21, 2006
1 parent 9515f39 commit cd340d8
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 171 deletions.
9 changes: 7 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ $Source$
* libraries/Table.class.php: *NEW* class PMA_Table
* tbl_addfield.php, tbl_create.php:
use Table.class.php
* libraries/common.lib.php:
moved PMA_generateFieldSpec() into PMA_Table
* db_details_qbe.php, db_details_structure.php, tbl_alter.php, sql.php,
libraries/common.lib.php, libraries/tbl_properties_table_info.inc.php,
libraries/display_export.lib.php, libraries/display_tbl.lib.php,
libraries/get_foreign.lib.php, libraries/relation.lib.php:
moved PMA_generateFieldSpec(), PMA_tableIsView(), PMA_countRecords(),
PMA_generateAlterTable() into class PMA_Table


2006-02-20 Marc Delisle <[email protected]>
### 2.8.0-rc1 released
Expand Down
3 changes: 2 additions & 1 deletion db_details_qbe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* requirements
*/
require_once('./libraries/common.lib.php');
require_once './libraries/Table.class.php';
require_once('./libraries/relation.lib.php');


Expand Down Expand Up @@ -805,7 +806,7 @@ function showColumnSelectCell( $columns, $column_number, $selected = '' )
$checked_tables = $col_cand;
foreach ($col_cand AS $tab) {
if ($checked_tables[$tab] != 1 ) {
$tsize[$tab] = PMA_countRecords($db, $tab, true, false);
$tsize[$tab] = PMA_Table::countRecords($db, $tab, true, false);
$checked_tables[$tab] = 1;
}
$csize[$tab] = $tsize[$tab];
Expand Down
3 changes: 2 additions & 1 deletion db_details_structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// vim: expandtab sw=4 ts=4 sts=4:

require_once './libraries/common.lib.php';
require_once './libraries/Table.class.php';

/**
* Prepares the tables list if the user where not redirected to this script
Expand Down Expand Up @@ -175,7 +176,7 @@ function PMA_TableHeader($db_is_information_schema = false)

foreach ($tables as $keyname => $each_table) {
if ($each_table['TABLE_ROWS'] === null || $each_table['TABLE_ROWS'] < $GLOBALS['cfg']['MaxExactCount']) {
$each_table['TABLE_ROWS'] = PMA_countRecords($db,
$each_table['TABLE_ROWS'] = PMA_Table::countRecords($db,
$each_table['TABLE_NAME'], $return = true, $force_exact = true);
}

Expand Down
107 changes: 105 additions & 2 deletions libraries/Table.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ function getFullName($quoted = false)
return $this->getDbName($quoted) . '.' . $this->getName($quoted);
}

function isView()
function isView($db = null, $table = null)
{
if (null !== $db && null !== $table) {
return PMA_Table::_isView($db, $table);
}

if ( strpos($this->get('TABLE TYPE'), 'VIEW') ) {
return true;
}
Expand Down Expand Up @@ -161,7 +165,7 @@ function loadStructure()
$this->settings = $table_info;

if ( $this->get('TABLE_ROWS') === null ) {
$this->set('TABLE_ROWS', PMA_countRecords($this->getDbName(),
$this->set('TABLE_ROWS', PMA_Table::countRecords($this->getDbName(),
$this->getName(), true, true));
}

Expand All @@ -187,6 +191,35 @@ function PMA_Table()
$this->__construct();
}

/**
* Checks if this "table" is a view
*
* @deprecated
* @param string the database name
* @param string the table name
*
* @return boolean whether this is a view
*
* @access public
*/
function _isView($db, $table) {
// maybe we already know if the table is a view
// TODO: see what we could do with the possible existence
// of $table_is_view
if (isset($GLOBALS['tbl_is_view']) && $GLOBALS['tbl_is_view']) {
return true;
}
// old MySQL version: no view
if (PMA_MYSQL_INT_VERSION < 50000) {
return false;
}
if ( false === PMA_DBI_fetch_value('SELECT TABLE_NAME FROM `information_schema`.`VIEWS` WHERE `TABLE_SCHEMA` = \'' . $db . '\' AND `TABLE_NAME` = \'' . $table . '\';')) {
return false;
} else {
return true;
}
}

/**
* @TODO add documentation
*
Expand Down Expand Up @@ -258,5 +291,75 @@ function generateFieldSpec($name, $type, $length, $attribute,
return $query;
} // end function

/**
* Counts and returns (or displays) the number of records in a table
*
* Revision 13 July 2001: Patch for limiting dump size from
* [email protected] & [email protected]
*
* @param string the current database name
* @param string the current table name
* @param boolean whether to retain or to displays the result
* @param boolean whether to force an exact count
*
* @return mixed the number of records if retain is required, true else
*
* @access public
*/
function countRecords($db, $table, $ret = false, $force_exact = false)
{
global $err_url, $cfg;
if (!$force_exact) {
$result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table, true) . '\';');
$showtable = PMA_DBI_fetch_assoc($result);
$num = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
if ($num < $cfg['MaxExactCount']) {
unset($num);
}
PMA_DBI_free_result($result);
}

$tbl_is_view = PMA_Table::isView($db, $table);

if (!isset($num)) {
if (! $tbl_is_view) {
$num = PMA_DBI_fetch_value('SELECT COUNT(*) AS num FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table));
// necessary?
if (! $num) {
$num = 0;
}
// since counting all rows of a view could be too long
} else {
$result = PMA_DBI_query('SELECT 1 FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT ' . $cfg['MaxExactCount'], null, PMA_DBI_QUERY_STORE);
$num = PMA_DBI_num_rows($result);
}
}
if ($ret) {
return $num;
} else {
// Note: as of PMA 2.8.0, we no longer seem to be using
// PMA_Table::countRecords() in display mode.
echo number_format($num, 0, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
if ($tbl_is_view) {
echo '&nbsp;' . sprintf($GLOBALS['strViewMaxExactCount'], $cfg['MaxExactCount'], '[a@./Documentation.html#cfg_MaxExactCount@_blank]', '[/a]');
}
return true;
}
} // end of the 'PMA_Table::countRecords()' function

/**
* @TODO add documentation
*/
function generateAlter($oldcol, $newcol, $type, $length,
$attribute, $collation, $null, $default, $default_current_timestamp,
$extra, $comment='', $default_orig)
{
$empty_a = array();
return PMA_backquote($oldcol) . ' '
. PMA_generateFieldSpec($newcol, $type, $length, $attribute,
$collation, $null, $default, $default_current_timestamp, $extra,
$comment, $empty_a, -1, $default_orig);
} // end function

}
?>
107 changes: 5 additions & 102 deletions libraries/common.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ function PMA_checkPageValidity(&$page, $whitelist)
require_once './libraries/Theme.class.php';
require_once './libraries/Theme_Manager.class.php';
require_once './libraries/Config.class.php';
require_once './libraries/Table.class.php';



Expand Down Expand Up @@ -1147,14 +1148,14 @@ function PMA_getTableList($db, $tables = null)
// if row count is invalid possibly the table is defect
// and this would break left frame;
// but we can check row count if this is a view,
// since PMA_countRecords() returns a limited row count
// since PMA_Table::countRecords() returns a limited row count
// in this case.

// set this because PMA_countRecords() can use it
$tbl_is_view = PMA_tableIsView($db, $table['Name']);
// set this because PMA_Table::countRecords() can use it
$tbl_is_view = PMA_Table::isView($db, $table['Name']);

if ($tbl_is_view) {
$table['Rows'] = PMA_countRecords($db, $table['Name'],
$table['Rows'] = PMA_Table::countRecords($db, $table['Name'],
$return = true);
}
}
Expand Down Expand Up @@ -1311,90 +1312,6 @@ function PMA_whichCrlf()
return $the_crlf;
} // end of the 'PMA_whichCrlf()' function

/**
* Checks if this "table" is a view
*
* @param string the database name
* @param string the table name
*
* @return boolean whether this is a view
*
* @access public
*/
function PMA_tableIsView($db, $table) {
// maybe we already know if the table is a view
// TODO: see what we could do with the possible existence
// of $table_is_view
if (isset($GLOBALS['tbl_is_view']) && $GLOBALS['tbl_is_view']) {
return true;
}
// old MySQL version: no view
if (PMA_MYSQL_INT_VERSION < 50000) {
return false;
}
if ( false === PMA_DBI_fetch_value('SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = \'' . $db . '\' AND TABLE_NAME = \'' . $table . '\';')) {
return false;
} else {
return true;
}
}

/**
* Counts and returns (or displays) the number of records in a table
*
* Revision 13 July 2001: Patch for limiting dump size from
* [email protected] & [email protected]
*
* @param string the current database name
* @param string the current table name
* @param boolean whether to retain or to displays the result
* @param boolean whether to force an exact count
*
* @return mixed the number of records if retain is required, true else
*
* @access public
*/
function PMA_countRecords($db, $table, $ret = false, $force_exact = false)
{
global $err_url, $cfg;
if (!$force_exact) {
$result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table, true) . '\';');
$showtable = PMA_DBI_fetch_assoc($result);
$num = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
if ($num < $cfg['MaxExactCount']) {
unset($num);
}
PMA_DBI_free_result($result);
}

$tbl_is_view = PMA_tableIsView($db, $table);

if (!isset($num)) {
if (! $tbl_is_view) {
$num = PMA_DBI_fetch_value('SELECT COUNT(*) AS num FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table));
// necessary?
if (! $num) {
$num = 0;
}
// since counting all rows of a view could be too long
} else {
$result = PMA_DBI_query('SELECT 1 FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT ' . $cfg['MaxExactCount'], null, PMA_DBI_QUERY_STORE);
$num = PMA_DBI_num_rows($result);
}
}
if ($ret) {
return $num;
} else {
// Note: as of PMA 2.8.0, we no longer seem to be using
// PMA_countRecords() in display mode.
echo number_format($num, 0, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
if ($tbl_is_view) {
echo '&nbsp;' . sprintf($GLOBALS['strViewMaxExactCount'], $cfg['MaxExactCount'], '[a@./Documentation.html#cfg_MaxExactCount@_blank]', '[/a]');
}
return true;
}
} // end of the 'PMA_countRecords()' function

/**
* Reloads navigation if needed.
*
Expand Down Expand Up @@ -2505,20 +2422,6 @@ function PMA_pageselector($url, $rows, $pageNow = 1, $nbTotalPage = 1,
return $gotopage;
} // end function

/**
* @TODO add documentation
*/
function PMA_generateAlterTable($oldcol, $newcol, $type, $length,
$attribute, $collation, $null, $default, $default_current_timestamp,
$extra, $comment='', $default_orig)
{
$empty_a = array();
return PMA_backquote($oldcol) . ' '
. PMA_generateFieldSpec($newcol, $type, $length, $attribute,
$collation, $null, $default, $default_current_timestamp, $extra,
$comment, $empty_a, -1, $default_orig);
} // end function

/**
* @TODO add documentation
*/
Expand Down
4 changes: 3 additions & 1 deletion libraries/display_export.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:

require_once './libraries/Table.class.php';

// Get relations & co. status
require_once('./libraries/relation.lib.php');
$cfgRelation = PMA_getRelationsParam();
Expand Down Expand Up @@ -774,7 +776,7 @@ function show_checked_option() {
<?php
echo sprintf( $strDumpXRows,
'<input type="text" name="limit_to" size="5" value="'
. ( isset( $unlim_num_rows ) ? $unlim_num_rows : PMA_countRecords( $db, $table, TRUE ) )
. ( isset( $unlim_num_rows ) ? $unlim_num_rows : PMA_Table::countRecords( $db, $table, TRUE ) )
. '" onfocus="this.select()" />',
'<input type="text" name="limit_from" value="0" size="5"'
.' onfocus="this.select()" /> ');
Expand Down
Loading

0 comments on commit cd340d8

Please sign in to comment.