TableStats.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Contains abstract class to hold table preferences/statistics
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Table preferences/statistics
  13. *
  14. * This class preserves the table co-ordinates,fields
  15. * and helps in drawing/generating the tables.
  16. *
  17. * @package PhpMyAdmin
  18. * @abstract
  19. */
  20. abstract class TableStats
  21. {
  22. protected $diagram;
  23. protected $db;
  24. protected $pageNumber;
  25. protected $tableName;
  26. protected $showKeys;
  27. protected $showInfo;
  28. public $displayfield;
  29. public $fields = array();
  30. public $primary = array();
  31. public $x, $y;
  32. public $width = 0;
  33. public $heightCell = 0;
  34. /**
  35. * Constructor
  36. *
  37. * @param object $diagram schema diagram
  38. * @param string $db current db name
  39. * @param integer $pageNumber current page number (from the
  40. * $cfg['Servers'][$i]['table_coords'] table)
  41. * @param string $tableName table name
  42. * @param boolean $showKeys whether to display keys or not
  43. * @param boolean $showInfo whether to display table position or not
  44. */
  45. public function __construct(
  46. $diagram, $db, $pageNumber, $tableName, $showKeys, $showInfo
  47. ) {
  48. $this->diagram = $diagram;
  49. $this->db = $db;
  50. $this->pageNumber = $pageNumber;
  51. $this->tableName = $tableName;
  52. $this->showKeys = $showKeys;
  53. $this->showInfo = $showInfo;
  54. // checks whether the table exists
  55. // and loads fields
  56. $this->validateTableAndLoadFields();
  57. // load table coordinates
  58. $this->loadCoordinates();
  59. // loads display field
  60. $this->loadDisplayField();
  61. // loads primary keys
  62. $this->loadPrimaryKey();
  63. }
  64. /**
  65. * Validate whether the table exists.
  66. *
  67. * @return void
  68. */
  69. protected function validateTableAndLoadFields()
  70. {
  71. $sql = 'DESCRIBE ' . PMA_Util::backquote($this->tableName);
  72. $result = $GLOBALS['dbi']->tryQuery(
  73. $sql, null, PMA_DatabaseInterface::QUERY_STORE
  74. );
  75. if (! $result || ! $GLOBALS['dbi']->numRows($result)) {
  76. $this->showMissingTableError();
  77. }
  78. if ($this->showKeys) {
  79. $indexes = PMA_Index::getFromTable($this->tableName, $this->db);
  80. $all_columns = array();
  81. foreach ($indexes as $index) {
  82. $all_columns = array_merge(
  83. $all_columns,
  84. array_flip(array_keys($index->getColumns()))
  85. );
  86. }
  87. $this->fields = array_keys($all_columns);
  88. } else {
  89. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  90. $this->fields[] = $row[0];
  91. }
  92. }
  93. }
  94. /**
  95. * Displays an error when the table cannot be found.
  96. *
  97. * @return void
  98. * @abstract
  99. */
  100. protected abstract function showMissingTableError();
  101. /**
  102. * Loads coordinates of a table
  103. *
  104. * @return void
  105. */
  106. protected function loadCoordinates()
  107. {
  108. global $cfgRelation;
  109. $sql = "SELECT x, y FROM "
  110. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . "."
  111. . PMA_Util::backquote($cfgRelation['table_coords'])
  112. . " WHERE db_name = '" . PMA_Util::sqlAddSlashes($this->db) . "'"
  113. . " AND table_name = '" . PMA_Util::sqlAddSlashes($this->tableName) . "'"
  114. . " AND pdf_page_number = " . $this->pageNumber;
  115. $result = PMA_queryAsControlUser(
  116. $sql, false, PMA_DatabaseInterface::QUERY_STORE
  117. );
  118. if (! $result || ! $GLOBALS['dbi']->numRows($result)) {
  119. $this->showMissingCoordinatesError();
  120. }
  121. list($this->x, $this->y) = $GLOBALS['dbi']->fetchRow($result);
  122. $this->x = (double) $this->x;
  123. $this->y = (double) $this->y;
  124. }
  125. /**
  126. * Displays an error on missing coordinates
  127. *
  128. * @return void
  129. * @abstract
  130. */
  131. protected abstract function showMissingCoordinatesError();
  132. /**
  133. * Loads the table's display field
  134. *
  135. * @return void
  136. */
  137. protected function loadDisplayField()
  138. {
  139. $this->displayfield = PMA_getDisplayField($this->db, $this->tableName);
  140. }
  141. /**
  142. * Loads the PRIMARY key.
  143. *
  144. * @return void
  145. */
  146. protected function loadPrimaryKey()
  147. {
  148. $result = $GLOBALS['dbi']->query(
  149. 'SHOW INDEX FROM ' . PMA_Util::backquote($this->tableName) . ';',
  150. null, PMA_DatabaseInterface::QUERY_STORE
  151. );
  152. if ($GLOBALS['dbi']->numRows($result) > 0) {
  153. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  154. if ($row['Key_name'] == 'PRIMARY') {
  155. $this->primary[] = $row['Column_name'];
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * Returns title of the current table,
  162. * title can have the dimensions/co-ordinates of the table
  163. *
  164. * @return string title of the current table
  165. */
  166. protected function getTitle()
  167. {
  168. return ($this->showInfo
  169. ? sprintf('%.0fx%0.f', $this->width, $this->heightCell)
  170. : ''
  171. )
  172. . ' ' . $this->tableName;
  173. }
  174. }
  175. ?>