create_addfield.lib.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * set of functions for tbl_create.php and tbl_addfield.php
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Transforms the radio button field_key into 4 arrays
  13. *
  14. * @return array An array of arrays which represents column keys for each index type
  15. */
  16. function PMA_getIndexedColumns()
  17. {
  18. $field_cnt = count($_REQUEST['field_name']);
  19. $field_primary = array();
  20. $field_index = array();
  21. $field_unique = array();
  22. $field_fulltext = array();
  23. for ($i = 0; $i < $field_cnt; ++$i) {
  24. if (isset($_REQUEST['field_key'][$i])
  25. && strlen($_REQUEST['field_name'][$i])
  26. ) {
  27. if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
  28. $field_primary[] = $i;
  29. }
  30. if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
  31. $field_index[] = $i;
  32. }
  33. if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
  34. $field_unique[] = $i;
  35. }
  36. if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
  37. $field_fulltext[] = $i;
  38. }
  39. } // end if
  40. } // end for
  41. return array(
  42. $field_cnt, $field_primary, $field_index, $field_unique,
  43. $field_fulltext
  44. );
  45. }
  46. /**
  47. * Initiate the column creation statement according to the table creation or
  48. * add columns to a existing table
  49. *
  50. * @param int $field_cnt number of columns
  51. * @param int &$field_primary primary index field
  52. * @param boolean $is_create_tbl true if requirement is to get the statement
  53. * for table creation
  54. *
  55. * @return array $definitions An array of initial sql statements
  56. * according to the request
  57. */
  58. function PMA_buildColumnCreationStatement(
  59. $field_cnt, &$field_primary, $is_create_tbl = true
  60. ) {
  61. $definitions = array();
  62. for ($i = 0; $i < $field_cnt; ++$i) {
  63. // '0' is also empty for php :-(
  64. if (empty($_REQUEST['field_name'][$i])
  65. && $_REQUEST['field_name'][$i] != '0'
  66. ) {
  67. continue;
  68. }
  69. $definition = PMA_getStatementPrefix($is_create_tbl) .
  70. PMA_Table::generateFieldSpec(
  71. $_REQUEST['field_name'][$i],
  72. $_REQUEST['field_type'][$i],
  73. $i,
  74. $_REQUEST['field_length'][$i],
  75. $_REQUEST['field_attribute'][$i],
  76. isset($_REQUEST['field_collation'][$i])
  77. ? $_REQUEST['field_collation'][$i]
  78. : '',
  79. isset($_REQUEST['field_null'][$i])
  80. ? $_REQUEST['field_null'][$i]
  81. : 'NOT NULL',
  82. $_REQUEST['field_default_type'][$i],
  83. $_REQUEST['field_default_value'][$i],
  84. isset($_REQUEST['field_extra'][$i])
  85. ? $_REQUEST['field_extra'][$i]
  86. : false,
  87. isset($_REQUEST['field_comments'][$i])
  88. ? $_REQUEST['field_comments'][$i]
  89. : '',
  90. $field_primary
  91. );
  92. $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl);
  93. $definitions[] = $definition;
  94. } // end for
  95. return $definitions;
  96. }
  97. /**
  98. * Set column creation suffix according to requested position of the new column
  99. *
  100. * @param int $current_field_num current column number
  101. * @param boolean $is_create_tbl true if requirement is to get the statement
  102. * for table creation
  103. *
  104. * @return string $sql_suffix suffix
  105. */
  106. function PMA_setColumnCreationStatementSuffix($current_field_num,
  107. $is_create_tbl = true
  108. ) {
  109. // no suffix is needed if request is a table creation
  110. $sql_suffix = " ";
  111. if ($is_create_tbl) {
  112. return $sql_suffix;
  113. }
  114. if ($_REQUEST['field_where'] == 'last') {
  115. return $sql_suffix;
  116. }
  117. // Only the first field can be added somewhere other than at the end
  118. if ($current_field_num == 0) {
  119. if ($_REQUEST['field_where'] == 'first') {
  120. $sql_suffix .= ' FIRST';
  121. } else {
  122. $sql_suffix .= ' AFTER '
  123. . PMA_Util::backquote($_REQUEST['after_field']);
  124. }
  125. } else {
  126. $sql_suffix .= ' AFTER '
  127. . PMA_Util::backquote(
  128. $_REQUEST['field_name'][$current_field_num - 1]
  129. );
  130. }
  131. return $sql_suffix;
  132. }
  133. /**
  134. * Create relevant index statements
  135. *
  136. * @param array $indexed_fields an array of index columns
  137. * @param string $index_type index type that which represents
  138. * the index type of $indexed_fields
  139. * @param boolean $is_create_tbl true if requirement is to get the statement
  140. * for table creation
  141. *
  142. * @return array an array of sql statements for indexes
  143. */
  144. function PMA_buildIndexStatements($indexed_fields, $index_type,
  145. $is_create_tbl = true
  146. ) {
  147. $statement = array();
  148. if (!count($indexed_fields)) {
  149. return $statement;
  150. }
  151. $fields = array();
  152. foreach ($indexed_fields as $field_nr) {
  153. $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
  154. }
  155. $statement[] = PMA_getStatementPrefix($is_create_tbl)
  156. . ' ' . $index_type . ' (' . implode(', ', $fields) . ') ';
  157. unset($fields);
  158. return $statement;
  159. }
  160. /**
  161. * Statement prefix for the PMA_buildColumnCreationStatement()
  162. *
  163. * @param boolean $is_create_tbl true if requirement is to get the statement
  164. * for table creation
  165. *
  166. * @return string $sql_prefix prefix
  167. */
  168. function PMA_getStatementPrefix($is_create_tbl = true)
  169. {
  170. $sql_prefix = " ";
  171. if (! $is_create_tbl) {
  172. $sql_prefix = ' ADD ';
  173. }
  174. return $sql_prefix;
  175. }
  176. /**
  177. * Returns sql statement according to the column and index specifications as
  178. * requested
  179. *
  180. * @param boolean $is_create_tbl true if requirement is to get the statement
  181. * for table creation
  182. *
  183. * @return string sql statement
  184. */
  185. function PMA_getColumnCreationStatements($is_create_tbl = true)
  186. {
  187. $definitions = array();
  188. $sql_statement = "";
  189. list($field_cnt, $field_primary, $field_index,
  190. $field_unique, $field_fulltext
  191. ) = PMA_getIndexedColumns();
  192. $definitions = PMA_buildColumnCreationStatement(
  193. $field_cnt, $field_primary, $is_create_tbl
  194. );
  195. // Builds the primary keys statements
  196. $primary_key_statements = PMA_buildIndexStatements(
  197. $field_primary, " PRIMARY KEY ", $is_create_tbl
  198. );
  199. $definitions = array_merge($definitions, $primary_key_statements);
  200. // Builds the indexes statements
  201. $index_statements = PMA_buildIndexStatements(
  202. $field_index, " INDEX ", $is_create_tbl
  203. );
  204. $definitions = array_merge($definitions, $index_statements);
  205. // Builds the uniques statements
  206. $unique_statements = PMA_buildIndexStatements(
  207. $field_unique, " UNIQUE ", $is_create_tbl
  208. );
  209. $definitions = array_merge($definitions, $unique_statements);
  210. // Builds the fulltext statements
  211. $fulltext_statements = PMA_buildIndexStatements(
  212. $field_fulltext, " FULLTEXT ", $is_create_tbl
  213. );
  214. $definitions = array_merge($definitions, $fulltext_statements);
  215. if (count($definitions)) {
  216. $sql_statement = implode(', ', $definitions);
  217. }
  218. $sql_statement = preg_replace('@, $@', '', $sql_statement);
  219. return $sql_statement;
  220. }
  221. /**
  222. * Function to get table creation sql query
  223. *
  224. * @param string $db database name
  225. * @param string $table table name
  226. *
  227. * @return string
  228. */
  229. function PMA_getTableCreationQuery($db, $table)
  230. {
  231. // get column addition statements
  232. $sql_statement = PMA_getColumnCreationStatements(true);
  233. // Builds the 'create table' statement
  234. $sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.'
  235. . PMA_Util::backquote($table) . ' (' . $sql_statement . ')';
  236. // Adds table type, character set, comments and partition definition
  237. if (!empty($_REQUEST['tbl_storage_engine'])
  238. && ($_REQUEST['tbl_storage_engine'] != 'Default')
  239. ) {
  240. $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
  241. }
  242. if (!empty($_REQUEST['tbl_collation'])) {
  243. $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
  244. }
  245. if (!empty($_REQUEST['comment'])) {
  246. $sql_query .= ' COMMENT = \''
  247. . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
  248. }
  249. if (!empty($_REQUEST['partition_definition'])) {
  250. $sql_query .= ' ' . PMA_Util::sqlAddSlashes(
  251. $_REQUEST['partition_definition']
  252. );
  253. }
  254. $sql_query .= ';';
  255. return $sql_query;
  256. }
  257. /**
  258. * Function to get the number of fields for the table creation form
  259. *
  260. * @return int
  261. */
  262. function PMA_getNumberOfFieldsFromRequest()
  263. {
  264. if (isset($_REQUEST['submit_num_fields'])) {
  265. $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
  266. } elseif (isset($_REQUEST['num_fields'])
  267. && intval($_REQUEST['num_fields']) > 0
  268. ) {
  269. $num_fields = (int) $_REQUEST['num_fields'];
  270. } else {
  271. $num_fields = 4;
  272. }
  273. return $num_fields;
  274. }
  275. /**
  276. * Function to execute the column creation statement
  277. *
  278. * @param string $db current database
  279. * @param string $table current table
  280. * @param string $err_url error page url
  281. *
  282. * @return array
  283. */
  284. function PMA_tryColumnCreationQuery($db, $table, $err_url)
  285. {
  286. // get column addition statements
  287. $sql_statement = PMA_getColumnCreationStatements(false);
  288. // To allow replication, we first select the db to use and then run queries
  289. // on this db.
  290. $GLOBALS['dbi']->selectDb($db)
  291. or PMA_Util::mysqlDie(
  292. $GLOBALS['dbi']->getError(),
  293. 'USE ' . PMA_Util::backquote($db),
  294. false,
  295. $err_url
  296. );
  297. $sql_query = 'ALTER TABLE ' .
  298. PMA_Util::backquote($table) . ' ' . $sql_statement . ';';
  299. return array($GLOBALS['dbi']->tryQuery($sql_query) , $sql_query);
  300. }
  301. ?>