web/lib/Zend/Test/DbAdapter.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Test
       
    17  * @subpackage PHPUnit
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: DbAdapter.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Db_Adapter_Abstract
       
    25  */
       
    26 require_once "Zend/Db/Adapter/Abstract.php";
       
    27 
       
    28 /**
       
    29  * @see Zend_Test_DbStatement
       
    30  */
       
    31 require_once "Zend/Test/DbStatement.php";
       
    32 
       
    33 /**
       
    34  * @see Zend_Db_Profiler
       
    35  */
       
    36 require_once 'Zend/Db/Profiler.php';
       
    37 
       
    38 /**
       
    39  * Testing Database Adapter which acts as a stack for SQL Results
       
    40  *
       
    41  * @category   Zend
       
    42  * @package    Zend_Test
       
    43  * @subpackage PHPUnit
       
    44  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    46  */
       
    47 class Zend_Test_DbAdapter extends Zend_Db_Adapter_Abstract
       
    48 {
       
    49     /**
       
    50      * @var array
       
    51      */
       
    52     protected $_statementStack = array();
       
    53 
       
    54     /**
       
    55      * @var boolean
       
    56      */
       
    57     protected $_connected = false;
       
    58 
       
    59     /**
       
    60      * @var array
       
    61      */
       
    62     protected $_listTables = array();
       
    63 
       
    64     /**
       
    65      * @var array
       
    66      */
       
    67     protected $_lastInsertIdStack = array();
       
    68 
       
    69     /**
       
    70      * @var array
       
    71      */
       
    72     protected $_describeTables = array();
       
    73 
       
    74     /**
       
    75      * @var string
       
    76      */ 
       
    77     protected $_quoteIdentifierSymbol = '';
       
    78 
       
    79     /**
       
    80      * Empty constructor to make it parameterless.
       
    81      */
       
    82     public function __construct()
       
    83     {
       
    84         $profiler = new Zend_Db_Profiler();
       
    85         $profiler->setEnabled(true);
       
    86         $this->setProfiler($profiler);
       
    87     }
       
    88 
       
    89     /**
       
    90      * Append a new Statement to the SQL Result Stack.
       
    91      *
       
    92      * @param  Zend_Test_DbStatement $stmt
       
    93      * @return Zend_Test_DbAdapter
       
    94      */
       
    95     public function appendStatementToStack(Zend_Test_DbStatement $stmt)
       
    96     {
       
    97         array_push($this->_statementStack, $stmt);
       
    98         return $this;
       
    99     }
       
   100 
       
   101     /**
       
   102      * Append a new Insert Id to the {@see lastInsertId}.
       
   103      *
       
   104      * @param  int|string $id
       
   105      * @return Zend_Test_DbAdapter
       
   106      */
       
   107     public function appendLastInsertIdToStack($id)
       
   108     {
       
   109         array_push($this->_lastInsertIdStack, $id);
       
   110         return $this;
       
   111     }
       
   112 
       
   113     /**
       
   114      * @var string
       
   115      */ 
       
   116     public function setQuoteIdentifierSymbol($symbol)
       
   117     {
       
   118         $this->_quoteIdentifierSymbol = $symbol;
       
   119     }
       
   120 
       
   121     /**
       
   122      * Returns the symbol the adapter uses for delimited identifiers.
       
   123      *
       
   124      * @return string
       
   125      */
       
   126     public function getQuoteIdentifierSymbol()
       
   127     {
       
   128         return $this->_quoteIdentifierSymbol;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Set the result from {@see listTables()}.
       
   133      *
       
   134      * @param array $listTables
       
   135      */
       
   136     public function setListTables(array $listTables)
       
   137     {
       
   138         $this->_listTables = $listTables;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Returns a list of the tables in the database.
       
   143      *
       
   144      * @return array
       
   145      */
       
   146     public function listTables()
       
   147     {
       
   148        return $this->_listTables;
       
   149     }
       
   150 
       
   151     /**
       
   152      *
       
   153      * @param  string $table
       
   154      * @param  array $tableInfo
       
   155      * @return Zend_Test_DbAdapter
       
   156      */
       
   157     public function setDescribeTable($table, $tableInfo)
       
   158     {
       
   159         $this->_describeTables[$table] = $tableInfo;
       
   160         return $this;
       
   161     }
       
   162 
       
   163     /**
       
   164      * Returns the column descriptions for a table.
       
   165      *
       
   166      * The return value is an associative array keyed by the column name,
       
   167      * as returned by the RDBMS.
       
   168      *
       
   169      * The value of each array element is an associative array
       
   170      * with the following keys:
       
   171      *
       
   172      * SCHEMA_NAME => string; name of database or schema
       
   173      * TABLE_NAME  => string;
       
   174      * COLUMN_NAME => string; column name
       
   175      * COLUMN_POSITION => number; ordinal position of column in table
       
   176      * DATA_TYPE   => string; SQL datatype name of column
       
   177      * DEFAULT     => string; default expression of column, null if none
       
   178      * NULLABLE    => boolean; true if column can have nulls
       
   179      * LENGTH      => number; length of CHAR/VARCHAR
       
   180      * SCALE       => number; scale of NUMERIC/DECIMAL
       
   181      * PRECISION   => number; precision of NUMERIC/DECIMAL
       
   182      * UNSIGNED    => boolean; unsigned property of an integer type
       
   183      * PRIMARY     => boolean; true if column is part of the primary key
       
   184      * PRIMARY_POSITION => integer; position of column in primary key
       
   185      *
       
   186      * @param string $tableName
       
   187      * @param string $schemaName OPTIONAL
       
   188      * @return array
       
   189      */
       
   190     public function describeTable($tableName, $schemaName = null)
       
   191     {
       
   192         if(isset($this->_describeTables[$tableName])) {
       
   193             return $this->_describeTables[$tableName];
       
   194         } else {
       
   195             return array();
       
   196         }
       
   197     }
       
   198 
       
   199     /**
       
   200      * Creates a connection to the database.
       
   201      *
       
   202      * @return void
       
   203      */
       
   204     protected function _connect()
       
   205     {
       
   206         $this->_connected = true;
       
   207     }
       
   208 
       
   209     /**
       
   210      * Test if a connection is active
       
   211      *
       
   212      * @return boolean
       
   213      */
       
   214     public function isConnected()
       
   215     {
       
   216         return $this->_connected;
       
   217     }
       
   218 
       
   219     /**
       
   220      * Force the connection to close.
       
   221      *
       
   222      * @return void
       
   223      */
       
   224     public function closeConnection()
       
   225     {
       
   226         $this->_connected = false;
       
   227     }
       
   228 
       
   229     /**
       
   230      * Prepare a statement and return a PDOStatement-like object.
       
   231      *
       
   232      * @param string|Zend_Db_Select $sql SQL query
       
   233      * @return Zend_Db_Statment|PDOStatement
       
   234      */
       
   235     public function prepare($sql)
       
   236     {
       
   237         $queryId = $this->getProfiler()->queryStart($sql);
       
   238 
       
   239         if(count($this->_statementStack)) {
       
   240             $stmt = array_pop($this->_statementStack);
       
   241         } else {
       
   242             $stmt = new Zend_Test_DbStatement();
       
   243         }
       
   244 
       
   245         if($this->getProfiler()->getEnabled() == true) {
       
   246             $qp = $this->getProfiler()->getQueryProfile($queryId);
       
   247             $stmt->setQueryProfile($qp);
       
   248         }
       
   249 
       
   250         return $stmt;
       
   251     }
       
   252 
       
   253     /**
       
   254      * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
       
   255      *
       
   256      * As a convention, on RDBMS brands that support sequences
       
   257      * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
       
   258      * from the arguments and returns the last id generated by that sequence.
       
   259      * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
       
   260      * returns the last value generated for such a column, and the table name
       
   261      * argument is disregarded.
       
   262      *
       
   263      * @param string $tableName   OPTIONAL Name of table.
       
   264      * @param string $primaryKey  OPTIONAL Name of primary key column.
       
   265      * @return string
       
   266      */
       
   267     public function lastInsertId($tableName = null, $primaryKey = null)
       
   268     {
       
   269         if(count($this->_lastInsertIdStack)) {
       
   270             return array_pop($this->_lastInsertIdStack);
       
   271         } else {
       
   272             return false;
       
   273         }
       
   274     }
       
   275 
       
   276     /**
       
   277      * Begin a transaction.
       
   278      */
       
   279     protected function _beginTransaction()
       
   280     {
       
   281         return;
       
   282     }
       
   283 
       
   284     /**
       
   285      * Commit a transaction.
       
   286      */
       
   287     protected function _commit()
       
   288     {
       
   289         return;
       
   290     }
       
   291 
       
   292     /**
       
   293      * Roll-back a transaction.
       
   294      */
       
   295     protected function _rollBack()
       
   296     {
       
   297 
       
   298     }
       
   299 
       
   300     /**
       
   301      * Set the fetch mode.
       
   302      *
       
   303      * @param integer $mode
       
   304      * @return void
       
   305      * @throws Zend_Db_Adapter_Exception
       
   306      */
       
   307     public function setFetchMode($mode)
       
   308     {
       
   309         return;
       
   310     }
       
   311 
       
   312     /**
       
   313      * Adds an adapter-specific LIMIT clause to the SELECT statement.
       
   314      *
       
   315      * @param mixed $sql
       
   316      * @param integer $count
       
   317      * @param integer $offset
       
   318      * @return string
       
   319      */
       
   320     public function limit($sql, $count, $offset = 0)
       
   321     {
       
   322         return sprintf('%s LIMIT %d,%d', $sql, $offset, $count);
       
   323     }
       
   324 
       
   325     /**
       
   326      * Check if the adapter supports real SQL parameters.
       
   327      *
       
   328      * @param string $type 'positional' or 'named'
       
   329      * @return bool
       
   330      */
       
   331     public function supportsParameters($type)
       
   332     {
       
   333         return true;
       
   334     }
       
   335 
       
   336     /**
       
   337      * Retrieve server version in PHP style
       
   338      *
       
   339      * @return string
       
   340      */
       
   341     function getServerVersion()
       
   342     {
       
   343         return "1.0.0";
       
   344     }
       
   345 }