[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/creole/drivers/pgsql/ -> PgSQLResultSet.php (source)

   1  <?php
   2  /*
   3   *  $Id: PgSQLResultSet.php,v 1.31 2006/01/17 19:44:40 hlellelid Exp $
   4   *
   5   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   6   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   7   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   8   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   9   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16   *
  17   * This software consists of voluntary contributions made by many individuals
  18   * and is licensed under the LGPL. For more information please see
  19   * <http://creole.phpdb.org>.
  20   */
  21   
  22  require_once 'creole/ResultSet.php';
  23  require_once 'creole/common/ResultSetCommon.php';
  24  
  25  /**
  26   * PostgreSQL implementation of ResultSet.
  27   *
  28   * @author    Hans Lellelid <hans@xmpl.org>
  29   * @version   $Revision: 1.31 $
  30   * @package   creole.drivers.pgsql
  31   */
  32  class PgSQLResultSet extends ResultSetCommon implements ResultSet {
  33  
  34  
  35      /**
  36       * Gets optimized PgSQLResultSetIterator.
  37       * @return PgSQLResultSetIterator
  38       */
  39      /*
  40      public function getIterator()
  41      {   
  42          require_once 'creole/drivers/pgsql/PgSQLResultSetIterator.php';
  43          return new PgSQLResultSetIterator($this);
  44      }
  45      */
  46  
  47      /**
  48       * Postgres doesn't actually move the db pointer.  The specific row
  49       * is fetched by call to pg_fetch_array() rather than by a seek and
  50       * then an unspecified pg_fetch_array() call.
  51       * 
  52       * The only side-effect of this situation is that we don't really know 
  53       * if the seek will fail or succeed until we have called next().  This
  54       * behavior is acceptible - and explicitly documented in 
  55       * ResultSet::seek() phpdoc.
  56       * 
  57       * @see ResultSet::seek()
  58       */ 
  59  	public function seek($rownum)
  60      {
  61          if ($rownum < 0) {
  62              return false;
  63          }
  64          
  65          // PostgreSQL rows start w/ 0, but this works, because we are
  66          // looking to move the position _before_ the next desired position
  67          $this->cursorPos = $rownum;
  68          return true;
  69      }
  70      
  71      /**
  72       * @see ResultSet::next()
  73       */ 
  74  	public function next()
  75      {
  76  
  77          $this->fields = @pg_fetch_array($this->result, $this->cursorPos, $this->fetchmode);
  78  
  79          if (!$this->fields) {
  80              $err = @pg_result_error($this->result);
  81              if (!$err) {
  82                  // We've advanced beyond end of recordset.
  83                  $this->afterLast();
  84                  return false;
  85              } else {
  86                  throw new SQLException("Error fetching result", $err);                
  87              }
  88          }
  89  
  90          if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) {
  91              $this->fields = array_change_key_case($this->fields, CASE_LOWER);
  92          }
  93          // Advance cursor position
  94          $this->cursorPos++;    
  95          return true;
  96      }
  97  
  98      /**
  99       * @see ResultSet::getRecordCount()
 100       */
 101  	public function getRecordCount()
 102      {
 103          $rows = @pg_num_rows($this->result);
 104          if ($rows === null) {
 105              throw new SQLException("Error fetching num rows", pg_result_error($this->result));
 106          }
 107          return (int) $rows;
 108      }    
 109  
 110      /**
 111       * @see ResultSet::close()
 112       */
 113  	public function close()
 114      {
 115          $this->fields = array();
 116          @pg_free_result($this->result);
 117      }
 118          
 119      /**
 120       * Convert Postgres string representation of array into native PHP array.
 121       * @param string $str Postgres string array rep: {1223, 2343} or {{"welcome", "home"}, {"test2", ""}}
 122       * @return array
 123       */
 124  	private function strToArray($str)
 125      {
 126          $str = substr($str, 1, -1); // remove { }
 127          $res = array();
 128          
 129          $subarr = array();
 130          $in_subarr = 0;
 131          
 132          $toks = explode(',', $str);
 133          foreach($toks as $tok) {                    
 134              if ($in_subarr > 0) { // already in sub-array?
 135                  $subarr[$in_subarr][] = $tok;
 136                  if ('}' === substr($tok, -1, 1)) { // check to see if we just added last component                    
 137                      $res[] = $this->strToArray(implode(',', $subarr[$in_subarr]));
 138                      $in_subarr--;
 139                  }
 140              } elseif ($tok{0} === '{') { // we're inside a new sub-array                               
 141                  if ('}' !== substr($tok, -1, 1)) {
 142                      $in_subarr++;
 143                      // if sub-array has more than one element
 144                      $subarr[$in_subarr] = array();
 145                      $subarr[$in_subarr][] = $tok;                    
 146                  } else {
 147                      $res[] = $this->strToArray($tok);
 148                  }
 149              } else { // not sub-array
 150                  $val = trim($tok, '"'); // remove " (surrounding strings)
 151                  // perform type castng here?
 152                  $res[] = $val;
 153              }
 154          }
 155          
 156          return $res;
 157      }
 158  
 159      /**
 160       * Reads a column as an array.
 161       * The value of the column is unserialized & returned as an array.
 162       * @param mixed $column Column name (string) or index (int) starting with 1.
 163       * @return array
 164       * @throws SQLException - If the column specified is not a valid key in current field array.
 165       */
 166  	public function getArray($column) 
 167      {
 168          if (is_int($column)) { $column--; } // because Java convention is to start at 1 
 169          if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
 170          if ($this->fields[$column] === null) { return null; }
 171          return $this->strToArray($this->fields[$column]);
 172      } 
 173      
 174      /**
 175       * Returns Blob with contents of column value.
 176       * 
 177       * @param mixed $column Column name (string) or index (int) starting with 1 (if ResultSet::FETCHMODE_NUM was used).
 178       * @return Blob New Blob with data from column.
 179       * @throws SQLException - If the column specified is not a valid key in current field array.
 180       */
 181  	public function getBlob($column) 
 182      {
 183          if (is_int($column)) { $column--; } // because Java convention is to start at 1 
 184          if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
 185          if ($this->fields[$column] === null) { return null; }
 186          require_once 'creole/util/Blob.php';
 187          $b = new Blob();
 188          $b->setContents(pg_unescape_bytea($this->fields[$column]));
 189          return $b;
 190      }     
 191  
 192      /**
 193       * @param mixed $column Column name (string) or index (int) starting with 1.
 194       * @return boolean
 195       * @throws SQLException - If the column specified is not a valid key in current field array.
 196       */
 197  	public function getBoolean($column) 
 198      {
 199          if (is_int($column)) { $column--; } // because Java convention is to start at 1 
 200          if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
 201          if ($this->fields[$column] === null) { return null; }
 202          return ($this->fields[$column] === 't');
 203      }
 204  
 205  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7