Passed
Push — master ( 2d02cb...58fbc2 )
by Petr
03:02
created

MariaDB::selectLimited()   F

Complexity

Conditions 14
Paths 2560

Size

Total Lines 59
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 43
c 1
b 0
f 1
dl 0
loc 59
rs 2.1
cc 14
nc 2560
nop 1

How to fix   Long Method    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\Sources\PDO;
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
    /**
15
     * {@inheritdoc}
16
     */
17
    public function selectCount(Support\Options $options) : int
18
    {
19
        $joinChild = $this->canJoinChild($options);
20
        $sql = 'SELECT ';
21
        $sql .= ' `parent`.`' . $this->settings->idColumnName . '` AS p_cid';
22
        $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '` AS p_pid';
23
        if ($this->settings->softDelete) {
24
            $sql .= ', `parent`.`' . $this->settings->softDelete->columnName . '`';
25
        }
26
        if ($joinChild) {
27
            $sql .= ', `child`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`';
28
            $sql .= ', `child`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`';
29
            $sql .= ', `child`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`';
30
        }
31
        $sql .= $this->addAdditionalColumns($options);
32
        $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`';
33
34
        if ($joinChild) {
35
            // if there is filter or search, there must be inner join to select all of filtered children.
36
            $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`';
37
            $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`';
38
        }
39
40
        $sql .= ' WHERE TRUE';
41
        $sql .= $this->addFilterBy($options);
42
        $sql .= $this->addCurrentId($options, '`parent`.');
43
        $sql .= $this->addParentId($options, '`parent`.');
44
        $sql .= $this->addSearch($options, '`parent`.');
45
        $sql .= $this->addCustomQuery($options->where);
46
        $sql .= $this->addSoftDelete('`parent`.');
47
        $sql .= $this->addSorting($options);
48
49
        // prepare and get 'total' count.
50
        $Sth = $this->pdo->prepare($sql);
51
        $this->bindCurrentId($options->currentId, $Sth);
52
        $this->bindParentId($options->parentId, $Sth, true);
53
        $this->bindSearch($options, $Sth);
54
        $this->bindCustomQuery($options->where, $Sth);
55
        $this->bindSoftDelete($Sth);
56
57
        $Sth->execute();
58
        $result = $Sth->fetchAll();
59
60
        // "a bit" hardcore - get all lines and then count them
61
        return $result ? count($result) : 0;
62
    }
63
64
    public function selectLimited(Support\Options $options) : array
65
    {
66
        $joinChild = $this->canJoinChild($options);
67
        $sql = 'SELECT';
68
        $sql .= ' `parent`.`' . $this->settings->idColumnName . '`' . ($joinChild ? '' : ' AS `' . $this->settings->idColumnName . '`');
69
        $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '`' . ($joinChild ? '' : ' AS `' . $this->settings->parentIdColumnName . '`');
70
        $sql .= ', `parent`.`' . $this->settings->leftColumnName . '`' . ($joinChild ? '' : ' AS `' . $this->settings->leftColumnName . '`');
71
        $sql .= ', `parent`.`' . $this->settings->rightColumnName . '`' . ($joinChild ? '' : ' AS `' . $this->settings->rightColumnName . '`');
72
        $sql .= ', `parent`.`' . $this->settings->levelColumnName . '`' . ($joinChild ? '' : ' AS `' . $this->settings->levelColumnName . '`');
73
        $sql .= ', `parent`.`' . $this->settings->positionColumnName . '`' . ($joinChild ? '' : ' AS `' . $this->settings->positionColumnName . '`');
74
        if ($joinChild) {
75
            $sql .= ', `child`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`';
76
            $sql .= ', `child`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`';
77
            $sql .= ', `child`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`';
78
            $sql .= ', `child`.`' . $this->settings->rightColumnName . '` AS `' . $this->settings->rightColumnName . '`';
79
            $sql .= ', `child`.`' . $this->settings->levelColumnName . '` AS `' . $this->settings->levelColumnName . '`';
80
            $sql .= ', `child`.`' . $this->settings->positionColumnName . '` AS `' . $this->settings->positionColumnName . '`';
81
        }
82
        $sql .= $this->addAdditionalColumns($options);
83
        $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`';
84
85
        if ($joinChild) {
86
            // if there is filter or search, there must be inner join to select all of filtered children.
87
            $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`';
88
            $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`';
89
        }
90
91
        $sql .= ' WHERE TRUE';
92
        $sql .= $this->addFilterBy($options);
93
        $sql .= $this->addCurrentId($options, '`parent`.');
94
        $sql .= $this->addParentId($options, '`parent`.');
95
        $sql .= $this->addSearch($options, '`parent`.');
96
        $sql .= $this->addCustomQuery($options->where);
97
        $sql .= $this->addSoftDelete('`parent`.');
98
        $sql .= $this->addSorting($options);
99
100
        // re-create query and prepare. second step is for set limit and fetch all items.
101
        if (!$options->unlimited) {
102
            if (empty($options->offset)) {
103
                $options->offset = 0;
104
            }
105
            if (empty($options->limit) || (10000 < $options->limit)) {
106
                $options->limit = 20;
107
            }
108
109
            $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit;
110
        }
111
112
        $Sth = $this->pdo->prepare($sql);
113
        $this->bindCurrentId($options->currentId, $Sth);
114
        $this->bindParentId($options->parentId, $Sth, true);
115
        $this->bindSearch($options, $Sth);
116
        $this->bindCustomQuery($options->where, $Sth);
117
        $this->bindSoftDelete($Sth);
118
119
        $Sth->execute();
120
        $result = $Sth->fetchAll();
121
122
        return $result ? $this->fromDbRows($result, $joinChild) : [];
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function selectWithParents(Support\Options $options) : array
129
    {
130
        $sql = 'SELECT';
131
        $sql .= ' `parent`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`';
132
        $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`';
133
        $sql .= ', `parent`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`';
134
        $sql .= ', `parent`.`' . $this->settings->rightColumnName . '` AS `' . $this->settings->rightColumnName . '`';
135
        $sql .= ', `parent`.`' . $this->settings->levelColumnName . '` AS `' . $this->settings->levelColumnName . '`';
136
        $sql .= ', `parent`.`' . $this->settings->positionColumnName . '` AS `' . $this->settings->positionColumnName . '`';
137
        $sql .= $this->addAdditionalColumns($options);
138
        $sql .= ' FROM `' . $this->settings->tableName . '` AS `node`,';
139
        $sql .= ' `' . $this->settings->tableName . '` AS `parent`';
140
        $sql .= ' WHERE';
141
        $sql .= ' (`node`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`)';
142
        $sql .= $this->addCurrentId($options, '`node`.');
143
        $sql .= $this->addSearch($options, '`node`.');
144
        $sql .= $this->addCustomQuery($options->where);
145
        $sql .= $this->addSoftDelete('`node`.');
146
        $sql .= ' GROUP BY `parent`.`' . $this->settings->idColumnName . '`';
147
        $sql .= ' ORDER BY `parent`.`' . $this->settings->leftColumnName . '`';
148
149
        $Sth = $this->pdo->prepare($sql);
150
        $this->bindCurrentId($options->currentId, $Sth);
151
        $this->bindSearch($options, $Sth);
152
        $this->bindCustomQuery($options->where, $Sth);
153
        $this->bindSoftDelete($Sth);
154
155
        $Sth->execute();
156
        $result = $Sth->fetchAll();
157
        $Sth->closeCursor();
158
159
        if (empty($result)) {
160
            return [];
161
        }
162
        if ($options->skipCurrent) {
163
            unset($result[count($result)-1]);
164
        }
165
166
        return $this->fromDbRows($result);
167
    }
168
}
169