-
Notifications
You must be signed in to change notification settings - Fork 17
/
Database.php
260 lines (233 loc) · 7.69 KB
/
Database.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace PHPSupabase;
use Exception;
class Database {
private $suffix = 'rest/v1/';
private $service;
private $tableName;
private $primaryKey;
private $result;
/**
* Construct method (Set the Service instance, the table to be used and the table primary key)
* @access public
* @param $service Service The Supabase Service instance
* @param $tableName String The table
* @param $primaryKey String The table primary key
* @return void
*/
public function __construct(Service $service, string $tableName, string $primaryKey)
{
$this->service = $service;
$this->tableName = $tableName;
$this->primaryKey = $primaryKey;
}
/**
* Returns the Service instance
* @access public
* @return Service
*/
public function getService() : Service
{
return $this->service;
}
/**
* Returns the error generated
* @access public
* @return string
*/
public function getError() : string
{
return $this->service->getError();
}
/**
* Returns the result (data) generated by a fetch
* @access public
* @return array
*/
public function getResult() : array
{
return $this->result;
}
/**
* Returns the first result (data) generated by a fetch
* @access public
* @return object
*/
public function getFirstResult() : object
{
return count($this->result) > 0
? $this->result[0]
: new \stdClass;
}
/**
* Execute a query in database
* @access private
* @param $queryString String The parameters to be used in the request
* @param $table String (optional) Use a different table that the set in construct method
* @return void
*/
private function executeQuery(string $queryString, string $table = null) : void
{
$table = is_null($table)
? $this->tableName
: $table;
$uri = $this->service->getUriBase($this->suffix . $table . '?' . $queryString);
$options = [
'headers' => $this->service->getHeaders()
];
$this->result = $this->service->executeHttpRequest('GET', $uri, $options);
}
/**
* Execute a DML (Data Manipulation Language) query in database
* @access private
* @param $method String The request method (GET, POST, PUT, DELETE, PATCH, ...)
* @param $data array The fields to be used in query/request
* @param $queryString String (optional) The parameters to be used in the requests
* @return array|object|null
*/
private function executeDml(string $method, array $data, string $queryString = null)
{
$endPoint = ($queryString == null) ? $this->tableName : $this->tableName . '?' . $queryString;
$uri = $this->service->getUriBase($this->suffix . $endPoint);
$this->service->setHeader('Prefer', 'return=representation');
$options = [
'headers' => $this->service->getHeaders(),
'body' => json_encode($data)
];
return $this->service->executeHttpRequest($method, $uri, $options);
}
/**
* Insert a new register into table
* @access public
* @param $data array The values to be inserted
* @return array|object|null
*/
public function insert(array $data)
{
return $this->executeDml('POST', $data);
}
/**
* Update a register into table
* @access public
* @param $id String The "id" (PK) of the register, to be used in WHERE clause
* @param $data array The values to be updated
* @return array|object|null
*/
public function update(string $id, array $data)
{
return $this->executeDml('PATCH', $data, $this->primaryKey . '=eq.' . $id);
}
/**
* Delete a register into table
* @access public
* @param $id String The "id" (PK) of the register, to be used in WHERE clause
* @return array|object|null
*/
public function delete(string $id)
{
return $this->executeDml('DELETE', [], $this->primaryKey . '=eq.' . $id);
}
/**
* Fetch all registers of table
* @access public
* @return Database
*/
public function fetchAll() : Database
{
$this->executeQuery('select=*');
return $this;
}
/**
* Fetch registers of table by a especific column/value
* @access public
* @param $column String The column name
* @param $value String The value
* @return Database
*/
public function findBy(string $column, string $value) : Database
{
$this->executeQuery($column . '=eq.' . $value);
return $this;
}
/**
* Fetch registers of table by a especific column/value, using LIKE operator
* @access public
* @param $column String The column name
* @param $value String The value
* @return Database
*/
public function findByLike(string $column, string $value) : Database
{
$this->executeQuery($column . '=like.%' . $value . '%');
return $this;
}
/**
* Make a "join" between the seted table and another table related
* @access public
* @param $foreignTable String The related table
* @param $foreignKey String The foreign key (usually "id")
* @return Database
*/
public function join(string $foreignTable, string $foreignKey) : Database
{
$this->executeQuery('select=*,' . $foreignTable . '(' . $foreignKey . ', *)');
return $this;
}
/**
* Create a custom query to fetch into database
* @access public
* @param $args array The query structure (Available keys: "select", "from", "join", "where", "range")
* @return Database
*/
public function createCustomQuery(array $args) : Database
{
$queryBuilder = $this->service->initializeQueryBuilder();
$select = isset($args['select'])
? $args['select']
: '*';
$queryBuilder->select($select);
$from = isset($args['from'])
? $args['from']
: $this->tableName;
$queryBuilder->from($from);
if(isset($args['join'])){
if(is_array($args['join']) && count($args['join']) > 0){
foreach ($args['join'] as $join){
if(is_array($join) && isset($join['table']) && isset($join['tablekey'])){
$select = isset($join['select'])
? $join['select']
: null;
$queryBuilder->join($join['table'], $join['tablekey'], $select);
}
else{
throw new Exception('"JOIN" argument must have "table" and "tablekey" keys');
}
}
}
else {
throw new Exception('"JOIN" argument must be an array');
}
}
if(isset($args['where'])){
if(is_array($args['where']) && count($args['where']) > 0){
foreach ($args['where'] as $key => $where){
$queryBuilder->where($key, $where);
}
}
else{
throw new Exception('"WHERE" argument must be an array');
}
}
if(isset($args['limit'])){
$queryBuilder->limit($args['limit']);
}
if(isset($args['order'])){
$queryBuilder->order($args['order']);
}
if(isset($args['range'])){
$queryBuilder->range($args['range']);
}
$this->result = $queryBuilder->execute()->getResult();
return $this;
}
}