Truncate.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once "PHPUnit/Extensions/Database/Operation/IDatabaseOperation.php";
  23. require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
  24. require_once "PHPUnit/Extensions/Database/DataSet/IDataSet.php";
  25. require_once "PHPUnit/Extensions/Database/Operation/Exception.php";
  26. /**
  27. * @see Zend_Test_PHPUnit_Db_Connection
  28. */
  29. require_once "Zend/Test/PHPUnit/Db/Connection.php";
  30. /**
  31. * Operation for Truncating on setup or teardown of a database tester.
  32. *
  33. * @uses PHPUnit_Extensions_Database_Operation_IDatabaseOperation
  34. * @category Zend
  35. * @package Zend_Test
  36. * @subpackage PHPUnit
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Test_PHPUnit_Db_Operation_Truncate implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
  41. {
  42. /**
  43. *
  44. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  45. * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  46. * @return void
  47. */
  48. public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
  49. {
  50. if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection)) {
  51. require_once "Zend/Test/PHPUnit/Db/Exception.php";
  52. throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
  53. }
  54. foreach ($dataSet as $table) {
  55. try {
  56. $tableName = $table->getTableMetaData()->getTableName();
  57. $this->truncate($connection->getConnection(), $tableName);
  58. } catch (Exception $e) {
  59. throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE '.$tableName.'', array(), $table, $e->getMessage());
  60. }
  61. }
  62. }
  63. /**
  64. * Truncate a given table.
  65. *
  66. * @param Zend_Db_Adapter_Abstract $db
  67. * @param string $tableName
  68. * @return void
  69. */
  70. private function truncate(Zend_Db_Adapter_Abstract $db, $tableName)
  71. {
  72. $tableName = $db->quoteIdentifier($tableName);
  73. if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) {
  74. $db->query('DELETE FROM '.$tableName);
  75. } else if($db instanceof Zend_Db_Adapter_Db2) {
  76. if(strstr(PHP_OS, "WIN")) {
  77. $file = tempnam(sys_get_temp_dir(), "zendtestdbibm_");
  78. file_put_contents($file, "");
  79. $db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName);
  80. unlink($file);
  81. } else {
  82. $db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName);
  83. }
  84. } else if($db instanceof Zend_Db_Adapter_Pdo_Mssql) {
  85. $db->query('TRUNCATE TABLE '.$tableName);
  86. } else {
  87. $db->query('TRUNCATE '.$tableName);
  88. }
  89. }
  90. }