Passed
Push — master ( f7d6c6...2d02cb )
by Petr
02:57
created

MariaDB::selectLimited()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 40
c 1
b 0
f 1
dl 0
loc 56
rs 8.0355
cc 8
nc 40
nop 1

How to fix   Long Method   

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 . '` AS p_cid';
69
        $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '` AS p_pid';
70
        $sql .= ', `parent`.`' . $this->settings->leftColumnName . '` AS p_lf';
71
        if ($joinChild) {
72
            $sql .= ', `child`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`';
73
            $sql .= ', `child`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`';
74
            $sql .= ', `child`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`';
75
            $sql .= ', `child`.`' . $this->settings->rightColumnName . '` AS `' . $this->settings->rightColumnName . '`';
76
            $sql .= ', `child`.`' . $this->settings->levelColumnName . '` AS `' . $this->settings->levelColumnName . '`';
77
            $sql .= ', `child`.`' . $this->settings->positionColumnName . '` AS `' . $this->settings->positionColumnName . '`';
78
        }
79
        $sql .= $this->addAdditionalColumns($options);
80
        $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`';
81
82
        if ($joinChild) {
83
            // if there is filter or search, there must be inner join to select all of filtered children.
84
            $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`';
85
            $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`';
86
        }
87
88
        $sql .= ' WHERE TRUE';
89
        $sql .= $this->addFilterBy($options);
90
        $sql .= $this->addCurrentId($options, '`parent`.');
91
        $sql .= $this->addParentId($options, '`parent`.');
92
        $sql .= $this->addSearch($options, '`parent`.');
93
        $sql .= $this->addCustomQuery($options->where);
94
        $sql .= $this->addSoftDelete('`parent`.');
95
        $sql .= $this->addSorting($options);
96
97
        // re-create query and prepare. second step is for set limit and fetch all items.
98
        if (!$options->unlimited) {
99
            if (empty($options->offset)) {
100
                $options->offset = 0;
101
            }
102
            if (empty($options->limit) || (10000 < $options->limit)) {
103
                $options->limit = 20;
104
            }
105
106
            $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit;
107
        }
108
109
        $Sth = $this->pdo->prepare($sql);
110
        $this->bindCurrentId($options->currentId, $Sth);
111
        $this->bindParentId($options->parentId, $Sth, true);
112
        $this->bindSearch($options, $Sth);
113
        $this->bindCustomQuery($options->where, $Sth);
114
        $this->bindSoftDelete($Sth);
115
116
        $Sth->execute();
117
        $result = $Sth->fetchAll();
118
119
        return $result ? $this->fromDbRows($result, $joinChild) : [];
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function selectWithParents(Support\Options $options) : array
126
    {
127
        $sql = 'SELECT';
128
        $sql .= ' `parent`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`';
129
        $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`';
130
        $sql .= ', `parent`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`';
131
        $sql .= ', `parent`.`' . $this->settings->rightColumnName . '` AS `' . $this->settings->rightColumnName . '`';
132
        $sql .= ', `parent`.`' . $this->settings->levelColumnName . '` AS `' . $this->settings->levelColumnName . '`';
133
        $sql .= ', `parent`.`' . $this->settings->positionColumnName . '` AS `' . $this->settings->positionColumnName . '`';
134
        $sql .= $this->addAdditionalColumns($options);
135
        $sql .= ' FROM `' . $this->settings->tableName . '` AS `node`,';
136
        $sql .= ' `' . $this->settings->tableName . '` AS `parent`';
137
        $sql .= ' WHERE';
138
        $sql .= ' (`node`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`)';
139
        $sql .= $this->addCurrentId($options, '`node`.');
140
        $sql .= $this->addSearch($options, '`node`.');
141
        $sql .= $this->addCustomQuery($options->where);
142
        $sql .= $this->addSoftDelete('`node`.');
143
        $sql .= ' GROUP BY `parent`.`' . $this->settings->idColumnName . '`';
144
        $sql .= ' ORDER BY `parent`.`' . $this->settings->leftColumnName . '`';
145
146
        $Sth = $this->pdo->prepare($sql);
147
        $this->bindCurrentId($options->currentId, $Sth);
148
        $this->bindSearch($options, $Sth);
149
        $this->bindCustomQuery($options->where, $Sth);
150
        $this->bindSoftDelete($Sth);
151
152
        $Sth->execute();
153
        $result = $Sth->fetchAll();
154
        $Sth->closeCursor();
155
156
        if (empty($result)) {
157
            return [];
158
        }
159
        if ($options->skipCurrent) {
160
            unset($result[count($result)-1]);
161
        }
162
163
        return $this->fromDbRows($result);
164
    }
165
}
166