InsertBuilder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 118
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequiredParts() 0 3 1
A buildValues() 0 13 3
A getInitialParts() 0 3 1
A buildColumns() 0 13 3
A buildInto() 0 7 2
A buildValueItems() 0 9 2
A getLastParts() 0 3 1
A getPartBuilders() 0 6 1
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\Pdo\MySql\Builders;
19
20
use ArekX\PQL\Contracts\StructuredQuery;
21
use ArekX\PQL\Drivers\Pdo\MySql\Builders\Traits\QuoteNameTrait;
22
use ArekX\PQL\Drivers\Pdo\MySql\Builders\Traits\SubQueryTrait;
23
use ArekX\PQL\Drivers\Pdo\MySql\Builders\Traits\WrapValueTrait;
24
use ArekX\PQL\Drivers\Pdo\MySql\MySqlQueryBuilderState;
25
use ArekX\PQL\Sql\Query\Insert;
26
27
/**
28
 * Represents a query builder for building an INSERT query
29
 *
30
 * @see Insert
31
 */
32
class InsertBuilder extends QueryPartBuilder
33
{
34
    use QuoteNameTrait;
35
    use WrapValueTrait;
36
    use SubQueryTrait;
37
38
    /**
39
     * @inheritDoc
40
     */
41 23
    protected function getInitialParts(): array
42
    {
43 23
        return ['INSERT'];
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 23
    protected function getRequiredParts(): array
50
    {
51 23
        return ['into', 'values'];
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 23
    protected function getPartBuilders(): array
58
    {
59 23
        return [
60 23
            'into' => fn($part, $state) => $this->buildInto($part, $state),
61 23
            'columns' => fn($part, $state) => $this->buildColumns($part, $state),
62 23
            'values' => fn($part, $state) => $this->buildValueItems($part, $state),
63 23
        ];
64
    }
65
66
    /**
67
     * Build INTO part.
68
     *
69
     * @param StructuredQuery|string $part Table name into the data to be inserted
70
     * @param MySqlQueryBuilderState $state Query builder state
71
     * @return string
72
     */
73 23
    protected function buildInto($part, $state)
74
    {
75 23
        if ($part instanceof StructuredQuery) {
76 1
            return 'INTO ' . $this->buildQuery($part, $state);
77
        }
78
79 23
        return 'INTO ' . $this->quoteName($part);
80
    }
81
82
    /**
83
     * Build specific columns which will be inserted.
84
     *
85
     * @param array|StructuredQuery $columns Columns to be inserted
86
     * @param MySqlQueryBuilderState $state Query builder state
87
     * @return string
88
     */
89 21
    protected function buildColumns(StructuredQuery|array $columns, MySqlQueryBuilderState $state): string
90
    {
91 21
        if ($columns instanceof StructuredQuery) {
92 1
            return $this->buildQuery($columns, $state);
93
        }
94
95 21
        $result = [];
96
97 21
        foreach ($columns as $column) {
98 21
            $result[] = $this->quoteName($column);
99
        }
100
101 21
        return '(' . implode(', ', $result) . ')';
102
    }
103
104
    /**
105
     * Build multiple value items.
106
     *
107
     * @param array $valuesList List of values
108
     * @param MySqlQueryBuilderState $state Query builder state
109
     * @return string
110
     */
111 23
    protected function buildValueItems(array $valuesList, MySqlQueryBuilderState $state): string
112
    {
113 23
        $results = [];
114
115 23
        foreach ($valuesList as $values) {
116 23
            $results[] = $this->buildValues($values, $state);
117
        }
118
119 23
        return 'VALUES ' . implode(' ', $results);
120
    }
121
122
    /**
123
     * Build specific values which will be inserted.
124
     *
125
     * @param array|StructuredQuery $values Values to be inserted
126
     * @param MySqlQueryBuilderState $state Query builder state
127
     * @return string
128
     */
129 23
    protected function buildValues(StructuredQuery|array $values, MySqlQueryBuilderState $state): string
130
    {
131 23
        if ($values instanceof StructuredQuery) {
132 1
            return $this->buildQuery($values, $state);
133
        }
134
135 23
        $result = [];
136
137 23
        foreach ($values as $value) {
138 23
            $result[] = $this->buildWrapValue($value, $state);
139
        }
140
141 23
        return '(' . implode(', ', $result) . ')';
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147 23
    protected function getLastParts(): array
148
    {
149 23
        return [];
150
    }
151
}
152