Passed
Push — master ( cc4828...00e84f )
by Igor
10:05
created

src/Query/Query.php (4 issues)

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
    private $supportFormats=[
26
        "FORMAT\\s+TSVRaw",
27
        "FORMAT\\s+TSVWithNamesAndTypes",
28
        "FORMAT\\s+TSVWithNames",
29
        "FORMAT\\s+TSV",
30
        "FORMAT\\s+Vertical",
31
        "FORMAT\\s+JSONCompact",
32
        "FORMAT\\s+JSONEachRow",
33
        "FORMAT\\s+TSKV",
34
        "FORMAT\\s+TabSeparatedWithNames",
35
        "FORMAT\\s+TabSeparatedWithNamesAndTypes",
36
        "FORMAT\\s+TabSeparatedRaw",
37
        "FORMAT\\s+BlockTabSeparated",
38
        "FORMAT\\s+CSVWithNames",
39
        "FORMAT\\s+CSV",
40
        "FORMAT\\s+JSON",
41
        "FORMAT\\s+TabSeparated"
42
    ];
43
44
    /**
45
     * Query constructor.
46
     * @param string $sql
47
     * @param array $degenerations
48
     */
49 44
    public function __construct($sql, $degenerations = [])
50
    {
51 44
        if (!trim($sql))
52
        {
53 1
            throw new QueryException('Empty Query');
54
        }
55 43
        $this->sql = $sql;
56 43
        $this->degenerations = $degenerations;
57 43
    }
58
59
    /**
60
     * @param string|null $format
61
     */
62 37
    public function setFormat($format)
63
    {
64 37
        $this->format = $format;
65 37
    }
66
67
68 37
    private function applyFormatQuery()
69
    {
70
        // FORMAT\s(\w)*$
71 37
        if (null === $this->format) {
72
            return false;
73
        }
74 37
        $supportFormats = implode("|",$this->supportFormats);
75
76 37
        $this->sql = trim($this->sql);
0 ignored issues
show
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
77 37
        if (substr($this->sql, -1) == ';') {
0 ignored issues
show
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Operator == is disallowed, use === instead.
Loading history...
78
            $this->sql = substr($this->sql, 0, -1);
0 ignored issues
show
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
79
        }
80
81 37
        $matches = [];
82 37
        if (preg_match_all('%(' . $supportFormats . ')%ius', $this->sql, $matches)) {
83
84
            // skip add "format json"
85 2
            if (isset($matches[0]))
86
            {
87
88 2
                $this->format = trim(str_ireplace('format', '', $matches[0][0]));
89
90
            }
91
        } else {
92 35
            $this->sql = $this->sql . ' FORMAT ' . $this->format;
93
        }
94
95
96
97
98
99
100 37
    }
101
102
    /**
103
     * @return null|string
104
     */
105 43
    public function getFormat()
106
    {
107
108 43
        return $this->format;
109
    }
110
111 43
    public function toSql()
112
    {
113 43
        if ($this->format !== null) {
114 37
            $this->applyFormatQuery();
115
        }
116
117 43
        if (sizeof($this->degenerations))
118
        {
119 43
            foreach ($this->degenerations as $degeneration)
120
            {
121 43
                if ($degeneration instanceof Degeneration) {
122 43
                    $this->sql = $degeneration->process($this->sql);
123
                }
124
            }
125
        }
126
127 43
        return $this->sql;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function __toString()
134
    {
135
        return $this->toSql();
136
    }
137
}
138