-
Notifications
You must be signed in to change notification settings - Fork 103
/
Delete.php
103 lines (85 loc) · 2.2 KB
/
Delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* @license MIT
* @license http://opensource.org/licenses/MIT
*/
namespace FaaPz\PDO\Statement;
use FaaPz\PDO\AdvancedStatement;
use FaaPz\PDO\Database;
class Delete extends AdvancedStatement implements DeleteInterface
{
/** @var ?string|?array<string, string> $table */
protected $table = null;
/**
* @param Database $dbh
* @param ?string|?array<string, string> $table
*/
public function __construct(Database $dbh, $table = null)
{
parent::__construct($dbh);
if ($table != null) {
$this->from($table);
}
}
/**
* @param string|array<string, string> $table
*
* @return self
*/
public function from($table): self
{
$this->table = $table;
return $this;
}
/**
* @return string
*/
protected function renderFrom(): string
{
if (empty($this->table)) {
trigger_error('No table set for delete statement', E_USER_ERROR);
}
if (is_array($this->table)) {
$table = reset($this->table);
$alias = key($this->table);
if (is_string($alias)) {
$table .= " AS {$alias}";
}
} else {
$table = $this->table;
}
return " FROM {$table}";
}
/**
* @return array<int, mixed>
*/
public function getValues(): array
{
$values = [];
foreach ($this->join as $join) {
$values = array_merge($values, $join->getValues());
}
if ($this->where != null) {
$values = array_merge($values, $this->where->getValues());
}
if (!empty($this->orderBy)) {
$values = array_merge($values, $this->orderBy);
}
if ($this->limit != null) {
$values = array_merge($values, $this->limit->getValues());
}
return $values;
}
/**
* @return string
*/
public function __toString(): string
{
return 'DELETE'
. $this->renderFrom()
. $this->renderJoin()
. $this->renderWhere()
. $this->renderOrderBy()
. $this->renderLimit();
}
}