Documentation TYPO3 par Ameos

tbl_move_copy.php

00001 <?php
00002 /* $Id: tbl_move_copy.php,v 1.6 2005/08/08 19:54:57 lem9 Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
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         // must use PMA_DBI_QUERY_STORE here, since we execute another
00053         // query inside the loop
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         } // end while
00072 
00073         return $last_id;
00074     }
00075 
00076     return true;
00077 } // end of 'PMA_duplicate_table_info()' function
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     // set export settings we need
00090     $GLOBALS['use_backquotes'] = 1;
00091     $GLOBALS['asfile']         = 1;
00092 
00093     // Ensure the target is valid
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     // Doing a select_db could avoid some problems with replicated databases,
00103     // when moving table from replicated one to not replicated one
00104     PMA_DBI_select_db($target_db);
00105 
00106     $target = PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
00107 
00108     // do not create the table if dataonly
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         /* nijel: Find table name in query and replace it */
00119         $i = 0;
00120         while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++;
00121 
00122         /* no need to PMA_backquote() */
00123         $parsed_sql[$i]['data'] = $target;
00124 
00125         /* Generate query back */
00126         $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only');
00127         // If table exists, and 'add drop table' is selected: Drop it!
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             // garvin: If an existing table gets deleted, maintain any entries
00140             // for the PMA_* tables
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             // find the first quote_backtick, it must be the source table name
00156             while ($parsed_sql[$i]['type'] != 'quote_backtick') {
00157                 $i++;
00158             }
00159 
00160             // replace it by the target table name, no need to PMA_backquote()
00161             $parsed_sql[$i]['data'] = $target;
00162 
00163             // now we must remove all quote_backtick that follow a CONSTRAINT
00164             // keyword, because a constraint name must be unique in a db
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             // Generate query back
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     // Copy the data
00195     //if ($result != FALSE && ($what == 'data' || $what == 'dataonly')) {
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     // Drops old table if the user has requested to move it
00206     if ($move) {
00207 
00208         // This could avoid some problems with replicated databases, when
00209         // moving table from replicated one to not replicated one
00210         PMA_DBI_select_db($source_db);
00211 
00212         $sql_drop_table = 'DROP TABLE ' . $source;
00213         PMA_DBI_query($sql_drop_table);
00214 
00215         // garvin: Move old entries from PMA-DBs to new table
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         // garvin: updating bookmarks is not possible since only a single table is moved,
00227         // and not the whole DB.
00228         // if ($cfgRelation['bookmarkwork']) {
00229         //     $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['bookmark'])
00230         //                   . ' SET     dbase = \'' . PMA_sqlAddslashes($target_db) . '\''
00231         //                   . ' WHERE dbase  = \'' . PMA_sqlAddslashes($source_db) . '\'';
00232         //     $rmv_rs    = PMA_query_as_cu($remove_query);
00233         //     unset($rmv_query);
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         // garvin: [TODO] Can't get moving PDFs the right way. The page numbers always
00268         // get screwed up independently from duplication because the numbers do not
00269         // seem to be stored on a per-database basis. Would the author of pdf support
00270         // please have a look at it?
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             $pdf_query = 'SELECT pdf_page_number '
00283                        . ' FROM ' . PMA_backquote($cfgRelation['table_coords'])
00284                        . ' WHERE db_name  = \'' . PMA_sqlAddslashes($target_db) . '\''
00285                        . ' AND table_name = \'' . PMA_sqlAddslashes($target_table) . '\'';
00286             $pdf_rs = PMA_query_as_cu($pdf_query);
00287 
00288             while ($pdf_copy_row = PMA_DBI_fetch_assoc($pdf_rs)) {
00289                 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['pdf_pages'])
00290                                 . ' SET     db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
00291                                 . ' WHERE db_name  = \'' . PMA_sqlAddslashes($source_db) . '\''
00292                                 . ' AND page_nr = \'' . PMA_sqlAddslashes($pdf_copy_row['pdf_page_number']) . '\'';
00293                 $tb_rs    = PMA_query_as_cu($table_query);
00294                 unset($table_query);
00295                 unset($tb_rs);
00296             }
00297             */
00298         }
00299 
00300         $sql_query      .= "\n\n" . $sql_drop_table . ';';
00301     } else {
00302         // garvin: Create new entries as duplicates from old PMA DBs
00303         if ($what != 'dataonly' && !isset($maintain_relations)) {
00304             if ($cfgRelation['commwork']) {
00305                 // Get all comments and MIME-Types for current table
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                 // Write every comment as new copied entry. [MIME]
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                 } // end while
00329             }
00330 
00331             // duplicating the bookmarks must not be done here, but
00332             // just once per db
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             // garvin: [TODO] Can't get duplicating PDFs the right way. The page numbers always
00350             // get screwed up independently from duplication because the numbers do not
00351             // seem to be stored on a per-database basis. Would the author of pdf support
00352             // please have a look at it?
00353             /*
00354             $get_fields = array('page_descr');
00355             $where_fields = array('db_name' => $source_db);
00356             $new_fields = array('db_name' => $target_db);
00357             $last_id = PMA_duplicate_table_info('pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields);
00358 
00359             if (isset($last_id) && $last_id >= 0) {
00360                 $get_fields = array('x', 'y');
00361                 $where_fields = array('db_name' => $source_db, 'table_name' => $source_table);
00362                 $new_fields = array('db_name' => $target_db, 'table_name' => $target_table, 'pdf_page_number' => $last_id);
00363                 PMA_duplicate_table_info('pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields);
00364             }
00365             */
00366         }
00367     }
00368 
00369 }
00370 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6