• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / datagrid-nette-database-data-source / #6

14 Mar 2026 11:10AM UTC coverage: 75.0% (-1.1%) from 76.05%
#6

push

github

f3l1x
Makefile: run coverage targets with php

Use the PHP CLI for coverage targets to match the org-wide phpdbg removal tracked in contributte/contributte#73.

165 of 220 relevant lines covered (75.0%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.21
/src/QueryHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Ublaboo\NetteDatabaseDataSource;
6

7
use PHPSQLParser\PHPSQLCreator;
8
use PHPSQLParser\PHPSQLParser;
9

10
class QueryHelper
11
{
12

13
        /**
14
         * @var PHPSQLParser
15
         */
16
        protected $sqlParser;
17

18
        /**
19
         * @var mixed[]
20
         */
21
        protected $query;
22

23
        /**
24
         * @var PHPSQLCreator
25
         */
26
        private $sqlCreator;
27

28
        public function __construct(string $sql)
29
        {
30
                $this->sqlParser = new PHPSQLParser;
1✔
31
                $this->sqlCreator = new PHPSQLCreator;
1✔
32

33
                $this->query = $this->prepare($this->sqlParser->parse($sql));
1✔
34
        }
1✔
35

36
        public function resetQuery(string $sql): void
37
        {
38
                $this->query = $this->prepare($this->sqlParser->parse($sql));
×
39
        }
×
40

41
        /**
42
         * In case query contains a more complicated query, place it within brackets: (<complicated_expr>)
43
         *
44
         * @param mixed[] $query
45
         * @return mixed[]
46
         */
47
        public function prepare(array $query): array
48
        {
49
                if (isset($query['WHERE']) && sizeof($query['WHERE']) > 1) {
1✔
50
                        $where = $query['WHERE'];
1✔
51

52
                        $query['WHERE'] = [[
1✔
53
                                'expr_type' => 'bracket_expression',
1✔
54
                                'base_expr' => '',
1✔
55
                                'sub_tree' => $where,
1✔
56
                        ]];
57

58
                        foreach ($where as $where_data) {
1✔
59
                                $query['WHERE'][0]['base_expr'] .= ' ' . $where_data['base_expr'];
1✔
60
                        }
61

62
                        $query['WHERE'][0]['base_expr'] = '(' . trim($query['WHERE'][0]['base_expr']) . ')';
1✔
63
                }
64

65
                return $query;
1✔
66
        }
67

68
        public function getCountSelect(): string
69
        {
70
                $query = $this->query;
×
71

72
                $query['SELECT'] = [[
×
73
                        'expr_type' => 'aggregate_function',
74
                        'alias' => [
75
                                'as' => true,
76
                                'name' => 'count',
77
                                'base_expr' => 'AS count',
78
                                'no_quotes' => [
79
                                        'delim' => false,
80
                                        'parts' => ['count'],
81
                                ],
82
                        ],
83
                        'base_expr' => 'COUNT',
84
                        'sub_tree' => [[
85
                                'expr_type' => 'colref',
86
                                'base_expr' => '*',
87
                                'sub_tree' => false,
88
                        ]],
89
                ]];
90

91
                return $this->sqlCreator->create($query);
×
92
        }
93

94
        public function limit(int $limit, int $offset): string
95
        {
96
                $this->query['LIMIT'] = [
×
97
                        'offset' => $offset,
×
98
                        'rowcount' => $limit,
×
99
                ];
100

101
                return $this->sqlCreator->create($this->query);
×
102
        }
103

104
        public function orderBy(string $column, string $order): string
105
        {
106
                $this->query['ORDER'][] = [
1✔
107
                        'expr_type' => 'colref',
1✔
108
                        'base_expr' => $column,
1✔
109
                        'no_quotes' => [
110
                                'delim' => false,
111
                                'parts' => [$column],
1✔
112
                        ],
113
                        'subtree' => false,
114
                        'direction' => $order,
1✔
115
                ];
116

117
                return $this->sqlCreator->create($this->query);
1✔
118
        }
119

120
        /** @param mixed $value */
121
        public function where(string $column, $value, string $operator): string
122
        {
123
                if (!isset($this->query['WHERE'])) {
1✔
124
                        $this->query['WHERE'] = [];
1✔
125
                } else {
126
                        $this->query['WHERE'][] = [
1✔
127
                                'expr_type' => 'operator',
128
                                'base_expr' => 'AND',
129
                                'sub_tree' => false,
130
                        ];
131
                }
132

133
                /**
134
                 * Column
135
                 */
136
                if (strpos($column, '.') !== false) {
1✔
137
                        /**
138
                         * Column prepanded with table/alias
139
                         */
140
                        [$alias, $column] = explode('.', $column);
1✔
141

142
                        $this->query['WHERE'][] = [
1✔
143
                                'expr_type' => 'colref',
1✔
144
                                'base_expr' => "{$alias}.{$column}",
1✔
145
                                'no_quotes' => [
146
                                        'delim' => '.',
1✔
147
                                        'parts' => [$alias, $column],
1✔
148
                                ],
149
                                'sub_tree' => false,
150
                        ];
151
                } else {
152
                        /**
153
                         * Simple column name
154
                         */
155
                        $this->query['WHERE'][] = [
1✔
156
                                'expr_type' => 'colref',
1✔
157
                                'base_expr' => $column,
1✔
158
                                'no_quotes' => [
159
                                        'delim' => false,
160
                                        'parts' => [$column],
1✔
161
                                ],
162
                                'sub_tree' => false,
163
                        ];
164
                }
165

166
                /**
167
                 * =
168
                 */
169
                $this->query['WHERE'][] = [
1✔
170
                        'expr_type' => 'operator',
1✔
171
                        'base_expr' => $operator,
1✔
172
                        'sub_tree' => false,
173
                ];
174

175
                /**
176
                 * ?
177
                 *    ($value == '_?_')
178
                 */
179
                $this->query['WHERE'][] = [
1✔
180
                        'expr_type' => 'const',
1✔
181
                        'base_expr' => $value,
1✔
182
                        'sub_tree' => false,
183
                ];
184

185
                return $this->sqlCreator->create($this->query);
1✔
186
        }
187

188
        public function whereSql(string $sql): string
189
        {
190
                if (!isset($this->query['WHERE'])) {
1✔
191
                        $this->query['WHERE'] = [];
1✔
192
                } else {
193
                        $this->query['WHERE'][] = [
1✔
194
                                'expr_type' => 'operator',
195
                                'base_expr' => 'AND',
196
                                'sub_tree' => false,
197
                        ];
198
                }
199

200
                $help_sql = 'SELECT * FROM TEMP WHERE' . $sql;
1✔
201
                $help_query = $this->sqlParser->parse($help_sql);
1✔
202

203
                $this->query['WHERE'][] = $help_query['WHERE'][0];
1✔
204

205
                return $this->sqlCreator->create($this->query);
1✔
206
        }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc