tbl_indexes.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions related to table indexes
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Function to get the name and type of the columns of a table
  13. *
  14. * @param string $db current database
  15. * @param string $table current table
  16. *
  17. * @return array
  18. */
  19. function PMA_getNameAndTypeOfTheColumns($db, $table)
  20. {
  21. $columns = array();
  22. foreach ($GLOBALS['dbi']->getColumnsFull($db, $table) as $row) {
  23. if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
  24. $tmp[2] = substr(
  25. preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
  26. );
  27. $columns[$row['Field']] = $tmp[1] . '('
  28. . str_replace(',', ', ', $tmp[2]) . ')';
  29. } else {
  30. $columns[$row['Field']] = $row['Type'];
  31. }
  32. } // end while
  33. return $columns;
  34. }
  35. /**
  36. * Function to handle the creation or edit of an index
  37. *
  38. * @param string $db current db
  39. * @param string $table current table
  40. * @param PMA_Index $index current index
  41. *
  42. * @return void
  43. */
  44. function PMA_handleCreateOrEditIndex($db, $table, $index)
  45. {
  46. $error = false;
  47. $sql_query = PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, $error);
  48. if (! $error) {
  49. $GLOBALS['dbi']->query($sql_query);
  50. $message = PMA_Message::success(
  51. __('Table %1$s has been altered successfully.')
  52. );
  53. $message->addParam($table);
  54. if ($GLOBALS['is_ajax_request'] == true) {
  55. $response = PMA_Response::getInstance();
  56. $response->addJSON('message', $message);
  57. $response->addJSON('index_table', PMA_Index::getView($table, $db));
  58. $response->addJSON(
  59. 'sql_query',
  60. PMA_Util::getMessage(null, $sql_query)
  61. );
  62. } else {
  63. include 'tbl_structure.php';
  64. }
  65. exit;
  66. } else {
  67. $response = PMA_Response::getInstance();
  68. $response->isSuccess(false);
  69. $response->addJSON('message', $error);
  70. exit;
  71. }
  72. }
  73. /**
  74. * Function to get the sql query for index creation or edit
  75. *
  76. * @param string $db current db
  77. * @param string $table current table
  78. * @param PMA_Index $index current index
  79. * @param bool &$error whether error occoured or not
  80. *
  81. * @return string
  82. */
  83. function PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, &$error)
  84. {
  85. // $sql_query is the one displayed in the query box
  86. $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
  87. . '.' . PMA_Util::backquote($table);
  88. // Drops the old index
  89. if (! empty($_REQUEST['old_index'])) {
  90. if ($_REQUEST['old_index'] == 'PRIMARY') {
  91. $sql_query .= ' DROP PRIMARY KEY,';
  92. } else {
  93. $sql_query .= ' DROP INDEX '
  94. . PMA_Util::backquote($_REQUEST['old_index']) . ',';
  95. }
  96. } // end if
  97. // Builds the new one
  98. switch ($index->getType()) {
  99. case 'PRIMARY':
  100. if ($index->getName() == '') {
  101. $index->setName('PRIMARY');
  102. } elseif ($index->getName() != 'PRIMARY') {
  103. $error = PMA_Message::error(
  104. __('The name of the primary key must be "PRIMARY"!')
  105. );
  106. }
  107. $sql_query .= ' ADD PRIMARY KEY';
  108. break;
  109. case 'FULLTEXT':
  110. case 'UNIQUE':
  111. case 'INDEX':
  112. case 'SPATIAL':
  113. if ($index->getName() == 'PRIMARY') {
  114. $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
  115. }
  116. $sql_query .= ' ADD ' . $index->getType() . ' '
  117. . ($index->getName() ? PMA_Util::backquote($index->getName()) : '');
  118. break;
  119. } // end switch
  120. $index_fields = array();
  121. foreach ($index->getColumns() as $key => $column) {
  122. $index_fields[$key] = PMA_Util::backquote($column->getName());
  123. if ($column->getSubPart()) {
  124. $index_fields[$key] .= '(' . $column->getSubPart() . ')';
  125. }
  126. } // end while
  127. if (empty($index_fields)) {
  128. $error = PMA_Message::error(__('No index parts defined!'));
  129. } else {
  130. $sql_query .= ' (' . implode(', ', $index_fields) . ')';
  131. }
  132. if (PMA_MYSQL_INT_VERSION > 50500) {
  133. $sql_query .= " COMMENT '"
  134. . PMA_Util::sqlAddSlashes($index->getComment())
  135. . "'";
  136. }
  137. $sql_query .= ';';
  138. return $sql_query;
  139. }
  140. /**
  141. * Function to prepare the form values for index
  142. *
  143. * @param string $db curent database
  144. * @param string $table current table
  145. *
  146. * @return PMA_Index
  147. */
  148. function PMA_prepareFormValues($db, $table)
  149. {
  150. if (isset($_REQUEST['index'])) {
  151. if (is_array($_REQUEST['index'])) {
  152. // coming already from form
  153. $index = new PMA_Index($_REQUEST['index']);
  154. } else {
  155. $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
  156. }
  157. } else {
  158. $index = new PMA_Index;
  159. }
  160. return $index;
  161. }
  162. /**
  163. * Function to get the number of fields for the form
  164. *
  165. * @param PMA_Index $index index
  166. *
  167. * @return int
  168. */
  169. function PMA_getNumberOfFieldsForForm($index)
  170. {
  171. if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
  172. // coming already from form
  173. $add_fields
  174. = count($_REQUEST['index']['columns']['names'])
  175. - $index->getColumnCount();
  176. if (isset($_REQUEST['add_fields'])) {
  177. $add_fields += $_REQUEST['added_fields'];
  178. }
  179. } elseif (isset($_REQUEST['create_index'])) {
  180. $add_fields = $_REQUEST['added_fields'];
  181. } else {
  182. $add_fields = 1;
  183. }// end preparing form values
  184. return $add_fields;
  185. }
  186. /**
  187. * Function to get form parameters
  188. *
  189. * @param string $db current db
  190. * @param string $table current table
  191. *
  192. * @return array
  193. */
  194. function PMA_getFormParameters($db, $table)
  195. {
  196. $form_params = array(
  197. 'db' => $db,
  198. 'table' => $table,
  199. );
  200. if (isset($_REQUEST['create_index'])) {
  201. $form_params['create_index'] = 1;
  202. } elseif (isset($_REQUEST['old_index'])) {
  203. $form_params['old_index'] = $_REQUEST['old_index'];
  204. } elseif (isset($_REQUEST['index'])) {
  205. $form_params['old_index'] = $_REQUEST['index'];
  206. }
  207. return $form_params;
  208. }
  209. /**
  210. * Function to get html for displaying the index form
  211. *
  212. * @param array $fields fields
  213. * @param PMA_Index $index index
  214. * @param array $form_params form parameters
  215. * @param int $add_fields number of fields in the form
  216. *
  217. * @return string
  218. */
  219. function PMA_getHtmlForIndexForm($fields, $index, $form_params, $add_fields)
  220. {
  221. $html = "";
  222. $html .= '<form action="tbl_indexes.php" method="post" name="index_frm" id="'
  223. . 'index_frm" class="ajax"'
  224. . 'onsubmit="if (typeof(this.elements[\'index[Key_name]\'].disabled) !='
  225. . ' \'undefined\') {'
  226. . 'this.elements[\'index[Key_name]\'].disabled = false}">';
  227. $html .= PMA_URL_getHiddenInputs($form_params);
  228. $html .= '<fieldset id="index_edit_fields">';
  229. $html .= '<div class="index_info">';
  230. $html .= '<div>'
  231. . '<div class="label">'
  232. . '<strong>'
  233. . '<label for="input_index_name">'
  234. . __('Index name:')
  235. . PMA_Util::showHint(
  236. PMA_Message::notice(
  237. __(
  238. '"PRIMARY" <b>must</b> be the name of'
  239. . ' and <b>only of</b> a primary key!'
  240. )
  241. )
  242. )
  243. . '</label>'
  244. . '</strong>'
  245. . '</div>'
  246. . '<input type="text" name="index[Key_name]" id="input_index_name"'
  247. . ' size="25"'
  248. . 'value="' . htmlspecialchars($index->getName()) . '"'
  249. . 'onfocus="this.select()" />'
  250. . '</div>';
  251. if (PMA_MYSQL_INT_VERSION > 50500) {
  252. $html .= '<div>'
  253. . '<div class="label">'
  254. . '<strong>'
  255. . '<label for="input_index_comment">'
  256. . __('Comment:')
  257. . '</label>'
  258. . '</strong>'
  259. . '</div>'
  260. . '<input type="text" name="index[Index_comment]" '
  261. . 'id="input_index_comment" size="30"'
  262. . 'value="' . htmlspecialchars($index->getComment()) . '"'
  263. . 'onfocus="this.select()" />'
  264. . '</div>';
  265. }
  266. $html .= '<div>'
  267. . '<div class="label">'
  268. . '<strong>'
  269. . '<label for="select_index_type">'
  270. . __('Index type:')
  271. . PMA_Util::showMySQLDocu('ALTER_TABLE')
  272. . '</label>'
  273. . '</strong>'
  274. . '</div>'
  275. . '<select name="index[Index_type]" id="select_index_type" >'
  276. . $index->generateIndexSelector()
  277. . '</select>'
  278. . '</div>';
  279. $html .= '<div class="clearfloat"></div>';
  280. $html .= '</div>';
  281. $html .= '<table id="index_columns">';
  282. $html .= '<thead>'
  283. . '<tr>'
  284. . '<th>' . __('Column') . '</th>'
  285. . '<th>' . __('Size') . '</th>'
  286. . '</tr>'
  287. . '</thead>';
  288. $odd_row = true;
  289. $spatial_types = array(
  290. 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
  291. 'multilinestring', 'multipolygon', 'geomtrycollection'
  292. );
  293. $html .= '<tbody>';
  294. /* @var $column PMA_Index_Column */
  295. foreach ($index->getColumns() as $column) {
  296. $html .= '<tr class="';
  297. $html .= $odd_row ? 'odd' : 'even';
  298. $html .= 'noclick">';
  299. $html .= '<td>';
  300. $html .= '<select name="index[columns][names][]">';
  301. $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
  302. foreach ($fields as $field_name => $field_type) {
  303. if (($index->getType() != 'FULLTEXT'
  304. || preg_match('/(char|text)/i', $field_type))
  305. && ($index->getType() != 'SPATIAL'
  306. || in_array($field_type, $spatial_types))
  307. ) {
  308. $html .= '<option value="' . htmlspecialchars($field_name) . '"'
  309. . (($field_name == $column->getName())
  310. ? ' selected="selected"'
  311. : '') . '>'
  312. . htmlspecialchars($field_name) . ' ['
  313. . htmlspecialchars($field_type) . ']'
  314. . '</option>' . "\n";
  315. }
  316. } // end foreach $fields
  317. $html .= '</select>';
  318. $html .= '</td>';
  319. $html .= '<td>';
  320. $html .= '<input type="text" size="5" onfocus="this.select()"'
  321. . 'name="index[columns][sub_parts][]" value="';
  322. if ($index->getType() != 'SPATIAL') {
  323. $html .= $column->getSubPart();
  324. }
  325. $html .= '"/>';
  326. $html .= '</td>';
  327. $html .= '</tr>';
  328. $odd_row = !$odd_row;
  329. } // end foreach $edited_index_info['Sequences']
  330. for ($i = 0; $i < $add_fields; $i++) {
  331. $html .= '<tr class="';
  332. $html .= $odd_row ? 'odd' : 'even';
  333. $html .= 'noclick">';
  334. $html .= '<td>';
  335. $html .= '<select name="index[columns][names][]">';
  336. $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
  337. foreach ($fields as $field_name => $field_type) {
  338. $html .= '<option value="' . htmlspecialchars($field_name) . '">'
  339. . htmlspecialchars($field_name) . ' ['
  340. . htmlspecialchars($field_type) . ']'
  341. . '</option>' . "\n";
  342. } // end foreach $fields
  343. $html .= '</select>';
  344. $html .= '</td>';
  345. $html .= '<td>'
  346. . '<input type="text" size="5" onfocus="this.select()"'
  347. . 'name="index[columns][sub_parts][]" value="" />'
  348. . '</td>';
  349. $html .= '</tr>';
  350. $odd_row = !$odd_row;
  351. } // end foreach $edited_index_info['Sequences']
  352. $html .= '</tbody>';
  353. $html .= '</table>';
  354. $html .= '</fieldset>';
  355. $html .= '<fieldset class="tblFooters">';
  356. $btn_value = sprintf(__('Add %s column(s) to index'), 1);
  357. $html .= '<div class="slider"></div>';
  358. $html .= '<div class="add_fields">';
  359. $html .= '<input type="submit" value="' . $btn_value . '" />';
  360. $html .= '</div>';
  361. $html .= '</fieldset>';
  362. $html .= '</form>';
  363. return $html;
  364. }
  365. ?>