From d5382afa069f21e0160216d7c52da67f0289b99e Mon Sep 17 00:00:00 2001 From: Alex Barker Date: Fri, 20 Sep 2019 16:25:53 -0700 Subject: [PATCH] Adding TRUNCATE() and DROP() (#117) * Adding truncate() * Adding TruncateStatement for truncate() * Adding example of truncate() * Added drop() method * A bit to fast on the commit. Forgot TABLE IF EXISTS in the getDrop() * Update DropStatement.php * Update TruncateStatement.php * Update Database.php * Fixing CI issues --- README.md | 5 +++ composer.json | 2 +- src/Clause/ClauseContainer.php | 2 +- src/Clause/HavingClause.php | 2 +- src/Clause/JoinClause.php | 2 +- src/Clause/WhereClause.php | 2 +- src/Database.php | 46 +++++++++++++++++++------- src/Statement/DropStatement.php | 48 ++++++++++++++++++++++++++++ src/Statement/SelectStatement.php | 2 +- src/Statement/StatementContainer.php | 21 ++++++------ src/Statement/TruncateStatement.php | 48 ++++++++++++++++++++++++++++ 11 files changed, 152 insertions(+), 28 deletions(-) create mode 100644 src/Statement/DropStatement.php create mode 100644 src/Statement/TruncateStatement.php diff --git a/README.md b/README.md index 204815b..6267483 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,11 @@ $deleteStatement = $pdo->delete() ->where('id', '=', 1234); $affectedRows = $deleteStatement->execute(); + +// TRUNCATE user +$truncateStatement = $pdo->truncate('users'); + +$truncateStatement->execute(); ``` > The `sqlsrv` extension will fail to connect when using error mode `PDO::ERRMODE_EXCEPTION` (default). To connect, you will need to explicitly pass `array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING)` (or `PDO::ERRMODE_SILENT`) into the constructor, or override the `getDefaultOptions()` method when using `sqlsrv`. diff --git a/composer.json b/composer.json index 1e3e041..188472b 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "docs": "https://github.com/FaaPz/PDO/blob/master/docs/README.md" }, "require": { - "php": ">=5.3.0", + "php": ">=5.6.0", "ext-pdo": "*" }, "autoload": { diff --git a/src/Clause/ClauseContainer.php b/src/Clause/ClauseContainer.php index fe91519..1289b85 100644 --- a/src/Clause/ClauseContainer.php +++ b/src/Clause/ClauseContainer.php @@ -17,5 +17,5 @@ abstract class ClauseContainer /** * @var array */ - protected $container = array(); + protected $container = []; } diff --git a/src/Clause/HavingClause.php b/src/Clause/HavingClause.php index a90f4e7..adad98a 100644 --- a/src/Clause/HavingClause.php +++ b/src/Clause/HavingClause.php @@ -97,7 +97,7 @@ public function __toString() return ''; } - $args = array(); + $args = []; foreach ($this->container as $having) { $args[] = $having; diff --git a/src/Clause/JoinClause.php b/src/Clause/JoinClause.php index 643e845..5517d20 100644 --- a/src/Clause/JoinClause.php +++ b/src/Clause/JoinClause.php @@ -68,7 +68,7 @@ public function __toString() return ''; } - $args = array(); + $args = []; foreach ($this->container as $join) { $args[] = $join; diff --git a/src/Clause/WhereClause.php b/src/Clause/WhereClause.php index 0ff295e..da1e8a4 100644 --- a/src/Clause/WhereClause.php +++ b/src/Clause/WhereClause.php @@ -222,7 +222,7 @@ public function __toString() return ''; } - $args = array(); + $args = []; foreach ($this->container as $where) { $args[] = $where; diff --git a/src/Database.php b/src/Database.php index b6a9a0c..aa4e40a 100644 --- a/src/Database.php +++ b/src/Database.php @@ -7,10 +7,12 @@ namespace FaaPz\PDO; -use FaaPz\PDO\Statement\SelectStatement; +use FaaPz\PDO\Statement\DeleteStatement; +use FaaPz\PDO\Statement\DropStatement; use FaaPz\PDO\Statement\InsertStatement; +use FaaPz\PDO\Statement\SelectStatement; +use FaaPz\PDO\Statement\TruncateStatement; use FaaPz\PDO\Statement\UpdateStatement; -use FaaPz\PDO\Statement\DeleteStatement; /** * Class Database. @@ -27,7 +29,7 @@ class Database extends \PDO * @param null $pwd * @param array $options */ - public function __construct($dsn, $usr = null, $pwd = null, array $options = array()) + public function __construct($dsn, $usr = null, $pwd = null, array $options = []) { $options = $options + $this->getDefaultOptions(); @@ -39,12 +41,12 @@ public function __construct($dsn, $usr = null, $pwd = null, array $options = arr */ protected function getDefaultOptions() { - return array( - \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, - \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, - \PDO::ATTR_EMULATE_PREPARES => false, - \PDO::ATTR_STATEMENT_CLASS => array('FaaPz\\PDO\\Statement', array($this)), - ); + return [ + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_EMULATE_PREPARES => false, + \PDO::ATTR_STATEMENT_CLASS => [Statement::class, [$this]], + ]; } /** @@ -52,7 +54,7 @@ protected function getDefaultOptions() * * @return SelectStatement */ - public function select(array $columns = array('*')) + public function select(array $columns = ['*']) { return new SelectStatement($this, $columns); } @@ -62,7 +64,7 @@ public function select(array $columns = array('*')) * * @return InsertStatement */ - public function insert(array $columnsOrPairs = array()) + public function insert(array $columnsOrPairs = []) { return new InsertStatement($this, $columnsOrPairs); } @@ -72,7 +74,7 @@ public function insert(array $columnsOrPairs = array()) * * @return UpdateStatement */ - public function update(array $pairs = array()) + public function update(array $pairs = []) { return new UpdateStatement($this, $pairs); } @@ -86,4 +88,24 @@ public function delete($table = null) { return new DeleteStatement($this, $table); } + + /** + * @param null $table + * + * @return TruncateStatement + */ + public function truncate($table = null) + { + return new TruncateStatement($this, $table); + } + + /** + * @param null $table + * + * @return DropStatement + */ + public function drop($table = null) + { + return new DropStatement($this, $table); + } } diff --git a/src/Statement/DropStatement.php b/src/Statement/DropStatement.php new file mode 100644 index 0000000..e8fff0f --- /dev/null +++ b/src/Statement/DropStatement.php @@ -0,0 +1,48 @@ +setTable($table); + } + + /** + * @return string + */ + public function __toString() + { + if (empty($this->table)) { + trigger_error('No table is set for selection', E_USER_ERROR); + } + + $sql = $this->getDrop().' '.$this->table; + + return $sql; + } + + protected function getDrop() + { + return 'DROP TABLE IF EXISTS'; + } + + /** + * @return \PDOStatement + */ + public function execute() + { + return parent::execute(); + } +} diff --git a/src/Statement/SelectStatement.php b/src/Statement/SelectStatement.php index 6dea98c..47eee4c 100644 --- a/src/Statement/SelectStatement.php +++ b/src/Statement/SelectStatement.php @@ -61,7 +61,7 @@ public function __construct(Database $dbh, array $columns) parent::__construct($dbh); if (empty($columns)) { - $columns = array('*'); + $columns = ['*']; } $this->setColumns($columns); diff --git a/src/Statement/StatementContainer.php b/src/Statement/StatementContainer.php index 613e641..c19b587 100644 --- a/src/Statement/StatementContainer.php +++ b/src/Statement/StatementContainer.php @@ -27,17 +27,17 @@ abstract class StatementContainer /** * @var array */ - protected $columns = array(); + protected $columns = []; /** * @var array */ - protected $values = array(); + protected $values = []; /** * @var array */ - protected $placeholders = array(); + protected $placeholders = []; /** * @var @@ -435,8 +435,9 @@ public function execute() /** * Bind values to their parameters in the given statement. * - * @param \PDOStatement $statement - * @param array $bindings + * @param \PDOStatement $statement + * @param array $bindings + * * @return void */ protected function bindValues($statement, $bindings) @@ -500,7 +501,7 @@ protected function getPlaceholders() { $placeholders = $this->placeholders; - $this->placeholders = array(); + $this->placeholders = []; return '( '.implode(' , ', $placeholders).' )'; } @@ -511,7 +512,7 @@ protected function getPlaceholders() protected function setPlaceholders(array $values) { foreach ($values as $value) { - $this->placeholders[] = $this->setPlaceholder('?', is_null($value) ? 1 : (is_array($value) ? sizeof($value) : 1)); + $this->placeholders[] = $this->setPlaceholder('?', is_null($value) ? 1 : (is_array($value) ? count($value) : 1)); } } @@ -536,7 +537,7 @@ private function getStatement() } /** - * @param $text + * @param string $text * @param int $count * @param string $separator * @@ -544,10 +545,10 @@ private function getStatement() */ private function setPlaceholder($text, $count = 0, $separator = ' , ') { - $result = array(); + $result = []; if ($count > 0) { - for ($x = 0; $x < $count; ++$x) { + for ($x = 0; $x < $count; $x++) { $result[] = $text; } } diff --git a/src/Statement/TruncateStatement.php b/src/Statement/TruncateStatement.php new file mode 100644 index 0000000..0b3674c --- /dev/null +++ b/src/Statement/TruncateStatement.php @@ -0,0 +1,48 @@ +setTable($table); + } + + /** + * @return string + */ + public function __toString() + { + if (empty($this->table)) { + trigger_error('No table is set for selection', E_USER_ERROR); + } + + $sql = $this->getTruncate().' '.$this->table; + + return $sql; + } + + protected function getTruncate() + { + return 'TRUNCATE'; + } + + /** + * @return \PDOStatement + */ + public function execute() + { + return parent::execute(); + } +}