1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class UpdateAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource UpdateAbstract.php |
6
|
|
|
* @created 03.06.2017 |
7
|
|
|
* @package chillerlan\Database\Query\Dialects |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Query\Traits\WhereTrait; |
16
|
|
|
|
17
|
|
|
abstract class UpdateAbstract extends StatementAbstract implements UpdateInterface{ |
18
|
|
|
use WhereTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $table; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $set = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
* @throws \chillerlan\Database\Query\QueryException |
33
|
|
|
*/ |
34
|
|
|
public function sql():string{ |
35
|
|
|
|
36
|
|
|
if(empty($this->table)){ |
37
|
|
|
throw new QueryException('no table specified'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if(empty($this->set)){ |
41
|
|
|
throw new QueryException('no fields to update specified'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$sql = 'UPDATE '; |
45
|
|
|
$sql .= $this->quote($this->table); |
46
|
|
|
$sql .= ' SET '; |
47
|
|
|
$sql .= implode(', ', $this->set); |
48
|
|
|
$sql .= $this->_getWhere(); |
49
|
|
|
|
50
|
|
|
return $sql; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|null $tablename |
55
|
|
|
* |
56
|
|
|
* @return \chillerlan\Database\Query\UpdateInterface |
57
|
|
|
*/ |
58
|
|
|
public function table(string $tablename):UpdateInterface{ |
59
|
|
|
$this->table = $tablename; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $set |
66
|
|
|
* @param bool $bind |
67
|
|
|
* |
68
|
|
|
* @return \chillerlan\Database\Query\UpdateInterface |
69
|
|
|
*/ |
70
|
|
|
public function set(array $set, bool $bind = true):UpdateInterface{ |
71
|
|
|
|
72
|
|
|
foreach($set as $k => $v){ |
73
|
|
|
|
74
|
|
|
if($v instanceof StatementInterface){ |
75
|
|
|
$this->set[] = $this->quote($k).' = ('.$v->sql().')'; |
76
|
|
|
$this->bindValues = array_merge($this->bindValues, $v->bindValues()); |
77
|
|
|
} |
78
|
|
|
elseif(is_array($v)){ |
|
|
|
|
79
|
|
|
// [expr, bindval1, bindval2, ...] |
|
|
|
|
80
|
|
|
} |
81
|
|
|
else{ |
82
|
|
|
if($bind){ |
83
|
|
|
$this->set[] = $this->quote($k).' = ?'; |
84
|
|
|
$this->bindValues[] = $v; |
85
|
|
|
} |
86
|
|
|
else{ |
87
|
|
|
$this->set[] = $this->quote($k).' = '.$v; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $val1 |
97
|
|
|
* @param $val2 |
98
|
|
|
* @param string $operator |
99
|
|
|
* @param bool $bind |
100
|
|
|
* @param string $join |
101
|
|
|
* |
102
|
|
|
* @return \chillerlan\Database\Query\UpdateInterface |
103
|
|
|
*/ |
104
|
|
|
public function where($val1, $val2, $operator = '=', $bind = true, $join = 'AND'):UpdateInterface{ |
105
|
|
|
return $this->_addWhere($val1, $val2, $operator, $bind, $join); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param null $join |
110
|
|
|
* |
111
|
|
|
* @return \chillerlan\Database\Query\UpdateInterface |
112
|
|
|
*/ |
113
|
|
|
public function openBracket($join = null):UpdateInterface{ |
114
|
|
|
return $this->_openBracket($join); // @codeCoverageIgnore |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \chillerlan\Database\Query\UpdateInterface |
119
|
|
|
*/ |
120
|
|
|
public function closeBracket():UpdateInterface{ |
121
|
|
|
return $this->_closeBracket(); // @codeCoverageIgnore |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
This check looks for the bodies of
elseif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elseif
bodies can be removed. If you have an empty elseif but statements in theelse
branch, consider inverting the condition.