Skip to content

Commit

Permalink
Database Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
midorikocak committed Feb 4, 2020
1 parent 7a4f56c commit e5de444
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .phpcs-cache

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
- Nothing


## [1.1.5] - 2020-02-03

### Added
- Database Query method

### Deprecated
- Nothing

### Fixed
- Repository readAll signature.

### Removed
- Nothing

### Security
- Nothing


49 changes: 13 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,14 @@ declare(strict_types=1);

namespace midorikocak\nanodb;

use midorikocak\querymaker\QueryInterface;

interface RepositoryInterface
{
public function read(string $id);

public function readAll(
array $filter = [],
array $columns = ['*'],
?int $limit = null,
?int $offset = null
): array;
public function readAll(?QueryInterface $query = null): array;


public function save($item);

Expand Down Expand Up @@ -234,9 +232,9 @@ namespace midorikocak\nanodb;

use Exception;

use midorikocak\querymaker\QueryInterface;

use function array_map;
use function key;
use function reset;

class Users implements RepositoryInterface
{
Expand All @@ -259,36 +257,15 @@ class Users implements RepositoryInterface
return self::fromArray($data);
}

public function readAll(
array $filter = [],
array $columns = ['*'],
?int $limit = null,
?int $offset = null
):array {
$db = $this->db->select('users', $columns);

if (!empty($filter)) {
$value = reset($filter);
$key = key($filter);
$db->where($key, $value);

unset($filter[key($filter)]);

foreach ($filter as $key => $value) {
$db->and($key, $value);
}
}

if ($limit) {
$this->db->limit($limit);
}

if ($limit && $offset) {
$this->db->offset($offset);
public function readAll(?QueryInterface $query = null): array
{
if ($query !== null) {
$db = $this->db->query($query);
} else {
$db = $this->db->select('users');
}

$db->execute();
return array_map(fn($data) => self::fromArray($data), $db->fetchAll());
return array_map(fn($data) => User::fromArray($data), $db->fetchAll());
}

/**
Expand Down
36 changes: 7 additions & 29 deletions src/ArrayRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

use Exception;
use midorikocak\arraytools\ArrayValidator;
use midorikocak\querymaker\QueryInterface;

use function array_key_exists;
use function key;
use function reset;

/**
* Repository is a class that receives arrays as input data and returns array of arrays
Expand Down Expand Up @@ -78,34 +77,13 @@ public function read(?string $id = null): array
return $this->db->fetch();
}

public function readAll(
array $filter = [],
array $columns = ['*'],
?int $limit = null,
?int $offset = null
): array {
$db = $this->db->select($this->tableName, $columns);

if (!empty($filter)) {
$value = reset($filter);
$key = key($filter);
$db->where($key, $value);

unset($filter[key($filter)]);

foreach ($filter as $key => $value) {
$db->and($key, $value);
}
}

if ($limit) {
$this->db->limit($limit);
}

if ($limit && $offset) {
$this->db->offset($offset);
public function readAll(?QueryInterface $query = null): array
{
if ($query !== null) {
$db = $this->db->query($query);
} else {
$db = $this->db->select($this->tableName);
}

$db->execute();
return $db->fetchAll();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public function __construct(PDO $db)
$this->db = $db;
}

public function query(QueryInterface $queryMaker): self
{
$this->reset();
$this->queryMaker = $queryMaker;
return $this;
}

public function select($table, array $columns = ['*']): self
{
$this->reset();
Expand Down
3 changes: 3 additions & 0 deletions src/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace midorikocak\nanodb;

use midorikocak\querymaker\QueryInterface;
use PDO;

interface DatabaseInterface
Expand Down Expand Up @@ -41,4 +42,6 @@ public function execute(): bool;
public function fetch(): array;

public function fetchAll(): array;

public function query(QueryInterface $query);
}
9 changes: 3 additions & 6 deletions src/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace midorikocak\nanodb;

use midorikocak\querymaker\QueryInterface;

interface RepositoryInterface
{
/**
Expand All @@ -14,12 +16,7 @@ public function read(string $id);
/**
* @return array[] | object[]
*/
public function readAll(
array $filter = [],
array $columns = ['*'],
?int $limit = null,
?int $offset = null
): array;
public function readAll(?QueryInterface $query = null): array;

/**
* @param array|object $item if has id key or property, updates, else creates
Expand Down
38 changes: 7 additions & 31 deletions tests/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace midorikocak\nanodb;

use Exception;
use midorikocak\querymaker\QueryInterface;

use function array_map;
use function key;
use function reset;

class Users implements RepositoryInterface
{
Expand All @@ -28,36 +27,13 @@ public function read(string $id): User
return User::fromArray($data);
}

public function readAll(
array $filter = [],
array $columns = ['*'],
?int $limit = null,
?int $offset = null
): array {
$db = $this->db->select('users', $columns);

//$db = $this->db->select('users', $columns);

if (!empty($filter)) {
$value = reset($filter);
$key = key($filter);
$db->where($key, $value);

unset($filter[key($filter)]);

foreach ($filter as $key => $value) {
$db->and($key, $value);
}
}

if ($limit) {
$this->db->limit($limit);
}

if ($limit && $offset) {
$this->db->offset($offset);
public function readAll(?QueryInterface $query = null): array
{
if ($query !== null) {
$db = $this->db->query($query);
} else {
$db = $this->db->select('users');
}

$db->execute();
return array_map(fn($data) => User::fromArray($data), $db->fetchAll());
}
Expand Down

0 comments on commit e5de444

Please sign in to comment.