1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace JAQB\Query; |
12
|
|
|
|
13
|
|
|
use JAQB\Operations\Executable; |
14
|
|
|
use JAQB\Statement\FromStatement; |
15
|
|
|
use JAQB\Statement\LimitStatement; |
16
|
|
|
use JAQB\Statement\OrderStatement; |
17
|
|
|
use JAQB\Statement\WhereStatement; |
18
|
|
|
use JAQB\Query\Traits\Limit; |
19
|
|
|
use JAQB\Query\Traits\OrderBy; |
20
|
|
|
use JAQB\Query\Traits\Where; |
21
|
|
|
|
22
|
|
|
class DeleteQuery extends AbstractQuery |
23
|
|
|
{ |
24
|
|
|
use Executable, Limit, OrderBy, Where; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FromStatement |
28
|
|
|
*/ |
29
|
|
|
protected $from; |
30
|
|
|
|
31
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->from = new FromStatement(FromStatement::DELETE); |
34
|
|
|
$this->where = new WhereStatement(); |
|
|
|
|
35
|
|
|
$this->orderBy = new OrderStatement(); |
|
|
|
|
36
|
|
|
$this->limit = new LimitStatement(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets the table for the query. |
41
|
|
|
* |
42
|
|
|
* @param string $table table name |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
|
|
public function from($table) |
47
|
|
|
{ |
48
|
|
|
$this->from->addTable($table); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the from statement for the query. |
55
|
|
|
* |
56
|
|
|
* @return FromStatement |
57
|
|
|
*/ |
58
|
|
|
public function getFrom() |
59
|
|
|
{ |
60
|
|
|
return $this->from; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Generates the raw SQL string for the query. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function build() |
69
|
|
|
{ |
70
|
|
|
$sql = [ |
71
|
|
|
$this->from->build(), |
72
|
|
|
$this->where->build(), |
73
|
|
|
$this->orderBy->build(), |
74
|
|
|
$this->limit->build(), |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$this->values = $this->where->getValues(); |
78
|
|
|
|
79
|
|
|
return implode(' ', array_filter($sql)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function __clone() |
83
|
|
|
{ |
84
|
|
|
$this->from = clone $this->from; |
85
|
|
|
$this->where = clone $this->where; |
86
|
|
|
$this->orderBy = clone $this->orderBy; |
87
|
|
|
$this->limit = clone $this->limit; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.