"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.12.5/src/system/Papaya/Database/Connection/AbstractResult.php" (7 Jul 2020, 3172 Bytes) of package /linux/www/papayacms-core-6.12.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractResult.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Papaya\Database\Connection {
    4 
    5   use Papaya\Database\Connection as DatabaseConnection;
    6   use Papaya\Database\Result as DatabaseResult;
    7   use Papaya\Database\Result\Iterator as DatabaseResultIterator;
    8   use Papaya\Database\Statement as DatabaseStatement;
    9   use Papaya\Database\Statement\Count as CountStatement;
   10 
   11   abstract class AbstractResult implements DatabaseResult {
   12 
   13     /**
   14      * @var DatabaseConnection
   15      */
   16     private $_connection;
   17 
   18     /**
   19      * @var DatabaseStatement
   20      */
   21     private $_statement;
   22 
   23     /**
   24      * @var int
   25      */
   26     private $_absoluteCount = -1;
   27 
   28     public function __construct(DatabaseConnection $connection, DatabaseStatement $statement) {
   29       $this->_connection = $connection;
   30       $this->_statement = $statement;
   31     }
   32 
   33     /**
   34      * @return DatabaseConnection
   35      */
   36     public function getConnection() {
   37       return $this->_connection;
   38     }
   39 
   40     /**
   41      * @return DatabaseStatement
   42      */
   43     public function getStatement() {
   44       return $this->_statement;
   45     }
   46 
   47     /**
   48      * Return and Iterator for the result, allowing to use foreach on it.
   49      *
   50      * @return \Iterator
   51      */
   52     public function getIterator() {
   53       return new DatabaseResultIterator($this);
   54     }
   55 
   56     /**
   57      * @return array|NULL
   58      */
   59     public function fetchAssoc() {
   60       return $this->fetchRow(self::FETCH_ASSOC);
   61     }
   62 
   63     /**
   64      * @param mixed $fieldIndex Index/Name of field
   65      * @return string|number|bool|NULL
   66      */
   67     public function fetchField($fieldIndex = 0) {
   68       if (is_int($fieldIndex)) {
   69         $data = $this->fetchRow();
   70         return $data[$fieldIndex];
   71       }
   72       if (is_string($fieldIndex)) {
   73         $data = $this->fetchRow(self::FETCH_ASSOC);
   74         return $data[$fieldIndex];
   75       }
   76       return FALSE;
   77     }
   78 
   79     /**
   80     * @return boolean
   81     */
   82     public function seekFirst() {
   83       return $this->seek(0);
   84     }
   85 
   86     /**
   87     * @return boolean
   88     */
   89     public function seekLast() {
   90       if (FALSE !== ($count = $this->count())) {
   91         return $this->seek($count);
   92       }
   93       return FALSE;
   94     }
   95 
   96     /**
   97      * @param int $absoluteCount
   98      */
   99     public function setAbsoluteCount($absoluteCount) {
  100       $this->_absoluteCount = (int)$absoluteCount;
  101     }
  102 
  103     /**
  104      * Acquire absolute number of database records
  105      *
  106      * @return integer|NULL
  107      */
  108     public function absCount() {
  109       if ($this->_absoluteCount === -1) {
  110         $absoluteCount = $this->queryRecordCount();
  111         $this->_absoluteCount = (NULL === $absoluteCount) ? NULL : (int)$absoluteCount;
  112       }
  113       return $this->_absoluteCount;
  114     }
  115 
  116     /**
  117      * Rewrite query to get record count of a limited query and execute it.
  118      *
  119      * @return integer|NULL record count or failure
  120      */
  121     private function queryRecordCount() {
  122       if (
  123         ($connection = $this->getConnection()) &&
  124         ($countSql = new CountStatement($connection, $this->getStatement())) &&
  125         ($dbmsResult = $connection->execute($countSql))
  126       ) {
  127         $result = $dbmsResult->fetchField();
  128         $dbmsResult->free();
  129         return $result;
  130       }
  131       return NULL;
  132     }
  133   }
  134 }