File indexing completed on 2024-12-22 05:37: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_Adapter_Abstract 0025 */ 0026 // require_once "Zend/Db/Adapter/Abstract.php"; 0027 0028 /** 0029 * @see Zend_Test_DbStatement 0030 */ 0031 // require_once "Zend/Test/DbStatement.php"; 0032 0033 /** 0034 * @see Zend_Db_Profiler 0035 */ 0036 // require_once 'Zend/Db/Profiler.php'; 0037 0038 /** 0039 * Testing Database Adapter which acts as a stack for SQL Results 0040 * 0041 * @category Zend 0042 * @package Zend_Test 0043 * @subpackage PHPUnit 0044 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0045 * @license http://framework.zend.com/license/new-bsd New BSD License 0046 */ 0047 class Zend_Test_DbAdapter extends Zend_Db_Adapter_Abstract 0048 { 0049 /** 0050 * @var array 0051 */ 0052 protected $_statementStack = array(); 0053 0054 /** 0055 * @var boolean 0056 */ 0057 protected $_connected = false; 0058 0059 /** 0060 * @var array 0061 */ 0062 protected $_listTables = array(); 0063 0064 /** 0065 * @var array 0066 */ 0067 protected $_lastInsertIdStack = array(); 0068 0069 /** 0070 * @var array 0071 */ 0072 protected $_describeTables = array(); 0073 0074 /** 0075 * @var string 0076 */ 0077 protected $_quoteIdentifierSymbol = ''; 0078 0079 /** 0080 * Empty constructor to make it parameterless. 0081 */ 0082 public function __construct() 0083 { 0084 $profiler = new Zend_Db_Profiler(); 0085 $profiler->setEnabled(true); 0086 $this->setProfiler($profiler); 0087 } 0088 0089 /** 0090 * Append a new Statement to the SQL Result Stack. 0091 * 0092 * @param Zend_Test_DbStatement $stmt 0093 * @return Zend_Test_DbAdapter 0094 */ 0095 public function appendStatementToStack(Zend_Test_DbStatement $stmt) 0096 { 0097 array_push($this->_statementStack, $stmt); 0098 return $this; 0099 } 0100 0101 /** 0102 * Append a new Insert Id to the {@see lastInsertId}. 0103 * 0104 * @param int|string $id 0105 * @return Zend_Test_DbAdapter 0106 */ 0107 public function appendLastInsertIdToStack($id) 0108 { 0109 array_push($this->_lastInsertIdStack, $id); 0110 return $this; 0111 } 0112 0113 /** 0114 * @var string 0115 */ 0116 public function setQuoteIdentifierSymbol($symbol) 0117 { 0118 $this->_quoteIdentifierSymbol = $symbol; 0119 } 0120 0121 /** 0122 * Returns the symbol the adapter uses for delimited identifiers. 0123 * 0124 * @return string 0125 */ 0126 public function getQuoteIdentifierSymbol() 0127 { 0128 return $this->_quoteIdentifierSymbol; 0129 } 0130 0131 /** 0132 * Set the result from {@see listTables()}. 0133 * 0134 * @param array $listTables 0135 */ 0136 public function setListTables(array $listTables) 0137 { 0138 $this->_listTables = $listTables; 0139 } 0140 0141 /** 0142 * Returns a list of the tables in the database. 0143 * 0144 * @return array 0145 */ 0146 public function listTables() 0147 { 0148 return $this->_listTables; 0149 } 0150 0151 /** 0152 * 0153 * @param string $table 0154 * @param array $tableInfo 0155 * @return Zend_Test_DbAdapter 0156 */ 0157 public function setDescribeTable($table, $tableInfo) 0158 { 0159 $this->_describeTables[$table] = $tableInfo; 0160 return $this; 0161 } 0162 0163 /** 0164 * Returns the column descriptions for a table. 0165 * 0166 * The return value is an associative array keyed by the column name, 0167 * as returned by the RDBMS. 0168 * 0169 * The value of each array element is an associative array 0170 * with the following keys: 0171 * 0172 * SCHEMA_NAME => string; name of database or schema 0173 * TABLE_NAME => string; 0174 * COLUMN_NAME => string; column name 0175 * COLUMN_POSITION => number; ordinal position of column in table 0176 * DATA_TYPE => string; SQL datatype name of column 0177 * DEFAULT => string; default expression of column, null if none 0178 * NULLABLE => boolean; true if column can have nulls 0179 * LENGTH => number; length of CHAR/VARCHAR 0180 * SCALE => number; scale of NUMERIC/DECIMAL 0181 * PRECISION => number; precision of NUMERIC/DECIMAL 0182 * UNSIGNED => boolean; unsigned property of an integer type 0183 * PRIMARY => boolean; true if column is part of the primary key 0184 * PRIMARY_POSITION => integer; position of column in primary key 0185 * 0186 * @param string $tableName 0187 * @param string $schemaName OPTIONAL 0188 * @return array 0189 */ 0190 public function describeTable($tableName, $schemaName = null) 0191 { 0192 if(isset($this->_describeTables[$tableName])) { 0193 return $this->_describeTables[$tableName]; 0194 } else { 0195 return array(); 0196 } 0197 } 0198 0199 /** 0200 * Creates a connection to the database. 0201 * 0202 * @return void 0203 */ 0204 protected function _connect() 0205 { 0206 $this->_connected = true; 0207 } 0208 0209 /** 0210 * Test if a connection is active 0211 * 0212 * @return boolean 0213 */ 0214 public function isConnected() 0215 { 0216 return $this->_connected; 0217 } 0218 0219 /** 0220 * Force the connection to close. 0221 * 0222 * @return void 0223 */ 0224 public function closeConnection() 0225 { 0226 $this->_connected = false; 0227 } 0228 0229 /** 0230 * Prepare a statement and return a PDOStatement-like object. 0231 * 0232 * @param string|Zend_Db_Select $sql SQL query 0233 * @return Zend_Db_Statment|PDOStatement 0234 */ 0235 public function prepare($sql) 0236 { 0237 $queryId = $this->getProfiler()->queryStart($sql); 0238 0239 if(count($this->_statementStack)) { 0240 $stmt = array_pop($this->_statementStack); 0241 } else { 0242 $stmt = new Zend_Test_DbStatement(); 0243 } 0244 0245 if($this->getProfiler()->getEnabled() == true) { 0246 $qp = $this->getProfiler()->getQueryProfile($queryId); 0247 $stmt->setQueryProfile($qp); 0248 } 0249 0250 return $stmt; 0251 } 0252 0253 /** 0254 * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column. 0255 * 0256 * As a convention, on RDBMS brands that support sequences 0257 * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence 0258 * from the arguments and returns the last id generated by that sequence. 0259 * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method 0260 * returns the last value generated for such a column, and the table name 0261 * argument is disregarded. 0262 * 0263 * @param string $tableName OPTIONAL Name of table. 0264 * @param string $primaryKey OPTIONAL Name of primary key column. 0265 * @return string 0266 */ 0267 public function lastInsertId($tableName = null, $primaryKey = null) 0268 { 0269 if(count($this->_lastInsertIdStack)) { 0270 return array_pop($this->_lastInsertIdStack); 0271 } else { 0272 return false; 0273 } 0274 } 0275 0276 /** 0277 * Begin a transaction. 0278 */ 0279 protected function _beginTransaction() 0280 { 0281 return; 0282 } 0283 0284 /** 0285 * Commit a transaction. 0286 */ 0287 protected function _commit() 0288 { 0289 return; 0290 } 0291 0292 /** 0293 * Roll-back a transaction. 0294 */ 0295 protected function _rollBack() 0296 { 0297 0298 } 0299 0300 /** 0301 * Set the fetch mode. 0302 * 0303 * @param integer $mode 0304 * @return void 0305 * @throws Zend_Db_Adapter_Exception 0306 */ 0307 public function setFetchMode($mode) 0308 { 0309 return; 0310 } 0311 0312 /** 0313 * Adds an adapter-specific LIMIT clause to the SELECT statement. 0314 * 0315 * @param mixed $sql 0316 * @param integer $count 0317 * @param integer $offset 0318 * @return string 0319 */ 0320 public function limit($sql, $count, $offset = 0) 0321 { 0322 return sprintf('%s LIMIT %d,%d', $sql, $offset, $count); 0323 } 0324 0325 /** 0326 * Check if the adapter supports real SQL parameters. 0327 * 0328 * @param string $type 'positional' or 'named' 0329 * @return bool 0330 */ 0331 public function supportsParameters($type) 0332 { 0333 return true; 0334 } 0335 0336 /** 0337 * Retrieve server version in PHP style 0338 * 0339 * @return string 0340 */ 0341 function getServerVersion() 0342 { 0343 return "1.0.0"; 0344 } 0345 }