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