Source for file mysql.dbtools.php

Documentation is available at mysql.dbtools.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage db_driver
  5. * @author     Croes Gérald, Laurent Jouanneau
  6. * @contributor Laurent Jouanneau
  7. * @copyright  2001-2005 CopixTeam, 2005-2010 Laurent Jouanneau
  8. *  This class was get originally from the Copix project (CopixDbToolsMysql, Copix 2.3dev20050901, http://www.copix.org)
  9. *  Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence).
  10. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  11. *  and this class was adapted/improved for Jelix by Laurent Jouanneau
  12. *
  13. * @link      http://www.jelix.org
  14. * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  15. */
  16.  
  17. /**
  18.  * Provides utilities methods for a mysql database
  19.  * @package    jelix
  20.  * @subpackage db_driver
  21.  */
  22. class mysqlDbTools extends jDbTools {
  23.  
  24.     /**
  25.     * retourne la liste des tables
  26.     * @return   array    $tab[] = $nomDeTable
  27.     */
  28.     function _getTableList (){
  29.         $results = array ();
  30.         if (isset($this->_connector->profile['database'])) {
  31.             $db = $this->_connector->profile['database'];
  32.         }
  33.         else if (isset($this->_connector->profile['dsn'])
  34.                  && preg_match('/dbname=([a-z0-9_ ]*)/', $this->_connector->profile['dsn'], $m)){
  35.             $db = $m[1];  
  36.         }
  37.         else {
  38.             throw new jException("jelix~error.no.database.name", $this->_connector->profile['name']);
  39.         }
  40.         $rs = $this->_connector->query ('SHOW TABLES FROM `'.$db.'`');
  41.         $col_name = 'Tables_in_'.$db;
  42.  
  43.         while ($line = $rs->fetch ()){
  44.             $results[] = $line->$col_name;
  45.         }
  46.  
  47.         return $results;
  48.     }
  49.  
  50.     /**
  51.     * récupère la liste des champs pour une base donnée.
  52.     * @return   array    $tab[NomDuChamp] = obj avec prop (tye, length, lengthVar, notnull)
  53.     */
  54.     function _getFieldList ($tableName){
  55.         $results = array ();
  56.  
  57.         $rs = $this->_connector->query ('SHOW FIELDS FROM `'.$tableName.'`');
  58.  
  59.         while ($line = $rs->fetch ()){
  60.             $field = new jDbFieldProperties();
  61.  
  62.             if (preg_match('/^(\w+)\s*(\((\d+)\))?.*$/',$line->Type,$m)) {
  63.                 $field->type = strtolower($m[1]);
  64.                 if ($field->type == 'varchar' && isset($m[3])) {
  65.                     $field->length = intval($m[3]);
  66.                 }
  67.             } else {
  68.                 $field->type = $line->Type;
  69.             }
  70.  
  71.             $field->name = $line->Field;
  72.             $field->notNull = ($line->Null == 'NO');
  73.             $field->primary = ($line->Key == 'PRI');
  74.             $field->autoIncrement  = ($line->Extra == 'auto_increment');
  75.             $field->hasDefault = ($line->Default != '' || !($line->Default == null && $field->notNull));
  76.             // to fix a bug in php 5.2.5 or mysql 5.0.51
  77.             if($field->notNull && $line->Default === null && !$field->autoIncrement)
  78.                 $field->default ='';
  79.             else
  80.                 $field->default = $line->Default;
  81.             $results[$line->Field] = $field;
  82.         }
  83.         return $results;
  84.     }
  85.     
  86.     public function execSQLScript ($file) {
  87.         if(!isset($this->_connector->profile['table_prefix']))
  88.             $prefix = '';
  89.         else
  90.             $prefix = $this->_connector->profile['table_prefix'];
  91.         $sqlQueries = str_replace('%%PREFIX%%', $prefix, file_get_contents($file));
  92.         $queries = $this->parseSQLScript($sqlQueries);
  93.         foreach($queries as $query)
  94.             $this->_connector->exec($query);
  95.         return count($queries);
  96.     }
  97.  
  98.     /**
  99.      *
  100.      */
  101.     protected function parseSQLScript($script) {
  102.  
  103.         $delimiters = array();
  104.         $distinctDelimiters = array(';');
  105.         if(preg_match_all("/DELIMITER ([^\n]*)/i", $script, $d, PREG_SET_ORDER)) {
  106.             $delimiters = $d[1];
  107.             $distinctDelimiters = array_unique(array_merge($distinctDelimiters,$delimiters));
  108.         }
  109.         $preg= '';
  110.         foreach($distinctDelimiters as $dd) {
  111.             $preg.='|'.preg_quote($dd);
  112.         }
  113.  
  114.         $tokens = preg_split('!(\'|"|\\\\|`|DELIMITER |#|/\\*|\\*/|\\-\\- |'."\n".$preg.')!i', $script, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  115.  
  116.         $currentDelimiter = ';';
  117.         $context = 0;
  118.         $queries = array();
  119.         $query = '';
  120.         $previousToken = '';
  121.         foreach ($tokens as $k=>$token) {
  122.             switch ($context) {
  123.             // 0 : statement
  124.             case 0:
  125.                 $previousToken = $token;
  126.                 switch($token) {
  127.                 case $currentDelimiter:
  128.                     $queries[] = trim($query);
  129.                     $query = '';
  130.                     break;
  131.                 case '\'':
  132.                     $context = 1;
  133.                     $previousToken = '';
  134.                     $query.=$token;
  135.                     break;
  136.                 case '"':
  137.                     $context = 2;
  138.                     $previousToken = '';
  139.                     $query.=$token;
  140.                     break;
  141.                 case '`':
  142.                     $context = 3;
  143.                     $query.=$token;
  144.                     $previousToken = '';
  145.                     break;
  146.                 case 'DELIMITER ':
  147.                     $context = 6;
  148.                     break;
  149.                 case '#':
  150.                 case '-- ':
  151.                     $context = 4;
  152.                     break;
  153.                 case '/*':
  154.                     $context = 5;
  155.                     break;
  156.                 case "\n":
  157.                 default :
  158.                     $query.=$token;
  159.                 }
  160.                 break;
  161.             // 1 : string '
  162.             case 1:
  163.                 if ($token =="'") {
  164.                     if ($previousToken != "\\" && $previousToken != "'") {
  165.                         if(isset($tokens[$k+1])) {
  166.                             if ($tokens[$k+1] != "'") {
  167.                                 $context = 0;
  168.                             }
  169.                         }
  170.                         else
  171.                             $context = 0;
  172.                     }
  173.                 }
  174.                 $previousToken = $token;
  175.                 $query.=$token;
  176.                 break;
  177.             // 2 : string "
  178.             case 2:
  179.                 if ($token =='"') {
  180.                     if ($previousToken != "\\" && $previousToken != '"') {
  181.                         if(isset($tokens[$k+1])) {
  182.                             if ($tokens[$k+1] != '"') {
  183.                                 $context = 0;
  184.                             }
  185.                         }
  186.                         else
  187.                             $context = 0;
  188.                     }
  189.                 }
  190.                 $previousToken = $token;
  191.                 $query.=$token;
  192.                 break;
  193.             // 3 : name with `
  194.             case 3:
  195.                 if ($token =='`') {
  196.                     if ($previousToken != "\\" && $previousToken != '`') {
  197.                         if(isset($tokens[$k+1])) {
  198.                             if ($tokens[$k+1] != '`') {
  199.                                 $context = 0;
  200.                             }
  201.                         }
  202.                         else
  203.                             $context = 0;
  204.                     }
  205.                 }
  206.                 $previousToken = $token;
  207.                 $query.=$token;
  208.                 break;
  209.             // 4 : comment single line
  210.             case 4:
  211.                 if ($token == "\n") {
  212.                     $query.=$token;
  213.                     $context = 0;
  214.                 }
  215.                 break;
  216.             // 5 : comment multi line
  217.             case 5:
  218.                 if ($token == "*/") {
  219.                     $context = 0;
  220.                 }
  221.                 break;
  222.             // 6 : delimiter definition
  223.             case 6:
  224.                 $currentDelimiter = $token;
  225.                 $context = 0;
  226.                 break;
  227.             }
  228.             
  229.         }
  230.         if (trim($query) != '')
  231.             $queries[] = trim($query);
  232.         return $queries;
  233.     }
  234. }

Documentation generated on Thu, 22 Mar 2012 22:17:55 +0100 by phpDocumentor 1.4.3