1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @see http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace JAQB\Query; |
13
|
|
|
|
14
|
|
|
use JAQB\Operations\Executable; |
15
|
|
|
use JAQB\Query\Traits\Limit; |
16
|
|
|
use JAQB\Query\Traits\OrderBy; |
17
|
|
|
use JAQB\Query\Traits\Where; |
18
|
|
|
use JAQB\Statement\FromStatement; |
19
|
|
|
use JAQB\Statement\LimitStatement; |
20
|
|
|
use JAQB\Statement\OrderStatement; |
21
|
|
|
use JAQB\Statement\SetStatement; |
22
|
|
|
use JAQB\Statement\WhereStatement; |
23
|
|
|
|
24
|
|
|
class UpdateQuery extends AbstractQuery |
25
|
|
|
{ |
26
|
|
|
use Executable; |
27
|
|
|
use Limit; |
28
|
|
|
use OrderBy; |
29
|
|
|
use Where; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FromStatement |
33
|
|
|
*/ |
34
|
|
|
protected $table; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SetStatement |
38
|
|
|
*/ |
39
|
|
|
protected $set; |
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
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function values(array $values) |
70
|
|
|
{ |
71
|
|
|
$this->set->addValues($values); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the table name for the query. |
78
|
|
|
* |
79
|
|
|
* @return FromStatement |
80
|
|
|
*/ |
81
|
|
|
public function getTable() |
82
|
|
|
{ |
83
|
|
|
return $this->table; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the values for the query. |
88
|
|
|
* |
89
|
|
|
* @return SetStatement |
90
|
|
|
*/ |
91
|
|
|
public function getSet() |
92
|
|
|
{ |
93
|
|
|
return $this->set; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Generates the raw SQL string for the query. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function build() |
102
|
|
|
{ |
103
|
|
|
$sql = [ |
104
|
|
|
$this->table->build(), |
105
|
|
|
$this->set->build(), |
106
|
|
|
$this->where->build(), |
107
|
|
|
$this->orderBy->build(), |
108
|
|
|
$this->limit->build(), |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$this->values = array_merge( |
112
|
|
|
array_values($this->set->getValues()), |
113
|
|
|
$this->where->getValues() |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return implode(' ', array_filter($sql)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function __clone() |
120
|
|
|
{ |
121
|
|
|
$this->table = clone $this->table; |
122
|
|
|
$this->set = clone $this->set; |
123
|
|
|
$this->where = clone $this->where; |
124
|
|
|
$this->orderBy = clone $this->orderBy; |
125
|
|
|
$this->limit = clone $this->limit; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|