Truncate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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-2014 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. /**
  23. * @see Zend_Test_PHPUnit_Db_Connection
  24. */
  25. require_once "Zend/Test/PHPUnit/Db/Connection.php";
  26. /**
  27. * Operation for Truncating on setup or teardown of a database tester.
  28. *
  29. * @uses PHPUnit_Extensions_Database_Operation_IDatabaseOperation
  30. * @category Zend
  31. * @package Zend_Test
  32. * @subpackage PHPUnit
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Test_PHPUnit_Db_Operation_Truncate implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
  37. {
  38. /**
  39. *
  40. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  41. * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  42. * @return void
  43. */
  44. public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
  45. {
  46. if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection)) {
  47. require_once "Zend/Test/PHPUnit/Db/Exception.php";
  48. throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
  49. }
  50. foreach ($dataSet->getReverseIterator() AS $table) {
  51. try {
  52. $tableName = $table->getTableMetaData()->getTableName();
  53. $this->_truncate($connection->getConnection(), $tableName);
  54. } catch (Exception $e) {
  55. throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE '.$tableName.'', array(), $table, $e->getMessage());
  56. }
  57. }
  58. }
  59. /**
  60. * Truncate a given table.
  61. *
  62. * @param Zend_Db_Adapter_Abstract $db
  63. * @param string $tableName
  64. * @return void
  65. */
  66. protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName)
  67. {
  68. $tableName = $db->quoteIdentifier($tableName, true);
  69. if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) {
  70. $db->query('DELETE FROM '.$tableName);
  71. } else if($db instanceof Zend_Db_Adapter_Db2) {
  72. /*if(strstr(PHP_OS, "WIN")) {
  73. $file = tempnam(sys_get_temp_dir(), "zendtestdbibm_");
  74. file_put_contents($file, "");
  75. $db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName);
  76. unlink($file);
  77. } else {
  78. $db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName);
  79. }*/
  80. require_once "Zend/Exception.php";
  81. throw Zend_Exception("IBM Db2 TRUNCATE not supported.");
  82. } else if($this->_isMssqlOrOracle($db)) {
  83. $db->query('TRUNCATE TABLE '.$tableName);
  84. } else if($db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
  85. $db->query('TRUNCATE '.$tableName.' CASCADE');
  86. } else {
  87. $db->query('TRUNCATE '.$tableName);
  88. }
  89. }
  90. /**
  91. * Detect if an adapter is for Mssql or Oracle Databases.
  92. *
  93. * @param Zend_Db_Adapter_Abstract $db
  94. * @return bool
  95. */
  96. private function _isMssqlOrOracle($db)
  97. {
  98. return (
  99. $db instanceof Zend_Db_Adapter_Pdo_Mssql ||
  100. $db instanceof Zend_Db_Adapter_Sqlsrv ||
  101. $db instanceof Zend_Db_Adapter_Pdo_Oci ||
  102. $db instanceof Zend_Db_Adapter_Oracle
  103. );
  104. }
  105. }