Passed
Push — master ( ded3fd...8acf3f )
by Aleksandar
03:45
created

InsertBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 110
ccs 31
cts 31
cp 1
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequiredParts() 0 3 1
A getInitialParts() 0 3 1
A buildInto() 0 7 2
A getPartBuilders() 0 6 1
A buildValueItems() 0 9 2
A buildValues() 0 13 3
A buildColumns() 0 13 3
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
}