SqliteCompiler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _buildSelectPart() 0 7 2
A _buildUnionPart() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lampager\Cake\Database;
6
7
use Cake\Database\Query;
8
use Cake\Database\QueryCompiler;
9
use Cake\Database\ValueBinder;
10
11
class SqliteCompiler extends QueryCompiler
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected function _buildSelectPart(array $parts, Query $query, ValueBinder $generator): string
17
    {
18
        if (!$query->clause('union')) {
19
            return parent::_buildSelectPart($parts, $query, $generator);
20
        }
21
22
        return sprintf('SELECT * FROM (%s', parent::_buildSelectPart($parts, $query, $generator));
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function _buildUnionPart(array $parts, Query $query, ValueBinder $generator): string
29
    {
30
        $parts = array_map(function ($p) use ($generator) {
31
            $p['query'] = $p['query']->sql($generator);
32
            $p['query'] = $p['query'][0] === '(' ? trim($p['query'], '()') : $p['query'];
33
            $prefix = $p['all'] ? 'ALL ' : '';
34
            return sprintf('%sSELECT * FROM (%s)', $prefix, $p['query']);
35
        }, $parts);
36
37
        return sprintf(")\nUNION %s", implode("\nUNION ", $parts));
38
    }
39
}
40