*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Test / PHPUnit / Db / Operation / Truncate.php
blob05be935f776cb81ef26e9f30c9a0b2bf20cd88f8
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Test
17 * @subpackage PHPUnit
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";
31 /**
32 * @see Zend_Test_PHPUnit_Db_Connection
34 require_once "Zend/Test/PHPUnit/Db/Connection.php";
36 /**
37 * Operation for Truncating on setup or teardown of a database tester.
39 * @uses PHPUnit_Extensions_Database_Operation_IDatabaseOperation
40 * @category Zend
41 * @package Zend_Test
42 * @subpackage PHPUnit
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
48 /**
50 * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
51 * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
52 * @return void
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) {
62 try {
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());
71 /**
72 * Truncate a given table.
74 * @param Zend_Db_Adapter_Abstract $db
75 * @param string $tableName
76 * @return void
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);
88 unlink($file);
89 } else {
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);
94 } else {
95 $db->query('TRUNCATE '.$tableName);