Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/05/2011, 16:23
Avatar de Deathmetalrules
Deathmetalrules
 
Fecha de Ingreso: diciembre-2010
Ubicación: Mas - aya
Mensajes: 65
Antigüedad: 13 años, 5 meses
Puntos: 8
Mensaje Mostrar alter table

Hola amig@s hace tiempo abimaelrc posteo un excelente aporte sobre dump de bd quisiera saber como interpretarlo o pasarlo a un codigo mas sencillo para mostrar los alter table

este es el codigo del aporte

Código PHP:
   1.
      <?php
   2.
      
# % -> esta línea la pueden quitar es para evitar un error con el highlight
   
3.
      set_time_limit
(0);
   
4.
       
   5.
      define
('DB_NAME''nombre');
   
6.
      define
('DB_HOST''localhost');
   
7.
      define
('DB_USER''usuario');
   
8.
      define
('DB_PASS''contraseña');
   
9.
       
  10.
       
  11.
      
function setQuery($setSelectQueryStr$fetchType NULL){
  
12.
          $arr 
= array();
  
13.
       
  14.
          
try{
  
15.
              $db 
= new PDO('mysql:host=' DB_HOST ';dbname=' DB_NAMEDB_USERDB_PASS);
  
16.
              $db
->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  
17.
       
  18.
              $pdoQuery 
$db->query($setSelectQueryStr);
  
19.
              $fetchType 
= empty($fetchType) ? PDO::FETCH_ASSOC $fetchType;
  
20.
              $arr 
= ($pdoQuery->rowCount() > 0) ? $pdoQuery->fetchAll($fetchType) : array();
  
21.
       
  22.
          
}catch(PDOException $e){
  
23.
              
echo $e->getMessage();
  
24.
              
exit;
  
25.
          
}
  
26.
       
  27.
          
return $arr;
  
28.
      
}
  
29.
       
  30.
       
  31.
       
  32.
       
  33.
      define
('NL'PHP_EOL);
  
34.
       
  35.
      $drop 
true;
  
36.
      $tables 
= array();
  
37.
      $extra 
= array();
  
38.
      $constraints 
= array();
  
39.
       
  40.
      $f 
fopen(DB_NAME '.sql''w');
  
41.
       
  42.
      $query 
setQuery('SHOW TABLES FROM `' DB_NAME '`'PDO::FETCH_NUM);
  
43.
      
foreach($query as $row){
  
44.
          $tables
[] = $row[0];
  
45.
      
}
  
46.
       
  47.
      $extra
['dumpVersion'] = "1.0";
  
48.
      $extra
['dtTm'] = date("Y-m-d H:i:s");
  
49.
      $extra
['serverVersion'] = $db->getAttribute(PDO::ATTR_SERVER_VERSION);
  
50.
      $extra
['phpVersion'] = phpversion();
  
51.
      $extra
['dbName'] = DB_NAME;
  
52.
       
  53.
      $t 
= array();
  
54.
      
foreach($tables as $k => $v){
  
55.
          $t
[] = "[$k] => $v;";
  
56.
      
}
  
57.
      $extra
['tables'] = implode(NL '--           '$t);
  
58.
       
  59.
      $text 
= <<<HEADERTEXT
  60.
      -- dumpFDW  
  61.
      -- version 
{$extra['dumpVersion']}
  62.
      -- http://www.forosdelweb.com/miembros/abimaelrc/
  63.
      --  
  64.
      -- Host: 
{$_SERVER['HTTP_HOST']}
  65.
      -- Generation Time: 
{$extra['dtTm']}
  66.
      -- Server version: 
{$extra['serverVersion']}
  67.
      -- PHP Version: 
{$extra['phpVersion']}
  68.
      -- Database: '
{$extra['dbName']}'
  69.
      -- Tables: 
{$extra['tables']}
  70.
      HEADERTEXT;
  71.
       
  72.
      fwrite($f, $text);
  73.
       
  74.
      foreach ($tables as $table){
  75.
          fwrite($f, NL . NL .($drop ? "DROP TABLE IF EXISTS `$table`;" : "-- No especificado.") . NL);
  76.
       
  77.
          $query = setQuery("SHOW CREATE TABLE `$table`", PDO::FETCH_NUM);
  78.
          foreach($query as $row){
  79.
              $arr = explode("\n", $row
[1]);
  80.
              $tmpArr = array();
  81.
              foreach($arr as $key => $value){
  82.
                  if(stripos($value, "CONSTRAINT") !== false){
  83.
                      $tmpArr
[] = '  ADD ' . trim($value);
  84.
                      if(array_key_exists($key - 1, $arr)){
  85.
                          $arr
[$key - 1] = str_replace(',','',$arr[$key - 1]);
  86.
                      }
  87.
                      unset($arr
[$key]);
  88.
                  }
  89.
              }
  90.
              if(!empty($tmpArr)){
  91.
                  $constraints
[] = 'ALTER TABLE ' . $row[0] . NL . implode(NL, $tmpArr) . ';';
  92.
              }
  93.
              fwrite($f, implode(NL, $arr) . ';' . NL . NL);
  94.
          }
  95.
       
  96.
       
  97.
          $query = setQuery("SELECT * FROM `$table`");
  98.
       
  99.
          $n = 0;
 100.
          $nR = count($query) - 1;
 101.
          foreach($query as $qry){
 102.
              $columnas = array_keys($qry);
 103.
              $values = array();
 104.
              $keys = array();
 105.
       
 106.
              foreach($columnas as $columna){
 107.
                  $keys
[] = "`".$columna."`";
 108.
                  if( is_numeric($qry
[$columna]) || is_null($qry[$columna]) ){
 109.
                      $values
[] = $qry[$columna];
 110.
                  } else{
 111.
                      $values
[] = "'" . str_replace(array("'", NL), array("''", '\r\n'), addcslashes($qry[$columna], '\\')) . "'";
 112.
                  }
 113.
              }
 114.
       
 115.
              /* $sC = special Char, saber cual cáracter especial colocar al final del código */
 116.
              $sC = ($n%2000 == 1999 || $n == $nR ? ";" : ",");
 117.
       
 118.
              if($n%2000 == 0){
 119.
                  fwrite($f, "INSERT INTO `$table`(".implode(", ", $keys).") VALUES " . NL . "(" . implode(", ", $values) . ")" . $sC . NL);
 120.
              }else{
 121.
                  fwrite($f, "(" . implode(", ", $values) . ")" . $sC . NL);
 122.
              }
 123.
              $n++;
 124.
          }
 125.
      }
 126.
       
 127.
      if(!empty($constraints)){
 128.
          fwrite($f, NL . NL . implode(NL . NL, $constraints));
 129.
      }
 130.
       
 131.
      fclose($f);
como pueden ver se hace un fichero .sql pero se usan llamadas a bd de tipo PDO en eso estoy claro pero quisiera pasarlo a algo mas sencillo para obtener los alter table...

Última edición por Deathmetalrules; 02/05/2011 a las 16:31