File indexing completed on 2025-02-23 05:32:49

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_Test_PHPUnit_Db_Connection
0025  */
0026 // require_once "Zend/Test/PHPUnit/Db/Connection.php";
0027 
0028 /**
0029  * Operation for Truncating on setup or teardown of a database tester.
0030  *
0031  * @uses       PHPUnit_Extensions_Database_Operation_IDatabaseOperation
0032  * @category   Zend
0033  * @package    Zend_Test
0034  * @subpackage PHPUnit
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Test_PHPUnit_Db_Operation_Truncate implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
0039 {
0040     /**
0041      *
0042      * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
0043      * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
0044      * @return void
0045      */
0046     public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
0047     {
0048         if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection)) {
0049             // require_once "Zend/Test/PHPUnit/Db/Exception.php";
0050             throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
0051         }
0052 
0053         foreach ($dataSet->getReverseIterator() AS $table) {
0054             try {
0055                 $tableName = $table->getTableMetaData()->getTableName();
0056                 $this->_truncate($connection->getConnection(), $tableName);
0057             } catch (Exception $e) {
0058                 throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE '.$tableName.'', array(), $table, $e->getMessage());
0059             }
0060         }
0061     }
0062 
0063     /**
0064      * Truncate a given table.
0065      *
0066      * @param Zend_Db_Adapter_Abstract $db
0067      * @param string $tableName
0068      * @return void
0069      */
0070     protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName)
0071     {
0072         $tableName = $db->quoteIdentifier($tableName, true);
0073         if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) {
0074             $db->query('DELETE FROM '.$tableName);
0075         } else if($db instanceof Zend_Db_Adapter_Db2) {
0076             /*if(strstr(PHP_OS, "WIN")) {
0077                 $file = tempnam(sys_get_temp_dir(), "zendtestdbibm_");
0078                 file_put_contents($file, "");
0079                 $db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName);
0080                 unlink($file);
0081             } else {
0082                 $db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName);
0083             }*/
0084             // require_once "Zend/Exception.php";
0085             throw Zend_Exception("IBM Db2 TRUNCATE not supported.");
0086         } else if($this->_isMssqlOrOracle($db)) {
0087             $db->query('TRUNCATE TABLE '.$tableName);
0088         } else if($db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
0089             $db->query('TRUNCATE '.$tableName.' CASCADE');
0090         } else {
0091             $db->query('TRUNCATE '.$tableName);
0092         }
0093     }
0094 
0095     /**
0096      * Detect if an adapter is for Mssql or Oracle Databases.
0097      *
0098      * @param  Zend_Db_Adapter_Abstract $db
0099      * @return bool
0100      */
0101     private function _isMssqlOrOracle($db)
0102     {
0103         return (
0104             $db instanceof Zend_Db_Adapter_Pdo_Mssql ||
0105             $db instanceof Zend_Db_Adapter_Sqlsrv ||
0106             $db instanceof Zend_Db_Adapter_Pdo_Oci ||
0107             $db instanceof Zend_Db_Adapter_Oracle
0108         );
0109     }
0110 }