De-duplicate truncate method of drivers

This commit is contained in:
Timothy Warren 2014-04-24 16:25:04 -04:00
parent 9b3cb6c42a
commit 5b531cf588
5 changed files with 33 additions and 62 deletions

View File

@ -71,6 +71,12 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/
public $table_prefix = '';
/**
* Whether the driver supports 'TRUNCATE'
* @var bool
*/
public $has_truncate = TRUE;
/**
* PDO constructor wrapper
*
@ -607,17 +613,26 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
return $this->table_prefix.$str;
}
// -------------------------------------------------------------------------
// ! Abstract public functions to implement in child classes
// -------------------------------------------------------------------------
/**
* Empty the passed table
*
* @param string $table
* @return void
* @return \PDOStatement
*/
abstract public function truncate($table);
public function truncate($table)
{
$sql = ($this->has_truncate)
? 'TRUNCATE '
: 'DELETE FROM ';
$sql .= $this->quote_table($table);
$this->statement = $this->query($sql);
return $this->statement;
}
}
// End of db_pdo.php

View File

@ -65,6 +65,13 @@ class Firebird extends Abstract_Driver {
*/
protected $service = NULL;
/**
* Firebird doesn't have the truncate keyword
*
* @var bool
*/
protected $has_truncate = FALSE;
/**
* Open the link to the database
*
@ -118,21 +125,6 @@ class Firebird extends Abstract_Driver {
return $this->service;
}
// --------------------------------------------------------------------------
/**
* Empty a database table
*
* @param string $table
* @return \PDOStatement
*/
public function truncate($table)
{
// Firebird lacks a truncate command
$sql = 'DELETE FROM '.$this->quote_table($table);
$this->statement = $this->query($sql);
return $this->statement;
}
// --------------------------------------------------------------------------

View File

@ -52,18 +52,5 @@ class MySQL extends Abstract_Driver {
parent::__construct($dsn, $username, $password, $options);
}
// --------------------------------------------------------------------------
/**
* Empty a table
*
* @param string $table
*/
public function truncate($table)
{
$table = $this->prefix_table($table);
$this->query("TRUNCATE `{$table}`");
}
}
//End of mysql_driver.php

View File

@ -40,19 +40,6 @@ class PgSQL extends Abstract_Driver {
// --------------------------------------------------------------------------
/**
* Empty a table
*
* @param string $table
*/
public function truncate($table)
{
$sql = 'TRUNCATE "' . $table . '"';
$this->query($sql);
}
// --------------------------------------------------------------------------
/**
* Get a list of schemas for the current connection
*

View File

@ -30,6 +30,13 @@ class SQLite extends Abstract_Driver {
*/
protected $statement;
/**
* SQLite has a truncate optimization,
* but no support for the actual keyword
* @var bool
*/
protected $has_truncate = FALSE;
/**
* Open SQLite Database
*
@ -44,23 +51,6 @@ class SQLite extends Abstract_Driver {
parent::__construct("sqlite:{$dsn}", $user, $pass);
}
// --------------------------------------------------------------------------
/**
* Empty a table
*
* @param string $table
*/
public function truncate($table)
{
// SQLite has a TRUNCATE optimization,
// but no support for the actual command.
$sql = 'DELETE FROM "'.$table.'"';
$this->statement = $this->query($sql);
return $this->statement;
}
// --------------------------------------------------------------------------