Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / create_addfield.lib.php
blobae92f5903cf22862068fb5a865b46e2d60ee40a6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * set of functions that needed for tbl_create.php and tbl_addfield.php in pma
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Transforms the radio button field_key into 4 arrays
16 * @return array An array of arrays which represents column keys for each index type
18 function PMA_getIndexedColumns()
20 $field_cnt = count($_REQUEST['field_name']);
21 $field_primary = array();
22 $field_index = array();
23 $field_unique = array();
24 $field_fulltext = array();
25 for ($i = 0; $i < $field_cnt; ++$i) {
26 if (isset($_REQUEST['field_key'][$i])
27 && strlen($_REQUEST['field_name'][$i])
28 ) {
29 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
30 $field_primary[] = $i;
32 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
33 $field_index[] = $i;
35 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
36 $field_unique[] = $i;
38 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
39 $field_fulltext[] = $i;
41 } // end if
42 } // end for
44 return array(
45 $field_cnt, $field_primary, $field_index, $field_unique, $field_fulltext );
48 /**
49 * Initiate the column creation statement according to the table creation or
50 * add columns to a existing table
52 * @param int $field_cnt number of columns
53 * @param boolean $is_create_tbl true if requirement is to get the statement
54 * for table creation
56 * @return array $definitions An array of initial sql statements
57 * according to the request
59 function PMA_buildColumnCreationStatement($field_cnt ,$is_create_tbl = true)
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;
70 $definition = PMA_getStatementPrefix($is_create_tbl) .
71 PMA_Table::generateFieldSpec(
72 $_REQUEST['field_name'][$i],
73 $_REQUEST['field_type'][$i],
74 $i,
75 $_REQUEST['field_length'][$i],
76 $_REQUEST['field_attribute'][$i],
77 isset($_REQUEST['field_collation'][$i])
78 ? $_REQUEST['field_collation'][$i]
79 : '',
80 isset($_REQUEST['field_null'][$i])
81 ? $_REQUEST['field_null'][$i]
82 : 'NOT NULL',
83 $_REQUEST['field_default_type'][$i],
84 $_REQUEST['field_default_value'][$i],
85 isset($_REQUEST['field_extra'][$i])
86 ? $_REQUEST['field_extra'][$i]
87 : false,
88 isset($_REQUEST['field_comments'][$i])
89 ? $_REQUEST['field_comments'][$i]
90 : '',
91 $field_primary
95 $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl);
96 $definitions[] = $definition;
97 } // end for
99 return $definitions;
103 * Set column creation suffix according to requested position of the new column
105 * @param int $current_field_num current column number
106 * @param boolean $is_create_tbl true if requirement is to get the statement
107 * for table creation
109 * @return string $sql_suffix suffix
111 function PMA_setColumnCreationStatementSuffix($current_field_num ,$is_create_tbl = true)
113 // no suffix is needed if request is a table creation
114 $sql_suffix = " ";
115 if (! $is_create_tbl) {
116 if ($_REQUEST['field_where'] != 'last') {
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']);
125 } else {
126 $sql_suffix .= ' AFTER '
127 . PMA_Util::backquote(
128 $_REQUEST['field_name'][$current_field_num - 1]
133 return $sql_suffix;
137 * Create relevent index statements
139 * @param array $indexed_fields an array of index columns
140 * @param string $index_type index type that which represents
141 * the index type of $indexed_fields
142 * @param boolean $is_create_tbl true if requirement is to get the statement
143 * for table creation
145 * @return array an array of sql statements for indexes
147 function PMA_buildIndexStatements($indexed_fields, $index_type, $is_create_tbl = true)
149 $statement = array();
150 if (count($indexed_fields)) {
151 $fields = array();
152 foreach ($indexed_fields as $field_nr) {
153 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
155 $statement[] = PMA_getStatementPrefix($is_create_tbl)
156 .' '.$index_type.' (' . implode(', ', $fields) . ') ';
157 unset($fields);
160 return $statement;
164 * Statement prefix for the PMA_buildColumnCreationStatement()
166 * @param boolean $is_create_tbl true if requirement is to get the statement
167 * for table creation
169 * @return string $sql_prefix prefix
171 function PMA_getStatementPrefix($is_create_tbl = true)
173 $sql_prefix = " ";
174 if (! $is_create_tbl) {
175 $sql_prefix = ' ADD ';
177 return $sql_prefix;
181 * Returns sql statement according to the column and index specifications as requested
183 * @param boolean $is_create_tbl true if requirement is to get the statement
184 * for table creation
186 * @return string sql statement
188 function PMA_getColumnCreationStatements($is_create_tbl = true)
190 $definitions = array();
191 $sql_statement = "";
192 list($field_cnt, $field_primary, $field_index,
193 $field_unique, $field_fulltext
194 ) = PMA_getIndexedColumns();
195 $definitions = PMA_buildColumnCreationStatement($field_cnt, $is_create_tbl);
197 // Builds the primary keys statements
198 $primary_key_statements = PMA_buildIndexStatements(
199 $field_primary, " PRIMARY KEY ", $is_create_tbl
201 $definitions = array_merge($definitions, $primary_key_statements);
203 // Builds the indexes statements
204 $index_statements = PMA_buildIndexStatements(
205 $field_index, " INDEX ", $is_create_tbl
207 $definitions = array_merge($definitions, $index_statements);
209 // Builds the uniques statements
210 $unique_statements = PMA_buildIndexStatements(
211 $field_unique, " UNIQUE ", $is_create_tbl
213 $definitions = array_merge($definitions, $unique_statements);
215 // Builds the fulltext statements
216 $fulltext_statements = PMA_buildIndexStatements(
217 $field_fulltext, " FULLTEXT ", $is_create_tbl
219 $definitions = array_merge($definitions, $fulltext_statements);
221 if (count($definitions)) {
222 $sql_statement = implode(', ', $definitions);
224 $sql_statement = preg_replace('@, $@', '', $sql_statement);
226 return $sql_statement;