00001 <?php
00002
00003
00004
00023 function PMA_duplicate_table_info($work, $pma_table, $get_fields, $where_fields, $new_fields) {
00024 global $cfgRelation;
00025
00026 $last_id = -1;
00027
00028 if ($cfgRelation[$work]) {
00029 $select_parts = array();
00030 $row_fields = array();
00031 foreach ($get_fields AS $nr => $get_field) {
00032 $select_parts[] = PMA_backquote($get_field);
00033 $row_fields[$get_field] = 'cc';
00034 }
00035
00036 $where_parts = array();
00037 foreach ($where_fields AS $_where => $_value) {
00038 $where_parts[] = PMA_backquote($_where) . ' = \'' . PMA_sqlAddslashes($_value) . '\'';
00039 }
00040
00041 $new_parts = array();
00042 $new_value_parts = array();
00043 foreach ($new_fields AS $_where => $_value) {
00044 $new_parts[] = PMA_backquote($_where);
00045 $new_value_parts[] = PMA_sqlAddslashes($_value);
00046 }
00047
00048 $table_copy_query = 'SELECT ' . implode(', ', $select_parts)
00049 . ' FROM ' . PMA_backquote($cfgRelation[$pma_table])
00050 . ' WHERE ' . implode(' AND ', $where_parts);
00051
00052
00053
00054 $table_copy_rs = PMA_query_as_cu($table_copy_query, TRUE, PMA_DBI_QUERY_STORE);
00055
00056 while ($table_copy_row = @PMA_DBI_fetch_assoc($table_copy_rs)) {
00057 $value_parts = array();
00058 foreach ($table_copy_row AS $_key => $_val) {
00059 if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') {
00060 $value_parts[] = PMA_sqlAddslashes($_val);
00061 }
00062 }
00063
00064 $new_table_query = 'INSERT IGNORE INTO ' . PMA_backquote($cfgRelation[$pma_table])
00065 . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ')'
00066 . ' VALUES '
00067 . ' (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')';
00068
00069 $new_table_rs = PMA_query_as_cu($new_table_query);
00070 $last_id = PMA_DBI_insert_id();
00071 }
00072
00073 return $last_id;
00074 }
00075
00076 return true;
00077 }
00078
00079
00086 function PMA_table_move_copy($source_db, $source_table, $target_db, $target_table, $what, $move) {
00087 global $cfgRelation, $dblist, $err_url, $sql_query;
00088
00089
00090 $GLOBALS['use_backquotes'] = 1;
00091 $GLOBALS['asfile'] = 1;
00092
00093
00094 if (count($dblist) > 0 &&
00095 (PMA_isInto($source_db, $dblist) == -1 || PMA_isInto($target_db, $dblist) == -1)) {
00096 exit();
00097 }
00098
00099 $source = PMA_backquote($source_db) . '.' . PMA_backquote($source_table);
00100 if (empty($target_db)) $target_db = $source_db;
00101
00102
00103
00104 PMA_DBI_select_db($target_db);
00105
00106 $target = PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
00107
00108
00109 if ($what != 'dataonly') {
00110 require_once('./libraries/export/sql.php');
00111
00112 $no_constraints_comments = true;
00113 $sql_structure = PMA_getTableDef($source_db, $source_table, "\n", $err_url);
00114 unset($no_constraints_comments);
00115
00116 $parsed_sql = PMA_SQP_parse($sql_structure);
00117
00118
00119 $i = 0;
00120 while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++;
00121
00122
00123 $parsed_sql[$i]['data'] = $target;
00124
00125
00126 $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only');
00127
00128 $drop_query = '';
00129 if (isset($GLOBALS['drop_if_exists']) && $GLOBALS['drop_if_exists'] == 'true') {
00130 $drop_query = 'DROP TABLE IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
00131 $result = PMA_DBI_query($drop_query);
00132
00133 if (isset($sql_query)) {
00134 $sql_query .= "\n" . $drop_query . ';';
00135 } else {
00136 $sql_query = $drop_query . ';';
00137 }
00138
00139
00140
00141 $maintain_relations = TRUE;
00142 }
00143
00144 $result = @PMA_DBI_query($sql_structure);
00145 if (isset($sql_query)) {
00146 $sql_query .= "\n" . $sql_structure . ';';
00147 } else {
00148 $sql_query = $sql_structure . ';';
00149 }
00150
00151 if (($move || isset($GLOBALS['constraints'])) && isset($GLOBALS['sql_constraints'])) {
00152 $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints']);
00153 $i = 0;
00154
00155
00156 while ($parsed_sql[$i]['type'] != 'quote_backtick') {
00157 $i++;
00158 }
00159
00160
00161 $parsed_sql[$i]['data'] = $target;
00162
00163
00164
00165
00166 $cnt = $parsed_sql['len'] - 1;
00167
00168 for ($j = $i; $j < $cnt; $j++) {
00169 if ($parsed_sql[$j]['type'] == 'alpha_reservedWord'
00170 && strtoupper($parsed_sql[$j]['data']) == 'CONSTRAINT') {
00171 if ($parsed_sql[$j+1]['type'] == 'quote_backtick') {
00172 $parsed_sql[$j+1]['data'] = '';
00173 }
00174 }
00175 }
00176
00177
00178
00179 $GLOBALS['sql_constraints'] = PMA_SQP_formatHtml($parsed_sql, 'query_only');
00180 $result = PMA_DBI_query($GLOBALS['sql_constraints']);
00181 if (isset($sql_query)) {
00182 $sql_query .= "\n" . $GLOBALS['sql_constraints'];
00183 } else {
00184 $sql_query = $GLOBALS['sql_constraints'];
00185 }
00186
00187 unset($GLOBALS['sql_constraints']);
00188 }
00189
00190 } else {
00191 $sql_query='';
00192 }
00193
00194
00195
00196 if ($what == 'data' || $what == 'dataonly') {
00197 $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source;
00198 PMA_DBI_query($sql_insert_data);
00199 $sql_query .= "\n\n" . $sql_insert_data . ';';
00200 }
00201
00202 require_once('./libraries/relation.lib.php');
00203 $cfgRelation = PMA_getRelationsParam();
00204
00205
00206 if ($move) {
00207
00208
00209
00210 PMA_DBI_select_db($source_db);
00211
00212 $sql_drop_table = 'DROP TABLE ' . $source;
00213 PMA_DBI_query($sql_drop_table);
00214
00215
00216 if ($cfgRelation['commwork']) {
00217 $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
00218 . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\', '
00219 . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
00220 . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
00221 . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
00222 $rmv_rs = PMA_query_as_cu($remove_query);
00223 unset($remove_query);
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 if ($cfgRelation['displaywork']) {
00237 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
00238 . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\', '
00239 . ' table_name = \'' . PMA_sqlAddslashes($target_table) . '\''
00240 . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
00241 . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
00242 $tb_rs = PMA_query_as_cu($table_query);
00243 unset($table_query);
00244 unset($tb_rs);
00245 }
00246
00247 if ($cfgRelation['relwork']) {
00248 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
00249 . ' SET foreign_table = \'' . PMA_sqlAddslashes($target_table) . '\','
00250 . ' foreign_db = \'' . PMA_sqlAddslashes($target_db) . '\''
00251 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($source_db) . '\''
00252 . ' AND foreign_table = \'' . PMA_sqlAddslashes($source_table) . '\'';
00253 $tb_rs = PMA_query_as_cu($table_query);
00254 unset($table_query);
00255 unset($tb_rs);
00256
00257 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
00258 . ' SET master_table = \'' . PMA_sqlAddslashes($target_table) . '\','
00259 . ' master_db = \'' . PMA_sqlAddslashes($target_db) . '\''
00260 . ' WHERE master_db = \'' . PMA_sqlAddslashes($source_db) . '\''
00261 . ' AND master_table = \'' . PMA_sqlAddslashes($source_table) . '\'';
00262 $tb_rs = PMA_query_as_cu($table_query);
00263 unset($table_query);
00264 unset($tb_rs);
00265 }
00266
00267
00268
00269
00270
00271
00272 if ($cfgRelation['pdfwork']) {
00273 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_coords'])
00274 . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\','
00275 . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
00276 . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
00277 . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
00278 $tb_rs = PMA_query_as_cu($table_query);
00279 unset($table_query);
00280 unset($tb_rs);
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 }
00299
00300 $sql_query .= "\n\n" . $sql_drop_table . ';';
00301 } else {
00302
00303 if ($what != 'dataonly' && !isset($maintain_relations)) {
00304 if ($cfgRelation['commwork']) {
00305
00306 $comments_copy_query = 'SELECT
00307 column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . '
00308 FROM ' . PMA_backquote($cfgRelation['column_info']) . '
00309 WHERE
00310 db_name = \'' . PMA_sqlAddslashes($source_db) . '\' AND
00311 table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
00312 $comments_copy_rs = PMA_query_as_cu($comments_copy_query);
00313
00314
00315 while ($comments_copy_row = PMA_DBI_fetch_assoc($comments_copy_rs)) {
00316 $new_comment_query = 'REPLACE INTO ' . PMA_backquote($cfgRelation['column_info'])
00317 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') '
00318 . ' VALUES('
00319 . '\'' . PMA_sqlAddslashes($target_db) . '\','
00320 . '\'' . PMA_sqlAddslashes($target_table) . '\','
00321 . '\'' . PMA_sqlAddslashes($comments_copy_row['column_name']) . '\''
00322 . ($cfgRelation['mimework'] ? ',\'' . PMA_sqlAddslashes($comments_copy_row['comment']) . '\','
00323 . '\'' . PMA_sqlAddslashes($comments_copy_row['mimetype']) . '\','
00324 . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation']) . '\','
00325 . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation_options']) . '\'' : '')
00326 . ')';
00327 $new_comment_rs = PMA_query_as_cu($new_comment_query);
00328 }
00329 }
00330
00331
00332
00333
00334 $get_fields = array('display_field');
00335 $where_fields = array('db_name' => $source_db, 'table_name' => $source_table);
00336 $new_fields = array('db_name' => $target_db, 'table_name' => $target_table);
00337 PMA_duplicate_table_info('displaywork', 'table_info', $get_fields, $where_fields, $new_fields);
00338
00339 $get_fields = array('master_field', 'foreign_db', 'foreign_table', 'foreign_field');
00340 $where_fields = array('master_db' => $source_db, 'master_table' => $source_table);
00341 $new_fields = array('master_db' => $target_db, 'master_table' => $target_table);
00342 PMA_duplicate_table_info('relwork', 'relation', $get_fields, $where_fields, $new_fields);
00343
00344 $get_fields = array('foreign_field', 'master_db', 'master_table', 'master_field');
00345 $where_fields = array('foreign_db' => $source_db, 'foreign_table' => $source_table);
00346 $new_fields = array('foreign_db' => $target_db, 'foreign_table' => $target_table);
00347 PMA_duplicate_table_info('relwork', 'relation', $get_fields, $where_fields, $new_fields);
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 }
00367 }
00368
00369 }
00370 ?>