1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Custom\BulkInsert; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Query\CompilableClause; |
6
|
|
|
use Bdf\Prime\Query\Compiler\AbstractCompiler; |
7
|
|
|
use Bdf\Prime\Types\TypeInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Compiler for @see BulkInsertQuery |
11
|
|
|
* |
12
|
|
|
* The query will be compiled into a prepared statement |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
class BulkInsertSqlCompiler extends AbstractCompiler |
15
|
|
|
{ |
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
411 |
|
protected function doCompileInsert(CompilableClause $query) |
|
|
|
|
20
|
|
|
{ |
21
|
411 |
|
$sql = $this->compileMode($query).' INTO '.$this->quoteIdentifier($query, $query->statements['table']); |
22
|
|
|
|
23
|
411 |
|
if (!isset($query->state()->compiledParts['columns'])) { |
24
|
118 |
|
$this->compileColumns($query); |
25
|
|
|
} |
26
|
|
|
|
27
|
411 |
|
$sql .= $query->state()->compiledParts['columns']['sql'].$this->compileValues($query); |
28
|
|
|
|
29
|
411 |
|
return $this->connection->prepare($sql); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
protected function doCompileUpdate(CompilableClause $query) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
throw new \BadMethodCallException(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
|
|
|
|
43
|
|
|
protected function doCompileDelete(CompilableClause $query) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
throw new \BadMethodCallException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
|
|
|
|
51
|
|
|
protected function doCompileSelect(CompilableClause $query) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
throw new \BadMethodCallException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
411 |
|
public function quoteIdentifier(CompilableClause $query, $column) |
60
|
|
|
{ |
61
|
411 |
|
if (!$query->isQuoteIdentifier()) { |
62
|
410 |
|
return $column; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $this->platform()->grammar()->quoteIdentifier($column); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
422 |
|
public function getBindings(CompilableClause $query) |
72
|
|
|
{ |
73
|
422 |
|
if ($query->statements['bulk']) { |
74
|
5 |
|
$bindings = []; |
75
|
|
|
|
76
|
5 |
|
foreach ($query->statements['values'] as $values) { |
77
|
5 |
|
foreach ($query->state()->compiledParts['columns']['types'] as $field => $type) { |
78
|
5 |
|
$bindings[] = $this->platform()->types()->toDatabase($values[$field] ?? null, $type); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
return $bindings; |
83
|
|
|
} |
84
|
|
|
|
85
|
417 |
|
$values = $query->statements['values'][0]; |
86
|
417 |
|
$bindings = []; |
87
|
|
|
|
88
|
417 |
|
foreach ($query->state()->compiledParts['columns']['types'] as $field => $type) { |
89
|
417 |
|
$bindings[] = $this->platform()->types()->toDatabase($values[$field] ?? null, $type); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
417 |
|
return $bindings; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Compile the INSERT mode |
97
|
|
|
* |
98
|
|
|
* @param CompilableClause $query |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
411 |
|
private function compileMode(CompilableClause $query) |
|
|
|
|
103
|
|
|
{ |
104
|
411 |
|
switch ($query->statements['mode']) { |
105
|
411 |
|
case BulkInsertQuery::MODE_REPLACE: |
106
|
3 |
|
return 'REPLACE'; |
|
|
|
|
107
|
|
|
|
108
|
410 |
|
case BulkInsertQuery::MODE_IGNORE: |
109
|
7 |
|
if ($this->platform()->grammar()->getName() === 'sqlite') { |
110
|
7 |
|
return 'INSERT OR IGNORE'; |
111
|
|
|
} else { |
112
|
|
|
return 'INSERT IGNORE'; |
113
|
|
|
} |
114
|
|
|
break; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
408 |
|
return 'INSERT'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Compile columns, and resolve types |
122
|
|
|
* |
123
|
|
|
* @param CompilableClause $query |
124
|
|
|
*/ |
125
|
118 |
|
private function compileColumns(CompilableClause $query) |
|
|
|
|
126
|
|
|
{ |
127
|
118 |
|
$columns = []; |
128
|
118 |
|
$types = []; |
129
|
|
|
|
130
|
118 |
|
foreach ($query->statements['columns'] as $column) { |
131
|
118 |
|
if (!empty($column['type'])) { |
132
|
1 |
|
$types[$column['name']] = $column['type']; |
133
|
1 |
|
$type = null; |
134
|
|
|
} else { |
135
|
117 |
|
$types[$column['name']] = null; |
136
|
117 |
|
$type = true; |
137
|
|
|
} |
138
|
|
|
|
139
|
118 |
|
$columns[] = $this->quoteIdentifier($query, $query->preprocessor()->field($column['name'], $type)); |
140
|
|
|
|
141
|
118 |
|
if ($type instanceof TypeInterface) { |
142
|
118 |
|
$types[$column['name']] = $type; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
118 |
|
$query->state()->compiledParts['columns'] = [ |
147
|
118 |
|
'sql' => '('.implode(', ', $columns).') ', |
148
|
118 |
|
'types' => $types, |
149
|
118 |
|
'values' => '('.str_repeat('?, ', count($types) - 1).'?)', |
|
|
|
|
150
|
|
|
]; |
151
|
118 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Compile values for bulk INSERT query |
155
|
|
|
* |
156
|
|
|
* @param CompilableClause $query |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
411 |
|
private function compileValues(CompilableClause $query) |
|
|
|
|
161
|
|
|
{ |
162
|
411 |
|
if (!$query->statements['bulk']) { |
163
|
405 |
|
return 'VALUES '.$query->state()->compiledParts['columns']['values']; |
164
|
|
|
} |
165
|
|
|
|
166
|
6 |
|
$values = $query->state()->compiledParts['columns']['values']; |
167
|
|
|
|
168
|
6 |
|
return 'VALUES '.str_repeat($values.', ', count($query->statements['values']) - 1).$values; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|