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\SetStatement; |
18
|
|
|
use JAQB\Statement\WhereStatement; |
19
|
|
|
use JAQB\Query\Traits\OrderBy; |
20
|
|
|
use JAQB\Query\Traits\WhereConditions; |
21
|
|
|
|
22
|
|
|
class UpdateQuery extends AbstractQuery |
23
|
|
|
{ |
24
|
|
|
use Executable, OrderBy, WhereConditions; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FromStatement |
28
|
|
|
*/ |
29
|
|
|
protected $table; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var SetStatement |
33
|
|
|
*/ |
34
|
|
|
protected $set; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LimitStatement |
38
|
|
|
*/ |
39
|
|
|
protected $limit; |
40
|
|
|
|
41
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->table = new FromStatement(FromStatement::UPDATE); |
44
|
|
|
$this->set = new SetStatement(); |
45
|
|
|
$this->where = new WhereStatement(); |
|
|
|
|
46
|
|
|
$this->orderBy = new OrderStatement(); |
|
|
|
|
47
|
|
|
$this->limit = new LimitStatement(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the table for the query. |
52
|
|
|
* |
53
|
|
|
* @param string $table table name |
54
|
|
|
* |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
|
public function table($table) |
58
|
|
|
{ |
59
|
|
|
$this->table->addTable($table); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets the values for the query. |
66
|
|
|
* |
67
|
|
|
* @param array $values |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function values(array $values) |
72
|
|
|
{ |
73
|
|
|
$this->set->addValues($values); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the limit for the query. |
80
|
|
|
* |
81
|
|
|
* @param int $limit |
82
|
|
|
* @param int $offset |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
|
|
public function limit($limit, $offset = 0) |
87
|
|
|
{ |
88
|
|
|
$this->limit->setLimit($limit, $offset); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the table name for the query. |
95
|
|
|
* |
96
|
|
|
* @return FromStatement |
97
|
|
|
*/ |
98
|
|
|
public function getTable() |
99
|
|
|
{ |
100
|
|
|
return $this->table; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets the values for the query. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getSet() |
109
|
|
|
{ |
110
|
|
|
return $this->set; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets the limit statement for the query. |
115
|
|
|
* |
116
|
|
|
* @return LimitStatement |
117
|
|
|
*/ |
118
|
|
|
public function getLimit() |
119
|
|
|
{ |
120
|
|
|
return $this->limit; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Generates the raw SQL string for the query. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function build() |
129
|
|
|
{ |
130
|
|
|
$sql = [ |
131
|
|
|
$this->table->build(), |
132
|
|
|
$this->set->build(), |
133
|
|
|
$this->where->build(), |
134
|
|
|
$this->orderBy->build(), |
135
|
|
|
$this->limit->build(), |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$this->values = array_merge( |
139
|
|
|
array_values($this->set->getValues()), |
140
|
|
|
$this->where->getValues()); |
141
|
|
|
|
142
|
|
|
return implode(' ', array_filter($sql)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function __clone() |
146
|
|
|
{ |
147
|
|
|
$this->table = clone $this->table; |
148
|
|
|
$this->set = clone $this->set; |
149
|
|
|
$this->where = clone $this->where; |
150
|
|
|
$this->orderBy = clone $this->orderBy; |
151
|
|
|
$this->limit = clone $this->limit; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.