-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
366 lines (327 loc) · 9.71 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
namespace anlprz;
use anlprz\Helper;
Class Database
{
private $databaseUser;
private $databaseName;
private $databasePassword;
private $databaseHost;
private $db;
private $resultObject;
private $qselect = '';
private $qfrom = '';
private $qwhere = [];
private $qlike = [];
private $qorder = '';
private $qlimit = '';
private $qgroup = '';
private $last_query = '';
public static $instance;
public function __construct()
{
self::$instance = $this;
}
public static function getInstance()
{
if( self::$instance === null )
{
self::$instance = new self();
}
return self::$instance;
}
public function setDatabaseUser( String $databaseUser )
{
$this->databaseUser = $databaseUser;
return $this;
}
public function getDatabaseUser()
{
return $this->databaseUser;
}
public function setDatabaseName( String $databaseName )
{
$this->databaseName = $databaseName;
return $this;
}
public function getDatabaseName()
{
return $this->databaseName;
}
public function setDatabasePassword( String $databasePassword )
{
$this->databasePassword = $databasePassword;
return $this;
}
public function getDatabasePassword()
{
return $this->databasePassword;
}
public function setDatabaseHost( String $databaseHost )
{
$this->databaseHost = $databaseHost;
return $this;
}
public function getDatabaseHost()
{
return $this->databaseHost;
}
public function schema_user()
{
return 'CREATE TABLE IF NOT EXISTS `table_users` (
`field_user_id` INT NOT NULL AUTO_INCREMENT,
`field_user_name` VARCHAR(200) NULL DEFAULT NULL,
`field_user_email` VARCHAR(200) NOT NULL,
`field_user_image` VARCHAR(250) NULL DEFAULT NULL,
`field_user_access_token_code` NULL DEFAULT NULL,
`field_user_userdata` TEXT NULL DEFAULT NULL,
`field_user_active` TINYINT(1) NOT NULL DEFAULT 0,
`field_created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`field_updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ( `field_user_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
';
}
public function schema_user_fitness()
{
return 'CREATE TABLE IF NOT EXISTS `table_user_fitness` (
`field_user_fitness_id` INT NOT NULL AUTO_INCREMENT,
`user_field_user_id` INT NOT NULL,
`steps` INT NOT NULL DEFAULT 0,
`data_source_id` VARCHAR(250) NULL DEFAULT NULL,
`start_time_millis` BIGINT NULL DEFAULT NULL,
`end_time_millis` BIGINT NULL DEFAULT NULL,
`stepdate` VARCHAR(10) NULL DEFAULT NULL,
`data` TEXT NULL DEFAULT NULL,
`field_created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`field_updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ( `field_user_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
';
}
/**
* Query to Database
*
* @param String $q
* @return Array Table/s Result
*/
public function query( String $q )
{
$q = $q;
// TODO: Create custom query to Database
// Also use escape(safe) queries
$this->mysql_connect();
$this->resultObject = $this->db->query(
$q
);
return $this;
}
private function mysql_connect()
{
$this->db = mysqli_connect($this->databaseHost,$this->databaseUser,$this->databasePassword,$this->databaseName);
if( !$this->db )
{
throw new \Exception( 'Database connection failed' );
}
return $this->db;
}
public function select( String $select = "*" )
{
$this->qselect = $select;
return $this;
}
public function from( String $from )
{
$this->qfrom = $from;
return $this;
}
public function where( Array $where )
{
$this->qwhere = array_merge( $this->qwhere, $where );
return $this;
}
public function like( Array $like )
{
$this->qlike = array_merge( $this->qlike, $like );
return $this;
}
public function order( String $order )
{
$this->qorder = $order;
return $this;
}
public function group( String $group )
{
$this->qgroup = $group;
return $this;
}
public function limit( String $limit )
{
$this->qlimit = $limit;
return $this;
}
public function get( String $tableName = "" )
{
if( empty( $this->qselect ) ){ $this->select(); }
$where = '';
if( !empty( $this->qwhere ) )
{
$where = ' WHERE '. implode(
" AND ",
Helper::array_map_assoc(
function( $k, $v ) {
if( substr( $k, 0, 1 ) !== "`" )
{
$k = "`". $k . "`";
}
return $k ." = \"". $v ."\"";
},
$this->qwhere
)
);
}
if( !empty( $this->qlike ) )
{
if( empty( $where ) )
{
$where = ' WHERE ';
}
$where .= implode(
" AND ",
Helper::array_map_assoc(
function( $k, $v ) {
if( substr( $k, 0, 1 ) !== "`" )
{
$k = "`". $k . "`";
}
return $k ." LIKE \"". $v ."\"";
},
$this->qlike
)
);
}
$limit = ( !empty( $this->qlimit ) ? ' LIMIT '. $this->qlimit : '' );
$group = ( !empty( $this->qgroup ) ? ' GROUP BY `'. $this->qgroup .'`' : '' );
$order = '';
if( !empty( $this->qorder ) )
{
$order = ' ORDER BY '. $this->qorder;
}
// TODO: Need to perform mysql safe field
// if( !empty( $this->qorder ) )
// {
// if( substr( $this->qorder, 0, 1 ) !== "`" )
// {
// $orderArray = explode( " ", $this->qorder, 1 );
// $orderArray[0] = "`". $orderArray[0] . "`";
// $order = ' ORDER BY '. implode( " ", $orderArray );
// }
// }
if( !empty( $tableName ) )
{
$this->qfrom = $tableName;
}
if( substr( $this->qfrom, 0, 1 ) !== "`" )
{
$fromArray = explode( " ", $this->qfrom, 1 );
$fromArray[0] = "`". $fromArray[0] . "`";
$this->qfrom = implode( " ", $fromArray );
}
$this->last_query =
'
SELECT '. $this->qselect .'
FROM '. $this->qfrom .'
'. $where .'
'. $group .'
'. $order .'
'. $limit .'
'
;
return $this->query( $this->last_query );
}
public function insert( $table, $data )
{
$q = 'INSERT INTO `'. $table .'` ';
$fields = [];
$values = [];
foreach( $data as $k => $v )
{
$fields[] = '`'. $k .'`';
$values[] = '"'. $v .'"';
}
$q .= '( '. implode( ',', $fields ) .' ) VALUES ';
$q .= '( '. implode( ',', $values ) .' )';
return $this->query( $q );
}
public function last_insert_id()
{
return $this->db->insert_id;
}
public function update( $table, $data, $whereData )
{
$set = implode(
", ",
Helper::array_map_assoc(
function( $k, $v ) {
return '`'. $k .'` = "'. $v .'"';
},
$data
)
);
$where = implode(
" AND ",
Helper::array_map_assoc(
function( $k, $v ) {
return '`'. $k .'` = "'. $v .'"';
},
$whereData
)
);
return $this->query( 'UPDATE `'. $table .'` SET '. $set .' WHERE '. $where );
}
public function resultRow()
{
if( !empty( $this->resultObject ) )
{
$result = $this->resultObject->fetch_assoc();
$this->reset();
return $result;
}
throw new \Exception( 'Result not found' );
}
public function resultArrays()
{
if( !empty( $this->resultObject ) )
{
$result = $this->resultObject->fetch_all( MYSQLI_ASSOC );
$this->reset();
return $result;
}
throw new \Exception( 'Result not found' );
}
public function count()
{
if( !empty( $this->resultObject ) )
{
return $this->resultObject->num_rows;
}
throw new \Exception( 'Result not found' );
}
public function reset()
{
$this->qselect = '';
$this->qfrom = '';
$this->qwhere = [];
$this->qlike = [];
$this->qorder = '';
$this->qlimit = '';
$this->qgroup = '';
$this->last_query = '';
$this->resultObject = '';
return true;
}
public function getLastQuery()
{
return $this->last_query;
}
}