2
/* $Id: tbl_addfield.php 9601 2006-10-25 10:55:20Z nijel $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
6
* Get some core libraries
8
require_once './libraries/common.lib.php';
9
require_once './libraries/Table.class.php';
11
$js_to_run = 'functions.js';
12
require_once './libraries/header.inc.php';
15
PMA_checkParameters(array('db', 'table'));
19
* Defines the url to return to in case of error in a sql statement
21
$err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
24
* The form used to define the field to add has been submitted
27
if (isset($submit_num_fields)) {
28
if (isset($orig_after_field)) {
29
$after_field = $orig_after_field;
31
if (isset($orig_field_where)) {
32
$field_where = $orig_field_where;
34
$num_fields = $orig_num_fields + $added_fields;
36
} elseif (isset($do_save_data)) {
39
// Transforms the radio button field_key into 3 arrays
40
$field_cnt = count($field_name);
41
for ($i = 0; $i < $field_cnt; ++$i) {
42
if (isset(${'field_key_' . $i})) {
43
if (${'field_key_' . $i} == 'primary_' . $i) {
44
$field_primary[] = $i;
46
if (${'field_key_' . $i} == 'index_' . $i) {
49
if (${'field_key_' . $i} == 'unique_' . $i) {
54
// Builds the field creation statement and alters the table
56
for ($i = 0; $i < $field_cnt; ++$i) {
57
// '0' is also empty for php :-(
58
if (empty($field_name[$i]) && $field_name[$i] != '0') {
62
$query .= PMA_Table::generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i);
64
if ($field_where != 'last') {
65
// Only the first field can be added somewhere other than at the end
67
if ($field_where == 'first') {
70
$query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
73
$query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
78
$query = preg_replace('@, ADD $@', '', $query);
80
// To allow replication, we first select the db to use and then run queries
82
PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
83
$sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
84
$error_create = FALSE;
85
PMA_DBI_try_query($sql_query) or $error_create = TRUE;
87
if ($error_create == false) {
89
$sql_query_cpy = $sql_query . ';';
91
// Builds the primary keys statements and updates the table
93
if (isset($field_primary)) {
94
$primary_cnt = count($field_primary);
95
for ($i = 0; $i < $primary_cnt; $i++) {
96
$j = $field_primary[$i];
97
if (isset($field_name[$j]) && strlen($field_name[$j])) {
98
$primary .= PMA_backquote($field_name[$j]) . ', ';
101
$primary = preg_replace('@, $@', '', $primary);
102
if (strlen($primary)) {
103
$sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ');';
104
$result = PMA_DBI_query($sql_query);
105
$sql_query_cpy .= "\n" . $sql_query . ';';
109
// Builds the indexes statements and updates the table
111
if (isset($field_index)) {
112
$index_cnt = count($field_index);
113
for ($i = 0; $i < $index_cnt; $i++) {
114
$j = $field_index[$i];
115
if (isset($field_name[$j]) && strlen($field_name[$j])) {
116
$index .= PMA_backquote($field_name[$j]) . ', ';
119
$index = preg_replace('@, $@', '', $index);
120
if (strlen($index)) {
121
$sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
122
$result = PMA_DBI_query($sql_query);
123
$sql_query_cpy .= "\n" . $sql_query . ';';
127
// Builds the uniques statements and updates the table
129
if (isset($field_unique)) {
130
$unique_cnt = count($field_unique);
131
for ($i = 0; $i < $unique_cnt; $i++) {
132
$j = $field_unique[$i];
133
if (isset($field_name[$j]) && strlen($field_name[$j])) {
134
$unique .= PMA_backquote($field_name[$j]) . ', ';
137
$unique = preg_replace('@, $@', '', $unique);
138
if (strlen($unique)) {
139
$sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
140
$result = PMA_DBI_query($sql_query);
141
$sql_query_cpy .= "\n" . $sql_query . ';';
146
// Builds the fulltext statements and updates the table
148
if (isset($field_fulltext)) {
149
$fulltext_cnt = count($field_fulltext);
150
for ($i = 0; $i < $fulltext_cnt; $i++) {
151
$j = $field_fulltext[$i];
152
$fulltext .= PMA_backquote($field_name[$j]) . ', ';
154
$fulltext = preg_replace('@, $@', '', $fulltext);
155
if (strlen($fulltext)) {
156
$sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
157
$result = PMA_DBI_query($sql_query);
158
$sql_query_cpy .= "\n" . $sql_query . ';';
162
// garvin: If comments were sent, enable relation stuff
163
require_once('./libraries/relation.lib.php');
164
require_once('./libraries/transformations.lib.php');
166
$cfgRelation = PMA_getRelationsParam();
168
// garvin: Update comment table, if a comment was set.
169
if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
170
foreach ($field_comments AS $fieldindex => $fieldcomment) {
171
if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
172
PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
177
// garvin: Update comment table for mime types [MIME]
178
if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
179
foreach ($field_mimetype AS $fieldindex => $mimetype) {
180
if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
181
PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
186
// Go back to the structure sub-page
187
$sql_query = $sql_query_cpy;
188
unset($sql_query_cpy);
189
$message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
190
$active_page = 'tbl_structure.php';
191
require('./tbl_structure.php');
193
PMA_mysqlDie('', '', '', $err_url, FALSE);
194
// garvin: An error happened while inserting/updating a table definition.
195
// to prevent total loss of that data, we embed the form once again.
196
// The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
197
$num_fields = $orig_num_fields;
198
if (isset($orig_after_field)) {
199
$after_field = $orig_after_field;
201
if (isset($orig_field_where)) {
202
$field_where = $orig_field_where;
206
} // end do alter table
209
* Displays the form used to define the new field
211
if ($abort == FALSE) {
213
* Gets tables informations
215
require_once('./libraries/tbl_common.php');
216
require_once('./libraries/tbl_info.inc.php');
218
* Displays top menu links
220
$active_page = 'tbl_structure.php';
221
require_once('./libraries/tbl_links.inc.php');
225
$action = 'tbl_addfield.php';
226
require_once('./libraries/tbl_properties.inc.php');
228
// Diplays the footer
230
require_once('./libraries/footer.inc.php');