From f2dabbb4e3233bc257665a3b8964ee7d0cf91791 Mon Sep 17 00:00:00 2001 From: scrs_zdenek Date: Tue, 27 Aug 2024 07:27:44 +0200 Subject: [PATCH] dev --- readme.md | 32 ++++++++++---- src/Drago/Attr/AttributeDetection.php | 14 ++++-- src/Drago/Attr/{From.php => Table.php} | 2 +- src/Drago/Database/Database.php | 43 ++++++++++++------- .../{FluentExtra.php => ExtraFluent.php} | 23 +++++++++- tests/Database/Database.phpt | 26 +++-------- tests/TestDatabase.php | 13 ++++-- 7 files changed, 100 insertions(+), 53 deletions(-) rename src/Drago/Attr/{From.php => Table.php} (96%) rename src/Drago/Database/{FluentExtra.php => ExtraFluent.php} (74%) diff --git a/readme.md b/readme.md index e703fbf..838b219 100644 --- a/readme.md +++ b/readme.md @@ -22,28 +22,36 @@ composer require drago-ex/database ## Use ```php -#[From('table', 'id')] -class Model extends Database {} +#[Table('table_name', 'primary_key')] +class Model +{ + use Database; +} ``` ## Basic queries in the Model -Get all columns from table. +Reading records from table. ```php $this->model->read(); ``` -Get specific columns from table. +Reading records from specific columns. ```php $this->model->read('column'); ``` -Find record by column name. +Find records by column name. ```php $this->model->find('column, 'value'); ``` -Delete record. +Get record by id (if a primary key is available). +```php +$this->model->get(1); +``` + +Delete record by column name. ```php $this->model->delete('column, 'value'); ``` @@ -57,7 +65,7 @@ $this->model->save(['column' => 'value']); ```php class SampleEntity extends Drago\Database\Entity { - public const Table = 'table'; + public const Table = 'name'; public const PrimaryKey = 'id'; public ?int $id = null; @@ -68,14 +76,20 @@ class SampleEntity extends Drago\Database\Entity Use a model with an entity. ```php #[From(SampleEntity::Table, SampleEntity::PrimarKey)] -class Model extends Database {} +class Model +{ + use Database; +} ``` A model with an entity and a class of fetched object. ```php /** @extends Database */ #[From(SampleEntity::Table, SampleEntity::PrimarKey, class: SampleEntity::class)] -class Model extends Database {} +class Model +{ + use Database; +} // We can directly call the model and the object. $row = $this->model->find('id', 1)->record(); diff --git a/src/Drago/Attr/AttributeDetection.php b/src/Drago/Attr/AttributeDetection.php index a7a0377..6c15ef2 100644 --- a/src/Drago/Attr/AttributeDetection.php +++ b/src/Drago/Attr/AttributeDetection.php @@ -58,17 +58,23 @@ public function getTableName(): string * The primary key of the table. * @throws AttributeDetectionException */ - public function getPrimaryKey(): string|null + public function getPrimaryKey(): string { - return $this->getAttributes() - ->primaryKey; + $key = $this->getAttributes()->primaryKey; + if ($key === null) { + throw new AttributeDetectionException( + 'In the model ' . static::class . ' you do not have a primary key in the From attribute.', + ); + } + return $key; } /** + * Row class for fetching object. * @throws AttributeDetectionException */ - public function getClassName(): string|null + public function getClassName(): ?string { return $this->getAttributes() ->class; diff --git a/src/Drago/Attr/From.php b/src/Drago/Attr/Table.php similarity index 96% rename from src/Drago/Attr/From.php rename to src/Drago/Attr/Table.php index a6e1fb7..d042d8b 100644 --- a/src/Drago/Attr/From.php +++ b/src/Drago/Attr/Table.php @@ -13,7 +13,7 @@ #[Attribute(Attribute::TARGET_CLASS)] -class From +class Table { public function __construct( public string $name, diff --git a/src/Drago/Database/Database.php b/src/Drago/Database/Database.php index da2e834..95651c3 100644 --- a/src/Drago/Database/Database.php +++ b/src/Drago/Database/Database.php @@ -18,43 +18,39 @@ /** * @template T + * @property-read Connection $connection */ -abstract class Database +trait Database { use AttributeDetection; - public function __construct( - protected Connection $db, - ) { - } - - /** * Database connection. */ public function getConnection(): Connection { - return $this->db; + return $this->connection; } /** - * @return FluentExtra + * @return ExtraFluent * @throws AttributeDetectionException */ - public function command(): FluentExtra + public function command(): ExtraFluent { - $fluent = new FluentExtra($this->getConnection()); + $fluent = new ExtraFluent($this->getConnection()); $fluent->className = $this->getClassName(); return $fluent; } /** - * @return FluentExtra + * Reading records from table. + * @return ExtraFluent * @throws AttributeDetectionException */ - public function read(...$args): FluentExtra + public function read(...$args): ExtraFluent { $command = $this->command(); $command = $args ? $command->select(...$args) : $command->select('*'); @@ -63,10 +59,11 @@ public function read(...$args): FluentExtra /** - * @return FluentExtra + * Find records by column name. + * @return ExtraFluent * @throws AttributeDetectionException */ - public function find(string $column, int|string $args): FluentExtra + public function find(string $column, int|string $args): ExtraFluent { return $this->read() ->where('%n = ?', $column, $args); @@ -74,9 +71,22 @@ public function find(string $column, int|string $args): FluentExtra /** + * Get record by id (if a primary key is available). + * @return ExtraFluent + * @throws AttributeDetectionException + */ + public function get(int $id): ExtraFluent + { + return $this->read() + ->where('%n = ?', $this->getPrimaryKey(), $id); + } + + + /** + * Delete record by column name. * @throws AttributeDetectionException */ - public function delete(string $column, int|string $args): FluentExtra + public function delete(string $column, int|string $args): ExtraFluent { return $this->command()->delete() ->from($this->getTableName()) @@ -85,6 +95,7 @@ public function delete(string $column, int|string $args): FluentExtra /** + * Insert or update. * @throws AttributeDetectionException * @throws Exception */ diff --git a/src/Drago/Database/FluentExtra.php b/src/Drago/Database/ExtraFluent.php similarity index 74% rename from src/Drago/Database/FluentExtra.php rename to src/Drago/Database/ExtraFluent.php index 38eaf97..575924f 100644 --- a/src/Drago/Database/FluentExtra.php +++ b/src/Drago/Database/ExtraFluent.php @@ -16,11 +16,25 @@ /** * @template T */ -class FluentExtra extends Fluent +class ExtraFluent extends Fluent { public ?string $className = null; + public function from($table, ...$args): self + { + parent::from($table, ...$args); + return $this; + } + + + public function select(...$field): self + { + parent::select(...$field); + return $this; + } + + public function where(...$cond): self { parent::where(...$cond); @@ -49,6 +63,13 @@ public function orderBy(...$field): self } + public function delete(...$cond): self + { + parent::delete(...$cond); + return $this; + } + + /** * @return T * @throws Exception diff --git a/tests/Database/Database.phpt b/tests/Database/Database.phpt index 9ab4e9b..67c47e2 100644 --- a/tests/Database/Database.phpt +++ b/tests/Database/Database.phpt @@ -20,20 +20,8 @@ function database(): TestDatabase } -function find(int $id): TestEntity|null -{ - return database()->find(TestEntity::PrimaryKey, $id)->record(); -} - - -function save(TestEntity $entity): Result|int|null -{ - return database()->save($entity); -} - - -test('Find record by id', function () { - $row = find(1); +test('Get record by id', function () { + $row = database()->get(1)->record(); Assert::same(1, $row->id); Assert::same('Hello', $row->sample); @@ -58,8 +46,8 @@ test('Insert a record with an entity', function () { $entity = new TestEntity; $entity->sample = 'Insert'; - save($entity); - $row = find(2); + database()->save($entity); + $row = database()->get(2)->record(); Assert::same(2, $row->id); Assert::same('Insert', $row->sample); @@ -67,10 +55,10 @@ test('Insert a record with an entity', function () { test('Update the record with the entity', function () { - $row = find(2); + $row = database()->get(2)->record(); $row->sample = 'Update'; - save($row); + database()->save($row); Assert::same(2, $row->id); Assert::same('Update', $row->sample); @@ -100,7 +88,7 @@ test('Get class name', function () { test('Delete record by id', function () { database()->delete('id', 2)->execute(); - $row = find(2); + $row = database()->get(2)->record(); Assert::null($row); }); diff --git a/tests/TestDatabase.php b/tests/TestDatabase.php index 578f225..88f34b6 100644 --- a/tests/TestDatabase.php +++ b/tests/TestDatabase.php @@ -2,14 +2,21 @@ declare(strict_types=1); -use Drago\Attr\From; +use Dibi\Connection; +use Drago\Attr\Table; use Drago\Database\Database; /** * @extends Database */ -#[From(TestEntity::Table, TestEntity::PrimaryKey, class: TestEntity::class)] -class TestDatabase extends Database +#[Table(TestEntity::Table, TestEntity::PrimaryKey, class: TestEntity::class)] +class TestDatabase { + use Database; + + public function __construct( + protected Connection $connection, + ) { + } }