Classes
4 months ago
Exceptions
1 year ago
.htaccess
1 year ago
Cache.php
4 months ago
Query.php
1 year ago
QueryBuilder.php
1 year ago
SleekDB.php
1 year ago
Store.php
4 months ago
autoload.php
1 year ago
index.html
1 year ago
web.config
1 year ago
SleekDB.php
579 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SleekDB; |
| 4 | |
| 5 | use Closure; |
| 6 | use SleekDB\Exceptions\InvalidArgumentException; |
| 7 | use SleekDB\Exceptions\IdNotAllowedException; |
| 8 | use SleekDB\Exceptions\InvalidConfigurationException; |
| 9 | use SleekDB\Exceptions\IOException; |
| 10 | use SleekDB\Exceptions\JsonException; |
| 11 | |
| 12 | /** |
| 13 | * Class SleekDB |
| 14 | * @package SleekDB |
| 15 | * @deprecated since version 2.0, use SleekDB\Store instead. |
| 16 | */ |
| 17 | class SleekDB |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * @var QueryBuilder |
| 22 | */ |
| 23 | protected $queryBuilder; |
| 24 | |
| 25 | /** |
| 26 | * @var Store |
| 27 | */ |
| 28 | protected $store; |
| 29 | |
| 30 | private $shouldKeepConditions = false; |
| 31 | |
| 32 | /** |
| 33 | * SleekDB constructor. |
| 34 | * @param string $storeName |
| 35 | * @param string $dataDir |
| 36 | * @param array $configuration |
| 37 | * @throws InvalidArgumentException |
| 38 | * @throws IOException |
| 39 | * @throws InvalidConfigurationException |
| 40 | */ |
| 41 | public function __construct(string $storeName, string $dataDir, array $configuration = []){ |
| 42 | $this->init($storeName, $dataDir, $configuration); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Initialize the SleekDB instance. |
| 47 | * @param string $storeName |
| 48 | * @param string $dataDir |
| 49 | * @param array $conf |
| 50 | * @throws InvalidArgumentException |
| 51 | * @throws IOException |
| 52 | * @throws InvalidConfigurationException |
| 53 | */ |
| 54 | public function init(string $storeName, string $dataDir, array $conf = []){ |
| 55 | $this->setStore(new Store($storeName, $dataDir, $conf)); |
| 56 | $this->setQueryBuilder($this->getStore()->createQueryBuilder()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Initialize the store. |
| 61 | * @param string $storeName |
| 62 | * @param string $dataDir |
| 63 | * @param array $configuration |
| 64 | * @return SleekDB |
| 65 | * @throws InvalidArgumentException |
| 66 | * @throws IOException |
| 67 | * @throws InvalidConfigurationException |
| 68 | */ |
| 69 | public static function store(string $storeName, string $dataDir, array $configuration = []): SleekDB |
| 70 | { |
| 71 | return new SleekDB($storeName, $dataDir, $configuration); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Execute Query and get Results |
| 76 | * @return array |
| 77 | * @throws InvalidArgumentException |
| 78 | * @throws IOException |
| 79 | */ |
| 80 | public function fetch(): array |
| 81 | { |
| 82 | return $this->getQuery()->fetch(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Check if data is found |
| 87 | * @return bool |
| 88 | * @throws InvalidArgumentException |
| 89 | * @throws IOException |
| 90 | */ |
| 91 | public function exists(): bool |
| 92 | { |
| 93 | return $this->getQuery()->exists(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return the first document. |
| 98 | * @return array |
| 99 | * @throws InvalidArgumentException |
| 100 | * @throws IOException |
| 101 | */ |
| 102 | public function first(): array |
| 103 | { |
| 104 | return $this->getQuery()->first(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Creates a new object in the store. |
| 109 | * It is stored as a plaintext JSON document. |
| 110 | * @param array $storeData |
| 111 | * @return array |
| 112 | * @throws IOException |
| 113 | * @throws IdNotAllowedException |
| 114 | * @throws InvalidArgumentException |
| 115 | * @throws JsonException |
| 116 | */ |
| 117 | public function insert(array $storeData): array |
| 118 | { |
| 119 | return $this->getStore()->insert($storeData); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Creates multiple objects in the store. |
| 124 | * @param array $storeData |
| 125 | * @return array |
| 126 | * @throws IOException |
| 127 | * @throws IdNotAllowedException |
| 128 | * @throws InvalidArgumentException |
| 129 | * @throws JsonException |
| 130 | */ |
| 131 | public function insertMany(array $storeData): array |
| 132 | { |
| 133 | return $this->getStore()->insertMany($storeData); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Update one or multiple documents, based on current query |
| 138 | * @param array $updatable |
| 139 | * @return bool |
| 140 | * @throws InvalidArgumentException |
| 141 | * @throws IOException |
| 142 | */ |
| 143 | public function update(array $updatable): bool |
| 144 | { |
| 145 | return $this->getQuery()->update($updatable); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Deletes matched store objects. |
| 150 | * @param int $returnOption |
| 151 | * @return bool|array|int |
| 152 | * @throws InvalidArgumentException |
| 153 | * @throws IOException |
| 154 | */ |
| 155 | public function delete(int $returnOption = Query::DELETE_RETURN_BOOL){ |
| 156 | return $this->getQuery()->delete($returnOption); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Deletes a store and wipes all the data and cache it contains. |
| 161 | * @return bool |
| 162 | * @throws IOException |
| 163 | */ |
| 164 | public function deleteStore(): bool |
| 165 | { |
| 166 | return $this->getStore()->deleteStore(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * This method would make a unique token for the current query. |
| 171 | * We would use this hash token as the id/name of the cache file. |
| 172 | * @return string |
| 173 | */ |
| 174 | public function getCacheToken(): string |
| 175 | { |
| 176 | return $this->getQueryBuilder()->getQuery()->getCache()->getToken(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Select specific fields |
| 181 | * @param string[] $fieldNames |
| 182 | * @return SleekDB |
| 183 | */ |
| 184 | public function select(array $fieldNames): SleekDB |
| 185 | { |
| 186 | $this->setQueryBuilder($this->getQueryBuilder()->select($fieldNames)); |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Exclude specific fields |
| 192 | * @param string[] $fieldNames |
| 193 | * @return SleekDB |
| 194 | * @throws InvalidArgumentException |
| 195 | */ |
| 196 | public function except(array $fieldNames): SleekDB |
| 197 | { |
| 198 | $this->setQueryBuilder($this->getQueryBuilder()->except($fieldNames)); |
| 199 | return $this; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Add conditions to filter data. |
| 204 | * @param string|array|mixed ...$conditions (string fieldName, string condition, mixed value) OR (array(array(string fieldName, string condition, mixed value)[, array(...)])) |
| 205 | * @return SleekDB |
| 206 | * @throws InvalidArgumentException |
| 207 | */ |
| 208 | public function where(...$conditions): SleekDB |
| 209 | { |
| 210 | foreach ($conditions as $key => $arg) { |
| 211 | if ($key > 0) { |
| 212 | throw new InvalidArgumentException("Allowed: (string fieldName, string condition, mixed value) OR (array(array(string fieldName, string condition, mixed value)[, array(...)]))"); |
| 213 | } |
| 214 | if (is_array($arg)) { |
| 215 | // parameters given as arrays for multiple "where" with "and" between each condition |
| 216 | $this->setQueryBuilder($this->getQueryBuilder()->where($arg)); |
| 217 | break; |
| 218 | } |
| 219 | if (count($conditions) === 3) { |
| 220 | // parameters given as (string fieldName, string condition, mixed value) for a single "where" |
| 221 | $this->setQueryBuilder($this->getQueryBuilder()->where($conditions)); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return $this; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Add "in" condition to filter data. |
| 231 | * @param string $fieldName |
| 232 | * @param array $values |
| 233 | * @return SleekDB |
| 234 | * @throws InvalidArgumentException |
| 235 | */ |
| 236 | public function in(string $fieldName, array $values = []): SleekDB |
| 237 | { |
| 238 | $this->setQueryBuilder($this->getQueryBuilder()->in($fieldName, $values)); |
| 239 | return $this; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Add "not in" condition to filter data. |
| 244 | * @param string $fieldName |
| 245 | * @param array $values |
| 246 | * @return SleekDB |
| 247 | * @throws InvalidArgumentException |
| 248 | */ |
| 249 | public function notIn(string $fieldName, array $values = []): SleekDB |
| 250 | { |
| 251 | $this->setQueryBuilder($this->getQueryBuilder()->notIn($fieldName, $values)); |
| 252 | return $this; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Add or-where conditions to filter data. |
| 257 | * @param string|array|mixed ...$conditions (string fieldName, string condition, mixed value) OR array(array(string fieldName, string condition, mixed value) [, array(...)]) |
| 258 | * @return SleekDB |
| 259 | * @throws InvalidArgumentException |
| 260 | */ |
| 261 | public function orWhere(...$conditions): SleekDB |
| 262 | { |
| 263 | foreach ($conditions as $key => $arg) { |
| 264 | if ($key > 0) { |
| 265 | throw new InvalidArgumentException("Allowed: (string fieldName, string condition, mixed value) OR array(array(string fieldName, string condition, mixed value) [, array(...)])"); |
| 266 | } |
| 267 | if (is_array($arg)) { |
| 268 | // parameters given as arrays for an "or where" with "and" between each condition |
| 269 | $this->setQueryBuilder($this->getQueryBuilder()->orWhere($arg)); |
| 270 | break; |
| 271 | } |
| 272 | if (count($conditions) === 3) { |
| 273 | // parameters given as (string fieldName, string condition, mixed value) for a single "or where" |
| 274 | $this->setQueryBuilder($this->getQueryBuilder()->orWhere($conditions)); |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return $this; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Set the amount of data record to skip. |
| 284 | * @param int $skip |
| 285 | * @return SleekDB |
| 286 | * @throws InvalidArgumentException |
| 287 | */ |
| 288 | public function skip(int $skip = 0): SleekDB |
| 289 | { |
| 290 | $this->setQueryBuilder($this->getQueryBuilder()->skip($skip)); |
| 291 | return $this; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Set the amount of data record to limit. |
| 296 | * @param int $limit |
| 297 | * @return SleekDB |
| 298 | * @throws InvalidArgumentException |
| 299 | */ |
| 300 | public function limit(int $limit = 0): SleekDB |
| 301 | { |
| 302 | $this->setQueryBuilder($this->getQueryBuilder()->limit($limit)); |
| 303 | return $this; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Set the sort order. |
| 308 | * @param string $order "asc" or "desc" |
| 309 | * @param string $orderBy |
| 310 | * @return SleekDB |
| 311 | * @throws InvalidArgumentException |
| 312 | */ |
| 313 | public function orderBy(string $order, string $orderBy = '_id'): SleekDB |
| 314 | { |
| 315 | $this->setQueryBuilder($this->getQueryBuilder()->orderBy([$orderBy => $order])); |
| 316 | return $this; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Do a fulltext like search against more than one field. |
| 321 | * @param string|array $field one fieldName or multiple fieldNames as an array |
| 322 | * @param string $keyword |
| 323 | * @return SleekDB |
| 324 | * @throws InvalidArgumentException |
| 325 | */ |
| 326 | public function search($field, string $keyword): SleekDB |
| 327 | { |
| 328 | $this->setQueryBuilder($this->getQueryBuilder()->search($field, $keyword)); |
| 329 | return $this; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @param Closure $joinedStore |
| 334 | * @param string $dataPropertyName |
| 335 | * @return SleekDB |
| 336 | */ |
| 337 | public function join(Closure $joinedStore, string $dataPropertyName): SleekDB |
| 338 | { |
| 339 | $this->setQueryBuilder($this->getQueryBuilder()->join($joinedStore, $dataPropertyName)); |
| 340 | return $this; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Re-generate the cache for the query. |
| 345 | * @return SleekDB |
| 346 | */ |
| 347 | public function makeCache(): SleekDB |
| 348 | { |
| 349 | $this->setQueryBuilder($this->getQueryBuilder()->regenerateCache()); |
| 350 | return $this; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Disable cache for the query. |
| 355 | * @return SleekDB |
| 356 | */ |
| 357 | public function disableCache(): SleekDB |
| 358 | { |
| 359 | $this->setQueryBuilder($this->getQueryBuilder()->disableCache()); |
| 360 | return $this; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Use caching for current query |
| 365 | * |
| 366 | * @param int|null $lifetime time to live as int in seconds or null to regenerate cache on every insert, update and delete |
| 367 | * |
| 368 | * @return SleekDB |
| 369 | * @throws InvalidArgumentException |
| 370 | */ |
| 371 | public function useCache(?int $lifetime = null): SleekDB |
| 372 | { |
| 373 | $this->setQueryBuilder($this->getQueryBuilder()->useCache($lifetime)); |
| 374 | return $this; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Delete cache file/s for current query. |
| 379 | * @return SleekDB |
| 380 | */ |
| 381 | public function deleteCache(): SleekDB |
| 382 | { |
| 383 | $this->getQueryBuilder()->getQuery()->getCache()->delete(); |
| 384 | return $this; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Delete all cache files for current store. |
| 389 | * @return SleekDB |
| 390 | */ |
| 391 | public function deleteAllCache(): SleekDB |
| 392 | { |
| 393 | $this->getQueryBuilder()->getQuery()->getCache()->deleteAll(); |
| 394 | return $this; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Keep the active query conditions. |
| 399 | * @return SleekDB |
| 400 | */ |
| 401 | public function keepConditions(): SleekDB |
| 402 | { |
| 403 | $this->shouldKeepConditions = true; |
| 404 | return $this; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Return distinct values. |
| 409 | * @param array|string $fields |
| 410 | * @return SleekDB |
| 411 | * @throws InvalidArgumentException |
| 412 | */ |
| 413 | public function distinct($fields = []): SleekDB |
| 414 | { |
| 415 | $this->setQueryBuilder($this->getQueryBuilder()->distinct($fields)); |
| 416 | return $this; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * @return QueryBuilder |
| 421 | */ |
| 422 | public function getQueryBuilder(): QueryBuilder |
| 423 | { |
| 424 | return $this->queryBuilder; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * @param QueryBuilder $queryBuilder |
| 429 | */ |
| 430 | private function setQueryBuilder(QueryBuilder $queryBuilder){ |
| 431 | $this->queryBuilder = $queryBuilder; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * @return Query |
| 436 | */ |
| 437 | public function getQuery(): Query |
| 438 | { |
| 439 | $query = $this->getQueryBuilder()->getQuery(); |
| 440 | $this->resetQueryBuilder(); |
| 441 | return $query; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * @return Cache |
| 446 | */ |
| 447 | public function getCache(): Cache |
| 448 | { |
| 449 | // we do not want to reset the QueryBuilder |
| 450 | return $this->getQueryBuilder()->getQuery()->getCache(); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /** |
| 455 | * @param Store $store |
| 456 | */ |
| 457 | private function setStore(Store $store){ |
| 458 | $this->store = $store; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @return Store |
| 463 | */ |
| 464 | public function getStore(): Store |
| 465 | { |
| 466 | return $this->store; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Handle shouldKeepConditions and reset queryBuilder accordingly |
| 471 | */ |
| 472 | private function resetQueryBuilder(){ |
| 473 | if($this->shouldKeepConditions === true) { |
| 474 | return; |
| 475 | } |
| 476 | $this->setQueryBuilder($this->getStore()->createQueryBuilder()); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /** |
| 481 | * Retrieve all documents. |
| 482 | * @return array |
| 483 | * @throws IOException |
| 484 | * @throws InvalidArgumentException |
| 485 | */ |
| 486 | public function findAll(): array |
| 487 | { |
| 488 | return $this->getStore()->findAll(); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Retrieve one document by its _id. Very fast because it finds the document by its file path. |
| 493 | * @param int $id |
| 494 | * @return array|null |
| 495 | * @throws InvalidArgumentException |
| 496 | */ |
| 497 | public function findById(int $id){ |
| 498 | return $this->getStore()->findById($id); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Retrieve one or multiple documents. |
| 503 | * |
| 504 | * @param array $criteria |
| 505 | * @param array|null $orderBy |
| 506 | * @param int|null $limit |
| 507 | * @param int|null $offset |
| 508 | * |
| 509 | * @return array |
| 510 | * @throws IOException |
| 511 | * @throws InvalidArgumentException |
| 512 | */ |
| 513 | public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array |
| 514 | { |
| 515 | return $this->getStore()->findBy($criteria, $orderBy, $limit, $offset); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Retrieve one document. |
| 520 | * @param array $criteria |
| 521 | * @return array|null single document or NULL if no document can be found |
| 522 | * @throws IOException |
| 523 | * @throws InvalidArgumentException |
| 524 | */ |
| 525 | public function findOneBy(array $criteria) |
| 526 | { |
| 527 | return $this->getStore()->findOneBy($criteria); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Update one or multiple documents. |
| 532 | * @param array $updatable true if all documents could be updated and false if one document did not exist |
| 533 | * @return bool |
| 534 | * @throws IOException |
| 535 | * @throws InvalidArgumentException |
| 536 | */ |
| 537 | public function updateBy(array $updatable): bool |
| 538 | { |
| 539 | return $this->getStore()->update($updatable); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Delete one or multiple documents. |
| 544 | * @param $criteria |
| 545 | * @param int $returnOption |
| 546 | * @return array|bool|int |
| 547 | * @throws IOException |
| 548 | * @throws InvalidArgumentException |
| 549 | */ |
| 550 | public function deleteBy($criteria, $returnOption = Query::DELETE_RETURN_BOOL){ |
| 551 | return $this->getStore()->deleteBy($criteria, $returnOption); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Delete one document by its _id. Very fast because it deletes the document by its file path. |
| 556 | * @param $id |
| 557 | * @return bool true if document does not exist or deletion was successful, false otherwise |
| 558 | * @throws IOException |
| 559 | */ |
| 560 | public function deleteById($id): bool |
| 561 | { |
| 562 | return $this->getStore()->deleteById($id); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Add a where statement that is nested. ( $x or ($y and $z) ) |
| 567 | * @param array $conditions |
| 568 | * @return $this |
| 569 | * @throws InvalidArgumentException |
| 570 | * @deprecated since version 2.3, use where and orWhere instead. |
| 571 | */ |
| 572 | public function nestedWhere(array $conditions): SleekDB |
| 573 | { |
| 574 | $this->setQueryBuilder($this->getQueryBuilder()->nestedWhere($conditions)); |
| 575 | return $this; |
| 576 | } |
| 577 | |
| 578 | } |
| 579 |