MariaDb::selectLimited()   C
last analyzed

Complexity

Conditions 14
Paths 40

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 32
c 1
b 0
f 0
nc 40
nop 1
dl 0
loc 47
rs 6.2666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kalanis\nested_tree_nette\Sources\Nette;
4
5
use kalanis\nested_tree\Support;
6
7
/**
8
 * Implementation without ANY_VALUE which will cause problems on MariaDB servers
9
 * @codeCoverageIgnore cannot connect both MySQL and MariaDB and set the ONLY_FULL_GROUP_BY variable out on Maria.
10
 * Both Github and Scrutinizer have this problem.
11
 */
12
class MariaDb extends MySql
13
{
14
    public function selectCount(Support\Options $options) : int
15
    {
16
        $sql = 'SELECT ';
17
        $sql .= ' parent.' . $this->settings->idColumnName . ' AS p_cid';
18
        $sql .= ', parent.' . $this->settings->parentIdColumnName . ' AS p_pid';
19
        if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) {
20
            $sql .= ', child.' . $this->settings->idColumnName . ' AS `' . $this->settings->idColumnName . '`';
21
            $sql .= ', child.' . $this->settings->parentIdColumnName . ' AS `' . $this->settings->parentIdColumnName . '`';
22
            $sql .= ', child.' . $this->settings->leftColumnName . ' AS `' . $this->settings->leftColumnName . '`';
23
        }
24
        $sql .= $this->addAdditionalColumns($options);
25
        $sql .= ' FROM ' . $this->settings->tableName . ' AS parent';
26
27
        if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) {
28
            // if there is filter or search, there must be inner join to select all of filtered children.
29
            $sql .= ' INNER JOIN ' . $this->settings->tableName . ' AS child';
30
            $sql .= ' ON child.' . $this->settings->leftColumnName . ' BETWEEN parent.' . $this->settings->leftColumnName . ' AND parent.' . $this->settings->rightColumnName;
31
        }
32
33
        $sql .= ' WHERE 1';
34
        $params = [];
35
        $sql .= $this->addFilterBySql($params, $options);
36
        $sql .= $this->addCurrentIdSql($params, $options, 'parent.');
37
        $sql .= $this->addParentIdSql($params, $options, 'parent.');
38
        $sql .= $this->addSearchSql($params, $options, 'parent.');
39
        $sql .= $this->addCustomQuerySql($params, $options->where);
40
        $sql .= $this->addSortingSql($params, $options);
41
42
        // get 'total' count.
43
        $result = $this->database->fetchAll($sql, ...$params);
44
45
        // "a bit" hardcore - get all lines and then count them
46
        return $result ? count($result) : 0;
47
    }
48
49
    public function selectLimited(Support\Options $options) : array
50
    {
51
        $sql = 'SELECT';
52
        $sql .= ' parent.' . $this->settings->idColumnName . ' AS p_pid';
53
        $sql .= ', parent.' . $this->settings->parentIdColumnName . ' AS p_cid';
54
        $sql .= ', parent.' . $this->settings->leftColumnName . ' AS p_plt';
55
        if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) {
56
            $sql .= ', child.' . $this->settings->idColumnName . ' AS `' . $this->settings->idColumnName . '`';
57
            $sql .= ', child.' . $this->settings->parentIdColumnName . ' AS `' . $this->settings->parentIdColumnName . '`';
58
            $sql .= ', child.' . $this->settings->leftColumnName . ' AS `' . $this->settings->leftColumnName . '`';
59
            $sql .= ', child.' . $this->settings->rightColumnName . ' AS `' . $this->settings->rightColumnName . '`';
60
            $sql .= ', child.' . $this->settings->levelColumnName . ' AS `' . $this->settings->levelColumnName . '`';
61
            $sql .= ', child.' . $this->settings->positionColumnName . ' AS `' . $this->settings->positionColumnName . '`';
62
        }
63
        $sql .= $this->addAdditionalColumns($options);
64
        $sql .= ' FROM ' . $this->settings->tableName . ' AS parent';
65
66
        if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) {
67
            // if there is filter or search, there must be inner join to select all of filtered children.
68
            $sql .= ' INNER JOIN ' . $this->settings->tableName . ' AS child';
69
            $sql .= ' ON child.' . $this->settings->leftColumnName . ' BETWEEN parent.' . $this->settings->leftColumnName . ' AND parent.' . $this->settings->rightColumnName;
70
        }
71
72
        $sql .= ' WHERE 1';
73
        $params = [];
74
        $sql .= $this->addFilterBySql($params, $options);
75
        $sql .= $this->addCurrentIdSql($params, $options, 'parent.');
76
        $sql .= $this->addParentIdSql($params, $options, 'parent.');
77
        $sql .= $this->addSearchSql($params, $options, 'parent.');
78
        $sql .= $this->addCustomQuerySql($params, $options->where);
79
        $sql .= $this->addSortingSql($params, $options);
80
81
        // re-create query and prepare. second step is for set limit and fetch all items.
82
        if (!$options->unlimited) {
83
            if (empty($options->offset)) {
84
                $options->offset = 0;
85
            }
86
            if (empty($options->limit) || (10000 < $options->limit)) {
87
                $options->limit = 20;
88
            }
89
90
            $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit;
91
        }
92
93
        $result = $this->database->fetchAll($sql, ...$params);
94
95
        return $result ? $this->fromDbRows($result) : [];
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function selectWithParents(Support\Options $options) : array
102
    {
103
        $params = [];
104
        $sql = 'SELECT';
105
        $sql .= ' parent.' . $this->settings->idColumnName . ' AS `' . $this->settings->idColumnName . '`';
106
        $sql .= ', parent.' . $this->settings->parentIdColumnName . ' AS `' . $this->settings->parentIdColumnName . '`';
107
        $sql .= ', parent.' . $this->settings->leftColumnName . ' AS `' . $this->settings->leftColumnName . '`';
108
        $sql .= ', parent.' . $this->settings->rightColumnName . ' AS `' . $this->settings->rightColumnName . '`';
109
        $sql .= ', parent.' . $this->settings->levelColumnName . ' AS `' . $this->settings->levelColumnName . '`';
110
        $sql .= ', parent.' . $this->settings->positionColumnName . ' AS `' . $this->settings->positionColumnName . '`';
111
        $sql .= $this->addAdditionalColumns($options);
112
        $sql .= ' FROM ' . $this->settings->tableName . ' AS node,';
113
        $sql .= ' ' . $this->settings->tableName . ' AS parent';
114
        $sql .= ' WHERE';
115
        $sql .= ' (node.' . $this->settings->leftColumnName . ' BETWEEN parent.' . $this->settings->leftColumnName . ' AND parent.' . $this->settings->rightColumnName . ')';
116
        $sql .= $this->addCurrentIdSql($params, $options, 'node.');
117
        $sql .= $this->addSearchSql($params, $options, 'node.');
118
        $sql .= $this->addCustomQuerySql($params, $options->where);
119
        $sql .= ' GROUP BY parent.`' . $this->settings->idColumnName . '`';
120
        $sql .= ' ORDER BY parent.`' . $this->settings->leftColumnName . '`';
121
122
        $result = $this->database->fetchAll($sql, ...$params);
123
124
        if (empty($result)) {
125
            return [];
126
        }
127
        if ($options->skipCurrent) {
128
            unset($result[count($result)-1]);
129
        }
130
131
        return $this->fromDbRows($result);
132
    }
133
}
134