[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/pgsql/ -> lmbPgsqlRecordSet.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  
  10  lmb_require('limb/dbal/src/drivers/lmbDbBaseRecordSet.class.php');
  11  lmb_require('limb/dbal/src/drivers/pgsql/lmbPgsqlRecord.class.php');
  12  
  13  /**

  14   * class lmbPgsqlRecordSet.

  15   *

  16   * @package dbal

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

  18   */
  19  class lmbPgsqlRecordSet extends lmbDbBaseRecordSet
  20  {
  21    protected $queryId;
  22    protected $query;
  23    protected $connection;
  24  
  25    protected $current;
  26    protected $valid;
  27    protected $key;
  28  
  29    function __construct($connection, $queryString)
  30    {
  31      $this->connection = $connection;
  32      $this->query = $queryString;
  33    }
  34  
  35    function freeQuery()
  36    {
  37      if(isset($this->queryId) && is_resource($this->queryId))
  38      {
  39        pg_free_result($this->queryId);
  40        $this->queryId = null;
  41      }
  42    }
  43  
  44    function rewind()
  45    {
  46      if(isset($this->queryId) && is_resource($this->queryId) && pg_num_rows($this->queryId))
  47      {
  48        if(pg_result_seek($this->queryId, 0) === false)
  49          $this->connection->_raiseError();
  50      }
  51      elseif(!$this->queryId)
  52      {
  53        $query = $this->query;
  54  
  55        if(is_array($this->sort_params))
  56        {
  57          if(preg_match('~\s+ORDER\s+BY\s+~i', $query))
  58            $query .= ',';
  59          else
  60            $query .= ' ORDER BY ';
  61          foreach($this->sort_params as $field => $order)
  62            $query .= $this->connection->quoteIdentifier($field) . " $order,";
  63  
  64          $query = rtrim($query, ',');
  65        }
  66  
  67        if($this->limit)
  68        {
  69          $query .= ' LIMIT ' . $this->limit;
  70          $query .= ' OFFSET ' . $this->offset;
  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 lmbPgsqlRecord();
  81      $values = pg_fetch_assoc($this->queryId);
  82      $this->current->import($values);
  83      $this->valid = is_array($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 = pg_fetch_assoc($queryId);
 117      pg_free_result($queryId);
 118      if($res)
 119      {
 120        $record = new lmbPgsqlRecord();
 121        $record->import($res);
 122        return $record;
 123      }
 124    }
 125  
 126    function countPaginated()
 127    {
 128      if(is_null($this->queryId))
 129        $this->rewind();
 130      return pg_num_rows($this->queryId);
 131    }
 132  
 133    function count()
 134    {
 135      if(!(preg_match("/^\s*SELECT\s+DISTINCT/is", $this->query) || preg_match('/\s+GROUP\s+BY\s+/is',$this->query)))
 136      {
 137        $rewritesql = preg_replace('/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ', $this->query);
 138        $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is', '', $rewritesql);
 139  
 140        $queryId = $this->connection->execute($rewritesql);
 141        $row = pg_fetch_row($queryId);
 142        pg_free_result($queryId);
 143        if(is_array($row))
 144          return $row[0];
 145      }
 146  
 147      // could not re-write the query, try a different method.
 148      $queryId = $this->connection->execute($this->query);
 149      $count = pg_num_rows($queryId);
 150      pg_free_result($queryId);
 151      return $count;
 152    }
 153  }
 154  
 155  ?>


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