Node_Table.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functionality for the navigation tree
  5. *
  6. * @package PhpMyAdmin-Navigation
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'libraries/navigation/Nodes/Node_DatabaseChild.class.php';
  12. /**
  13. * Represents a columns node in the navigation tree
  14. *
  15. * @package PhpMyAdmin-Navigation
  16. */
  17. class Node_Table extends Node_DatabaseChild
  18. {
  19. /**
  20. * Initialises the class
  21. *
  22. * @param string $name An identifier for the new node
  23. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  24. * @param bool $is_group Whether this object has been created
  25. * while grouping nodes
  26. *
  27. * @return Node_Table
  28. */
  29. public function __construct($name, $type = Node::OBJECT, $is_group = false)
  30. {
  31. parent::__construct($name, $type, $is_group);
  32. switch($GLOBALS['cfg']['NavigationTreeDefaultTabTable']) {
  33. case 'tbl_structure.php':
  34. $this->icon = PMA_Util::getImage('b_props.png', __('Structure'));
  35. break;
  36. case 'tbl_select.php':
  37. $this->icon = PMA_Util::getImage('b_search.png', __('Search'));
  38. break;
  39. case 'tbl_change.php':
  40. $this->icon = PMA_Util::getImage('b_insrow.png', __('Insert'));
  41. break;
  42. case 'tbl_sql.php':
  43. $this->icon = PMA_Util::getImage('b_sql.png', __('SQL'));
  44. break;
  45. case 'sql.php':
  46. $this->icon = PMA_Util::getImage('b_browse.png', __('Browse'));
  47. break;
  48. }
  49. $this->links = array(
  50. 'text' => $GLOBALS['cfg']['DefaultTabTable']
  51. . '?server=' . $GLOBALS['server']
  52. . '&amp;db=%2$s&amp;table=%1$s'
  53. . '&amp;pos=0&amp;token=' . $GLOBALS['token'],
  54. 'icon' => $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
  55. . '?server=' . $GLOBALS['server']
  56. . '&amp;db=%2$s&amp;table=%1$s&amp;token=' . $GLOBALS['token']
  57. );
  58. $this->classes = 'table';
  59. }
  60. /**
  61. * Returns the number of children of type $type present inside this container
  62. * This method is overridden by the Node_Database and Node_Table classes
  63. *
  64. * @param string $type The type of item we are looking for
  65. * ('columns' or 'indexes')
  66. * @param string $searchClause A string used to filter the results of the query
  67. *
  68. * @return int
  69. */
  70. public function getPresence($type = '', $searchClause = '')
  71. {
  72. $retval = 0;
  73. $db = $this->realParent()->real_name;
  74. $table = $this->real_name;
  75. switch ($type) {
  76. case 'columns':
  77. $db = PMA_Util::sqlAddSlashes($db);
  78. $table = PMA_Util::sqlAddSlashes($table);
  79. $query = "SELECT COUNT(*) ";
  80. $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
  81. $query .= "WHERE `TABLE_NAME`='$table' ";
  82. $query .= "AND `TABLE_SCHEMA`='$db'";
  83. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  84. break;
  85. case 'indexes':
  86. $db = PMA_Util::backquote($db);
  87. $table = PMA_Util::backquote($table);
  88. $query = "SHOW INDEXES FROM $table FROM $db";
  89. $retval = (int)$GLOBALS['dbi']->numRows(
  90. $GLOBALS['dbi']->tryQuery($query)
  91. );
  92. break;
  93. case 'triggers':
  94. $db = PMA_Util::sqlAddSlashes($db);
  95. $table = PMA_Util::sqlAddSlashes($table);
  96. $query = "SELECT COUNT(*) ";
  97. $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
  98. $query .= "WHERE `EVENT_OBJECT_SCHEMA`='$db' ";
  99. $query .= "AND `EVENT_OBJECT_TABLE`='$table'";
  100. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  101. break;
  102. default:
  103. break;
  104. }
  105. return $retval;
  106. }
  107. /**
  108. * Returns the names of children of type $type present inside this container
  109. * This method is overridden by the Node_Database and Node_Table classes
  110. *
  111. * @param string $type The type of item we are looking for
  112. * ('tables', 'views', etc)
  113. * @param int $pos The offset of the list within the results
  114. * @param string $searchClause A string used to filter the results of the query
  115. *
  116. * @return array
  117. */
  118. public function getData($type, $pos, $searchClause = '')
  119. {
  120. $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
  121. $retval = array();
  122. $db = $this->realParent()->real_name;
  123. $table = $this->real_name;
  124. switch ($type) {
  125. case 'columns':
  126. $db = PMA_Util::sqlAddSlashes($db);
  127. $table = PMA_Util::sqlAddSlashes($table);
  128. $query = "SELECT `COLUMN_NAME` AS `name` ";
  129. $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
  130. $query .= "WHERE `TABLE_NAME`='$table' ";
  131. $query .= "AND `TABLE_SCHEMA`='$db' ";
  132. $query .= "ORDER BY `COLUMN_NAME` ASC ";
  133. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  134. $retval = $GLOBALS['dbi']->fetchResult($query);
  135. break;
  136. case 'indexes':
  137. $db = PMA_Util::backquote($db);
  138. $table = PMA_Util::backquote($table);
  139. $query = "SHOW INDEXES FROM $table FROM $db";
  140. $handle = $GLOBALS['dbi']->tryQuery($query);
  141. if ($handle === false) {
  142. break;
  143. }
  144. $count = 0;
  145. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  146. if (! in_array($arr['Key_name'], $retval)) {
  147. if ($pos <= 0 && $count < $maxItems) {
  148. $retval[] = $arr['Key_name'];
  149. $count++;
  150. }
  151. $pos--;
  152. }
  153. }
  154. break;
  155. case 'triggers':
  156. $db = PMA_Util::sqlAddSlashes($db);
  157. $table = PMA_Util::sqlAddSlashes($table);
  158. $query = "SELECT `TRIGGER_NAME` AS `name` ";
  159. $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
  160. $query .= "WHERE `EVENT_OBJECT_SCHEMA`='$db' ";
  161. $query .= "AND `EVENT_OBJECT_TABLE`='$table' ";
  162. $query .= "ORDER BY `TRIGGER_NAME` ASC ";
  163. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  164. $retval = $GLOBALS['dbi']->fetchResult($query);
  165. break;
  166. default:
  167. break;
  168. }
  169. return $retval;
  170. }
  171. /**
  172. * Returns the type of the item represented by the node.
  173. *
  174. * @return string type of the item
  175. */
  176. protected function getItemType()
  177. {
  178. return 'table';
  179. }
  180. }
  181. ?>