diff -r bd595ad770fc -r 1c2f13fd785c web/enmi/Zend/Test/PHPUnit/Db/Operation/Truncate.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/enmi/Zend/Test/PHPUnit/Db/Operation/Truncate.php Thu Jan 20 19:30:54 2011 +0100 @@ -0,0 +1,130 @@ +getReverseIterator() AS $table) { + try { + $tableName = $table->getTableMetaData()->getTableName(); + $this->_truncate($connection->getConnection(), $tableName); + } catch (Exception $e) { + throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE '.$tableName.'', array(), $table, $e->getMessage()); + } + } + } + + /** + * Truncate a given table. + * + * @param Zend_Db_Adapter_Abstract $db + * @param string $tableName + * @return void + */ + protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName) + { + $tableName = $db->quoteIdentifier($tableName); + if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) { + $db->query('DELETE FROM '.$tableName); + } else if($db instanceof Zend_Db_Adapter_Db2) { + /*if(strstr(PHP_OS, "WIN")) { + $file = tempnam(sys_get_temp_dir(), "zendtestdbibm_"); + file_put_contents($file, ""); + $db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName); + unlink($file); + } else { + $db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName); + }*/ + require_once "Zend/Exception.php"; + throw Zend_Exception("IBM Db2 TRUNCATE not supported."); + } else if($this->_isMssqlOrOracle($db)) { + $db->query('TRUNCATE TABLE '.$tableName); + } else if($db instanceof Zend_Db_Adapter_Pdo_Pgsql) { + $db->query('TRUNCATE '.$tableName.' CASCADE'); + } else { + $db->query('TRUNCATE '.$tableName); + } + } + + /** + * Detect if an adapter is for Mssql or Oracle Databases. + * + * @param Zend_Db_Adapter_Abstract $db + * @return bool + */ + private function _isMssqlOrOracle($db) + { + return ( + $db instanceof Zend_Db_Adapter_Pdo_Mssql || + $db instanceof Zend_Db_Adapter_Sqlsrv || + $db instanceof Zend_Db_Adapter_Pdo_Oci || + $db instanceof Zend_Db_Adapter_Oracle + ); + } +} \ No newline at end of file