[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/sqlite/ -> lmbSqliteRecordSet.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/drivers/lmbDbBaseRecordSet.class.php');
  10  lmb_require('limb/dbal/src/drivers/sqlite/lmbSqliteRecord.class.php');
  11  
  12  /**
  13   * class lmbSqliteRecordSet.
  14   *
  15   * @package dbal
  16   * @version $Id$
  17   */
  18  class lmbSqliteRecordSet extends lmbDbBaseRecordSet
  19  {
  20    protected $queryId;
  21    protected $query;
  22    protected $connection;
  23  
  24    protected $current;
  25    protected $valid;
  26    protected $key;
  27  
  28    function __construct($connection, $queryString)
  29    {
  30      $this->connection = $connection;
  31      $this->query = $queryString;
  32    }
  33  
  34    function freeQuery()
  35    {
  36      if(isset($this->queryId) && is_resource($this->queryId))
  37        $this->queryId = null;
  38    }
  39  
  40    function rewind()
  41    {
  42      if(isset($this->queryId) && is_resource($this->queryId) && sqlite_num_rows($this->queryId))
  43      {
  44        if(sqlite_seek($this->queryId, 0) === false)
  45        {
  46          $this->connection->_raiseError();
  47        }
  48      }
  49      elseif(!$this->queryId)
  50      {
  51        $query = $this->query;
  52  
  53        if(is_array($this->sort_params))
  54        {
  55          if(preg_match('~\s+ORDER\s+BY\s+~i', $query))
  56            $query .= ',';
  57          else
  58            $query .= ' ORDER BY ';
  59          foreach($this->sort_params as $field => $order)
  60            $query .= $this->connection->quoteIdentifier($field) . " $order,";
  61  
  62          $query = rtrim($query, ',');
  63        }
  64  
  65        if($this->limit)
  66        {
  67          $query .= ' LIMIT ' .
  68          $this->limit . ' OFFSET ' .
  69          $this->offset;
  70        }
  71  
  72        $this->queryId = $this->connection->execute($query);
  73      }
  74      $this->key = 0;
  75      $this->next();
  76    }
  77  
  78    function next()
  79    {
  80      $this->current = new lmbSqliteRecord();
  81      $values = sqlite_fetch_array($this->queryId, SQLITE_ASSOC);
  82      if($this->valid = is_array($values))
  83        $this->current->importRaw($values);
  84      $this->key++;
  85    }
  86  
  87    function valid()
  88    {
  89      return $this->valid;
  90    }
  91  
  92    function current()
  93    {
  94      return $this->current;
  95    }
  96  
  97    function key()
  98    {
  99      return $this->key;
 100    }
 101  
 102    function at($pos)
 103    {
 104      $query = $this->query;
 105  
 106      if(is_array($this->sort_params))
 107      {
 108        $query .= ' ORDER BY ';
 109        foreach($this->sort_params as $field => $order)
 110          $query .= $this->connection->quoteIdentifier($field) . " $order,";
 111        $query = rtrim($query, ',');
 112      }
 113  
 114      $queryId = $this->connection->execute($query . " LIMIT 1 OFFSET $pos");
 115  
 116      $res = sqlite_fetch_array($queryId, SQLITE_ASSOC);
 117      if(is_array($res))
 118      {
 119        $record = new lmbSqliteRecord();
 120        $record->importRaw($res);
 121        return $record;
 122      }
 123    }
 124  
 125    function countPaginated()
 126    {
 127      if(is_null($this->queryId))
 128        $this->rewind();
 129      return sqlite_num_rows($this->queryId);
 130    }
 131  
 132    function count()
 133    {
 134      if(!(preg_match("/^\s*SELECT\s+DISTINCT/is", $this->query) || preg_match('/\s+GROUP\s+BY\s+/is',$this->query)))
 135      {
 136        $rewritesql = preg_replace('/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ', $this->query);
 137        $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','', $rewritesql);
 138  
 139        $queryId = $this->connection->execute($rewritesql);
 140        return sqlite_fetch_single($queryId);
 141      }
 142  
 143      // could not re-write the query, try a different method.
 144      $queryId = $this->connection->execute($this->query);
 145      $count = sqlite_num_rows($queryId);
 146      return $count;
 147    }
 148  }
 149  
 150  ?>


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