pat_db_install.php
Upload User: feiyaoda
Upload Date: 2016-11-21
Package Size: 9556k
Code Size: 4k
Category:

WEB Mail

Development Platform:

PHP

  1. <?php
  2. /*
  3.  *  SQL file parser
  4.  *
  5.  *  by Jean Philippe Giot <jpgiot@ifrance.com>
  6.  *
  7.  *  GNU GPL 
  8.  */
  9. function get_begin_str($subject,$separator)
  10. {
  11.     return substr($subject, 0, strlen($subject)-strlen (strstr ($subject,$separator)));    
  12. }
  13. /*
  14.  *  CLASS for parsing and storing all instructions
  15.  *
  16.  */
  17. class sql_instructions
  18. {
  19.     var $instructions           = array();
  20.     
  21.     var $show_parse             = FALSE;
  22.     
  23.     // number of comments during parsing
  24.     var $comment                = 0;
  25.     // internal. stores current instruction during parsing
  26.     var $current_instruction    = '';
  27.     
  28.     function new_instruction($content='')
  29.     {
  30.         // we save old instruction
  31.         if ('' != $this->current_instruction)
  32.         {
  33.             $this->instructions[] = $this->current_instruction;
  34.         }
  35.         // we save text
  36.         $this->current_instruction = $content;
  37.                 
  38.     }
  39.     
  40.     function add_text($text='')
  41.     {
  42.         if ('' != $this->current_instruction)
  43.             $this->current_instruction .= $text;
  44.     }    
  45.     function get_instructions()
  46.     {
  47.         $this->new_instruction();
  48.         return $this->instructions;
  49.     }
  50.     
  51.     function parse_file($filename)
  52.     {
  53.         $lines = file($filename);
  54.         $comment    = 0;
  55.         $blank      = 0;
  56.         $unknown    = 0;
  57.         $unknowns   = array();
  58.         $parsed_instructions = array('create' => 0,'insert' => 0,'update' => 0,'drop' => 0);
  59.         if ($this->show_parse) echo "<table border=1 cellpadding=2 cellspacing=0>";
  60.         foreach ($lines as $id => $linecontent)
  61.         {
  62.             $begining = strtolower(get_begin_str($linecontent,' '));
  63.             switch ($begining)
  64.             {
  65.                 case 'insert' : 
  66.                 case 'create' :
  67.                 case 'update' :
  68.                 {
  69.                     $parsed_instructions[$begining]++;
  70.                     if ($this->show_parse) echo "<tr><th>$begining</th><td>";
  71.                     $this->new_instruction($linecontent);
  72.                     break;
  73.                 }        
  74.                 case '#'      :
  75.                 {
  76.                     $this->comment++;
  77.                     if ($this->show_parse) echo "<tr><th>Comment</th><td>";
  78.                     break;
  79.                 }
  80.                 default       : 
  81.                 {
  82.                     if (trim($linecontent) == '#')
  83.                     {
  84.                         $comment++;
  85.                         if ($this->show_parse) echo "<tr><th>Comment</th><td>";
  86.                         break;
  87.                     }
  88.                     elseif (trim($linecontent) == '')
  89.                     {
  90.                         // added to content
  91.                         if ($this->show_parse) echo "<tr><th>Separator</th><td>";
  92.                         $this->add_text($linecontent);
  93.                         break;
  94.                     }            
  95.                     else
  96.                     {
  97.                         $unknowns[] = $linecontent;
  98.                         $unknown++;
  99.                         if ($this->show_parse) echo "<tr><th>Unknown</th><td>";
  100.                         // will be added if an instuction is currently available
  101.                         $this->add_text($linecontent);                
  102.                     }
  103.                     break;
  104.                 }
  105.             }
  106.             if ($this->show_parse) echo $linecontent."</td></tr>n";
  107.         }
  108.         if ($this->show_parse) 
  109.         {
  110.             echo "</table>";
  111.             echo 'comment '.$comment."<br>n";
  112.             echo 'blank '.$blank."<br>n";
  113.             echo 'unknown '.$unknown." :<br>n";
  114.             foreach($unknowns as $id => $value) echo $id." ".$value."<br>n";
  115.         }        
  116.     }
  117. }