[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/dump/ -> lmbSQLDumpLoader.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/lmbSimpleDb.class.php');
  10  lmb_require('limb/fs/src/exception/lmbFileNotFoundException.class.php');
  11  
  12  /**

  13   * class lmbSQLDumpLoader.

  14   *

  15   * @package dbal

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

  17   */
  18  class lmbSQLDumpLoader
  19  {
  20    protected $affected_tables = array();
  21    protected $statements = array();
  22  
  23    function __construct($file_path = null)
  24    {
  25      if($file_path)
  26        $this->loadFile($file_path);
  27    }
  28  
  29    function getStatements()
  30    {
  31      return $this->statements;
  32    }
  33  
  34    function cleanTables($connection)
  35    {
  36      $db = new lmbSimpleDb($connection);
  37  
  38      foreach($this->affected_tables as $table)
  39        $db->delete($table);
  40    }
  41  
  42    function getAffectedTables()
  43    {
  44      return $this->affected_tables;
  45    }
  46  
  47    function execute($connection, $regex = '')
  48    {
  49      foreach($this->statements as $sql)
  50      {
  51        if($regex && !preg_match($regex, $sql, $m))
  52          continue;
  53  
  54        $stmt = $connection->newStatement($sql);
  55        $stmt->execute();
  56      }
  57    }
  58  
  59    function loadFile($file_path)
  60    {
  61      if(!file_exists($file_path))
  62        throw new lmbFileNotFoundException($file_path);
  63  
  64      $this->loadStatements(file_get_contents($file_path));
  65    }
  66  
  67    function loadStatements($sql)
  68    {
  69      $this->statements = $this->_retrieveStatements($sql);
  70      $this->affected_tables = $this->_getAffectedTables($this->statements);
  71    }
  72  
  73    protected function _getAffectedTables($stmts)
  74    {
  75      $affected_tables = array();
  76      foreach($stmts as $sql)
  77      {
  78        if(preg_match("|insert\s+?into\s+?([^\s]+)|i", $sql, $matches))
  79        {
  80          if(!in_array($matches[1], $affected_tables))
  81          {
  82            $affected_tables[] = $this->_processTableName($matches[1]);
  83          }
  84        }
  85      }
  86      return $affected_tables;
  87    }
  88  
  89    protected function _processTableName($name)
  90    {
  91      return $name;
  92    }
  93  
  94    protected function _retrieveStatements($raw_sql)
  95    {
  96      //naive implementation

  97      $stmts = preg_split('/;\s*\n/', $raw_sql);
  98      $processed = array();
  99      foreach($stmts as $stmt)
 100      {
 101        if($stmt = $this->_processStatement($stmt))
 102          $processed[] = $stmt;
 103      }
 104      return $processed;
 105    }
 106  
 107    protected function _processStatement($sql)
 108    {
 109      if(!$sql = trim($sql))
 110        return null;
 111  
 112      if(strpos($sql, ';') == (strlen($sql) - 1))
 113        return substr($sql, 0, strlen($sql) - 1);
 114      else
 115        return $sql;
 116    }
 117  }
 118  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7