[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/mysql/ -> lmbMysqlRecordSet.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/mysql/lmbMysqlRecord.class.php');
  11  
  12  /**

  13   * class lmbMysqlRecordSet.

  14   *

  15   * @package dbal

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

  17   */
  18  class lmbMysqlRecordSet 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      {
  38        mysql_free_result($this->queryId);
  39        $this->queryId = null;
  40      }
  41    }
  42  
  43    function rewind()
  44    {
  45      if(isset($this->queryId) && is_resource($this->queryId) && mysql_num_rows($this->queryId))
  46      {
  47        if(mysql_data_seek($this->queryId, 0) === false)
  48        {
  49          $this->connection->_raiseError();
  50        }
  51      }
  52      elseif(!$this->queryId)
  53      {
  54        $query = $this->query;
  55  
  56        if(is_array($this->sort_params))
  57        {
  58          if(preg_match('~\s+ORDER\s+BY\s+~i', $query))
  59            $query .= ',';
  60          else
  61            $query .= ' ORDER BY ';
  62          foreach($this->sort_params as $field => $order)
  63            $query .= $this->connection->quoteIdentifier($field) . " $order,";
  64  
  65          $query = rtrim($query, ',');
  66        }
  67  
  68        if($this->limit)
  69        {
  70          $query .= ' LIMIT ' .
  71          $this->offset . ',' .
  72          $this->limit;
  73        }
  74  
  75        $this->queryId = $this->connection->execute($query);
  76      }
  77      $this->key = 0;
  78      $this->next();
  79    }
  80  
  81    function next()
  82    {
  83      $this->current = new lmbMysqlRecord();
  84      $values = mysql_fetch_assoc($this->queryId);
  85      $this->current->import($values);
  86      $this->valid = is_array($values);
  87      $this->key++;
  88    }
  89  
  90    function valid()
  91    {
  92      return $this->valid;
  93    }
  94  
  95    function current()
  96    {
  97      return $this->current;
  98    }
  99  
 100    function key()
 101    {
 102      return $this->key;
 103    }
 104  
 105    function at($pos)
 106    {
 107      $query = $this->query;
 108  
 109      if(is_array($this->sort_params))
 110      {
 111        $query .= ' ORDER BY ';
 112        foreach($this->sort_params as $field => $order)
 113          $query .= $this->connection->quoteIdentifier($field) . " $order,";
 114        $query = rtrim($query, ',');
 115      }
 116  
 117      $queryId = $this->connection->execute($query . " LIMIT $pos, 1");
 118  
 119      $res = mysql_fetch_assoc($queryId);
 120      mysql_free_result($queryId);
 121      if($res)
 122      {
 123        $record = new lmbMysqlRecord();
 124        $record->import($res);
 125        return $record;
 126      }
 127    }
 128  
 129    function countPaginated()
 130    {
 131      if(is_null($this->queryId))
 132        $this->rewind();
 133      return mysql_num_rows($this->queryId);
 134    }
 135  
 136    function count()
 137    {
 138      if(!(preg_match("/^\s*SELECT\s+DISTINCT/is", $this->query) || preg_match('/\s+GROUP\s+BY\s+/is',$this->query)))
 139      {
 140        $rewritesql = preg_replace('/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ', $this->query);
 141        $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','', $rewritesql);
 142  
 143        $queryId = $this->connection->execute($rewritesql);
 144        $row = mysql_fetch_row($queryId);
 145        mysql_free_result($queryId);
 146        if(is_array($row))
 147          return $row[0];
 148      }
 149  
 150      // could not re-write the query, try a different method.
 151      $queryId = $this->connection->execute($this->query);
 152      $count = mysql_num_rows($queryId);
 153      mysql_free_result($queryId);
 154      return $count;
 155    }
 156  }
 157  
 158  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7