Passed
Branch master (8a55cf)
by Igor
12:47 queued 09:06
created

Query::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ClickHouseDB\Query;
4
5
use ClickHouseDB\Exception\QueryException;
6
use function sizeof;
7
8
class Query
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $sql;
14
15
    /**
16
     * @var string|null
17
     */
18
    protected $format = null;
19
20
    /**
21
     * @var array
22
     */
23
    private $degenerations = [];
24
25
    /**
26
     * Query constructor.
27
     * @param string $sql
28
     * @param array $degenerations
29
     */
30 36
    public function __construct($sql, $degenerations = [])
31
    {
32 36
        if (!trim($sql))
33
        {
34 1
            throw new QueryException('Empty Query');
35
        }
36 36
        $this->sql = $sql;
37 36
        $this->degenerations = $degenerations;
38 36
    }
39
40
    /**
41
     * @param string|null $format
42
     */
43 36
    public function setFormat($format)
44
    {
45 36
        $this->format = $format;
46 36
    }
47
48
49 36
    private function applyFormatQuery()
50
    {
51
        // FORMAT\s(\w)*$
52 36
        if (null === $this->format) {
53
            return false;
54
        }
55
        $supportFormats =
56 36
            "FORMAT\\s+TSV|FORMAT\\s+TSVRaw|FORMAT\\s+TSVWithNames|FORMAT\\s+TSVWithNamesAndTypes|FORMAT\\s+Vertical|FORMAT\\s+JSONCompact|FORMAT\\s+JSONEachRow|FORMAT\\s+TSKV|FORMAT\\s+TabSeparatedWithNames|FORMAT\\s+TabSeparatedWithNamesAndTypes|FORMAT\\s+TabSeparatedRaw|FORMAT\\s+BlockTabSeparated|FORMAT\\s+CSVWithNames|FORMAT\\s+CSV|FORMAT\\s+JSON|FORMAT\\s+TabSeparated";
57
58 36
        $matches = [];
59 36
        if (preg_match_all('%(' . $supportFormats . ')%ius', $this->sql, $matches)) {
60
61
            // skip add "format json"
62 1
            if (isset($matches[0]))
63
            {
64
65 1
                $this->format = trim(str_ireplace('format', '', $matches[0][0]));
66
67
            }
68
        } else {
69 36
            $this->sql = $this->sql . ' FORMAT ' . $this->format;
70
        }
71
72
73
74
75
76
77 36
    }
78
79
    /**
80
     * @return null|string
81
     */
82 36
    public function getFormat()
83
    {
84
85 36
        return $this->format;
86
    }
87
88 36
    public function toSql()
89
    {
90 36
        if ($this->format !== null) {
91 36
            $this->applyFormatQuery();
92
        }
93
94 36
        if (sizeof($this->degenerations))
95
        {
96 36
            foreach ($this->degenerations as $degeneration)
97
            {
98 36
                if ($degeneration instanceof Degeneration) {
99 36
                    $this->sql = $degeneration->process($this->sql);
100
                }
101
            }
102
        }
103
104 36
        return $this->sql;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function __toString()
111
    {
112
        return $this->toSql();
113
    }
114
}
115