[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/dump/ -> lmbMysqlDumpLoader.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/dbal/src/dump/lmbSQLDumpLoader.class.php');
  10  
  11  /**

  12   * class lmbMysqlDumpLoader.

  13   *

  14   * @package dbal

  15   * @version $Id: lmbMysqlDumpLoader.class.php 5945 2007-06-06 08:31:43Z pachanga $

  16   */
  17  class lmbMysqlDumpLoader extends lmbSQLDumpLoader
  18  {
  19    // the parsing code was taken from somewhere else...

  20    // and it's ugly, ugly, ugly!!!

  21    // use some sort of lexer later...

  22    protected function _retrieveStatements($sql)
  23    {
  24      $ret = array();
  25  
  26      $sql          = trim($sql);
  27      $sql_len      = strlen($sql);
  28      $char         = '';
  29      $string_start = '';
  30      $in_string    = false;
  31  
  32      for($i = 0; $i < $sql_len; ++$i)
  33      {
  34        $char = $sql[$i];
  35  
  36        // We are in a string, check for not escaped end of strings except for

  37        // backquotes that can't be escaped

  38        if($in_string)
  39        {
  40          for(;;)
  41          {
  42            $i = strpos($sql, $string_start, $i);
  43            // No end of string found->add the current substring to the

  44            // returned array

  45            if (!$i)
  46            {
  47              $ret[] = $sql;
  48              return $ret;
  49            }
  50            // Backquotes or no backslashes before quotes: it's indeed the

  51            // end of the string->exit the loop

  52            elseif ($string_start == '`' || $sql[$i-1] != '\\')
  53            {
  54              $string_start      = '';
  55              $in_string         = false;
  56              break;
  57            }
  58            // one or more Backslashes before the presumed end of string...

  59            else
  60            {
  61              // ... first checks for escaped backslashes

  62              $j                     = 2;
  63              $escaped_backslash     = false;
  64              while ($i-$j > 0 && $sql[$i-$j] == '\\')
  65              {
  66                $escaped_backslash = !$escaped_backslash;
  67                $j++;
  68              }
  69              // ... if escaped backslashes: it's really the end of the

  70              // string->exit the loop

  71              if ($escaped_backslash)
  72              {
  73                $string_start  = '';
  74                $in_string     = false;
  75                break;
  76              }
  77              else
  78                $i++;
  79            }
  80          }
  81        }
  82        // We are not in a string, first check for delimiter...

  83        elseif($char == ';')
  84        {
  85          // if delimiter found, add the parsed part to the returned array

  86          $ret[]      = substr($sql, 0, $i);
  87          $sql        = ltrim(substr($sql, min($i + 1, $sql_len)));
  88          $sql_len    = strlen($sql);
  89          if($sql_len)
  90            $i = -1;
  91          else
  92            return $ret;
  93        }
  94        // ... then check for start of a string,...

  95        elseif (($char == '"') || ($char == '\'') || ($char == '`'))
  96        {
  97          $in_string    = true;
  98          $string_start = $char;
  99        }
 100        // ... for start of a comment (and remove this comment if found)...

 101        elseif ($char == '#'
 102                 || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--'))
 103        {
 104          // starting position of the comment depends on the comment type

 105          $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
 106          $end_of_comment = self :: _getEndOfCommentPosition($sql, $i+2);
 107  
 108          if(!$end_of_comment)
 109          {
 110            // no eol found after '#', add the parsed part to the returned

 111            // array if required and exit

 112            if($start_of_comment > 0)
 113              $ret[] = trim(substr($sql, 0, $start_of_comment));
 114  
 115            return $ret;
 116          }
 117          else
 118          {
 119            $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
 120            $sql_len = strlen($sql);
 121            $i--;
 122          }
 123        }
 124        // ... for start of a comment /* and remove it

 125        elseif($char == '/' && isset($sql[$i+1]) && $sql[$i+1] == '*')
 126        {
 127          $start_of_comment = $i;
 128          $end_of_comment = self :: _getEndOfCommentPosition($sql, $i+2);
 129  
 130          if(!$end_of_comment)
 131          {
 132            // no eol found after '#', add the parsed part to the returned

 133            // array if required and exit

 134            if($start_of_comment > 0)
 135              $ret[] = trim(substr($sql, 0, $start_of_comment));
 136  
 137            return $ret;
 138          }
 139          else
 140          {
 141            $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
 142            $sql_len = strlen($sql);
 143            $i--;
 144          }
 145        }
 146      }
 147      // add any rest to the returned array

 148      if (!empty($sql) && preg_match('/\S+/', $sql))
 149        $ret[] = $sql;
 150  
 151      return $ret;
 152    }
 153  
 154    protected function _getEndOfCommentPosition($str, $start)
 155    {
 156      // if no "\n" exits in the remaining string, checks for "\r"

 157      // (Mac eol style)

 158  
 159      if($pos = strpos('  ' . $str, "*/\012", $start))
 160        return $pos;
 161      if($pos = strpos('  ' . $str, "*/\015", $start))
 162        return $pos;
 163      if($pos = strpos('  ' . $str, "*/;", $start))
 164        return $pos+1;
 165  
 166      return false;
 167    }
 168  
 169    protected function _processTableName($table)
 170    {
 171      return trim($table, '`');
 172    }
 173  }
 174  ?>


Generated: Sat Aug 30 04:38:32 2008 Cross-referenced by PHPXref 0.7