Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
scrs_zdenek committed Aug 27, 2024
1 parent 9be112c commit f2dabbb
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 53 deletions.
32 changes: 23 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
Expand All @@ -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;
Expand All @@ -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<SampleEntity> */
#[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();
Expand Down
14 changes: 10 additions & 4 deletions src/Drago/Attr/AttributeDetection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Drago/Attr/From.php → src/Drago/Attr/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


#[Attribute(Attribute::TARGET_CLASS)]
class From
class Table
{
public function __construct(
public string $name,
Expand Down
43 changes: 27 additions & 16 deletions src/Drago/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
* @return ExtraFluent<T>
* @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<T>
* Reading records from table.
* @return ExtraFluent<T>
* @throws AttributeDetectionException
*/
public function read(...$args): FluentExtra
public function read(...$args): ExtraFluent
{
$command = $this->command();
$command = $args ? $command->select(...$args) : $command->select('*');
Expand All @@ -63,20 +59,34 @@ public function read(...$args): FluentExtra


/**
* @return FluentExtra<T>
* Find records by column name.
* @return ExtraFluent<T>
* @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);
}


/**
* Get record by id (if a primary key is available).
* @return ExtraFluent<T>
* @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())
Expand All @@ -85,6 +95,7 @@ public function delete(string $column, int|string $args): FluentExtra


/**
* Insert or update.
* @throws AttributeDetectionException
* @throws Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -49,6 +63,13 @@ public function orderBy(...$field): self
}


public function delete(...$cond): self
{
parent::delete(...$cond);
return $this;
}


/**
* @return T
* @throws Exception
Expand Down
26 changes: 7 additions & 19 deletions tests/Database/Database.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -58,19 +46,19 @@ 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);
});


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);
Expand Down Expand Up @@ -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);
});
13 changes: 10 additions & 3 deletions tests/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestEntity>
*/
#[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,
) {
}
}

0 comments on commit f2dabbb

Please sign in to comment.