3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Insert data from one table to another one
8 * @param string the original insert statement
10 * @global string the database name
11 * @global string the original table name
12 * @global string the target database and table names
13 * @global string the sql query used to copy the data
15 function PMA_myHandler($sql_insert = '')
17 global $db, $table, $target;
18 global $sql_insert_data;
20 $sql_insert = eregi_replace('INSERT INTO (`?)' . $table . '(`?)', 'INSERT INTO ' . $target, $sql_insert);
21 $result = PMA_mysql_query($sql_insert) or PMA_mysqlDie('', $sql_insert, '', $GLOBALS['err_url']);
23 $sql_insert_data .= $sql_insert . ';' . "\n";
24 } // end of the 'PMA_myHandler()' function
27 * Inserts existing entries in a PMA_* table by reading a value from an old entry
29 * @param string The array index, which Relation feature to check
30 * ('relwork', 'commwork', ...)
31 * @param string The array index, which PMA-table to update
32 * ('bookmark', 'relation', ...)
33 * @param array Which fields will be SELECT'ed from the old entry
34 * @param array Which fields will be used for the WHERE query
35 * (array('FIELDNAME' => 'FIELDVALUE'))
36 * @param array Which fields will be used as new VALUES. These are the important
37 * keys which differ from the old entry.
38 * (array('FIELDNAME' => 'NEW FIELDVALUE'))
40 * @global string relation variable
42 * @author Garvin Hicking <me@supergarv.de>
44 function PMA_duplicate_table($work, $pma_table, $get_fields, $where_fields, $new_fields) {
49 if ($cfgRelation[$work]) {
51 $select_parts = array();
52 $row_fields = array();
53 while(list($nr, $get_field) = each($get_fields)) {
54 $select_parts[] = PMA_backquote($get_field);
55 $row_fields[$get_field] = 'cc';
58 @reset
($where_fields);
59 $where_parts = array();
60 while(list($_where, $_value) = each($where_fields)) {
61 $where_parts[] = PMA_backquote($_where) . ' = \'' . PMA_sqlAddslashes($_value) . '\'';
66 $new_value_parts = array();
67 while(list($_where, $_value) = each($new_fields)) {
68 $new_parts[] = PMA_backquote($_where);
69 $new_value_parts[] = PMA_sqlAddslashes($_value);
72 $table_copy_query = 'SELECT ' . implode(', ', $select_parts)
73 . ' FROM ' . PMA_backquote($cfgRelation[$pma_table])
74 . ' WHERE ' . implode(' AND ', $where_parts);
75 $table_copy_rs = PMA_query_as_cu($table_copy_query);
77 while ($table_copy_row = @PMA_mysql_fetch_array
($table_copy_rs)) {
78 $value_parts = array();
79 while(list($_key, $_val) = each($table_copy_row)) {
80 if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') {
81 $value_parts[] = PMA_sqlAddslashes($_val);
85 $new_table_query = 'INSERT IGNORE INTO ' . PMA_backquote($cfgRelation[$pma_table])
86 . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ')'
88 . ' (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')';
90 $new_table_rs = PMA_query_as_cu($new_table_query);
91 $last_id = (@function_exists
('mysql_insert_id') ? @mysql_insert_id
() : -1);
98 } // end of 'PMA_duplicate_table()' function
101 * Gets some core libraries
103 require('./libraries/grab_globals.lib.php3');
104 require('./libraries/common.lib.php3');
108 * Defines the url to return to in case of error in a sql statement
110 $err_url = 'tbl_properties.php3?' . PMA_generate_common_url($db, $table);
114 * Selects the database to work with
116 PMA_mysql_select_db($db);
120 * A target table name has been sent to this script -> do the work
122 if (isset($new_name) && trim($new_name) != '') {
126 // Ensure the target is valid
127 if (count($dblist) > 0 &&
128 (PMA_isInto($db, $dblist) == -1 ||
PMA_isInto($target_db, $dblist) == -1)) {
131 if (PMA_MYSQL_INT_VERSION
< 32306) {
132 PMA_checkReservedWords($target_db, $err_url);
133 PMA_checkReservedWords($new_name, $err_url);
136 if ($db == $target_db && $new_name == $table) {
137 $message = (isset($submit_move) ?
$strMoveTableSameNames : $strCopyTableSameNames);
139 $source = PMA_backquote($db) . '.' . PMA_backquote($table);
140 if (empty($target_db)) $target_db = $db;
141 $target = PMA_backquote($target_db) . '.' . PMA_backquote($new_name);
143 include('./libraries/build_dump.lib.php3');
145 $sql_structure = PMA_getTableDef($db, $table, "\n", $err_url);
146 $parsed_sql = PMA_SQP_parse($sql_structure);
148 /* nijel: Find table name in query and replace it */
150 while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++
;
152 /* no need to PMA_backquote() */
153 $parsed_sql[$i]['data'] = $target;
155 /* Generate query back */
156 $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only');
158 // do not create the table if dataonly
159 if ($what != 'dataonly') {
160 // If table exists, and 'add drop table' is selected: Drop it!
162 if (isset($drop_if_exists) && $drop_if_exists == 'true') {
163 $drop_query = 'DROP TABLE IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($new_name);
164 $result = @PMA_mysql_query
($drop_query);
165 if (PMA_mysql_error()) {
166 include('./header.inc.php3');
167 PMA_mysqlDie('', $sql_structure, '', $err_url);
170 if (isset($sql_query)) {
171 $sql_query .= "\n" . $drop_query . ';';
173 $sql_query = $drop_query . ';';
176 // garvin: If an existing table gets deleted, maintain any entries
177 // for the PMA_* tables
178 $maintain_relations = true;
181 $result = @PMA_mysql_query
($sql_structure);
182 if (PMA_mysql_error()) {
183 include('./header.inc.php3');
184 PMA_mysqlDie('', $sql_structure, '', $err_url);
185 } else if (isset($sql_query)) {
186 $sql_query .= "\n" . $sql_structure . ';';
188 $sql_query = $sql_structure . ';';
195 if ($result != FALSE && ($what == 'data' ||
$what == 'dataonly')) {
196 // speedup copy table - staybyte - 22. Juni 2001
197 if (PMA_MYSQL_INT_VERSION
>= 32300) {
198 $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source;
199 $result = @PMA_mysql_query
($sql_insert_data);
200 if (PMA_mysql_error()) {
201 include('./header.inc.php3');
202 PMA_mysqlDie('', $sql_insert_data, '', $err_url);
204 } // end MySQL >= 3.23
206 $sql_insert_data = '';
207 PMA_getTableContent($db, $table, 0, 0, 'PMA_myHandler', $err_url,'');
208 } // end MySQL < 3.23
209 $sql_query .= "\n\n" . $sql_insert_data;
212 include('./libraries/relation.lib.php3');
213 $cfgRelation = PMA_getRelationsParam();
215 // Drops old table if the user has requested to move it
216 if (isset($submit_move)) {
217 $sql_drop_table = 'DROP TABLE ' . $source;
218 $result = @PMA_mysql_query
($sql_drop_table);
219 if (PMA_mysql_error()) {
220 include('./header.inc.php3');
221 PMA_mysqlDie('', $sql_drop_table, '', $err_url);
224 // garvin: Move old entries from PMA-DBs to new table
225 if ($cfgRelation['commwork']) {
226 $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
227 . ' SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\', '
228 . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
229 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
230 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
231 $rmv_rs = PMA_query_as_cu($remove_query);
235 // garvin: updating bookmarks is not possible since only a single table is moved,
236 // and not the whole DB.
237 // if ($cfgRelation['bookmarkwork']) {
238 // $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['bookmark'])
239 // . ' SET dbase = \'' . PMA_sqlAddslashes($target_db) . '\''
240 // . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\'';
241 // $rmv_rs = PMA_query_as_cu($remove_query);
242 // unset($rmv_query);
245 if ($cfgRelation['displaywork']) {
246 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
247 . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\', '
248 . ' table_name = \'' . PMA_sqlAddslashes($new_name) . '\''
249 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
250 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
251 $tb_rs = PMA_query_as_cu($table_query);
256 if ($cfgRelation['relwork']) {
257 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
258 . ' SET foreign_table = \'' . PMA_sqlAddslashes($new_name) . '\','
259 . ' foreign_db = \'' . PMA_sqlAddslashes($target_db) . '\''
260 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
261 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\'';
262 $tb_rs = PMA_query_as_cu($table_query);
266 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
267 . ' SET master_table = \'' . PMA_sqlAddslashes($new_name) . '\','
268 . ' master_db = \'' . PMA_sqlAddslashes($target_db) . '\''
269 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
270 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\'';
271 $tb_rs = PMA_query_as_cu($table_query);
276 // garvin: [TODO] Can't get moving PDFs the right way. The page numbers always
277 // get screwed up independently from duplication because the numbers do not
278 // seem to be stored on a per-database basis. Would the author of pdf support
279 // please have a look at it?
281 if ($cfgRelation['pdfwork']) {
282 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_coords'])
283 . ' SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\','
284 . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
285 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
286 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
287 $tb_rs = PMA_query_as_cu($table_query);
291 $pdf_query = 'SELECT pdf_page_number '
292 . ' FROM ' . PMA_backquote($cfgRelation['table_coords'])
293 . ' WHERE db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
294 . ' AND table_name = \'' . PMA_sqlAddslashes($new_name) . '\'';
295 $pdf_rs = PMA_query_as_cu($pdf_query);
297 while ($pdf_copy_row = @PMA_mysql_fetch_array($pdf_rs)) {
298 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['pdf_pages'])
299 . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
300 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
301 . ' AND page_nr = \'' . PMA_sqlAddslashes($pdf_copy_row['pdf_page_number']) . '\'';
302 $tb_rs = PMA_query_as_cu($table_query);
309 $sql_query .= "\n\n" . $sql_drop_table . ';';
311 // garvin: Create new entries as duplicates from old PMA DBs
312 if ($what != 'dataonly' && !isset($maintain_relations)) {
313 if ($cfgRelation['commwork']) {
314 // Get all comments and MIME-Types for current table
315 $comments_copy_query = 'SELECT
316 column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ?
', mimetype, transformation, transformation_options' : '') . '
317 FROM ' . PMA_backquote($cfgRelation['column_info']) . '
319 db_name = \'' . PMA_sqlAddslashes($db) . '\' AND
320 table_name = \'' . PMA_sqlAddslashes($table) . '\'';
321 $comments_copy_rs = PMA_query_as_cu($comments_copy_query);
323 // Write every comment as new copied entry. [MIME]
324 while ($comments_copy_row = @PMA_mysql_fetch_array
($comments_copy_rs)) {
325 $new_comment_query = 'REPLACE INTO ' . PMA_backquote($cfgRelation['column_info'])
326 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ?
', mimetype, transformation, transformation_options' : '') . ') '
328 . '\'' . PMA_sqlAddslashes($target_db) . '\','
329 . '\'' . PMA_sqlAddslashes($new_name) . '\','
330 . '\'' . PMA_sqlAddslashes($comments_copy_row['column_name']) . '\''
331 . ($cfgRelation['mimework'] ?
',\'' . PMA_sqlAddslashes($comments_copy_row['comment']) . '\','
332 . '\'' . PMA_sqlAddslashes($comments_copy_row['mimetype']) . '\','
333 . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation']) . '\','
334 . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation_options']) . '\'' : '')
336 $new_comment_rs = PMA_query_as_cu($new_comment_query);
340 if ($db != $target_db) {
341 $get_fields = array('user','label','query');
342 $where_fields = array('dbase' => $db);
343 $new_fields = array('dbase' => $target_db);
344 PMA_duplicate_table('bookmarkwork', 'bookmark', $get_fields, $where_fields, $new_fields);
347 $get_fields = array('display_field');
348 $where_fields = array('db_name' => $db, 'table_name' => $table);
349 $new_fields = array('db_name' => $target_db, 'table_name' => $new_name);
350 PMA_duplicate_table('displaywork', 'table_info', $get_fields, $where_fields, $new_fields);
352 $get_fields = array('master_field', 'foreign_db', 'foreign_table', 'foreign_field');
353 $where_fields = array('master_db' => $db, 'master_table' => $table);
354 $new_fields = array('master_db' => $target_db, 'master_table' => $new_name);
355 PMA_duplicate_table('relwork', 'relation', $get_fields, $where_fields, $new_fields);
357 $get_fields = array('foreign_field', 'master_db', 'master_table', 'master_field');
358 $where_fields = array('foreign_db' => $db, 'foreign_table' => $table);
359 $new_fields = array('foreign_db' => $target_db, 'foreign_table' => $new_name);
360 PMA_duplicate_table('relwork', 'relation', $get_fields, $where_fields, $new_fields);
362 // garvin: [TODO] Can't get duplicating PDFs the right way. The page numbers always
363 // get screwed up independently from duplication because the numbers do not
364 // seem to be stored on a per-database basis. Would the author of pdf support
365 // please have a look at it?
367 $get_fields = array('page_descr');
368 $where_fields = array('db_name' => $db);
369 $new_fields = array('db_name' => $target_db);
370 $last_id = PMA_duplicate_table('pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields);
372 if (isset($last_id) && $last_id >= 0) {
373 $get_fields = array('x', 'y');
374 $where_fields = array('db_name' => $db, 'table_name' => $table);
375 $new_fields = array('db_name' => $target_db, 'table_name' => $new_name, 'pdf_page_number' => $last_id);
376 PMA_duplicate_table('pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields);
382 $message = (isset($submit_move) ?
$strMoveTableOK : $strCopyTableOK);
383 $message = sprintf($message, $source, $target);
385 $js_to_run = 'functions.js';
386 /* Check: Work on new table or on old table? */
387 if (isset($submit_move)) {
391 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
392 if (isset($switch_to_new) && $switch_to_new == 'true') {
393 setcookie('pma_switch_to_new', 'true', 0, substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), '', ($pma_uri_parts['scheme'] == 'https'));
397 setcookie('pma_switch_to_new', '', 0, substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), '', ($pma_uri_parts['scheme'] == 'https'));
398 // garvin:Keep original table for work.
402 include('./header.inc.php3');
403 } // end is target table name
407 * No new name for the table!
410 include('./header.inc.php3');
411 PMA_mysqlDie($strTableEmpty, '', '', $err_url);
416 * Back to the calling script
419 require('./tbl_properties.php3');