|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Repositories\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Win\Repositories\Pagination; |
|
6
|
|
|
use Win\Models\Model; |
|
7
|
|
|
use Win\Repositories\Database\Orm\FilterTrait; |
|
8
|
|
|
use Win\Repositories\Database\Orm\PaginationTrait; |
|
9
|
|
|
use Win\Repositories\Database\Orm\SortTrait; |
|
10
|
|
|
use Win\Repositories\Database\Sql\Query; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Object Relational Mapping |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Orm |
|
16
|
|
|
{ |
|
17
|
|
|
use FilterTrait; |
|
|
|
|
|
|
18
|
|
|
use SortTrait; |
|
|
|
|
|
|
19
|
|
|
use PaginationTrait; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
const TABLE = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
const TITLE = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
const PK = 'id'; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Connection */ |
|
31
|
|
|
public $conn; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Model */ |
|
34
|
|
|
protected $model; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Query */ |
|
37
|
|
|
protected $query; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Model $model |
|
41
|
|
|
* @return mixed[] |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract public static function mapRow($model); |
|
44
|
|
|
|
|
45
|
|
|
abstract public static function mapModel($row); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Connection $connection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($connection = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->conn = $connection ?: MysqlConnection::instance(); |
|
53
|
|
|
$this->query = new Query(static::TABLE); |
|
54
|
|
|
$this->pagination = new Pagination(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Prepara raw Sql |
|
59
|
|
|
* @param string $query |
|
60
|
|
|
* @param mixed[] |
|
61
|
|
|
*/ |
|
62
|
|
|
public function rawQuery($query, $values = []) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->query = new Query(static::TABLE, $values, $query); |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Rota do raw sql |
|
71
|
|
|
*/ |
|
72
|
|
|
public function run() |
|
73
|
|
|
{ |
|
74
|
|
|
$query = $this->query->raw(); |
|
75
|
|
|
$values = $this->query->getValues(); |
|
76
|
|
|
return $this->conn->execute($query, $values); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retorna o primeiro resultado da busca |
|
81
|
|
|
*/ |
|
82
|
|
|
public function one() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->query->limit->set(0, 1); |
|
85
|
|
|
|
|
86
|
|
|
$query = $this->query->select(); |
|
87
|
|
|
$values = $this->query->getValues(); |
|
88
|
|
|
$row = $this->conn->fetch($query, $values); |
|
89
|
|
|
$this->flush(); |
|
90
|
|
|
|
|
91
|
|
|
return $this->mapModel($row); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retorna o registro pela PK |
|
96
|
|
|
* @param int $id |
|
97
|
|
|
*/ |
|
98
|
|
|
public function find($id) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->filterBy(static::PK, $id)->one(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Retorna o todos os resultados da busca |
|
105
|
|
|
*/ |
|
106
|
|
|
public function list() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->applyPagination(); |
|
109
|
|
|
|
|
110
|
|
|
$query = $this->query->select(); |
|
111
|
|
|
$values = $this->query->getValues(); |
|
112
|
|
|
$rows = $this->conn->fetchAll($query, $values); |
|
113
|
|
|
$this->flush(); |
|
114
|
|
|
|
|
115
|
|
|
return array_map([$this, 'mapModel'], $rows); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Retorna o total de resultados da busca |
|
120
|
|
|
* Sem aplicar LIMIT e FLUSH |
|
121
|
|
|
*/ |
|
122
|
|
|
public function count() |
|
123
|
|
|
{ |
|
124
|
|
|
$query = $this->query->selectCount(); |
|
125
|
|
|
$values = $this->query->getValues(); |
|
126
|
|
|
|
|
127
|
|
|
return $this->conn->fetchCount($query, $values); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Remove todos os registros da busca |
|
132
|
|
|
*/ |
|
133
|
|
|
public function delete() |
|
134
|
|
|
{ |
|
135
|
|
|
$query = $this->query->delete(); |
|
136
|
|
|
$values = $this->query->getValues(); |
|
137
|
|
|
$this->conn->execute($query, $values); |
|
138
|
|
|
$this->flush(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Remove o registro pela PK |
|
143
|
|
|
* @param int $id |
|
144
|
|
|
*/ |
|
145
|
|
|
public function destroy($id) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->filterBy(static::PK, $id); |
|
148
|
|
|
|
|
149
|
|
|
$query = $this->query->delete(); |
|
150
|
|
|
$values = $this->query->getValues(); |
|
151
|
|
|
$this->conn->execute($query, $values); |
|
152
|
|
|
$this->flush(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Salva o registro no banco |
|
157
|
|
|
* @param Model $model |
|
158
|
|
|
*/ |
|
159
|
|
|
public function save(Model $model) |
|
160
|
|
|
{ |
|
161
|
|
|
$model->validate(); |
|
162
|
|
|
$this->model = $model; |
|
163
|
|
|
!$this->modelExists() ? $this->insert() : $this->update(); |
|
164
|
|
|
|
|
165
|
|
|
$this->flush(); |
|
166
|
|
|
|
|
167
|
|
|
return $this->model; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Remove todos os filtros, ordenação, etc |
|
172
|
|
|
*/ |
|
173
|
|
|
public function flush() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->query = new Query(static::TABLE); |
|
176
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
private function insert() |
|
181
|
|
|
{ |
|
182
|
|
|
$this->query = new Query(static::TABLE, $this->mapRow($this->model)); |
|
183
|
|
|
$query = $this->query->insert(); |
|
184
|
|
|
$values = $this->query->getValues(); |
|
185
|
|
|
$this->conn->execute($query, $values); |
|
186
|
|
|
|
|
187
|
|
|
$this->model->id = (int) $this->conn->getLastInsertId(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function update() |
|
191
|
|
|
{ |
|
192
|
|
|
$this->query = new Query(static::TABLE, $this->mapRow($this->model)); |
|
193
|
|
|
$this->filterBy(static::PK, $this->model->id); |
|
194
|
|
|
|
|
195
|
|
|
$query = $this->query->update(); |
|
196
|
|
|
$values = $this->query->getValues(); |
|
197
|
|
|
$this->conn->execute($query, $values); |
|
198
|
|
|
$this->flush(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** @return bool */ |
|
202
|
|
|
protected function modelExists() |
|
203
|
|
|
{ |
|
204
|
|
|
$orm = new static($this->conn); |
|
205
|
|
|
$orm->filterBy(static::PK, $this->model->id); |
|
206
|
|
|
|
|
207
|
|
|
return $orm->count() > 0; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|