Node_Table.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /**
  12. * Represents a columns node in the navigation tree
  13. *
  14. * @package PhpMyAdmin-Navigation
  15. */
  16. class Node_Table extends Node
  17. {
  18. /**
  19. * Initialises the class
  20. *
  21. * @param string $name An identifier for the new node
  22. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  23. * @param bool $is_group Whether this object has been created
  24. * while grouping nodes
  25. *
  26. * @return Node_Table
  27. */
  28. public function __construct($name, $type = Node::OBJECT, $is_group = false)
  29. {
  30. parent::__construct($name, $type, $is_group);
  31. switch ($GLOBALS['cfg']['NavigationTreeDefaultTabTable']) {
  32. case 'tbl_structure.php':
  33. $title = __('Structure');
  34. break;
  35. case 'tbl_sql.php':
  36. $title = __('SQL');
  37. break;
  38. case 'tbl_select.php':
  39. $title = __('Search');
  40. break;
  41. case 'tbl_change.php':
  42. $title = __('Insert');
  43. break;
  44. case 'sql.php':
  45. $title = __('Browse');
  46. break;
  47. }
  48. $this->icon = PMA_Util::getImage('b_browse.png', $title);
  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. if (! $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['DisableIS']) {
  78. $db = PMA_Util::sqlAddSlashes($db);
  79. $table = PMA_Util::sqlAddSlashes($table);
  80. $query = "SELECT COUNT(*) ";
  81. $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
  82. $query .= "WHERE `TABLE_NAME`='$table' ";
  83. $query .= "AND `TABLE_SCHEMA`='$db'";
  84. $retval = (int)PMA_DBI_fetch_value($query);
  85. } else {
  86. $db = PMA_Util::backquote($db);
  87. $table = PMA_Util::backquote($table);
  88. $query = "SHOW COLUMNS FROM $table FROM $db";
  89. $retval = (int)PMA_DBI_num_rows(PMA_DBI_try_query($query));
  90. }
  91. break;
  92. case 'indexes':
  93. $db = PMA_Util::backquote($db);
  94. $table = PMA_Util::backquote($table);
  95. $query = "SHOW INDEXES FROM $table FROM $db";
  96. $retval = (int)PMA_DBI_num_rows(PMA_DBI_try_query($query));
  97. break;
  98. case 'triggers':
  99. if (! $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['DisableIS']) {
  100. $db = PMA_Util::sqlAddSlashes($db);
  101. $table = PMA_Util::sqlAddSlashes($table);
  102. $query = "SELECT COUNT(*) ";
  103. $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
  104. $query .= "WHERE `EVENT_OBJECT_SCHEMA`='$db' ";
  105. $query .= "AND `EVENT_OBJECT_TABLE`='$table'";
  106. $retval = (int)PMA_DBI_fetch_value($query);
  107. } else {
  108. $db = PMA_Util::backquote($db);
  109. $table = PMA_Util::sqlAddSlashes($table);
  110. $query = "SHOW TRIGGERS FROM $db WHERE `Table` = '$table'";
  111. $retval = (int)PMA_DBI_num_rows(PMA_DBI_try_query($query));
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. return $retval;
  118. }
  119. /**
  120. * Returns the names of children of type $type present inside this container
  121. * This method is overridden by the Node_Database and Node_Table classes
  122. *
  123. * @param string $type The type of item we are looking for
  124. * ('tables', 'views', etc)
  125. * @param int $pos The offset of the list within the results
  126. * @param string $searchClause A string used to filter the results of the query
  127. *
  128. * @return array
  129. */
  130. public function getData($type, $pos, $searchClause = '')
  131. {
  132. $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
  133. $retval = array();
  134. $db = $this->realParent()->real_name;
  135. $table = $this->real_name;
  136. switch ($type) {
  137. case 'columns':
  138. if (! $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['DisableIS']) {
  139. $db = PMA_Util::sqlAddSlashes($db);
  140. $table = PMA_Util::sqlAddSlashes($table);
  141. $query = "SELECT `COLUMN_NAME` AS `name` ";
  142. $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
  143. $query .= "WHERE `TABLE_NAME`='$table' ";
  144. $query .= "AND `TABLE_SCHEMA`='$db' ";
  145. $query .= "ORDER BY `COLUMN_NAME` ASC ";
  146. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  147. $retval = PMA_DBI_fetch_result($query);
  148. } else {
  149. $db = PMA_Util::backquote($db);
  150. $table = PMA_Util::backquote($table);
  151. $query = "SHOW COLUMNS FROM $table FROM $db";
  152. $handle = PMA_DBI_try_query($query);
  153. if ($handle !== false) {
  154. $count = 0;
  155. while ($arr = PMA_DBI_fetch_array($handle)) {
  156. if ($pos <= 0 && $count < $maxItems) {
  157. $retval[] = $arr['Field'];
  158. $count++;
  159. }
  160. $pos--;
  161. }
  162. }
  163. }
  164. break;
  165. case 'indexes':
  166. $db = PMA_Util::backquote($db);
  167. $table = PMA_Util::backquote($table);
  168. $query = "SHOW INDEXES FROM $table FROM $db";
  169. $handle = PMA_DBI_try_query($query);
  170. if ($handle !== false) {
  171. $count = 0;
  172. while ($arr = PMA_DBI_fetch_array($handle)) {
  173. if (! in_array($arr['Key_name'], $retval)) {
  174. if ($pos <= 0 && $count < $maxItems) {
  175. $retval[] = $arr['Key_name'];
  176. $count++;
  177. }
  178. $pos--;
  179. }
  180. }
  181. }
  182. break;
  183. case 'triggers':
  184. if (! $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['DisableIS']) {
  185. $db = PMA_Util::sqlAddSlashes($db);
  186. $table = PMA_Util::sqlAddSlashes($table);
  187. $query = "SELECT `TRIGGER_NAME` AS `name` ";
  188. $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
  189. $query .= "WHERE `EVENT_OBJECT_SCHEMA`='$db' ";
  190. $query .= "AND `EVENT_OBJECT_TABLE`='$table' ";
  191. $query .= "ORDER BY `TRIGGER_NAME` ASC ";
  192. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  193. $retval = PMA_DBI_fetch_result($query);
  194. } else {
  195. $db = PMA_Util::backquote($db);
  196. $table = PMA_Util::sqlAddSlashes($table);
  197. $query = "SHOW TRIGGERS FROM $db WHERE `Table` = '$table'";
  198. $handle = PMA_DBI_try_query($query);
  199. if ($handle !== false) {
  200. $count = 0;
  201. while ($arr = PMA_DBI_fetch_array($handle)) {
  202. if ($pos <= 0 && $count < $maxItems) {
  203. $retval[] = $arr['Trigger'];
  204. $count++;
  205. }
  206. $pos--;
  207. }
  208. }
  209. }
  210. break;
  211. default:
  212. break;
  213. }
  214. return $retval;
  215. }
  216. }
  217. ?>