-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapm.br.php
More file actions
345 lines (324 loc) · 9.68 KB
/
apm.br.php
File metadata and controls
345 lines (324 loc) · 9.68 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
// PDO Manager - APM (c) 2017 - 2019 Benjamin Rathelot
if(!isset($dbPort)) $dbPort = "3306";
$ApmHandler = new PDO("mysql:host=$dbHost;port=$dbPort;dbname=$dbName;charset=utf8", $dbUser, $dbPassword);
class Apm{
protected $table=""; // table name
protected $whrX=""; // current where statement (text)
protected $innerjoin = ""; // current inner joint statement
protected $select="*"; // current select policy
protected $whrV=[]; // current where statement values (cf. sql prepared queries)
protected $currentId; // current id if id mode enabled (cf. attr() function and get function with type = id)
protected $data; // current data if id mode enabled (cf. attr() function and get function with type = id)
/*
Usage: inits a new Apm object linked to a specific table
Arguments:
table = pg table name
(whr) = custom where statement for all queries (example: 'age=20 AND gender=2')
(innerjoin) = (default: []) enables the innerjoin system with one table or two: [table1, joinOn1] or [table1, joinOn1, table2, joinOn2]
(select) = (default: *), specifies which columns should be select on every select statement
*/
public function __construct($table, $whr=false, $innerjoin=[], $select="*"){
$this->table = $table;
$this->currentId = 0;
$this->select = $select;
$this->whr($whr);
$this->setInnerJoin($innerjoin);
}
/*
Usage: updates the table which will be used for the next queries
Arguments: table = table name
*/
public function setTable($table) {
$this->table = $table;
}
/*
Usage: updates the inner join clause for next queries
Arguments:
innerjoin = (default:[]) innerjoin system with one table or two: [table1, joinOn1] or [table1, joinOn1, table2, joinOn2], cf. SQL INNER JOIN
*/
public function setInnerJoin($innerjoin=[]) {
$this->innerjoin = "";
if(count($innerjoin)==2)$this->innerjoin = "INNER JOIN ".$innerjoin[0]." ON ".$innerjoin[1];
if(count($innerjoin)==4)$this->innerjoin = "INNER JOIN ".$innerjoin[0]." ON ".$innerjoin[1]." INNER JOIN ".$innerjoin[2]." ON ".$innerjoin[3];
}
/*
Usage: updates the selected columns for next queries (default = *)
Arguments :
s = new select part (ex: 'id,age, name')
*/
public function setSelect($s) {
$this->select = $s;
}
/*
Usage: returns data stored in the Apm object, will work if id() mode has been triggered (see below)
Arguments: none
*/
public function getData() {
return $this->data;
}
/*
Usage: inserts a new row in the table linked to the Apm object
Arguments:
array = an array of values to be stored
(hasId) bool = true by default. If true an additional blank column will be added first for the id column, so that you don't have to mention the id every time you make an insert statement
*/
public function insert($array,$hasId=true) {
global $ApmHandler;
$valsX = [];
$valsV = [];
if($hasId) { $valsX[]="''"; }
$query = "INSERT INTO ".$this->table." VALUES(";
$l = count($array);
for($i=0;$i!=$l;$i++) {
$valsX[] = "?";
}
foreach($array as $v) {
$valsV[] = $v;
}
$query.= implode(', ', $valsX).");";
$rq = $ApmHandler->prepare($query);
return $rq->execute($valsV) or die(print_r($rq->errorInfo()));
}
/*
Usage: Private function, internal use only. Converts an object to a prepared sql statement ('id=$1 AND age=$2 ...')
Arguments:
array = an array containing key and values to be converted to an WHERE or SET clause for instance ({id:37, age:20})
word = the clause which will be inserted before the returned statement (default = WHERE)
sep = the separator which will be used between elements (default = AND, could be OR)
=> As you can notice it does not allow to make complex statements combining AND and OR, customWhere can be used in this case
*/
private function parseFromArray($array, $word="WHERE", $sep="AND") {
$add = "";
$rt = $word;
$vals = [];
foreach($array as $k=>$v) {
$rt.=$add." $k=? ";
$vals[] = $v;
$add=$sep;
}
return array("result"=>$rt, "values"=>$vals);
}
/*
Usage: Sets a custom WHERE statement which will be used for the next queries. Useful in case of complex statements
Arguments:
string = the WHERE statement string (without the WHERE clause)
values = an Array of values
*/
public function customWhere($string, $values) {
$this->whrX="WHERE ".$string;
$this->whrV=$values;
}
/*
Usage: Sets a custom WHERE statement from an object
Arguments:
whereObject = an object containing key and values to be converted to an WHERE for instance ({id:37, age:20})
*/
public function whr($whereArray) {
if($whereArray) {
$r = $this->parseFromArray($whereArray);
$this->whrX = $r['result'];
$this->whrV = $r['values'];
}
else
{
$this->whrX = "";
}
}
/*
Usage: Executes a custom sql query with optional values using prepared statement, and returns the query result
Arguments:
query = the query to be executed
arr = the array of values in case of prepared statement
*/
public function customQuery($query, $arr=false) {
global $ApmHandler;
$rq = $ApmHandler->prepare($query);
if($arr){
$rq->execute($arr);
}
else
{
$rq->execute();
}
return $rq;
}
/*
Usage: Executes a select query and returns the result depending on the chosen type
Arguments:
whr = (default false) if not false will set the where statement using whr function (see above)
type =
multiple = returns an array of every result
single = returns the first line ; shortcut : one() function
count = returns the line count ; shortcut : count() function
exists = returns true if an element is retrieved, else false ; shortcut : exists() function
id = triggers the id mode (shortcut : id() function).
=> If the id mode is enabled:
- The current Apm object will be attached to the id of the first element retrieved by the function.
- The retrieved data will be stored in the object (see getData() above)
- The attr function will become available: enables quick update of the id-linked element (see attr())
*/
public function get($whr=false, $type="multiple") {
global $ApmHandler;
if($whr) {
$this->whr($whr);
}
$rq = $ApmHandler->prepare("SELECT ".$this->select." FROM ".$this->table." ".$this->innerjoin." ".$this->whrX);
if($this->whrX!='') {
$rq->execute($this->whrV) or print_r($rq->errorInfo());
}
else
{
$rq->execute();
}
switch($type) {
case "multiple":
if($rq->rowCount()>0) {
return $rq->fetchAll();
}
else
{
return false;
}
break;
case "single":
if($rq->rowCount()>0) {
return $rq->fetch();
}
else
{
return false;
}
break;
case "id":
if($rq->rowCount()>0) {
$this->data = $rq->fetch();
$this->currentId = $this->data['id'];
$this->whrX = "WHERE id=?";
$this->whrV = [$this->currentId];
return $this->data;
}
else
{
return false;
}
break;
case "count":
return $rq->rowCount();
break;
case "exists":
if($rq->rowCount()>0) {
return true;
}
else
{
return false;
}
break;
default:
echo "Error: Invalid get type.";
exit;
break;
}
}
/*
Usage: Shortcut for get() function with type = single
Arguments: cf. get()
*/
public function one($whr=false) {
return $this->get($whr, "single");
}
/*
Usage: Shortcut for get() function with type = id
Arguments: cf. get()
*/
public function id($whr=false) {
return $this->get($whr, "id");
}
/*
Usage: Shortcut for get() function with type = exists
Arguments: cf. get()
*/
public function exists($whr=false) {
return $this->get($whr, "exists");
}
/*
Usage: Shortcut for get() function with type = count
Arguments: cf. get()
*/
public function count($whr=false) {
return $this->get($whr, "count");
}
/*
Usage: Can be used when id mode is enabled (see get() function with type = id). Returns the stored value for attr if no newVal is provided, else performs a SQL update on attr to set the value to newVal
Arguments:
attr = the column name to be return from object's data if no newVal is specified, else the column to be updated
(newVal) = optional. If provided, an update will be performed on the column with the value provided, using the id attached to the current Apm object
*/
public function attr($attr, $newVal="APM_DEFAULT_VALUE_1208#--__--") {
if($this->currentId) {
if($newVal == "APM_DEFAULT_VALUE_1208#--__--") {
if(isset($this->data[$attr])) {
return $this->data[$attr];
}
else
{
return false;
}
}
else
{
global $ApmHandler;
$attr = addslashes($attr);
$rq = $ApmHandler->prepare("UPDATE ".$this->table." SET $attr=? WHERE id=?");
$rq->execute([$newVal, $this->currentId]);
return true;
}
}
else
{
echo "Error: not an ID object.";
exit;
}
}
/*
Usage: Performs an update query using an object as an argument
Arguments:
set = object containing the columns to be updated and their new value
(whr) = optional where object, cf. whr function
*/
public function update($set, $whr=false) {
global $ApmHandler;
if($whr) {
$this->whr($whr);
}
$set = $this->parseFromArray($set, "SET", ",");
$rq = $ApmHandler->prepare("UPDATE ".$this->table." ".$set['result']." ".$this->whrX);
$rq->execute(array_merge($set['values'], $this->whrV));
return true;
}
/*
Usage: Performs a delete query with the current where statement attached to the Apm object
Arguments:
(whr) = optional where object, cf. whr function
*/
public function delete($whr=false) {
global $ApmHandler;
if($whr) {
$this->whr($whr);
}
$rq = $ApmHandler->prepare("DELETE FROM ".$this->table." ".$this->whrX);
if($this->whrX!='') {
$rq->execute($this->whrV);
}
else
{
$rq->execute();
}
if($this->currentId!=0) {
$this->currentId=0;
$this->data = null;
}
return true;
}
}