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\Query; |
22
|
|
|
use Kristuff\Patabase\Query\InsertBase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Update |
26
|
|
|
* |
27
|
|
|
* Represents an [UPDATE] SQL query |
28
|
|
|
* |
29
|
|
|
* UPDATE [tablename] |
30
|
|
|
* SET [column/values] |
31
|
|
|
* WHERE [conditions] |
32
|
|
|
*/ |
33
|
|
|
class Update extends InsertBase |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Increment columns list |
37
|
|
|
* |
38
|
|
|
* @access private |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $incrementColumns = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Decrement columns list |
45
|
|
|
* |
46
|
|
|
* @access private |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $decrementColumns = array(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get a the SQL UPDATE columns statement |
53
|
|
|
* |
54
|
|
|
* @access private |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
private function sqlColumns(): string |
58
|
|
|
{ |
59
|
|
|
$columns = array(); |
60
|
|
|
foreach ($this->parameters as $key => $val) { |
61
|
|
|
$arg = ':_' . str_replace('.', '_', $key); |
62
|
|
|
$columns[] = $this->escape($key) . ' =' .$arg; |
63
|
|
|
$this->pdoParameters[$arg] = $val; |
64
|
|
|
} |
65
|
|
|
foreach ($this->incrementColumns as $key => $val) { |
66
|
|
|
$columns[] = $this->escape($key) . ' =' . $this->escape($key) . '+' .$val ; |
67
|
|
|
} |
68
|
|
|
foreach ($this->decrementColumns as $key => $val) { |
69
|
|
|
$columns[] = $this->escape($key) . ' =' . $this->escape($key) . '-' .$val ; |
70
|
|
|
} |
71
|
|
|
return implode(', ', $columns); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a WHERE statement object |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
* @return Where |
79
|
|
|
*/ |
80
|
|
|
public function where(): Where |
81
|
|
|
{ |
82
|
|
|
if (!isset($this->where)){ |
83
|
|
|
$this->where = new Query\Where($this, $this->driver); |
84
|
|
|
} |
85
|
|
|
return $this->where; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add a WHERE column = value condition |
90
|
|
|
* It's an alias for ->where()->equal($column, $value) |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
* @param string $column The column name |
94
|
|
|
* @param mixed $value The condition value |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function whereEqual(string $column, $value) |
99
|
|
|
{ |
100
|
|
|
$this->where()->equal($column, $value); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Increment a column this given value |
106
|
|
|
* |
107
|
|
|
* @access public |
108
|
|
|
* @param string $column The column name, could be Table.ColumnName format |
109
|
|
|
* @param int $value The increment value. Default is 1. |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function increment(string $column, int $value = 1) |
114
|
|
|
{ |
115
|
|
|
$this->incrementColumns[$column] = $value; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Decrement a column this given value |
121
|
|
|
* |
122
|
|
|
* @access public |
123
|
|
|
* @param string $column The column name, could be Table.ColumnName format |
124
|
|
|
* @param int $value The decrement value. Default is 1. |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function decrement(string $column, int $value = 1) |
129
|
|
|
{ |
130
|
|
|
$this->decrementColumns[$column] = $value; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Build the SQL UPDATE query |
136
|
|
|
* |
137
|
|
|
* @access public |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function sql(): string |
141
|
|
|
{ |
142
|
|
|
$sqlWhere = (isset($this->where)) ? $this->where->sql() : ''; |
143
|
|
|
$sqlTableName = $this->escape($this->tableName); |
144
|
|
|
return trim(sprintf('UPDATE %s SET %s %s', |
145
|
|
|
$sqlTableName, |
146
|
|
|
$this->sqlColumns(), |
147
|
|
|
$sqlWhere |
148
|
|
|
)); |
149
|
|
|
} |
150
|
|
|
} |