Skip to content

Commit d5382af

Browse files
authored
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
1 parent a84ac97 commit d5382af

File tree

11 files changed

+152
-28
lines changed

11 files changed

+152
-28
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ $deleteStatement = $pdo->delete()
5656
->where('id', '=', 1234);
5757

5858
$affectedRows = $deleteStatement->execute();
59+
60+
// TRUNCATE user
61+
$truncateStatement = $pdo->truncate('users');
62+
63+
$truncateStatement->execute();
5964
```
6065

6166
> 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`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"docs": "https://github.com/FaaPz/PDO/blob/master/docs/README.md"
1919
},
2020
"require": {
21-
"php": ">=5.3.0",
21+
"php": ">=5.6.0",
2222
"ext-pdo": "*"
2323
},
2424
"autoload": {

src/Clause/ClauseContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ abstract class ClauseContainer
1717
/**
1818
* @var array
1919
*/
20-
protected $container = array();
20+
protected $container = [];
2121
}

src/Clause/HavingClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function __toString()
9797
return '';
9898
}
9999

100-
$args = array();
100+
$args = [];
101101

102102
foreach ($this->container as $having) {
103103
$args[] = $having;

src/Clause/JoinClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __toString()
6868
return '';
6969
}
7070

71-
$args = array();
71+
$args = [];
7272

7373
foreach ($this->container as $join) {
7474
$args[] = $join;

src/Clause/WhereClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function __toString()
222222
return '';
223223
}
224224

225-
$args = array();
225+
$args = [];
226226

227227
foreach ($this->container as $where) {
228228
$args[] = $where;

src/Database.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
namespace FaaPz\PDO;
99

10-
use FaaPz\PDO\Statement\SelectStatement;
10+
use FaaPz\PDO\Statement\DeleteStatement;
11+
use FaaPz\PDO\Statement\DropStatement;
1112
use FaaPz\PDO\Statement\InsertStatement;
13+
use FaaPz\PDO\Statement\SelectStatement;
14+
use FaaPz\PDO\Statement\TruncateStatement;
1215
use FaaPz\PDO\Statement\UpdateStatement;
13-
use FaaPz\PDO\Statement\DeleteStatement;
1416

1517
/**
1618
* Class Database.
@@ -27,7 +29,7 @@ class Database extends \PDO
2729
* @param null $pwd
2830
* @param array $options
2931
*/
30-
public function __construct($dsn, $usr = null, $pwd = null, array $options = array())
32+
public function __construct($dsn, $usr = null, $pwd = null, array $options = [])
3133
{
3234
$options = $options + $this->getDefaultOptions();
3335

@@ -39,20 +41,20 @@ public function __construct($dsn, $usr = null, $pwd = null, array $options = arr
3941
*/
4042
protected function getDefaultOptions()
4143
{
42-
return array(
43-
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
44-
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
45-
\PDO::ATTR_EMULATE_PREPARES => false,
46-
\PDO::ATTR_STATEMENT_CLASS => array('FaaPz\\PDO\\Statement', array($this)),
47-
);
44+
return [
45+
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
46+
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
47+
\PDO::ATTR_EMULATE_PREPARES => false,
48+
\PDO::ATTR_STATEMENT_CLASS => [Statement::class, [$this]],
49+
];
4850
}
4951

5052
/**
5153
* @param array $columns
5254
*
5355
* @return SelectStatement
5456
*/
55-
public function select(array $columns = array('*'))
57+
public function select(array $columns = ['*'])
5658
{
5759
return new SelectStatement($this, $columns);
5860
}
@@ -62,7 +64,7 @@ public function select(array $columns = array('*'))
6264
*
6365
* @return InsertStatement
6466
*/
65-
public function insert(array $columnsOrPairs = array())
67+
public function insert(array $columnsOrPairs = [])
6668
{
6769
return new InsertStatement($this, $columnsOrPairs);
6870
}
@@ -72,7 +74,7 @@ public function insert(array $columnsOrPairs = array())
7274
*
7375
* @return UpdateStatement
7476
*/
75-
public function update(array $pairs = array())
77+
public function update(array $pairs = [])
7678
{
7779
return new UpdateStatement($this, $pairs);
7880
}
@@ -86,4 +88,24 @@ public function delete($table = null)
8688
{
8789
return new DeleteStatement($this, $table);
8890
}
91+
92+
/**
93+
* @param null $table
94+
*
95+
* @return TruncateStatement
96+
*/
97+
public function truncate($table = null)
98+
{
99+
return new TruncateStatement($this, $table);
100+
}
101+
102+
/**
103+
* @param null $table
104+
*
105+
* @return DropStatement
106+
*/
107+
public function drop($table = null)
108+
{
109+
return new DropStatement($this, $table);
110+
}
89111
}

src/Statement/DropStatement.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Statement;
4+
5+
use FaaPz\PDO\Database;
6+
7+
class DropStatement extends StatementContainer
8+
{
9+
/**
10+
* Constructor.
11+
*
12+
* @param Database $dbh
13+
* @param $table
14+
*/
15+
public function __construct(Database $dbh, $table)
16+
{
17+
parent::__construct($dbh);
18+
19+
$this->setTable($table);
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
public function __toString()
26+
{
27+
if (empty($this->table)) {
28+
trigger_error('No table is set for selection', E_USER_ERROR);
29+
}
30+
31+
$sql = $this->getDrop().' '.$this->table;
32+
33+
return $sql;
34+
}
35+
36+
protected function getDrop()
37+
{
38+
return 'DROP TABLE IF EXISTS';
39+
}
40+
41+
/**
42+
* @return \PDOStatement
43+
*/
44+
public function execute()
45+
{
46+
return parent::execute();
47+
}
48+
}

src/Statement/SelectStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(Database $dbh, array $columns)
6161
parent::__construct($dbh);
6262

6363
if (empty($columns)) {
64-
$columns = array('*');
64+
$columns = ['*'];
6565
}
6666

6767
$this->setColumns($columns);

src/Statement/StatementContainer.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ abstract class StatementContainer
2727
/**
2828
* @var array
2929
*/
30-
protected $columns = array();
30+
protected $columns = [];
3131

3232
/**
3333
* @var array
3434
*/
35-
protected $values = array();
35+
protected $values = [];
3636

3737
/**
3838
* @var array
3939
*/
40-
protected $placeholders = array();
40+
protected $placeholders = [];
4141

4242
/**
4343
* @var
@@ -435,8 +435,9 @@ public function execute()
435435
/**
436436
* Bind values to their parameters in the given statement.
437437
*
438-
* @param \PDOStatement $statement
439-
* @param array $bindings
438+
* @param \PDOStatement $statement
439+
* @param array $bindings
440+
*
440441
* @return void
441442
*/
442443
protected function bindValues($statement, $bindings)
@@ -500,7 +501,7 @@ protected function getPlaceholders()
500501
{
501502
$placeholders = $this->placeholders;
502503

503-
$this->placeholders = array();
504+
$this->placeholders = [];
504505

505506
return '( '.implode(' , ', $placeholders).' )';
506507
}
@@ -511,7 +512,7 @@ protected function getPlaceholders()
511512
protected function setPlaceholders(array $values)
512513
{
513514
foreach ($values as $value) {
514-
$this->placeholders[] = $this->setPlaceholder('?', is_null($value) ? 1 : (is_array($value) ? sizeof($value) : 1));
515+
$this->placeholders[] = $this->setPlaceholder('?', is_null($value) ? 1 : (is_array($value) ? count($value) : 1));
515516
}
516517
}
517518

@@ -536,18 +537,18 @@ private function getStatement()
536537
}
537538

538539
/**
539-
* @param $text
540+
* @param string $text
540541
* @param int $count
541542
* @param string $separator
542543
*
543544
* @return string
544545
*/
545546
private function setPlaceholder($text, $count = 0, $separator = ' , ')
546547
{
547-
$result = array();
548+
$result = [];
548549

549550
if ($count > 0) {
550-
for ($x = 0; $x < $count; ++$x) {
551+
for ($x = 0; $x < $count; $x++) {
551552
$result[] = $text;
552553
}
553554
}

0 commit comments

Comments
 (0)