1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ___ _ _ |
5
|
|
|
* | _ \__ _| |_ __ _| |__ __ _ ___ ___ |
6
|
|
|
* | _/ _` | _/ _` | '_ \/ _` (_-</ -_) |
7
|
|
|
* |_| \__,_|\__\__,_|_.__/\__,_/__/\___| |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\Patabase. |
10
|
|
|
* (c) Kristuff <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* @version 1.0.1 |
16
|
|
|
* @copyright 2017-2022 Christophe Buliard |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Kristuff\Patabase\Query; |
20
|
|
|
|
21
|
|
|
use Kristuff\Patabase\Driver\DatabaseDriver; |
22
|
|
|
use Kristuff\Patabase\Query; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Delete |
26
|
|
|
* |
27
|
|
|
* Represents a [DELETE FROM] SQL query |
28
|
|
|
* |
29
|
|
|
* DELETE FROM [tablename] |
30
|
|
|
* WHERE [conditions] |
31
|
|
|
*/ |
32
|
|
|
class Delete extends Query\QueryBuilder |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Table name (DELETE FROM [?]) |
37
|
|
|
* |
38
|
|
|
* @access private |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $tableName = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @param DatabaseDriver $driver The driver instance |
48
|
|
|
* @param string $tableName The table name |
49
|
|
|
*/ |
50
|
|
|
public function __construct(DatabaseDriver $driver, string $tableName) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($driver); |
53
|
|
|
$this->tableName = $tableName; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a WHERE stamement object |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* @return Where |
61
|
|
|
*/ |
62
|
|
|
public function where(): Where |
63
|
|
|
{ |
64
|
|
|
if (!isset($this->where)){ |
65
|
|
|
$this->where = new Query\Where($this, $this->driver); |
66
|
|
|
} |
67
|
|
|
return $this->where; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add a WHERE column = value condition |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @param string $column The columnn name |
75
|
|
|
* @param mixed $value |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function whereEqual(string $column, $value) |
80
|
|
|
{ |
81
|
|
|
$this->where()->equal($column, $value); |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build the SQL DELETE query |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function sql(): string |
92
|
|
|
{ |
93
|
|
|
// Build sql where and escape table name |
94
|
|
|
$sqlWhere = (isset($this->where)) ? $this->where->sql() : ''; |
95
|
|
|
$sqlTableName = $this->escape($this->tableName); |
96
|
|
|
|
97
|
|
|
// DELETE query |
98
|
|
|
return trim(sprintf('DELETE FROM %s %s', |
99
|
|
|
$sqlTableName, |
100
|
|
|
$sqlWhere)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|