1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Repo2 |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in all |
16
|
|
|
* copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
24
|
|
|
* SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Repo2\QueryBuilder\Query; |
28
|
|
|
|
29
|
|
|
use Repo2\QueryBuilder\DriverInterface; |
30
|
|
|
use Repo2\QueryBuilder\Exception\CompileException; |
31
|
|
|
use Repo2\QueryBuilder\Expression\Reference; |
32
|
|
|
use Repo2\QueryBuilder\ExpressionInterface; |
33
|
|
|
use Repo2\QueryBuilder\ExpressionBag; |
34
|
|
|
|
35
|
|
|
class InsertQuery implements ExpressionInterface |
36
|
|
|
{ |
37
|
|
|
/** @var Reference */ |
38
|
|
|
private $table; |
39
|
|
|
|
40
|
|
|
/** @var array<integer,string> */ |
41
|
|
|
private $fields; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
private $values; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Reference $table |
48
|
|
|
* @param array<string,integer|string> $firstRow |
49
|
|
|
*/ |
50
|
21 |
|
public function __construct(Reference $table, array $firstRow = []) |
51
|
|
|
{ |
52
|
21 |
|
$this->table = $table; |
53
|
21 |
|
if (!empty($firstRow)) { |
54
|
6 |
|
$this->init($firstRow); |
55
|
4 |
|
} |
56
|
21 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array<string,integer|string> $firstRow |
60
|
|
|
*/ |
61
|
18 |
|
private function init(array $firstRow) |
62
|
|
|
{ |
63
|
18 |
|
$this->fields = array_keys($firstRow); |
64
|
18 |
|
$this->values = [array_values($firstRow)]; |
65
|
18 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array<string,integer|string> $row |
69
|
|
|
* @return $this |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
*/ |
72
|
15 |
|
public function pipe(array $row) |
73
|
|
|
{ |
74
|
15 |
|
if (empty($this->fields)) { |
75
|
12 |
|
$this->init($row); |
76
|
8 |
|
} else { |
77
|
12 |
|
$values = []; |
78
|
12 |
|
foreach ($this->fields as $index => $name) { |
79
|
12 |
|
if (!array_key_exists($name, $row)) { |
80
|
3 |
|
throw new \InvalidArgumentException(sprintf('The field "%s" is not found in the row.', $name)); |
81
|
|
|
} |
82
|
9 |
|
$values[$index] = $row[$name]; |
83
|
6 |
|
} |
84
|
9 |
|
$this->values[] = $values; |
85
|
|
|
} |
86
|
15 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param DriverInterface $driver |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
15 |
|
private function compileValues(DriverInterface $driver) |
94
|
|
|
{ |
95
|
15 |
|
$rows = []; |
96
|
15 |
|
foreach ($this->values as $values) { |
97
|
15 |
|
$rows[] = '"'. implode('", "', $driver->escapeValues($values)) . '"'; |
98
|
10 |
|
} |
99
|
15 |
|
return '(' . implode('), (', $rows) . ')'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param DriverInterface $driver |
104
|
|
|
* @return string |
105
|
|
|
* @throws CompileException |
106
|
|
|
*/ |
107
|
18 |
|
private function compileFields(DriverInterface $driver) |
108
|
|
|
{ |
109
|
18 |
|
if (empty($this->fields)) { |
110
|
3 |
|
throw new CompileException('No fields found for insert query.'); |
111
|
|
|
} |
112
|
15 |
|
$fields = new ExpressionBag(); |
113
|
15 |
|
foreach ($this->fields as $field) { |
114
|
15 |
|
$fields->add(new Reference($field)); |
115
|
10 |
|
} |
116
|
15 |
|
return $fields->concat($driver, ', '); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
18 |
|
public function compile(DriverInterface $driver) |
123
|
|
|
{ |
124
|
18 |
|
return 'INSERT INTO ' . $this->table->compile($driver) |
125
|
18 |
|
. '(' . $this->compileFields($driver) . ') VALUES ' . $this->compileValues($driver); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|