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.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Truncate.php 16607 2009-07-09 21:51:46Z beberlei $
23 require_once "PHPUnit/Extensions/Database/Operation/IDatabaseOperation.php";
25 require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
27 require_once "PHPUnit/Extensions/Database/DataSet/IDataSet.php";
29 require_once "PHPUnit/Extensions/Database/Operation/Exception.php";
32 * @see Zend_Test_PHPUnit_Db_Connection
34 require_once "Zend/Test/PHPUnit/Db/Connection.php";
37 * Operation for Truncating on setup or teardown of a database tester.
39 * @uses PHPUnit_Extensions_Database_Operation_IDatabaseOperation
43 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
44 * @license http://framework.zend.com/license/new-bsd New BSD License
46 class Zend_Test_PHPUnit_Db_Operation_Truncate
implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
50 * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
51 * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
54 public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection
$connection, PHPUnit_Extensions_Database_DataSet_IDataSet
$dataSet)
56 if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection
)) {
57 require_once "Zend/Test/PHPUnit/Db/Exception.php";
58 throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
61 foreach ($dataSet as $table) {
63 $tableName = $table->getTableMetaData()->getTableName();
64 $this->truncate($connection->getConnection(), $tableName);
65 } catch (Exception
$e) {
66 throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE '.$tableName.'', array(), $table, $e->getMessage());
72 * Truncate a given table.
74 * @param Zend_Db_Adapter_Abstract $db
75 * @param string $tableName
78 private function truncate(Zend_Db_Adapter_Abstract
$db, $tableName)
80 $tableName = $db->quoteIdentifier($tableName);
81 if($db instanceof Zend_Db_Adapter_Pdo_Sqlite
) {
82 $db->query('DELETE FROM '.$tableName);
83 } else if($db instanceof Zend_Db_Adapter_Db2
) {
84 if(strstr(PHP_OS
, "WIN")) {
85 $file = tempnam(sys_get_temp_dir(), "zendtestdbibm_");
86 file_put_contents($file, "");
87 $db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName);
90 $db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName);
92 } else if($db instanceof Zend_Db_Adapter_Pdo_Mssql
) {
93 $db->query('TRUNCATE TABLE '.$tableName);
95 $db->query('TRUNCATE '.$tableName);