File indexing completed on 2024-05-12 06:03:08

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Test
0017  * @subpackage PHPUnit
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Db_Statement_Interface
0025  */
0026 // require_once "Zend/Db/Statement/Interface.php";
0027 
0028 /**
0029  * Testing Database Statement that acts as a stack to SQL resultsets.
0030  *
0031  * @category   Zend
0032  * @package    Zend_Test
0033  * @subpackage PHPUnit
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Test_DbStatement implements Zend_Db_Statement_Interface
0038 {
0039     /**
0040      * @var array
0041      */
0042     protected $_fetchStack = array();
0043 
0044     /**
0045      * @var int
0046      */
0047     protected $_columnCount = 0;
0048 
0049     /**
0050      * @var int
0051      */
0052     protected $_rowCount = 0;
0053 
0054     /**
0055      * @var Zend_Db_Profiler_Query
0056      */
0057     protected $_queryProfile = null;
0058 
0059     /**
0060      * Create a Select statement which returns the given array of rows.
0061      *
0062      * @param array $rows
0063      * @return Zend_Test_DbStatement
0064      */
0065     static public function createSelectStatement(array $rows=array())
0066     {
0067         $stmt = new Zend_Test_DbStatement();
0068         foreach($rows AS $row) {
0069             $stmt->append($row);
0070         }
0071         return $stmt;
0072     }
0073 
0074     /**
0075      * Create an Insert Statement
0076      *
0077      * @param  int $affectedRows
0078      * @return Zend_Test_DbStatement
0079      */
0080     static public function createInsertStatement($affectedRows=0)
0081     {
0082         return self::_createRowCountStatement($affectedRows);
0083     }
0084 
0085     /**
0086      * Create an Delete Statement
0087      *
0088      * @param  int $affectedRows
0089      * @return Zend_Test_DbStatement
0090      */
0091     static public function createDeleteStatement($affectedRows=0)
0092     {
0093         return self::_createRowCountStatement($affectedRows);
0094     }
0095 
0096     /**
0097      * Create an Update Statement
0098      *
0099      * @param  int $affectedRows
0100      * @return Zend_Test_DbStatement
0101      */
0102     static public function createUpdateStatement($affectedRows=0)
0103     {
0104         return self::_createRowCountStatement($affectedRows);
0105     }
0106 
0107     /**
0108      * Create a Row Count Statement
0109      *
0110      * @param  int $affectedRows
0111      * @return Zend_Test_DbStatement
0112      */
0113     static protected function _createRowCountStatement($affectedRows)
0114     {
0115         $stmt = new Zend_Test_DbStatement();
0116         $stmt->setRowCount($affectedRows);
0117         return $stmt;
0118     }
0119 
0120     /**
0121      * @param Zend_Db_Profiler_Query $qp
0122      */
0123     public function setQueryProfile(Zend_Db_Profiler_Query $qp)
0124     {
0125         $this->_queryProfile = $qp;
0126     }
0127 
0128     /**
0129      * @param int $rowCount
0130      */
0131     public function setRowCount($rowCount)
0132     {
0133         $this->_rowCount = $rowCount;
0134     }
0135 
0136     /**
0137      * Append a new row to the fetch stack.
0138      *
0139      * @param array $row
0140      */
0141     public function append($row)
0142     {
0143         $this->_columnCount = count($row);
0144         $this->_fetchStack[] = $row;
0145     }
0146 
0147     /**
0148      * Bind a column of the statement result set to a PHP variable.
0149      *
0150      * @param string $column Name the column in the result set, either by
0151      *                       position or by name.
0152      * @param mixed  $param  Reference to the PHP variable containing the value.
0153      * @param mixed  $type   OPTIONAL
0154      * @return bool
0155      * @throws Zend_Db_Statement_Exception
0156      */
0157     public function bindColumn($column, &$param, $type = null)
0158     {
0159         return true;
0160     }
0161 
0162     /**
0163      * Binds a parameter to the specified variable name.
0164      *
0165      * @param mixed $parameter Name the parameter, either integer or string.
0166      * @param mixed $variable  Reference to PHP variable containing the value.
0167      * @param mixed $type      OPTIONAL Datatype of SQL parameter.
0168      * @param mixed $length    OPTIONAL Length of SQL parameter.
0169      * @param mixed $options   OPTIONAL Other options.
0170      * @return bool
0171      * @throws Zend_Db_Statement_Exception
0172      */
0173     public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
0174     {
0175         if($this->_queryProfile !== null) {
0176             $this->_queryProfile->bindParam($parameter, $variable);
0177         }
0178         return true;
0179     }
0180 
0181     /**
0182      * Binds a value to a parameter.
0183      *
0184      * @param mixed $parameter Name the parameter, either integer or string.
0185      * @param mixed $value     Scalar value to bind to the parameter.
0186      * @param mixed $type      OPTIONAL Datatype of the parameter.
0187      * @return bool
0188      * @throws Zend_Db_Statement_Exception
0189      */
0190     public function bindValue($parameter, $value, $type = null)
0191     {
0192         return true;
0193     }
0194 
0195     /**
0196      * Closes the cursor, allowing the statement to be executed again.
0197      *
0198      * @return bool
0199      * @throws Zend_Db_Statement_Exception
0200      */
0201     public function closeCursor()
0202     {
0203         return true;
0204     }
0205 
0206     /**
0207      * Returns the number of columns in the result set.
0208      * Returns null if the statement has no result set metadata.
0209      *
0210      * @return int The number of columns.
0211      * @throws Zend_Db_Statement_Exception
0212      */
0213     public function columnCount()
0214     {
0215         return $this->_columnCount;
0216     }
0217 
0218     /**
0219      * Retrieves the error code, if any, associated with the last operation on
0220      * the statement handle.
0221      *
0222      * @return string error code.
0223      * @throws Zend_Db_Statement_Exception
0224      */
0225     public function errorCode()
0226     {
0227         return false;
0228     }
0229 
0230     /**
0231      * Retrieves an array of error information, if any, associated with the
0232      * last operation on the statement handle.
0233      *
0234      * @return array
0235      * @throws Zend_Db_Statement_Exception
0236      */
0237     public function errorInfo()
0238     {
0239         return false;
0240     }
0241 
0242     /**
0243      * Executes a prepared statement.
0244      *
0245      * @param array $params OPTIONAL Values to bind to parameter placeholders.
0246      * @return bool
0247      * @throws Zend_Db_Statement_Exception
0248      */
0249     public function execute(array $params = array())
0250     {
0251         if($this->_queryProfile !== null) {
0252             $this->_queryProfile->bindParams($params);
0253             $this->_queryProfile->end();
0254         }
0255         return true;
0256     }
0257 
0258     /**
0259      * Fetches a row from the result set.
0260      *
0261      * @param int $style  OPTIONAL Fetch mode for this fetch operation.
0262      * @param int $cursor OPTIONAL Absolute, relative, or other.
0263      * @param int $offset OPTIONAL Number for absolute or relative cursors.
0264      * @return mixed Array, object, or scalar depending on fetch mode.
0265      * @throws Zend_Db_Statement_Exception
0266      */
0267     public function fetch($style = null, $cursor = null, $offset = null)
0268     {
0269         if(count($this->_fetchStack)) {
0270             $row = array_shift($this->_fetchStack);
0271             return $row;
0272         } else {
0273             return false;
0274         }
0275     }
0276 
0277     /**
0278      * Returns an array containing all of the result set rows.
0279      *
0280      * @param int $style OPTIONAL Fetch mode.
0281      * @param int $col   OPTIONAL Column number, if fetch mode is by column.
0282      * @return array Collection of rows, each in a format by the fetch mode.
0283      * @throws Zend_Db_Statement_Exception
0284      */
0285     public function fetchAll($style = null, $col = null)
0286     {
0287         $rows = $this->_fetchStack;
0288         $this->_fetchStack = array();
0289 
0290         return $rows;
0291     }
0292 
0293     /**
0294      * Returns a single column from the next row of a result set.
0295      *
0296      * @param int $col OPTIONAL Position of the column to fetch.
0297      * @return string
0298      * @throws Zend_Db_Statement_Exception
0299      */
0300     public function fetchColumn($col = 0)
0301     {
0302         $row = $this->fetch();
0303 
0304         if($row == false) {
0305             return false;
0306         } else {
0307             if(count($row) < $col) {
0308                 // require_once "Zend/Db/Statement/Exception.php";
0309                 throw new Zend_Db_Statement_Exception(
0310                     "Column Position '".$col."' is out of bounds."
0311                 );
0312             }
0313 
0314             $keys = array_keys($row);
0315             return $row[$keys[$col]];
0316         }
0317     }
0318 
0319     /**
0320      * Fetches the next row and returns it as an object.
0321      *
0322      * @param string $class  OPTIONAL Name of the class to create.
0323      * @param array  $config OPTIONAL Constructor arguments for the class.
0324      * @return mixed One object instance of the specified class.
0325      * @throws Zend_Db_Statement_Exception
0326      */
0327     public function fetchObject($class = 'stdClass', array $config = array())
0328     {
0329         if(!class_exists($class)) {
0330             throw new Zend_Db_Statement_Exception("Class '".$class."' does not exist!");
0331         }
0332 
0333         $object = new $class();
0334         $row = $this->fetch();
0335         foreach($row AS $k => $v) {
0336             $object->$k = $v;
0337         }
0338 
0339         return $object;
0340     }
0341 
0342     /**
0343      * Retrieve a statement attribute.
0344      *
0345      * @param string $key Attribute name.
0346      * @return mixed      Attribute value.
0347      * @throws Zend_Db_Statement_Exception
0348      */
0349     public function getAttribute($key)
0350     {
0351         return false;
0352     }
0353 
0354     /**
0355      * Retrieves the next rowset (result set) for a SQL statement that has
0356      * multiple result sets.  An example is a stored procedure that returns
0357      * the results of multiple queries.
0358      *
0359      * @return bool
0360      * @throws Zend_Db_Statement_Exception
0361      */
0362     public function nextRowset()
0363     {
0364         return false;
0365     }
0366 
0367     /**
0368      * Returns the number of rows affected by the execution of the
0369      * last INSERT, DELETE, or UPDATE statement executed by this
0370      * statement object.
0371      *
0372      * @return int     The number of rows affected.
0373      * @throws Zend_Db_Statement_Exception
0374      */
0375     public function rowCount()
0376     {
0377         return $this->_rowCount;
0378     }
0379 
0380     /**
0381      * Set a statement attribute.
0382      *
0383      * @param string $key Attribute name.
0384      * @param mixed  $val Attribute value.
0385      * @return bool
0386      * @throws Zend_Db_Statement_Exception
0387      */
0388     public function setAttribute($key, $val)
0389     {
0390         return true;
0391     }
0392 
0393     /**
0394      * Set the default fetch mode for this statement.
0395      *
0396      * @param int   $mode The fetch mode.
0397      * @return bool
0398      * @throws Zend_Db_Statement_Exception
0399      */
0400     public function setFetchMode($mode)
0401     {
0402         return true;
0403     }
0404 }