1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Drivers\MySql\Builder\Builders; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
21
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\QuoteNameTrait; |
22
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\SubQueryTrait; |
23
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\WrapValueTrait; |
24
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Represents a query builder for building an INSERT query |
28
|
|
|
*/ |
29
|
|
|
class InsertBuilder extends QueryPartBuilder |
30
|
|
|
{ |
31
|
|
|
use QuoteNameTrait; |
32
|
|
|
use WrapValueTrait; |
33
|
|
|
use SubQueryTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
5 |
|
protected function getInitialParts(): array |
39
|
|
|
{ |
40
|
5 |
|
return ['INSERT']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
5 |
|
protected function getRequiredParts(): array |
47
|
|
|
{ |
48
|
5 |
|
return ['into', 'values']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
5 |
|
protected function getPartBuilders(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
5 |
|
'into' => fn($part, $state) => $this->buildInto($part, $state), |
58
|
5 |
|
'columns' => fn($part, $state) => $this->buildColumns($part, $state), |
59
|
5 |
|
'values' => fn($part, $state) => $this->buildValueItems($part, $state), |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Build INTO part. |
65
|
|
|
* |
66
|
|
|
* @param StructuredQuery|string $part Table name into the data to be inserted |
67
|
|
|
* @param MySqlQueryBuilderState $state Query builder state |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
5 |
|
protected function buildInto($part, $state) |
71
|
|
|
{ |
72
|
5 |
|
if ($part instanceof StructuredQuery) { |
73
|
1 |
|
return 'INTO ' . $this->buildQuery($part, $state); |
74
|
|
|
} |
75
|
|
|
|
76
|
5 |
|
return 'INTO ' . $this->quoteName($part); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Build specific columns which will be inserted. |
81
|
|
|
* |
82
|
|
|
* @param StructuredQuery|array $columns Columns to be inserted |
83
|
|
|
* @param MySqlQueryBuilderState $state Query builder state |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
3 |
|
protected function buildColumns($columns, $state) |
87
|
|
|
{ |
88
|
3 |
|
if ($columns instanceof StructuredQuery) { |
89
|
1 |
|
return $this->buildQuery($columns, $state); |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
$result = []; |
93
|
|
|
|
94
|
3 |
|
foreach ($columns as $column) { |
95
|
3 |
|
$result[] = $this->quoteName($column); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return '(' . implode(', ', $result) . ')'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Build multiple value items. |
103
|
|
|
* |
104
|
|
|
* @param array $valuesList List of values |
105
|
|
|
* @param MySqlQueryBuilderState $state Query builder state |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
5 |
|
protected function buildValueItems($valuesList, $state) |
109
|
|
|
{ |
110
|
5 |
|
$results = []; |
111
|
|
|
|
112
|
5 |
|
foreach ($valuesList as $values) { |
113
|
5 |
|
$results[] = $this->buildValues($values, $state); |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
return 'VALUES ' . implode(' ' , $results); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Build specific values which will be inserted. |
121
|
|
|
* |
122
|
|
|
* @param StructuredQuery|array $values Values to be inserted |
123
|
|
|
* @param MySqlQueryBuilderState $state Query builder state |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
5 |
|
protected function buildValues($values, $state) |
127
|
|
|
{ |
128
|
5 |
|
if ($values instanceof StructuredQuery) { |
129
|
1 |
|
return $this->buildQuery($values, $state); |
130
|
|
|
} |
131
|
|
|
|
132
|
5 |
|
$result = []; |
133
|
|
|
|
134
|
5 |
|
foreach ($values as $value) { |
135
|
5 |
|
$result[] = $this->buildWrapValue($value, $state); |
136
|
|
|
} |
137
|
|
|
|
138
|
5 |
|
return '(' . implode(', ', $result) . ')'; |
139
|
|
|
} |
140
|
|
|
} |