Test Failed
Push — master ( 4153b6...1b303d )
by Petr
03:19
created

MariaDB::selectLimited()   C

Complexity

Conditions 14
Paths 40

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

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