scrawler-labs /
arca-orm
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Scrawler package. |
||
| 5 | * |
||
| 6 | * (c) Pranjal Pandey <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | namespace Scrawler\Arca\Facade; |
||
| 15 | |||
| 16 | use Scrawler\Arca\Collection; |
||
| 17 | use Scrawler\Arca\Database as DB; |
||
| 18 | use Scrawler\Arca\Factory\DatabaseFactory; |
||
| 19 | use Scrawler\Arca\Model; |
||
| 20 | use Scrawler\Arca\QueryBuilder; |
||
| 21 | |||
| 22 | class Database |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Store the instance of current connection. |
||
| 26 | */ |
||
| 27 | private static DB $database; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new Database instance. |
||
| 31 | * |
||
| 32 | * @param array<mixed> $connectionParams |
||
| 33 | */ |
||
| 34 | public static function connect(array $connectionParams): DB |
||
| 35 | { |
||
| 36 | $factory = new DatabaseFactory(); |
||
| 37 | self::$database = $factory->build($connectionParams); |
||
| 38 | |||
| 39 | return self::$database; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the instance of current connection. |
||
| 44 | */ |
||
| 45 | private static function getDB(): DB |
||
| 46 | { |
||
| 47 | return self::$database; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Create a new model. |
||
| 52 | */ |
||
| 53 | public static function create(string $name): Model |
||
| 54 | { |
||
| 55 | return self::getDB()->create($name); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Save a model. |
||
| 60 | */ |
||
| 61 | public static function get(string $table): Collection |
||
| 62 | { |
||
| 63 | return self::getDB()->get($table); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Save a model. |
||
| 68 | */ |
||
| 69 | public static function getOne(string $table, mixed $id): ?Model |
||
| 70 | { |
||
| 71 | return self::getDB()->getOne($table, $id); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Execure a raw sql query. |
||
| 76 | * |
||
| 77 | * @return int|numeric-string |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 78 | */ |
||
| 79 | public static function exec(string $sql): int|string |
||
| 80 | { |
||
| 81 | return self::getDB()->exec($sql); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Delete a model. |
||
| 86 | */ |
||
| 87 | public static function delete(Model $model): mixed |
||
| 88 | { |
||
| 89 | return self::getDB()->delete($model); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * QUery builder to find a model. |
||
| 94 | */ |
||
| 95 | public static function find(string $table): QueryBuilder |
||
| 96 | { |
||
| 97 | return self::getDB()->find($table); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |