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