1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\SqlQuery; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* Abstract query object for data manipulation (Insert, Update, and Delete). |
14
|
|
|
* |
15
|
|
|
* @package Aura.SqlQuery |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractDmlQuery extends AbstractQuery |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* Column values for INSERT or UPDATE queries; the key is the column name and the |
23
|
|
|
* value is the column value. |
24
|
|
|
* |
25
|
|
|
* @param array |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
protected $col_values; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* The columns to be returned. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
protected $returning = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* Does the query have any columns in it? |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
public function hasCols() |
47
|
|
|
{ |
48
|
|
|
return (bool) $this->col_values; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* Sets one column value placeholder; if an optional second parameter is |
54
|
|
|
* passed, that value is bound to the placeholder. |
55
|
|
|
* |
56
|
|
|
* @param string $col The column name. |
57
|
|
|
* |
58
|
|
|
* @param array $value Value of the column |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
62 |
|
protected function addCol($col, ...$value) |
63
|
|
|
{ |
64
|
62 |
|
$key = $this->quoter->quoteName($col); |
65
|
62 |
|
$this->col_values[$key] = ":$col"; |
66
|
62 |
|
if (count($value) > 0) { |
67
|
36 |
|
$this->bindValue($col, $value[0]); |
68
|
36 |
|
} |
69
|
62 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* Sets multiple column value placeholders. If an element is a key-value |
75
|
|
|
* pair, the key is treated as the column name and the value is bound to |
76
|
|
|
* that column. |
77
|
|
|
* |
78
|
|
|
* @param array $cols A list of column names, optionally as key-value |
79
|
|
|
* pairs where the key is a column name and the value is a bind value for |
80
|
|
|
* that column. |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
61 |
|
protected function addCols(array $cols) |
86
|
|
|
{ |
87
|
61 |
|
foreach ($cols as $key => $val) { |
88
|
61 |
|
if (is_int($key)) { |
89
|
|
|
// integer key means the value is the column name |
90
|
31 |
|
$this->addCol($val); |
91
|
31 |
|
} else { |
92
|
|
|
// the key is the column name and the value is a value to |
93
|
|
|
// be bound to that column |
94
|
36 |
|
$this->addCol($key, $val); |
95
|
|
|
} |
96
|
61 |
|
} |
97
|
61 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* Sets a column value directly; the value will not be escaped, although |
103
|
|
|
* fully-qualified identifiers in the value will be quoted. |
104
|
|
|
* |
105
|
|
|
* @param string $col The column name. |
106
|
|
|
* |
107
|
|
|
* @param string $value The column value expression. |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
* |
111
|
|
|
*/ |
112
|
45 |
|
protected function setCol($col, $value) |
113
|
|
|
{ |
114
|
45 |
|
if ($value === null) { |
115
|
30 |
|
$value = 'NULL'; |
116
|
30 |
|
} |
117
|
|
|
|
118
|
45 |
|
$key = $this->quoter->quoteName($col); |
119
|
45 |
|
$value = $this->quoter->quoteNamesIn($value); |
120
|
45 |
|
$this->col_values[$key] = $value; |
121
|
45 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
* Adds returning columns to the query. |
127
|
|
|
* |
128
|
|
|
* Multiple calls to returning() will append to the list of columns, not |
129
|
|
|
* overwrite the previous columns. |
130
|
|
|
* |
131
|
|
|
* @param array $cols The column(s) to add to the query. |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
* |
135
|
|
|
*/ |
136
|
3 |
|
protected function addReturning(array $cols) |
137
|
|
|
{ |
138
|
3 |
|
foreach ($cols as $col) { |
139
|
3 |
|
$this->returning[] = $this->quoter->quoteNamesIn($col); |
140
|
3 |
|
} |
141
|
3 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* Builds the `RETURNING` clause of the statement. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
68 |
|
protected function buildReturning() |
152
|
|
|
{ |
153
|
68 |
|
if (empty($this->returning)) { |
154
|
65 |
|
return ''; // not applicable |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
return PHP_EOL . 'RETURNING' . $this->indentCsv($this->returning); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|