Passed
Push — master ( b979f8...3829ec )
by Aleksandar
07:20 queued 11s
created

MySqlQueryBuilder::createBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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;
19
20
use ArekX\PQL\Contracts\QueryBuilder;
21
use ArekX\PQL\Contracts\QueryBuilderState;
22
use ArekX\PQL\Drivers\Pdo\MySql\Builders\CallBuilder;
23
use ArekX\PQL\Drivers\Pdo\MySql\Builders\CaseWhenBuilder;
24
use ArekX\PQL\Drivers\Pdo\MySql\Builders\DeleteBuilder;
25
use ArekX\PQL\Drivers\Pdo\MySql\Builders\InsertBuilder;
26
use ArekX\PQL\Drivers\Pdo\MySql\Builders\MethodBuilder;
27
use ArekX\PQL\Drivers\Pdo\MySql\Builders\QueryPartBuilder;
28
use ArekX\PQL\Drivers\Pdo\MySql\Builders\RawBuilder;
29
use ArekX\PQL\Drivers\Pdo\MySql\Builders\SelectBuilder;
30
use ArekX\PQL\Drivers\Pdo\MySql\Builders\UnionBuilder;
31
use ArekX\PQL\Drivers\Pdo\MySql\Builders\UpdateBuilder;
32
use ArekX\PQL\Sql\Query\Delete;
33
use ArekX\PQL\Sql\Query\Insert;
34
use ArekX\PQL\Sql\Query\Raw;
35
use ArekX\PQL\Sql\Query\Select;
36
use ArekX\PQL\Sql\Query\Union;
37
use ArekX\PQL\Sql\Query\Update;
38
use ArekX\PQL\Sql\SqlParamBuilder;
39
use ArekX\PQL\Sql\SqlQueryBuilderFactory;
40
use ArekX\PQL\Sql\Statement\Call;
41
use ArekX\PQL\Sql\Statement\CaseWhen;
42
use ArekX\PQL\Sql\Statement\Method;
43
44
/**
45
 * Represents a query builder for MySQL
46
 */
47
class MySqlQueryBuilder extends SqlQueryBuilderFactory
48
{
49
    /**
50
     * Builder map representing how each query will be built.
51
     *
52
     * @var string[]
53
     */
54
    public $builderMap = [
55
        Raw::class => RawBuilder::class,
56
        Select::class => SelectBuilder::class,
57
        Union::class => UnionBuilder::class,
58
        Delete::class => DeleteBuilder::class,
59
        Insert::class => InsertBuilder::class,
60
        Update::class => UpdateBuilder::class,
61
        Call::class => CallBuilder::class,
62
        Method::class => MethodBuilder::class,
63
        CaseWhen::class => CaseWhenBuilder::class,
64
    ];
65
66
    /**
67
     * @inheritDoc
68
     */
69 86
    public function createState(): QueryBuilderState
70
    {
71 86
        $state = MySqlQueryBuilderState::create();
72
73 86
        $state->setParamsBuilder(SqlParamBuilder::create());
74 86
        $state->setParentBuilder($this);
75
76 86
        return $state;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82 71
    protected function createBuilder(string $queryClass): QueryBuilder
83
    {
84 71
        if (empty($this->builderMap[$queryClass])) {
85 1
            throw new \Exception('No builder defined for: ' . $queryClass);
86
        }
87
88
        /** @var QueryPartBuilder $builderClass */
89 70
        $builderClass = $this->builderMap[$queryClass];
90
91 70
        return new $builderClass();
92
    }
93
}