-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDelete.php
85 lines (67 loc) · 2.02 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
<?php
declare(strict_types=1);
namespace System\Database\MyQuery;
use System\Database\MyPDO;
use System\Database\MyQuery\Join\AbstractJoin;
use System\Database\MyQuery\Traits\ConditionTrait;
use System\Database\MyQuery\Traits\SubQueryTrait;
class Delete extends Execute
{
use ConditionTrait;
use SubQueryTrait;
protected ?string $alias = null;
public function __construct(string $table_name, MyPDO $PDO)
{
$this->_table = $table_name;
$this->PDO = $PDO;
}
public function __toString()
{
return $this->builder();
}
/**
* Set alias for the table.
* If using an alias, conditions with binding values will be ignored,
* except when using subqueries, clause in join also will be generate as alias.
*/
public function alias(string $alias): self
{
$this->alias = $alias;
return $this;
}
/**
* Join statment:
* - inner join
* - left join
* - right join
* - full join.
*/
public function join(AbstractJoin $ref_table): self
{
$table = $this->alias ?? $this->_table;
$ref_table->table($table);
$this->_join[] = $ref_table->stringJoin();
$binds = (fn () => $this->{'sub_query'})->call($ref_table);
if (null !== $binds) {
$this->_binds = array_merge($this->_binds, $binds->getBind());
}
return $this;
}
private function getJoin(): string
{
return 0 === count($this->_join)
? ''
: implode(' ', $this->_join)
;
}
protected function builder(): string
{
$build = [];
$build['join'] = $this->getJoin();
$build['where'] = $this->getWhere();
$query_parts = implode(' ', array_filter($build, fn ($item) => $item !== ''));
return $this->_query = null === $this->alias
? "DELETE FROM {$this->_table} {$query_parts}"
: "DELETE {$this->alias} FROM {$this->_table} AS {$this->alias} {$query_parts}";
}
}