forked from anzawi/php-database-class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.class.php
315 lines (294 loc) · 8.39 KB
/
database.class.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
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
<?php
/**
* DB
* database Class to ease cintroll of database
* @package t3lam.cms
* @author Mr Mohammad Anzawi
* @copyright 2015
* @version 1.0.0
* @access public
*/
class DB
{
/**
* @var $_instace type object
* store DB class object to allow one connection with database (deny duplicate)
* @access private
*/
private static $_instace;
/**
* @var $_pdo type object PDO object
* @var $_query type string store sql statement
* @var $_results type array store sql statement result
* @var $_count type int store row count for _results variable
* @var $_error type bool if cant fetch sql statement = true otherwise = false
*/
private $_pdo,
$_query,
$_results,
$_count,
$_error = false;
/**
* DB::__construct()
* Connect with database
* @access private
* @return void
*/
private function __construct()
{
try {
$this->_pdo = new PDO("mysql:host=" . HOSTNAME . ";dbname=" . DBNAME, USERNAME, PASSWORD);
$this->_pdo->exec("set names " . CHARSET);
} catch(PDOException $e) {
die($e->getMessage());
}
}
/**
* DB::get()
* return instace
* @return object
*/
public static function get()
{
if(!isset(self::$_instace)) {
self::$_instace = new DB();
}
return self::$_instace;
}
/**
* DB::query()
* check if sql statement is prepare
* append value for sql statement if $parame is set
* featch results
* @param string $sql
* @param array $params
* @return mixed
*/
public function query($sql, $params = array())
{
// set _erroe. true to that if they can not be false for this function to work properly, this function makes the value of _error false if there is no implementation of the sentence correctly
$this->_error = false;
// check if sql statement is prepared
$this->_query = $this->_pdo->prepare($sql);
// if $params isset
if(count($params)) {
/**
* @var $x type int
* counter
*/
$x = 1;
foreach($params as $param) {
// append values to sql statement
$this->_query->bindValue($x, $param);
$x++;
}
}
// check if sql statement executed
if($this->_query->execute()) {
// set _results = data comes
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
// set _count = count rows comes
$this->_count = $this->_query->rowCount();
} else {
// set _error = true if sql statement not executed
$this->_error = true;
}
return $this;
}
/**
* DB::action()
* do sql statements
* @uses action('table_name', 'SELECT *', array('id', '=', 5))
* @param string $table
* @param string $action
* @param array $where
* @param array $moreWhere
* @return mixed
*/
private function action($table, $action, $where = array(), $moreWhere = array())
{
// check if where = 3 fields (field, operator, value))
if(count($where === 3)) {
$field = $where[0]; // name of feild
$operator = $where[1]; // operator
$value = $where[2]; // value of feild
/**
* @var $operators
* allowed operators
*/
$operators = array('=', '<', '>', '<=', '>=', 'BETWEEN', 'LIKE');
// check if operator user set is allowed
if(in_array($operator, $operators)) {
// do sql statement
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? ";
// check if query is not have errors
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
/**
* DB::insert()
* insert into database tables
* @param string $table
* @param array $values
* @return bool
*/
public function insert($table, $values = array())
{
// check if $values set
if(count($values)) {
/**
* @var $fields type array
* store fields user want insert value for them
*/
$fields = array_keys($values);
/**
* @var $value type string
* store value for fields user want inserted
*/
$value = '';
/**
* @var $x type int
* counter
*/
$x = 1;
foreach($values as $field) {
// add new value
$value .="?";
if($x < count($values)) {
// add comma between values
$value .= ", ";
}
$x++;
}
// generate sql statement
$sql = "INSERT INTO {$table} (`" . implode('`,`', $fields) ."`)";
$sql .= " VALUES({$value})";
// check if query is not have an error
if(!$this->query($sql, $values)->error()) {
return true;
}
}
return false;
}
/**
* DB::update()
*
* @param string $table
* @param array $values
* @param array $where
* @return bool
*/
public function update($table, $values = array(), $where = array())
{
/**
* @var $set type string
* store update value
* @example "colomn = value"
*/
$set = ''; // initialize $set
$x = 1;
// initialize feilds and values
foreach($values as $i => $row) {
$set .= "{$i} = ?";
// add comma between values
if($x < count($values)) {
$set .= " ,";
}
$x++;
}
// generate sql statement
$sql = "UPDATE {$table} SET {$set} WHERE " . implode(" ", $where);
// check if query is not have an error
if(!$this->query($sql, $values)->error()) {
return true;
}
return false;
}
/**
* DB::delete()
* delete row from table
* @param string $table
* @param array $where
* @return bool
*/
public function delete($table, $where = array())
{
// check if $where is set
if(count($where)) {
// call action method
if($this->action($table, "DELETE", $where)) {
return true;
}
}
return false;
}
/**
* DB::error()
* return _error variable
* @return bool
*/
public function error()
{
return $this->_error;
}
/**
* DB::getFirst()
* get first x rows
* @param string $table
* @param integer $countRow
* @param array $where
* @return
*/
public function getFirst($table,$countRow = 10, $where = array())
{
if($results = $this->query("SELECT * FROM {$table}", $where)->results()) {
$resultsFirstRows = array();
for($i = 0; $i < $countRow; $i++) {
$resultsFirstRows[$i] = $results[$i];
}
return $resultsFirstRows;
}
return false;
}
/**
* DB::getLast()
* get last x rows
* @param string $table
* @param integer $countRow
* @param array $where
* @return
*/
public function getLast($table, $countRow = 10, $where = array())
{
$resultsLastRows = array();
if($results = $this->query("SELECT * FROM {$table} ", $where)->results()) {
for($i = count($results) - 1; $i > $countRow + 1; $i--) {
$resultsLastRows[$i] = $results[$i];
}
return $resultsLastRows;
}
return false;
}
/**
* DB::error()
* return _results variable
* @return array
*/
public function results()
{
return $this->_results;
}
/**
* DB::error()
* return first key from results method
* @return string
*/
public function first()
{
return $this->results()[0];
}
}