-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepository.php
118 lines (100 loc) · 3.39 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php declare(strict_types=1);
namespace Igni\Storage\Driver\Pdo;
use Igni\Storage\Driver\GenericRepository;
use Igni\Storage\Exception\RepositoryException;
use Igni\Storage\Storable;
abstract class Repository extends GenericRepository
{
public function get($id): Storable
{
if ($this->entityManager->has($this->getEntityClass(), $id)) {
return $this->entityManager->get($this->getEntityClass(), $id);
}
$cursor = $this->buildSelectQuery($id);
$cursor->hydrateWith($this->hydrator);
$entity = $cursor->current();
$cursor->close();
if (!$entity instanceof Storable) {
throw RepositoryException::forNotFound($id);
}
return $entity;
}
public function create(Storable $entity): Storable
{
// Execute id auto-generation.
$entity->getId();
$cursor = $this->buildCreateQuery($entity);
$cursor->execute();
return $entity;
}
public function remove(Storable $entity): Storable
{
$cursor = $this->buildDeleteQuery($entity);
$cursor->execute();
return $entity;
}
public function update(Storable $entity): Storable
{
$cursor = $this->buildUpdateQuery($entity);
$cursor->execute();
return $entity;
}
protected function query($query, array $parameters = []): Cursor
{
$cursor = $this->connection->createCursor($query, $parameters);
$cursor->hydrateWith($this->hydrator);
return $cursor;
}
protected function buildSelectQuery($id): Cursor
{
$query = sprintf(
'SELECT *FROM %s WHERE %s = :_id',
$this->metaData->getSource(),
$this->metaData->getIdentifier()->getFieldName()
);
return $this->connection->createCursor($query, ['_id' => $id]);
}
protected function buildDeleteQuery(Storable $entity): Cursor
{
$query = sprintf(
'DELETE FROM %s WHERE %s = :_id',
$this->metaData->getSource(),
$this->metaData->getIdentifier()->getFieldName()
);
return $this->connection->createCursor($query, ['_id' => $entity->getId()]);
}
protected function buildCreateQuery(Storable $entity): Cursor
{
$data = $this->hydrator->extract($entity);
$fields = array_keys($data);
$binds = [];
$columns = [];
foreach ($fields as $columnName) {
$columns[] = "\"${columnName}\"";
$binds[] = ":${columnName}";
}
$sql = sprintf(
'INSERT INTO %s (%s) VALUES(%s)',
$this->metaData->getSource(),
implode(',', $columns),
implode(',', $binds)
);
return $this->connection->createCursor($sql, $data);
}
protected function buildUpdateQuery(Storable $entity): Cursor
{
$data = $this->hydrator->extract($entity);
$fields = array_keys($data);
$columns = [];
foreach ($fields as $columnName) {
$columns[] = "\"${columnName}\" = :${columnName}";
}
$sql = sprintf(
'UPDATE %s SET %s WHERE %s = :_id',
$this->metaData->getSource(),
implode(', ', $columns),
$this->metaData->getIdentifier()->getFieldName()
);
return $this->connection->createCursor($sql, array_merge($data, ['_id' => $entity->getId()]));
}
}