InsertCompiler::compileSelectForInsert()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * This file is part of UnderQuery 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\UnderQuery\QueryCompiler\ConcreteCompiler;
13
14
use Phuria\UnderQuery\QueryBuilder\AbstractInsertBuilder;
15
use Phuria\UnderQuery\QueryBuilder\BuilderInterface;
16
use Phuria\UnderQuery\QueryBuilder\InsertBuilder;
17
use Phuria\UnderQuery\QueryBuilder\InsertSelectBuilder;
18
use Phuria\UnderQuery\QueryCompiler\CompilerPayload;
19
use Phuria\UnderQuery\QueryCompiler\ReferenceCompiler;
20
use Phuria\UnderQuery\QueryCompiler\TableCompiler;
21
22
/**
23
 * @author Beniamin Jonatan Šimko <[email protected]>
24
 */
25
class InsertCompiler extends AbstractConcreteCompiler
26
{
27
    /**
28
     * InsertCompiler constructor.
29
     */
30
    public function __construct()
31
    {
32
        $tableCompiler = new TableCompiler();
33
        $referenceCompiler = new ReferenceCompiler();
34
35
        parent::__construct([
36
            [$this, 'compileInsert'],
37
            [$tableCompiler, 'compileRootTables'],
38
            [$tableCompiler, 'compileJoinTables'],
39
            [$this, 'compileInsertColumns'],
40
            [$this, 'compileSelectForInsert'],
41
            [$this, 'compileInsertValues'],
42
            [$referenceCompiler, 'compileReference'],
43
        ]);
44
    }
45
46
    /**
47
     * @param BuilderInterface $builder
48
     *
49
     * @return bool
50
     */
51
    public function supportsBuilder(BuilderInterface $builder)
52
    {
53
        return $builder instanceof AbstractInsertBuilder;
54
    }
55
56
    /**
57
     * @param CompilerPayload $payload
58
     *
59
     * @return CompilerPayload
60
     */
61
    public function compileInsert(CompilerPayload $payload)
62
    {
63
        $builder = $payload->getBuilder();
64
65
        if ($builder instanceof AbstractInsertBuilder) {
66
            $actualSQL = 'INSERT INTO';
67
            $payload = $payload->updateSQL($actualSQL);
68
        }
69
70
        return $payload;
71
    }
72
73
    /**
74
     * @param CompilerPayload $payload
75
     *
76
     * @return CompilerPayload
77
     */
78
    public function compileInsertColumns(CompilerPayload $payload)
79
    {
80
        $builder = $payload->getBuilder();
81
82
        if ($builder instanceof AbstractInsertBuilder) {
83
            $newSQL = '(' . implode(', ', $builder->getColumns()) . ')';
84
            $payload = $payload->appendSQL($newSQL);
85
        }
86
87
        return $payload;
88
    }
89
90
    /**
91
     * @param CompilerPayload $payload
92
     *
93
     * @return CompilerPayload
94
     */
95
    public function compileInsertValues(CompilerPayload $payload)
96
    {
97
        $builder = $payload->getBuilder();
98
99
        if ($builder instanceof InsertBuilder) {
100
            $newSQL = 'VALUES ' . implode(', ', array_map(function (array $values) {
101
                return '('. implode(', ', $values) . ')';
102
            }, $builder->getValues()));
103
            $payload = $payload->appendSQL($newSQL);
104
        }
105
106
        return $payload;
107
    }
108
109
    /**
110
     * @param CompilerPayload $payload
111
     *
112
     * @return CompilerPayload
113
     */
114
    public function compileSelectForInsert(CompilerPayload $payload)
115
    {
116
        $builder = $payload->getBuilder();
117
118
        if ($builder instanceof InsertSelectBuilder) {
119
            $newSQL = $builder->getSelectInsert()->buildSQL();
120
            $payload = $payload->appendSQL($newSQL);
121
        }
122
123
        return $payload;
124
    }
125
}