alex-kalanis /
nested-tree
| 1 | <?php |
||
| 2 | |||
| 3 | namespace kalanis\nested_tree\Sources\PDO; |
||
| 4 | |||
| 5 | use kalanis\nested_tree\Support; |
||
| 6 | use PDO as base_pdo; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The default SQL implementation |
||
| 10 | */ |
||
| 11 | class MySql extends PDO |
||
| 12 | { |
||
| 13 | 10 | public function selectLastPosition(?Support\Node $parentNode, ?Support\Conditions $where = null) : ?int |
|
| 14 | { |
||
| 15 | 10 | $sql = 'SELECT `' . $this->settings->idColumnName . '`, `' . $this->settings->parentIdColumnName . '`, `' . $this->settings->positionColumnName . '`' |
|
| 16 | 10 | . ' FROM `' . $this->settings->tableName . '`' |
|
| 17 | 10 | . ' WHERE TRUE'; |
|
| 18 | 10 | if (is_null($parentNode?->id)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 19 | 3 | $sql .= ' AND `' . $this->settings->parentIdColumnName . '` IS NULL'; |
|
| 20 | } else { |
||
| 21 | 9 | $sql .= ' AND `' . $this->settings->parentIdColumnName . '` = :filter_parent_id'; |
|
| 22 | } |
||
| 23 | 10 | $sql .= $this->addCustomQuery($where, ''); |
|
| 24 | 10 | $sql .= $this->addSoftDelete(); |
|
| 25 | 10 | $sql .= ' ORDER BY `' . $this->settings->positionColumnName . '` DESC'; |
|
| 26 | |||
| 27 | 10 | $Sth = $this->pdo->prepare($sql); |
|
| 28 | 10 | if (!is_null($parentNode?->id)) { |
|
|
0 ignored issues
–
show
|
|||
| 29 | 9 | $this->bindParentId($parentNode->id, $Sth); |
|
| 30 | } |
||
| 31 | 10 | $this->bindCustomQuery($where, $Sth); |
|
| 32 | 10 | $this->bindSoftDelete($Sth); |
|
| 33 | |||
| 34 | 10 | $Sth->execute(); |
|
| 35 | /** @var array<string|int, mixed>|false $row */ |
||
| 36 | 10 | $row = $Sth->fetch(); |
|
| 37 | 10 | $Sth->closeCursor(); |
|
| 38 | |||
| 39 | 10 | if (!empty($row)) { |
|
| 40 | 10 | return is_null($row[$this->settings->positionColumnName]) ? null : max(1, intval($row[$this->settings->positionColumnName])); |
|
| 41 | } |
||
| 42 | |||
| 43 | 1 | return null; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 117 | public function selectSimple(Support\Options $options) : array |
|
| 50 | { |
||
| 51 | 117 | $sql = 'SELECT node.`' . $this->settings->idColumnName . '`' |
|
| 52 | 117 | . ', node.`' . $this->settings->parentIdColumnName . '`' |
|
| 53 | 117 | . ', node.`' . $this->settings->leftColumnName . '`' |
|
| 54 | 117 | . ', node.`' . $this->settings->rightColumnName . '`' |
|
| 55 | 117 | . ', node.`' . $this->settings->levelColumnName . '`' |
|
| 56 | 117 | . ', node.`' . $this->settings->positionColumnName . '`' |
|
| 57 | 117 | ; |
|
| 58 | 117 | $sql .= $this->addAdditionalColumns($options, 'node.'); |
|
| 59 | 117 | $sql .= ' FROM `' . $this->settings->tableName . '` node'; |
|
| 60 | 117 | $sql .= ' WHERE 1'; |
|
| 61 | 117 | $sql .= $this->addCurrentId($options, 'node.'); |
|
| 62 | 117 | $sql .= $this->addCustomQuery($options->where, 'node.'); |
|
| 63 | 117 | $sql .= $this->addSoftDelete('node.'); |
|
| 64 | 117 | $sql .= ' ORDER BY node.`' . $this->settings->positionColumnName . '` ASC'; |
|
| 65 | |||
| 66 | 117 | $Sth = $this->pdo->prepare($sql); |
|
| 67 | 117 | $this->bindCurrentId($options->currentId, $Sth); |
|
| 68 | 117 | $this->bindCustomQuery($options->where, $Sth); |
|
| 69 | 117 | $this->bindSoftDelete($Sth); |
|
| 70 | |||
| 71 | 117 | $Sth->execute(); |
|
| 72 | 117 | $result = $Sth->fetchAll(); |
|
| 73 | |||
| 74 | 117 | return $result ? $this->fromDbRows($result) : []; |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | 33 | public function selectParent(Support\Node $node, Support\Options $options) : ?Support\Node |
|
| 81 | { |
||
| 82 | 33 | if (is_null($node->parentId)) { |
|
| 83 | 1 | return null; |
|
| 84 | } |
||
| 85 | |||
| 86 | 32 | $sql = 'SELECT node.`' . $this->settings->idColumnName . '`' |
|
| 87 | 32 | . ', node.`' . $this->settings->parentIdColumnName . '`' |
|
| 88 | 32 | . ', node.`' . $this->settings->leftColumnName . '`' |
|
| 89 | 32 | . ', node.`' . $this->settings->rightColumnName . '`' |
|
| 90 | 32 | . ', node.`' . $this->settings->levelColumnName . '`' |
|
| 91 | 32 | . ', node.`' . $this->settings->positionColumnName . '`' |
|
| 92 | 32 | ; |
|
| 93 | 32 | $sql .= $this->addAdditionalColumns($options, 'node.'); |
|
| 94 | 32 | $sql .= ' FROM `' . $this->settings->tableName . '` node'; |
|
| 95 | 32 | $sql .= ' WHERE node.`' . $this->settings->idColumnName . '` = :filter_taxonomy_id'; |
|
| 96 | 32 | $sql .= $this->addCustomQuery($options->where, 'node.'); |
|
| 97 | 32 | $sql .= $this->addSoftDelete('node.'); |
|
| 98 | |||
| 99 | 32 | $Sth = $this->pdo->prepare($sql); |
|
| 100 | 32 | $this->bindCurrentId($node->parentId, $Sth); |
|
| 101 | 32 | $this->bindCustomQuery($options->where, $Sth); |
|
| 102 | 32 | $this->bindSoftDelete($Sth); |
|
| 103 | |||
| 104 | 32 | $Sth->execute(); |
|
| 105 | /** @var array<string|int, mixed>|false $row */ |
||
| 106 | 32 | $row = $Sth->fetch(); |
|
| 107 | 32 | $node = $row ? $this->fillDataFromRow($row) : null; |
|
| 108 | 32 | $Sth->closeCursor(); |
|
| 109 | |||
| 110 | 32 | if (empty($node)) { |
|
| 111 | 4 | return $this->settings->rootIsNull ? null : new Support\Node(); |
|
| 112 | } |
||
| 113 | |||
| 114 | 28 | return $node; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | 77 | public function selectCount(Support\Options $options) : int |
|
| 121 | { |
||
| 122 | 77 | $sql = 'SELECT '; |
|
| 123 | 77 | $sql .= ' ANY_VALUE(`parent`.`' . $this->settings->idColumnName . '`)'; |
|
| 124 | 77 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->parentIdColumnName . '`)'; |
|
| 125 | 77 | if ($this->settings->softDelete) { |
|
| 126 | 17 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->softDelete->columnName . '`)'; |
|
| 127 | } |
||
| 128 | 77 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
|
| 129 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->idColumnName . '`) AS `' . $this->settings->idColumnName . '`'; |
|
| 130 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->parentIdColumnName . '`) AS `' . $this->settings->parentIdColumnName . '`'; |
|
| 131 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->leftColumnName . '`) AS `' . $this->settings->leftColumnName . '`'; |
|
| 132 | } |
||
| 133 | 77 | $sql .= $this->addAdditionalColumns($options); |
|
| 134 | 77 | $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`'; |
|
| 135 | |||
| 136 | 77 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
|
| 137 | // if there is filter or search, there must be inner join to select all of filtered children. |
||
| 138 | 69 | $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`'; |
|
| 139 | 69 | $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`'; |
|
| 140 | } |
||
| 141 | |||
| 142 | 77 | $sql .= ' WHERE TRUE'; |
|
| 143 | 77 | $sql .= $this->addFilterBy($options); |
|
| 144 | 77 | $sql .= $this->addCurrentId($options, '`parent`.'); |
|
| 145 | 77 | $sql .= $this->addParentId($options, '`parent`.'); |
|
| 146 | 77 | $sql .= $this->addSearch($options, '`parent`.'); |
|
| 147 | 77 | $sql .= $this->addCustomQuery($options->where); |
|
| 148 | 77 | $sql .= $this->addSoftDelete('`parent`.'); |
|
| 149 | 77 | $sql .= $this->addSorting($options); |
|
| 150 | |||
| 151 | // prepare and get 'total' count. |
||
| 152 | 77 | $Sth = $this->pdo->prepare($sql); |
|
| 153 | 77 | $this->bindCurrentId($options->currentId, $Sth); |
|
| 154 | 77 | $this->bindParentId($options->parentId, $Sth, true); |
|
| 155 | 77 | $this->bindSearch($options, $Sth); |
|
| 156 | 77 | $this->bindCustomQuery($options->where, $Sth); |
|
| 157 | 77 | $this->bindSoftDelete($Sth); |
|
| 158 | |||
| 159 | 77 | $Sth->execute(); |
|
| 160 | 77 | $result = $Sth->fetchAll(); |
|
| 161 | |||
| 162 | // "a bit" hardcore - get all lines and then count them |
||
| 163 | // that's due problems with COUNT() aggregator in MySQL |
||
| 164 | 77 | return $result ? count($result) : 0; |
|
| 165 | } |
||
| 166 | |||
| 167 | 77 | public function selectLimited(Support\Options $options) : array |
|
| 168 | { |
||
| 169 | 77 | $sql = 'SELECT'; |
|
| 170 | 77 | $sql .= ' ANY_VALUE(`parent`.`' . $this->settings->idColumnName . '`)'; |
|
| 171 | 77 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->parentIdColumnName . '`)'; |
|
| 172 | 77 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->leftColumnName . '`)'; |
|
| 173 | 77 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
|
| 174 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->idColumnName . '`) AS `' . $this->settings->idColumnName . '`'; |
|
| 175 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->parentIdColumnName . '`) AS `' . $this->settings->parentIdColumnName . '`'; |
|
| 176 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->leftColumnName . '`) AS `' . $this->settings->leftColumnName . '`'; |
|
| 177 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->rightColumnName . '`) AS `' . $this->settings->rightColumnName . '`'; |
|
| 178 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->levelColumnName . '`) AS `' . $this->settings->levelColumnName . '`'; |
|
| 179 | 69 | $sql .= ', ANY_VALUE(`child`.`' . $this->settings->positionColumnName . '`) AS `' . $this->settings->positionColumnName . '`'; |
|
| 180 | } |
||
| 181 | 77 | $sql .= $this->addAdditionalColumns($options); |
|
| 182 | 77 | $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`'; |
|
| 183 | |||
| 184 | 77 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
|
| 185 | // if there is filter or search, there must be inner join to select all of filtered children. |
||
| 186 | 69 | $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`'; |
|
| 187 | 69 | $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`'; |
|
| 188 | } |
||
| 189 | |||
| 190 | 77 | $sql .= ' WHERE TRUE'; |
|
| 191 | 77 | $sql .= $this->addFilterBy($options); |
|
| 192 | 77 | $sql .= $this->addCurrentId($options, '`parent`.'); |
|
| 193 | 77 | $sql .= $this->addParentId($options, '`parent`.'); |
|
| 194 | 77 | $sql .= $this->addSearch($options, '`parent`.'); |
|
| 195 | 77 | $sql .= $this->addCustomQuery($options->where); |
|
| 196 | 77 | $sql .= $this->addSoftDelete('`parent`.'); |
|
| 197 | 77 | $sql .= $this->addSorting($options); |
|
| 198 | |||
| 199 | // re-create query and prepare. second step is for set limit and fetch all items. |
||
| 200 | 77 | if (!$options->unlimited) { |
|
| 201 | 38 | if (empty($options->offset)) { |
|
| 202 | 38 | $options->offset = 0; |
|
| 203 | } |
||
| 204 | 38 | if (empty($options->limit) || (10000 < $options->limit)) { |
|
| 205 | 38 | $options->limit = 20; |
|
| 206 | } |
||
| 207 | |||
| 208 | 38 | $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit; |
|
| 209 | } |
||
| 210 | |||
| 211 | 77 | $Sth = $this->pdo->prepare($sql); |
|
| 212 | 77 | $this->bindCurrentId($options->currentId, $Sth); |
|
| 213 | 77 | $this->bindParentId($options->parentId, $Sth, true); |
|
| 214 | 77 | $this->bindSearch($options, $Sth); |
|
| 215 | 77 | $this->bindCustomQuery($options->where, $Sth); |
|
| 216 | 77 | $this->bindSoftDelete($Sth); |
|
| 217 | |||
| 218 | 77 | $Sth->execute(); |
|
| 219 | 77 | $result = $Sth->fetchAll(); |
|
| 220 | |||
| 221 | 77 | return $result ? $this->fromDbRows($result) : []; |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | 18 | public function selectWithParents(Support\Options $options) : array |
|
| 228 | { |
||
| 229 | 18 | $sql = 'SELECT'; |
|
| 230 | 18 | $sql .= ' ANY_VALUE(`parent`.`' . $this->settings->idColumnName . '`) AS `' . $this->settings->idColumnName . '`'; |
|
| 231 | 18 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->parentIdColumnName . '`) AS `' . $this->settings->parentIdColumnName . '`'; |
|
| 232 | 18 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->leftColumnName . '`) AS `' . $this->settings->leftColumnName . '`'; |
|
| 233 | 18 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->rightColumnName . '`) AS `' . $this->settings->rightColumnName . '`'; |
|
| 234 | 18 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->levelColumnName . '`) AS `' . $this->settings->levelColumnName . '`'; |
|
| 235 | 18 | $sql .= ', ANY_VALUE(`parent`.`' . $this->settings->positionColumnName . '`) AS `' . $this->settings->positionColumnName . '`'; |
|
| 236 | 18 | $sql .= $this->addAdditionalColumns($options); |
|
| 237 | 18 | $sql .= ' FROM `' . $this->settings->tableName . '` AS `node`,'; |
|
| 238 | 18 | $sql .= ' `' . $this->settings->tableName . '` AS `parent`'; |
|
| 239 | 18 | $sql .= ' WHERE'; |
|
| 240 | 18 | $sql .= ' (`node`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`)'; |
|
| 241 | 18 | $sql .= $this->addCurrentId($options, '`node`.'); |
|
| 242 | 18 | $sql .= $this->addSearch($options, '`node`.'); |
|
| 243 | 18 | $sql .= $this->addCustomQuery($options->where); |
|
| 244 | 18 | $sql .= $this->addSoftDelete('`node`.'); |
|
| 245 | 18 | $sql .= ' GROUP BY `parent`.`' . $this->settings->idColumnName . '`'; |
|
| 246 | 18 | $sql .= ' ORDER BY `parent`.`' . $this->settings->leftColumnName . '`'; |
|
| 247 | |||
| 248 | 18 | $Sth = $this->pdo->prepare($sql); |
|
| 249 | 18 | $this->bindCurrentId($options->currentId, $Sth); |
|
| 250 | 18 | $this->bindSearch($options, $Sth); |
|
| 251 | 18 | $this->bindCustomQuery($options->where, $Sth); |
|
| 252 | 18 | $this->bindSoftDelete($Sth); |
|
| 253 | |||
| 254 | 18 | $Sth->execute(); |
|
| 255 | 18 | $result = $Sth->fetchAll(); |
|
| 256 | 18 | $Sth->closeCursor(); |
|
| 257 | |||
| 258 | 18 | if (empty($result)) { |
|
| 259 | 2 | return []; |
|
| 260 | } |
||
| 261 | 18 | if ($options->skipCurrent) { |
|
| 262 | 2 | unset($result[count($result)-1]); |
|
| 263 | } |
||
| 264 | |||
| 265 | 18 | return $this->fromDbRows($result); |
|
| 266 | } |
||
| 267 | |||
| 268 | 2 | public function add(Support\Node $node, ?Support\Conditions $where = null) : Support\Node |
|
| 269 | { |
||
| 270 | // Insert itself |
||
| 271 | 2 | $sql = 'INSERT INTO `' . $this->settings->tableName . '`'; |
|
| 272 | 2 | $lookup = []; |
|
| 273 | 2 | $pairs = []; |
|
| 274 | 2 | foreach ((array) $node as $column => $value) { |
|
| 275 | if ( |
||
| 276 | 2 | !is_numeric($column) |
|
| 277 | 2 | && !$this->isColumnNameFromBasic($column) |
|
| 278 | ) { |
||
| 279 | 2 | $translateColumn = $this->translateColumn($this->settings, $column); |
|
| 280 | 2 | if (!is_null($translateColumn)) { |
|
| 281 | 2 | $lookup['`' . $translateColumn . '`'] = ':' . $translateColumn; |
|
| 282 | 2 | $pairs[':' . $translateColumn] = $value; |
|
| 283 | } |
||
| 284 | } |
||
| 285 | } |
||
| 286 | 2 | if (empty($lookup)) { |
|
| 287 | // @codeCoverageIgnoreStart |
||
| 288 | // when this happens it is problem with inserted data, not with library |
||
| 289 | throw new \RuntimeException('No lookup data!'); |
||
| 290 | } |
||
| 291 | // @codeCoverageIgnoreEnd |
||
| 292 | |||
| 293 | 2 | $sql .= '(' . implode(',', array_keys($lookup)) . ')'; |
|
| 294 | 2 | $sql .= ' VALUES (' . implode(',', array_keys($pairs)) . ')'; |
|
| 295 | |||
| 296 | 2 | $Sth = $this->pdo->prepare($sql); |
|
| 297 | |||
| 298 | 2 | foreach ($pairs as $column => $value) { |
|
| 299 | 2 | $Sth->bindValue($column, $value); |
|
| 300 | } |
||
| 301 | |||
| 302 | 2 | $execute = $Sth->execute(); |
|
| 303 | 2 | if (!$execute) { |
|
| 304 | // @codeCoverageIgnoreStart |
||
| 305 | // when this happens it is problem with DB, not with library |
||
| 306 | throw new \RuntimeException('Cannot save!'); |
||
| 307 | } |
||
| 308 | // @codeCoverageIgnoreEnd |
||
| 309 | 2 | $Sth->closeCursor(); |
|
| 310 | |||
| 311 | // Now get it back |
||
| 312 | 2 | $sql = 'SELECT * FROM `' . $this->settings->tableName . '` WHERE 1'; |
|
| 313 | 2 | foreach ($lookup as $column => $bind) { |
|
| 314 | 2 | if (is_null($pairs[$bind])) { |
|
| 315 | 1 | $sql .= ' AND ' . $column . ' IS NULL'; |
|
| 316 | } else { |
||
| 317 | 2 | $sql .= ' AND ' . $column . ' = ' . $bind; |
|
| 318 | } |
||
| 319 | } |
||
| 320 | 2 | $Lth = $this->pdo->prepare($sql); |
|
| 321 | 2 | foreach ($pairs as $column => $value) { |
|
| 322 | 2 | if (!is_null($value)) { |
|
| 323 | 2 | $Lth->bindValue($column, $value); |
|
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | 2 | $Lth->execute(); |
|
| 328 | /** @var array<string|int, mixed>|false $row */ |
||
| 329 | 2 | $row = $Lth->fetch(); |
|
| 330 | 2 | $node = $row ? $this->fillDataFromRow($row) : null; |
|
| 331 | 2 | $Lth->closeCursor(); |
|
| 332 | |||
| 333 | 2 | if (is_null($node)) { |
|
| 334 | // @codeCoverageIgnoreStart |
||
| 335 | // when this happens it is problem with DB, not with library |
||
| 336 | throw new \RuntimeException('Node not found in database'); |
||
| 337 | } |
||
| 338 | // @codeCoverageIgnoreEnd |
||
| 339 | |||
| 340 | 2 | return $node; |
|
| 341 | } |
||
| 342 | |||
| 343 | 4 | public function updateData(Support\Node $node, ?Support\Conditions $where = null) : bool |
|
| 344 | { |
||
| 345 | 4 | $sql = 'UPDATE `' . $this->settings->tableName . '`'; |
|
| 346 | 4 | $sql .= ' SET '; |
|
| 347 | 4 | $pairs = []; |
|
| 348 | 4 | $lookup = []; |
|
| 349 | 4 | foreach ((array) $node as $column => $value) { |
|
| 350 | if ( |
||
| 351 | 4 | !is_numeric($column) |
|
| 352 | 4 | && !$this->isColumnNameFromBasic($column) |
|
| 353 | 4 | && !$this->isColumnNameFromTree($column) |
|
| 354 | ) { |
||
| 355 | 4 | $translateColumn = $this->translateColumn($this->settings, $column); |
|
| 356 | 4 | if (!is_null($translateColumn)) { |
|
| 357 | 3 | $lookup[] = '`' . $translateColumn . '` = :' . $translateColumn; |
|
| 358 | 3 | $pairs[':' . $translateColumn] = $value; |
|
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 | 4 | if (empty($lookup)) { |
|
| 363 | 1 | return false; |
|
| 364 | } |
||
| 365 | 3 | $sql .= implode(',', $lookup); |
|
| 366 | 3 | $sql .= ' WHERE `' . $this->settings->idColumnName . '` = :id'; |
|
| 367 | |||
| 368 | 3 | $sql .= $this->addCustomQuery($where, ''); |
|
| 369 | 3 | $sql .= $this->addSoftDelete(); |
|
| 370 | 3 | $Sth = $this->pdo->prepare($sql); |
|
| 371 | |||
| 372 | 3 | $Sth->bindValue(':id', $node->id, base_pdo::PARAM_INT); |
|
| 373 | 3 | foreach ($pairs as $column => $value) { |
|
| 374 | 3 | $Sth->bindValue($column, $value); |
|
| 375 | } |
||
| 376 | 3 | $this->bindCustomQuery($where, $Sth); |
|
| 377 | 3 | $this->bindSoftDelete($Sth); |
|
| 378 | |||
| 379 | 3 | $execute = $Sth->execute(); |
|
| 380 | 3 | $Sth->closeCursor(); |
|
| 381 | |||
| 382 | 3 | return $execute; |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * {@inheritdoc} |
||
| 387 | */ |
||
| 388 | 30 | public function updateNodeParent(Support\Node $node, ?Support\Node $parent, int $position, ?Support\Conditions $where = null) : bool |
|
| 389 | { |
||
| 390 | 30 | $parent = $parent ?: ($this->settings->rootIsNull ? null : new Support\Node()); |
|
| 391 | |||
| 392 | 30 | $sql = 'UPDATE `' . $this->settings->tableName . '`'; |
|
| 393 | 30 | $sql .= ' SET `' . $this->settings->parentIdColumnName . '` = :filter_parent_id'; |
|
| 394 | 30 | $sql .= ' , `' . $this->settings->positionColumnName . '` = :position'; |
|
| 395 | 30 | $sql .= ' WHERE `' . $this->settings->idColumnName . '` = :filter_taxonomy_id'; |
|
| 396 | 30 | $sql .= $this->addCustomQuery($where, ''); |
|
| 397 | 30 | $sql .= $this->addSoftDelete(''); |
|
| 398 | |||
| 399 | 30 | $Sth = $this->pdo->prepare($sql); |
|
| 400 | 30 | $this->bindParentId($parent?->id, $Sth); |
|
| 401 | 30 | $Sth->bindValue(':position', $position, base_pdo::PARAM_INT); |
|
| 402 | 30 | $this->bindCurrentId($node->id, $Sth); |
|
| 403 | 30 | $this->bindCustomQuery($where, $Sth); |
|
| 404 | 30 | $this->bindSoftDelete($Sth); |
|
| 405 | |||
| 406 | 30 | $execute = $Sth->execute(); |
|
| 407 | 30 | $Sth->closeCursor(); |
|
| 408 | |||
| 409 | 30 | return $execute; |
|
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 415 | 7 | public function updateChildrenParent(Support\Node $node, ?Support\Node $parent, ?Support\Conditions $where = null) : bool |
|
| 416 | { |
||
| 417 | 7 | $parent = $parent ?: ($this->settings->rootIsNull ? null : new Support\Node()); |
|
| 418 | |||
| 419 | 7 | $sql = 'UPDATE `' . $this->settings->tableName . '`'; |
|
| 420 | 7 | $sql .= ' SET `' . $this->settings->parentIdColumnName . '` = :filter_parent_id'; |
|
| 421 | 7 | $sql .= ' WHERE `' . $this->settings->parentIdColumnName . '` = :filter_taxonomy_id'; |
|
| 422 | 7 | $sql .= $this->addCustomQuery($where, ''); |
|
| 423 | 7 | $sql .= $this->addSoftDelete(); |
|
| 424 | |||
| 425 | 7 | $Sth = $this->pdo->prepare($sql); |
|
| 426 | 7 | $this->bindParentId($parent?->id, $Sth); |
|
| 427 | 7 | $this->bindCurrentId($node->id, $Sth); |
|
| 428 | 7 | $this->bindCustomQuery($where, $Sth); |
|
| 429 | 7 | $this->bindSoftDelete($Sth); |
|
| 430 | |||
| 431 | 7 | $execute = $Sth->execute(); |
|
| 432 | 7 | $Sth->closeCursor(); |
|
| 433 | |||
| 434 | 7 | return $execute; |
|
| 435 | } |
||
| 436 | |||
| 437 | 113 | public function updateLeftRightPos(Support\Node $row, ?Support\Conditions $where = null) : bool |
|
| 438 | { |
||
| 439 | 113 | $sql = 'UPDATE `' . $this->settings->tableName . '`'; |
|
| 440 | 113 | $sql .= ' SET'; |
|
| 441 | 113 | $sql .= ' `' . $this->settings->levelColumnName . '` = :level,'; |
|
| 442 | 113 | $sql .= ' `' . $this->settings->leftColumnName . '` = :left,'; |
|
| 443 | 113 | $sql .= ' `' . $this->settings->rightColumnName . '` = :right,'; |
|
| 444 | 113 | $sql .= ' `' . $this->settings->positionColumnName . '` = :pos'; |
|
| 445 | 113 | $sql .= ' WHERE `' . $this->settings->idColumnName . '` = :id'; |
|
| 446 | 113 | $sql .= $this->addCustomQuery($where, ''); |
|
| 447 | 113 | $sql .= $this->addSoftDelete(); |
|
| 448 | |||
| 449 | 113 | $Sth = $this->pdo->prepare($sql); |
|
| 450 | 113 | $Sth->bindValue(':level', $row->level, base_pdo::PARAM_INT); |
|
| 451 | 113 | $Sth->bindValue(':left', $row->left, base_pdo::PARAM_INT); |
|
| 452 | 113 | $Sth->bindValue(':right', $row->right, base_pdo::PARAM_INT); |
|
| 453 | 113 | $Sth->bindValue(':pos', $row->position, base_pdo::PARAM_INT); |
|
| 454 | 113 | $Sth->bindValue(':id', $row->id, base_pdo::PARAM_INT); |
|
| 455 | 113 | $this->bindCustomQuery($where, $Sth); |
|
| 456 | 113 | $this->bindSoftDelete($Sth); |
|
| 457 | |||
| 458 | 113 | $execute = $Sth->execute(); |
|
| 459 | 113 | $Sth->closeCursor(); |
|
| 460 | |||
| 461 | 113 | return $execute; |
|
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * {@inheritdoc} |
||
| 466 | */ |
||
| 467 | 26 | public function makeHole(?Support\Node $parent, int $position, bool $moveUp, ?Support\Conditions $where = null) : bool |
|
| 468 | { |
||
| 469 | 26 | $parent = $parent ?: ($this->settings->rootIsNull ? null : new Support\Node()); |
|
| 470 | 26 | $direction = $moveUp ? '-' : '+'; |
|
| 471 | 26 | $compare = $moveUp ? '<=' : '>='; |
|
| 472 | 26 | $sql = 'UPDATE `' . $this->settings->tableName . '`'; |
|
| 473 | 26 | $sql .= ' SET `' . $this->settings->positionColumnName . '` = `' . $this->settings->positionColumnName . '` ' . $direction . ' 1'; |
|
| 474 | 26 | if (is_null($parent)) { |
|
| 475 | 1 | $sql .= ' WHERE `' . $this->settings->parentIdColumnName . '` IS NULL'; |
|
| 476 | } else { |
||
| 477 | 25 | $sql .= ' WHERE `' . $this->settings->parentIdColumnName . '` = :filter_parent_id'; |
|
| 478 | } |
||
| 479 | 26 | $sql .= ' AND `' . $this->settings->positionColumnName . '` ' . $compare . ' :position'; |
|
| 480 | |||
| 481 | 26 | $sql .= $this->addCustomQuery($where, ''); |
|
| 482 | 26 | $sql .= $this->addSoftDelete(); |
|
| 483 | 26 | $Sth = $this->pdo->prepare($sql); |
|
| 484 | |||
| 485 | 26 | $Sth->bindValue(':position', $position, base_pdo::PARAM_INT); |
|
| 486 | 26 | if (!is_null($parent)) { |
|
| 487 | 25 | $this->bindParentId($parent->id, $Sth); |
|
| 488 | } |
||
| 489 | 26 | $this->bindCustomQuery($where, $Sth); |
|
| 490 | 26 | $this->bindSoftDelete($Sth); |
|
| 491 | |||
| 492 | 26 | $execute = $Sth->execute(); |
|
| 493 | 26 | $Sth->closeCursor(); |
|
| 494 | |||
| 495 | 26 | return $execute; |
|
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * {@inheritdoc} |
||
| 500 | */ |
||
| 501 | 11 | public function deleteSolo(Support\Node $node, ?Support\Conditions $where = null) : bool |
|
| 502 | { |
||
| 503 | 11 | if ($this->settings->softDelete) { |
|
| 504 | 3 | $sql = 'UPDATE `' . $this->settings->tableName . '`' |
|
| 505 | 3 | . ' SET `' . $this->settings->softDelete->columnName . '` = ' . $this->settings->softDelete->bindAsKey . '_mk' |
|
| 506 | 3 | . ' WHERE `' . $this->settings->idColumnName . '` = :filter_taxonomy_id'; |
|
| 507 | 3 | $sql .= $this->addCustomQuery($where, ''); |
|
| 508 | 3 | $sql .= $this->addSoftDelete(); |
|
| 509 | 3 | $Sth = $this->pdo->prepare($sql); |
|
| 510 | |||
| 511 | 3 | $Sth->bindValue($this->settings->softDelete->bindAsKey . '_mk', $this->settings->softDelete->isDeleted); |
|
| 512 | 3 | $this->bindCurrentId($node->id, $Sth); |
|
| 513 | 3 | $this->bindCustomQuery($where, $Sth); |
|
| 514 | 3 | $this->bindSoftDelete($Sth); |
|
| 515 | |||
| 516 | 3 | $execute = $Sth->execute(); |
|
| 517 | 3 | $Sth->closeCursor(); |
|
| 518 | |||
| 519 | 3 | return $execute; |
|
| 520 | } |
||
| 521 | |||
| 522 | // delete the selected taxonomy ID |
||
| 523 | 8 | $sql = 'DELETE FROM `' . $this->settings->tableName . '` WHERE `' . $this->settings->idColumnName . '` = :filter_taxonomy_id'; |
|
| 524 | 8 | $sql .= $this->addCustomQuery($where, ''); |
|
| 525 | 8 | $Sth = $this->pdo->prepare($sql); |
|
| 526 | |||
| 527 | 8 | $this->bindCurrentId($node->id, $Sth); |
|
| 528 | 8 | $this->bindCustomQuery($where, $Sth); |
|
| 529 | |||
| 530 | 8 | $execute = $Sth->execute(); |
|
| 531 | 8 | $Sth->closeCursor(); |
|
| 532 | |||
| 533 | 8 | return $execute; |
|
| 534 | } |
||
| 535 | |||
| 536 | 58 | protected function replaceColumns(string $query, string $byWhat = '') : string |
|
| 537 | { |
||
| 538 | 58 | foreach (['`parent`.', '`child`.', '`node`.', 'parent.', 'child.', 'node.'] as $toReplace) { |
|
| 539 | 58 | $query = str_replace($toReplace, $byWhat, $query); |
|
| 540 | } |
||
| 541 | |||
| 542 | 58 | return $query; |
|
| 543 | } |
||
| 544 | |||
| 545 | 117 | protected function addAdditionalColumns(Support\Options $options, ?string $replaceName = null) : string |
|
| 546 | { |
||
| 547 | 117 | $sql = ''; |
|
| 548 | 117 | if (!empty($options->additionalColumns)) { |
|
| 549 | 75 | foreach ($options->additionalColumns as $column) { |
|
| 550 | 75 | $sql .= ', ' . (!is_null($replaceName) ? $this->replaceColumns($column, $replaceName) : $column); |
|
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | 117 | return $sql; |
|
| 555 | } |
||
| 556 | |||
| 557 | 77 | protected function addFilterBy(Support\Options $options) : string |
|
| 558 | { |
||
| 559 | 77 | $sql = ''; |
|
| 560 | 77 | if (!empty($options->filterIdBy)) { |
|
| 561 | // Due to IN() and NOT IN() cannot using bindValue directly. |
||
| 562 | // read more at http://stackoverflow.com/questions/17746667/php-pdo-for-not-in-query-in-mysql |
||
| 563 | // and http://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition |
||
| 564 | // it is possible to go around that, but it needs a bit more tinkering |
||
| 565 | |||
| 566 | // loop remove non-number for safety. |
||
| 567 | 4 | foreach ($options->filterIdBy as $key => $eachNodeId) { |
|
| 568 | 4 | if (!is_numeric($eachNodeId) || intval($eachNodeId) !== intval($eachNodeId)) { |
|
| 569 | 2 | unset($options->filterIdBy[$key]); |
|
| 570 | } |
||
| 571 | } |
||
| 572 | |||
| 573 | // build value for use with `IN()` function. Example: 1,3,4,5. |
||
| 574 | 4 | $nodeIdIn = implode(',', $options->filterIdBy); |
|
| 575 | 4 | $sql .= ' AND `parent`.`' . $this->settings->idColumnName . '` IN (' . $nodeIdIn . ')'; |
|
| 576 | } |
||
| 577 | |||
| 578 | 77 | return $sql; |
|
| 579 | } |
||
| 580 | |||
| 581 | 117 | protected function addCurrentId(Support\Options $options, string $dbPrefix = '') : string |
|
| 582 | { |
||
| 583 | 117 | $sql = ''; |
|
| 584 | 117 | if (!is_null($options->currentId)) { |
|
| 585 | 71 | $sql .= ' AND ' . $dbPrefix . '`' . $this->settings->idColumnName . '` = :filter_taxonomy_id'; |
|
| 586 | } |
||
| 587 | |||
| 588 | 117 | return $sql; |
|
| 589 | } |
||
| 590 | |||
| 591 | 77 | protected function addParentId(Support\Options $options, string $dbPrefix = '') : string |
|
| 592 | { |
||
| 593 | 77 | $sql = ''; |
|
| 594 | 77 | if (!is_null($options->parentId)) { |
|
| 595 | 2 | $sql .= ' AND ' . $dbPrefix . '`' . $this->settings->parentIdColumnName . '` = :filter_parent_id'; |
|
| 596 | } |
||
| 597 | |||
| 598 | 77 | return $sql; |
|
| 599 | } |
||
| 600 | |||
| 601 | 117 | protected function bindCurrentId(?int $currentId, \PDOStatement $pdo) : void |
|
| 602 | { |
||
| 603 | 117 | if (!is_null($currentId)) { |
|
| 604 | 71 | $pdo->bindValue(':filter_taxonomy_id', $currentId, base_pdo::PARAM_INT); |
|
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | 84 | protected function bindParentId(?int $parentId, \PDOStatement $pdo, bool $skipNull = false) : void |
|
| 609 | { |
||
| 610 | 84 | if (is_null($parentId) && !$skipNull) { |
|
| 611 | 1 | $pdo->bindValue(':filter_parent_id', null, base_pdo::PARAM_NULL); |
|
| 612 | 84 | } elseif (!is_null($parentId)) { |
|
| 613 | 43 | $pdo->bindValue(':filter_parent_id', $parentId, base_pdo::PARAM_INT); |
|
| 614 | } |
||
| 615 | } |
||
| 616 | |||
| 617 | 91 | protected function addSearch(Support\Options $options, string $dbPrefix = '') : string |
|
| 618 | { |
||
| 619 | 91 | $sql = ''; |
|
| 620 | if ( |
||
| 621 | 91 | !empty($options->search->columns) |
|
| 622 | 91 | && !empty($options->search->value) |
|
| 623 | ) { |
||
| 624 | 6 | $sql .= ' AND ('; |
|
| 625 | 6 | $array_keys = array_keys($options->search->columns); |
|
| 626 | 6 | $last_array_key = array_pop($array_keys); |
|
| 627 | 6 | foreach ($options->search->columns as $key => $column) { |
|
| 628 | 6 | $sql .= $dbPrefix . '`' . $column . '` LIKE :search'; |
|
| 629 | 6 | if ($key !== $last_array_key) { |
|
| 630 | 1 | $sql .= ' OR '; |
|
| 631 | } |
||
| 632 | } |
||
| 633 | 6 | $sql .= ')'; |
|
| 634 | } |
||
| 635 | |||
| 636 | 91 | return $sql; |
|
| 637 | } |
||
| 638 | |||
| 639 | 91 | protected function bindSearch(Support\Options $options, \PDOStatement $pdo) : void |
|
| 640 | { |
||
| 641 | 91 | if (!empty($options->search->value)) { |
|
| 642 | 6 | $pdo->bindValue(':search', '%' . $options->search->value . '%'); |
|
| 643 | } |
||
| 644 | } |
||
| 645 | |||
| 646 | 117 | protected function addCustomQuery(?Support\Conditions $where, ?string $replaceName = null, bool $clearName = false) : string |
|
| 647 | { |
||
| 648 | 117 | if (!empty($where->query)) { |
|
| 649 | 64 | $query = ( |
|
| 650 | 64 | !is_null($replaceName) |
|
| 651 | 58 | ? $this->replaceColumns($where->query, $replaceName) |
|
| 652 | 41 | : ($clearName ? $this->replaceColumns($where->query) : $where->query) |
|
| 653 | 64 | ); |
|
| 654 | 64 | if (!empty(trim($query))) { |
|
| 655 | 64 | return ' AND ' . $query; |
|
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | 82 | return ''; |
|
| 660 | } |
||
| 661 | |||
| 662 | 117 | protected function bindCustomQuery(?Support\Conditions $where, \PDOStatement $pdo) : void |
|
| 663 | { |
||
| 664 | 117 | if (!empty($where->bindValues)) { |
|
| 665 | 64 | foreach ($where->bindValues as $bindName => $bindValue) { |
|
| 666 | 64 | $pdo->bindValue($bindName, $bindValue); |
|
| 667 | } |
||
| 668 | } |
||
| 669 | } |
||
| 670 | |||
| 671 | 117 | protected function addSoftDelete(string $dbPrefix = '') : string |
|
| 672 | { |
||
| 673 | 117 | $sql = ''; |
|
| 674 | 117 | if (!is_null($this->settings->softDelete)) { |
|
| 675 | 27 | $sql .= ' AND ' . $dbPrefix . '`' . $this->settings->softDelete->columnName . '` = ' . $this->settings->softDelete->bindAsKey; |
|
| 676 | } |
||
| 677 | |||
| 678 | 117 | return $sql; |
|
| 679 | } |
||
| 680 | |||
| 681 | 117 | protected function bindSoftDelete(\PDOStatement $pdo) : void |
|
| 682 | { |
||
| 683 | 117 | if (!empty($this->settings->softDelete)) { |
|
| 684 | 27 | $pdo->bindValue( |
|
| 685 | 27 | $this->settings->softDelete->bindAsKey, |
|
| 686 | 27 | $this->settings->softDelete->canUse, |
|
| 687 | 27 | is_numeric($this->settings->softDelete->canUse) ? base_pdo::PARAM_INT : base_pdo::PARAM_STR |
|
| 688 | 27 | ); |
|
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | 77 | protected function addSorting(Support\Options $options) : string |
|
| 693 | { |
||
| 694 | 77 | $sql = ''; |
|
| 695 | 77 | if (!$options->noSortOrder) { |
|
| 696 | 75 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
|
| 697 | 67 | $sql .= ' GROUP BY `child`.`' . $this->settings->idColumnName . '`'; |
|
| 698 | 67 | $order_by = '`child`.`' . $this->settings->leftColumnName . '` ASC'; |
|
| 699 | 14 | } elseif (!empty($options->filterIdBy)) { |
|
| 700 | 1 | $nodeIdIn = implode(',', $options->filterIdBy); |
|
| 701 | 1 | $order_by = 'FIELD(`' . $this->settings->idColumnName . '`,' . $nodeIdIn . ')'; |
|
| 702 | } else { |
||
| 703 | 13 | $order_by = '`parent`.`' . $this->settings->leftColumnName . '` ASC'; |
|
| 704 | } |
||
| 705 | 75 | $sql .= ' ORDER BY ' . $order_by; |
|
| 706 | 2 | } elseif ($options->joinChild) { |
|
| 707 | 2 | $sql .= ' GROUP BY `' . $this->settings->idColumnName . '`'; |
|
| 708 | } |
||
| 709 | |||
| 710 | 77 | return $sql; |
|
| 711 | } |
||
| 712 | } |
||
| 713 |