Skip to content

Commit

Permalink
Adding TRUNCATE() and DROP() (#117)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kwhat authored Sep 20, 2019
1 parent a84ac97 commit d5382af
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 28 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Clause/ClauseContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ abstract class ClauseContainer
/**
* @var array
*/
protected $container = array();
protected $container = [];
}
2 changes: 1 addition & 1 deletion src/Clause/HavingClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __toString()
return '';
}

$args = array();
$args = [];

foreach ($this->container as $having) {
$args[] = $having;
Expand Down
2 changes: 1 addition & 1 deletion src/Clause/JoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __toString()
return '';
}

$args = array();
$args = [];

foreach ($this->container as $join) {
$args[] = $join;
Expand Down
2 changes: 1 addition & 1 deletion src/Clause/WhereClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function __toString()
return '';
}

$args = array();
$args = [];

foreach ($this->container as $where) {
$args[] = $where;
Expand Down
46 changes: 34 additions & 12 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

Expand All @@ -39,20 +41,20 @@ 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]],
];
}

/**
* @param array $columns
*
* @return SelectStatement
*/
public function select(array $columns = array('*'))
public function select(array $columns = ['*'])
{
return new SelectStatement($this, $columns);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
48 changes: 48 additions & 0 deletions src/Statement/DropStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\Database;

class DropStatement extends StatementContainer
{
/**
* Constructor.
*
* @param Database $dbh
* @param $table
*/
public function __construct(Database $dbh, $table)
{
parent::__construct($dbh);

$this->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();
}
}
2 changes: 1 addition & 1 deletion src/Statement/SelectStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(Database $dbh, array $columns)
parent::__construct($dbh);

if (empty($columns)) {
$columns = array('*');
$columns = ['*'];
}

$this->setColumns($columns);
Expand Down
21 changes: 11 additions & 10 deletions src/Statement/StatementContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -500,7 +501,7 @@ protected function getPlaceholders()
{
$placeholders = $this->placeholders;

$this->placeholders = array();
$this->placeholders = [];

return '( '.implode(' , ', $placeholders).' )';
}
Expand All @@ -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));
}
}

Expand All @@ -536,18 +537,18 @@ private function getStatement()
}

/**
* @param $text
* @param string $text
* @param int $count
* @param string $separator
*
* @return string
*/
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;
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/Statement/TruncateStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\Database;

class TruncateStatement extends StatementContainer
{
/**
* Constructor.
*
* @param Database $dbh
* @param $table
*/
public function __construct(Database $dbh, $table)
{
parent::__construct($dbh);

$this->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();
}
}

0 comments on commit d5382af

Please sign in to comment.