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 9f470c6 commit f0be25b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
7 changes: 1 addition & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ class Model

Reading records from table.
```php
$this->model->read();
```

Reading records from specific columns.
```php
$this->model->read('column');
$this->model->read('*');
```

Find records by column name.
Expand Down
18 changes: 10 additions & 8 deletions src/Drago/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function command(): ExtraFluent
*/
public function read(...$args): ExtraFluent
{
$command = $this->command();
$command = $args ? $command->select(...$args) : $command->select('*');
return $command->from($this->getTableName());
return $this->command()
->select(...$args)
->from($this->getTableName());
}


Expand All @@ -65,7 +65,7 @@ public function read(...$args): ExtraFluent
*/
public function find(string $column, int|string $args): ExtraFluent
{
return $this->read()
return $this->read('*')
->where('%n = ?', $column, $args);
}

Expand All @@ -77,7 +77,7 @@ public function find(string $column, int|string $args): ExtraFluent
*/
public function get(int $id): ExtraFluent
{
return $this->read()
return $this->read('*')
->where('%n = ?', $this->getPrimaryKey(), $id);
}

Expand All @@ -88,7 +88,8 @@ public function get(int $id): ExtraFluent
*/
public function delete(string $column, int|string $args): ExtraFluent
{
return $this->command()->delete()
return $this->command()
->delete()
->from($this->getTableName())
->where('%n = ?', $column, $args);
}
Expand All @@ -102,6 +103,7 @@ public function delete(string $column, int|string $args): ExtraFluent
public function save(mixed $values): Result|int|null
{
$key = $this->getPrimaryKey();
$table = $this->getTableName();
if ($values instanceof Entity) {
$values = $values->toArray();

Expand All @@ -112,8 +114,8 @@ public function save(mixed $values): Result|int|null

$id = $values[$key] ?? null;
$query = $id > 0
? $this->getConnection()->update($this->getTableName(), $values)->where('%n = ?', $key, $id)
: $this->getConnection()->insert($this->getTableName(), $values);
? $this->getConnection()->update($table, $values)->where('%n = ?', $key, $id)
: $this->getConnection()->insert($table, $values);
return $query->execute();
}

Expand Down
31 changes: 22 additions & 9 deletions tests/Database/Database.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ function database(): TestDatabase


test('Get record by id', function () {
$row = database()->get(1)->record();
$row = database()
->get(1)
->record();

Assert::same(1, $row->id);
Assert::same('Hello', $row->sample);
});


test('Find all records', function () {
$row = database()->read()->recordAll();
$row = database()
->read('*')
->recordAll();

Assert::type('array', $row);
});
Expand All @@ -46,17 +50,21 @@ test('Insert a record with an entity', function () {
$entity->sample = 'Insert';

database()->save($entity);
$row = database()->get(2)->record();
$row = database()
->get(2)
->record();

Assert::same(2, $row->id);
Assert::same('Insert', $row->sample);
});


test('Update the record with the entity', function () {
$row = database()->get(2)->record();
$row->sample = 'Update';
$row = database()
->get(2)
->record();

$row->sample = 'Update';
database()->save($row);

Assert::same(2, $row->id);
Expand All @@ -65,29 +73,34 @@ test('Update the record with the entity', function () {


test('Get table name', function () {
$table = database()->getTableName();
$table = database()
->getTableName();

Assert::same('test', $table);
});


test('Get table column primary key', function () {
$primaryKey = database()->getPrimaryKey();
$primaryKey = database()
->getPrimaryKey();

Assert::same('id', $primaryKey);
});


test('Get class name', function () {
$className = database()->getClassName();
$className = database()
->getClassName();

Assert::same(TestEntity::class, $className);
});


test('Delete record by id', function () {
database()->delete('id', 2)->execute();
$row = database()->get(2)->record();
$row = database()
->get(2)
->record();

Assert::null($row);
});

0 comments on commit f0be25b

Please sign in to comment.