1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Query; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\CompilerInterface; |
15
|
|
|
use Cycle\Database\Injection\Parameter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Insert statement query builder, support singular and batch inserts. |
19
|
|
|
*/ |
20
|
|
|
class InsertQuery extends ActiveQuery |
21
|
|
|
{ |
22
|
|
|
protected string $table; |
23
|
|
|
protected array $columns = []; |
24
|
|
|
protected array $values = []; |
25
|
|
|
|
26
|
78 |
|
public function __construct(string $table = null) |
27
|
|
|
{ |
28
|
78 |
|
$this->table = $table ?? ''; |
29
|
78 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set target insertion table. |
33
|
|
|
* |
34
|
|
|
* @psalm-param non-empty-string $into |
35
|
|
|
*/ |
36
|
466 |
|
public function into(string $into): self |
37
|
|
|
{ |
38
|
466 |
|
$this->table = $into; |
39
|
|
|
|
40
|
466 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set insertion column names. Names can be provided as array, set of parameters or comma |
45
|
|
|
* separated string. |
46
|
|
|
* |
47
|
|
|
* Examples: |
48
|
|
|
* $insert->columns(["name", "email"]); |
49
|
|
|
* $insert->columns("name", "email"); |
50
|
|
|
* $insert->columns("name, email"); |
51
|
|
|
*/ |
52
|
162 |
|
public function columns(array|string ...$columns): self |
53
|
|
|
{ |
54
|
162 |
|
$this->columns = $this->fetchIdentifiers($columns); |
55
|
|
|
|
56
|
162 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set insertion rowset values or multiple rowsets. Values can be provided in multiple forms |
61
|
|
|
* (method parameters, array of values, array of rowsets). Columns names will be automatically |
62
|
|
|
* fetched (if not already specified) from first provided rowset based on rowset keys. |
63
|
|
|
* |
64
|
|
|
* Examples: |
65
|
|
|
* $insert->columns("name", "balance")->values("Wolfy-J", 10); |
66
|
|
|
* $insert->values([ |
67
|
|
|
* "name" => "Wolfy-J", |
68
|
|
|
* "balance" => 10 |
69
|
|
|
* ]); |
70
|
|
|
* $insert->values([ |
71
|
|
|
* [ |
72
|
|
|
* "name" => "Wolfy-J", |
73
|
|
|
* "balance" => 10 |
74
|
|
|
* ], |
75
|
|
|
* [ |
76
|
|
|
* "name" => "Ben", |
77
|
|
|
* "balance" => 20 |
78
|
|
|
* ] |
79
|
|
|
* ]); |
80
|
|
|
*/ |
81
|
466 |
|
public function values(mixed $rowsets): self |
82
|
|
|
{ |
83
|
466 |
|
if (!is_array($rowsets)) { |
84
|
42 |
|
return $this->values(func_get_args()); |
85
|
|
|
} |
86
|
|
|
|
87
|
466 |
|
if ($rowsets === []) { |
88
|
16 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
//Checking if provided set is array of multiple |
92
|
450 |
|
reset($rowsets); |
93
|
|
|
|
94
|
450 |
|
if (!is_array($rowsets[key($rowsets)])) { |
95
|
346 |
|
if ($this->columns === []) { |
96
|
288 |
|
$this->columns = array_keys($rowsets); |
97
|
|
|
} |
98
|
|
|
|
99
|
346 |
|
$this->values[] = new Parameter(array_values($rowsets)); |
100
|
|
|
} else { |
101
|
106 |
|
if ($this->columns === []) { |
102
|
106 |
|
$this->columns = \array_keys($rowsets[\key($rowsets)]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($rowsets as $values) { |
106
|
450 |
|
$this->values[] = new Parameter(array_values($values)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
296 |
|
* Run the query and return last insert id. |
115
|
|
|
* |
116
|
296 |
|
* @psalm-return int|non-empty-string|null |
117
|
296 |
|
*/ |
118
|
|
|
public function run(): mixed |
119
|
296 |
|
{ |
120
|
|
|
$params = new QueryParameters(); |
121
|
296 |
|
$queryString = $this->sqlStatement($params); |
122
|
|
|
|
123
|
|
|
$this->driver->execute( |
|
|
|
|
124
|
272 |
|
$queryString, |
125
|
272 |
|
$params->getParameters() |
126
|
268 |
|
); |
127
|
|
|
|
128
|
|
|
$lastID = $this->driver->lastInsertID(); |
129
|
4 |
|
if (is_numeric($lastID)) { |
130
|
|
|
return (int)$lastID; |
131
|
|
|
} |
132
|
462 |
|
|
133
|
|
|
return $lastID; |
134
|
462 |
|
} |
135
|
|
|
|
136
|
|
|
public function getType(): int |
137
|
336 |
|
{ |
138
|
|
|
return CompilerInterface::INSERT_QUERY; |
139
|
|
|
} |
140
|
336 |
|
|
141
|
336 |
|
public function getTokens(): array |
142
|
336 |
|
{ |
143
|
|
|
return [ |
144
|
|
|
'table' => $this->table, |
145
|
|
|
'columns' => $this->columns, |
146
|
|
|
'values' => $this->values, |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.