1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Phuria SQL Builder package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\SQLBuilder\QueryCompiler\ConcreteCompiler; |
13
|
|
|
|
14
|
|
|
use Phuria\SQLBuilder\QueryBuilder\AbstractInsertBuilder; |
15
|
|
|
use Phuria\SQLBuilder\QueryBuilder\BuilderInterface; |
16
|
|
|
use Phuria\SQLBuilder\QueryBuilder\Clause; |
17
|
|
|
use Phuria\SQLBuilder\QueryBuilder\Component; |
18
|
|
|
use Phuria\SQLBuilder\QueryBuilder\InsertSelectBuilder; |
19
|
|
|
use Phuria\SQLBuilder\QueryCompiler\CompilerPayload; |
20
|
|
|
use Phuria\SQLBuilder\QueryCompiler\ReferenceCompiler; |
21
|
|
|
use Phuria\SQLBuilder\QueryCompiler\TableCompiler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class InsertCompiler extends AbstractConcreteCompiler |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* InsertCompiler constructor. |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct() |
32
|
|
|
{ |
33
|
6 |
|
$tableCompiler = new TableCompiler(); |
34
|
6 |
|
$referenceCompiler = new ReferenceCompiler(); |
35
|
|
|
|
36
|
6 |
|
parent::__construct([ |
37
|
6 |
|
[$this, 'compileInsert'], |
38
|
6 |
|
[$tableCompiler, 'compileRootTables'], |
39
|
6 |
|
[$tableCompiler, 'compileJoinTables'], |
40
|
6 |
|
[$this, 'compileInsertColumns'], |
41
|
6 |
|
[$this, 'compileSelectForInsert'], |
42
|
6 |
|
[$this, 'compileInsertValues'], |
43
|
6 |
|
[$referenceCompiler, 'compileReference'] |
44
|
6 |
|
]); |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param BuilderInterface $builder |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
10 |
|
public function supportsBuilder(BuilderInterface $builder) |
53
|
|
|
{ |
54
|
10 |
|
return $builder instanceof AbstractInsertBuilder; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param CompilerPayload $payload |
59
|
|
|
* |
60
|
|
|
* @return CompilerPayload |
61
|
|
|
*/ |
62
|
3 |
|
public function compileInsert(CompilerPayload $payload) |
63
|
|
|
{ |
64
|
3 |
|
$builder = $payload->getBuilder(); |
65
|
|
|
|
66
|
3 |
|
if ($builder instanceof AbstractInsertBuilder) { |
67
|
3 |
|
$actualSQL = 'INSERT INTO'; |
68
|
3 |
|
$payload = $payload->updateSQL($actualSQL); |
69
|
3 |
|
} |
70
|
|
|
|
71
|
3 |
|
return $payload; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param CompilerPayload $payload |
76
|
|
|
* |
77
|
|
|
* @return CompilerPayload |
78
|
|
|
*/ |
79
|
3 |
|
public function compileInsertColumns(CompilerPayload $payload) |
80
|
|
|
{ |
81
|
3 |
|
$builder = $payload->getBuilder(); |
82
|
|
|
|
83
|
3 |
|
if ($builder instanceof Clause\InsertColumnsClauseInterface) { |
84
|
3 |
|
$newSQL = '(' . implode(', ', $builder->getColumns()) . ')'; |
85
|
3 |
|
$payload = $payload->appendSQL($newSQL); |
86
|
3 |
|
} |
87
|
|
|
|
88
|
3 |
|
return $payload; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param CompilerPayload $payload |
93
|
|
|
* |
94
|
|
|
* @return CompilerPayload |
95
|
|
|
*/ |
96
|
3 |
|
public function compileInsertValues(CompilerPayload $payload) |
97
|
|
|
{ |
98
|
3 |
|
$builder = $payload->getBuilder(); |
99
|
|
|
|
100
|
3 |
|
if ($builder instanceof Component\InsertValuesComponentInterface) { |
101
|
2 |
|
$newSQL = 'VALUES ' . implode(', ', array_map(function (array $values) { |
102
|
2 |
|
return '('. implode(', ', $values) . ')'; |
103
|
2 |
|
}, $builder->getValues())); |
104
|
2 |
|
$payload = $payload->appendSQL($newSQL); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
3 |
|
return $payload; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param CompilerPayload $payload |
112
|
|
|
* |
113
|
|
|
* @return CompilerPayload |
114
|
|
|
*/ |
115
|
3 |
|
public function compileSelectForInsert(CompilerPayload $payload) |
116
|
|
|
{ |
117
|
3 |
|
$builder = $payload->getBuilder(); |
118
|
|
|
|
119
|
3 |
|
if ($builder instanceof InsertSelectBuilder) { |
120
|
1 |
|
$newSQL = $builder->getSelectInsert()->buildSQL(); |
121
|
1 |
|
$payload = $payload->appendSQL($newSQL); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
3 |
|
return $payload; |
125
|
|
|
} |
126
|
|
|
} |