-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.database.php
1516 lines (1319 loc) · 40.9 KB
/
class.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* MySQLi Database Class
*
* @category Database Access
* @package Database
* @author Vivek V <[email protected]> (forked)
* @copyright Copyright (c) 2016
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @version 1.5.5
**/
class Database
{
protected static $_instance;
/**
* MySQLi instance
*/
protected $_mysqli;
/**
* The SQL Query
*/
private $_query;
/**
* Affected rows after a select/update/delete query
*/
public $affected_rows = 0;
/**
* Limit and offset
*/
private $_limit;
private $_offset;
private $_result;
public $error = '';
public $debug = true;
public $die_on_error = true; // Script execution will stop if set to TRUE. Default is TRUE ;
private $_last_query = '';
private $_executed = false;
private $_delete = false;
private $_distinct = false;
protected $table_prefix;
private $_dryrun = false;
private $_parenthesis = '';
/**
* The table name used as FROM
*/
private $_fromTable;
/**
* Arrays
*/
private $array_where = array();
private $array_select = array();
private $array_wherein = array();
private $array_groupby = array();
private $array_having = array();
private $array_orderby = array();
private $array_join = array();
private $debug_array = array();
public function __construct($host, $username, $password, $db, $port = null)
{
// Get the default port number if not given.
if ($port == null)
$port = ini_get('mysqli.default_port');
$this->_mysqli = @new mysqli($host, $username, $password, $db, $port);
if (!$this->_mysqli)
die($this->error('There was a problem connecting to the database'));
$this->_mysqli->set_charset('utf8');
self::$_instance = $this;
$this->debug_array = array( 'host' => $host, 'username' => $username, 'database' => $db );
}
/**
* Close connection
*/
public function __destruct()
{
@$this->_mysqli->close();
}
/**
* Get the instance of the class.
*
* @uses $db = Database:getInstance();
*
* @return object Returns the current instance
*/
public static function getInstance()
{
return self::$_instance;
}
/**
* Reset function after execution
*
*/
private function reset()
{
unset($this->_query);
unset($this->_limit);
unset($this->_offset);
$this->_delete = false;
$this->_distinct = false;
$this->_dryrun = false;
$this->array_where = array();
$this->array_select = array();
$this->array_wherein = array();
$this->array_groupby = array();
$this->array_having = array();
$this->array_orderby = array();
$this->array_join = array();
}
/**
* Sets a limit and offset clause. Offset is optional
*
* @uses $db->limit(0,12); // Will list the first 12 rows
* @uses $db->limit(1); // Will list the first 1 row.
*
* @return object Returns the current instance
*/
public function limit($limit, $offset = null)
{
$this->_limit = (int)$limit;
if ($offset)
$this->_offset = (int)$offset;
return $this;
}
/**
* Executes raw sql query.
*
* @param $query string The raw query
* @param $sanitize boolean If true is provided, the query will be sanitized. Default is false
*
* @return object Returns the object. Use $db->fetch() to get the results array
*/
public function query($query, $sanitize = false)
{
if ($sanitize == true)
$this->_query = filter_var($query, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
else
$this->_query = $query;
$this->_executed = false; // If the user entered a custom SQL Query, then we set executed as FALSE, so that the second query can be executed
return $this;
}
/**
* Executes a raw query. This is same as query() function but it returns only the
* first row as result.
*
* @uses $db->query_first("SELECT * FROM table");
*
* @return object Returns the object. Use $db->fetch() to get the results array
*/
public function query_first($query)
{
$this->limit(1)->query($query);
return $this;
}
/**
* Sets the WHERE clause
* Multiple instances are joined by AND
*
* @param $key array Can either be string or array.
* @param $value string Need only if $key is a string. (optional)
*
* @uses $db->where('key', 'value');
* @uses $db->where(array('key' => 'value', 'key2' => 'value2'));
*
* @return object Returns the current instance
*/
public function where($key, $value = null)
{
return $this->_where($key, $value, 'AND ');
}
/**
* Sets the OR WHERE clause
* This function is identical to where() function except that multiple instances
* are joined by OR
*
* @param $key array Can either be string or array.
* @param $value string Optional. Need only if $key is a string.
*
* @return object Returns the current instance.
*/
public function or_where($key, $value = null)
{
return $this->_where($key, $value, 'OR ');
}
/**
* Tests whether the string has an SQL operator
*
* @param string $str The search string
*
* @return bool Returns true if the string has an operator
*/
function _has_operator($str)
{
$str = trim($str);
if (!preg_match('/(\s|<|>|!|=|is null|is not null)/i', $str)) {
return false;
}
return true;
}
/**
* @param $key The key
* @param $value The value
* @param string $type Where type, can be AND or OR
*
* @return object $this Returns the current instance
*/
protected function _where($key, $value, $type = 'AND ')
{
/**
* If user provided custom where() clauses then we do not need to process it
*/
if (!is_array($key) AND is_null($value)) {
$this->array_where[0] = $key;
return $this;
}
/**
* If the WHERE key is an array then we process the array
*/
if (is_array($key) AND is_null($value)) {
foreach ($key as $wkey => $wval) {
$this->_where($wkey, $wval, $type);
}
} else {
$prefix = (count($this->array_where) == 0) ? '' : $type;
$value = $this->escape($value);
if ($this->_has_operator($key)) {
if ($this->isReservedWord($key) == true) {
if (empty($this->_parenthesis))
$this->array_where[] = "$prefix`$key` '$value'";
else {
if ($this->_parenthesis == ')')
$this->array_where[] = "$this->_parenthesis $prefix `$key` '$value'";
else
$this->array_where[] = "$prefix $this->_parenthesis `$key` '$value'";
$this->_parenthesis = '';
}
} else
$this->array_where[] = "$prefix$key '$value'";
} else {
if ($this->isReservedWord($key) == true) {
if (empty($this->_parenthesis))
$this->array_where[] = "$prefix`$key` = '$value'";
else {
if ($this->_parenthesis == ')')
$this->array_where[] = "$this->_parenthesis $prefix `$key` = '$value'";
else
$this->array_where[] = "$prefix $this->_parenthesis `$key` = '$value'";
$this->_parenthesis = '';
}
} else {
if (empty($this->_parenthesis))
$this->array_where[] = "$prefix$key = '$value'";
else {
if ($this->_parenthesis == ')')
$this->array_where[] = "$this->_parenthesis $prefix $key = '$value'";
else
$this->array_where[] = "$prefix $this->_parenthesis $key = '$value'";
$this->_parenthesis = '';
}
}
}
}
return $this;
}
/**
* Opens a paranthesis before key/value pair in a WHERE statement.
*
*/
function open_where()
{
$this->_parenthesis = '(';
return $this;
}
/**
* Closes a paranthesis before key/value pair in a WHERE statement.
*/
function close_where()
{
$this->_parenthesis = ')';
return $this;
}
/**
* The SELECT portion of the query.
*
* @param $select Can either be a string or an array containing the columns to be
* selected. If none provided, * will be assigned by default
*
* @uses $db->select("id, email, password") ;
* @uses $db->select(array('id', 'email', 'password')) ;
*
* @return Database
*/
public function select($select = '*')
{
if (is_string($select)) {
$select = explode(',', $select);
}
foreach ($select as $val) {
$val = trim($val);
if ($val != '') {
if ($this->isReservedWord($val))
$this->array_select[] = "`$val`";
else
$this->array_select[] = "$val";
}
}
return $this;
}
/**
* Sets the FROM portion of the query.
*
* @param $table string Name of the table.
*
* @return object Returns the current instance
*/
public function from($table)
{
if (isset($this->table_prefix))
$this->_fromTable = $this->table_prefix . $table;
else
$this->_fromTable = $table;
return $this;
}
/**
* Builds the query string
*
* @return $this
*/
private function prepare()
{
/**
* We need to process $this->_query only if the user has not given a _query
* string.
*/
if (!isset($this->_query)) {
// Write the "SELECT" portion of the query
if (!empty($this->array_select)) {
$this->_query = (!$this->_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
if ($this->array_select == '*' OR count($this->array_select) == 0) {
$this->_query .= '*';
} else {
$this->_query .= implode(",", $this->array_select);
}
}
// If delete() is set, then the query is a DELETE query
if ($this->_delete == true) {
// If the query is to delete row(s), make sure we have the table name.
if ($this->_fromTable == null) {
$this->error('Table Name is required for delete function');
}
// Safeguard. If WHERE is not given then do not proceed
if (count($this->array_where) == 0 AND count($this->array_wherein) == 0)
$this->error('WHERE condition is required for DELETE query');
$this->_query = 'DELETE';
}
// If select() is not called but the call is a SELECT statement
if ($this->_delete == false && empty($this->array_select)) {
$this->_query = (!$this->_distinct) ? 'SELECT * ' : 'SELECT DISTINCT * ';
}
$this->_delete = false;
// unset delete flag
// Write the "FROM" portion of the query
if (isset($this->_fromTable)) {
if ($this->isReservedWord($this->_fromTable))
$this->_query .= " FROM `$this->_fromTable` ";
else
$this->_query .= " FROM $this->_fromTable ";
}
// Write the "JOIN" portion of the query
if (count($this->array_join) > 0) {
$this->_query .= " ";
$this->_query .= implode("\n", $this->array_join);
}
// Write the "WHERE" portion of the query
if (count($this->array_where) > 0) {
/*
* Bugfix #17. If nothing is provided as the where value then we assign it as no
* value
*/
for ($i = 0; $i < count($this->array_where); $i ++) {
if (!$this->_has_operator($this->array_where[$i])) {
$this->array_where[$i] = $this->array_where[$i] . " = ''";
}
}
$this->_query .= " WHERE ";
$this->_query .= implode("\n", $this->array_where);
if ($this->_parenthesis) {
$this->_query .= $this->_parenthesis;
$this->_parenthesis = '';
}
}
}
// Write the "GROUP BY" portion of the query
if (!empty($this->array_groupby)) {
$this->_query .= " GROUP BY ";
$this->_query .= implode(', ', $this->array_groupby);
}
// Write the "HAVING" portion of the query
if (!empty($this->array_having)) {
$this->_query .= " HAVING ";
$this->_query .= implode("\n", $this->array_having);
}
// Write the "ORDER BY" portion of the query
if (!empty($this->array_orderby)) {
$this->_query .= " ORDER BY ";
$this->_query .= implode(', ', $this->array_orderby);
}
// Write the "LIMIT" portion of the query
if (isset($this->_limit)) {
$this->_query .= ' LIMIT ' . $this->_limit;
}
// Write the "OFFSET" portion of the query
if (isset($this->_limit) && isset($this->_offset)) {
$this->_query .= ' OFFSET ' . $this->_offset;
}
return $this;
}
/**
* Dry Run function allows the developer to view the full query before it is executed
*
* @return object Returns the current instance
*/
public function dryrun()
{
$this->_dryrun = true;
return $this;
}
/**
* Execute the query. This function returns the object. For getting the result of
* the execution use fetch();
*
* @return object Returns the current instance
*/
public function execute()
{
$this->prepare();
if ($this->_dryrun == true) {
$q = $this->_query;
$this->reset();
$this->_query = $q;
$this->_dryrun = true;
return $this;
}
$this->_result = $this->_mysqli->query($this->_query);
if (!$this->_result)
$this->error();
$this->affected_rows = $this->_mysqli->affected_rows;
$this->_last_query = $this->_query;
$this->reset();
$this->_executed = true;
return $this;
}
/**
* Fetches the result of an execution.
*
* @return array Returns an Associate Array of results.
*/
public function fetch()
{
if ($this->_executed == false || !isset($this->_query))
$this->execute();
if (is_object($this->_result)) {
$this->_executed = false;
// Checks whether fetch_all method is available. It is available only with MySQL
// Native Driver.
if (method_exists('mysqli_result', 'fetch_all')) {
$results = $this->_result->fetch_all(MYSQLI_ASSOC);
} else {
for ($results = array(); $tmp = $this->_result->fetch_array(MYSQLI_ASSOC);)
$results[] = $tmp;
}
return $results;
} else {
$this->error('Unable to perform fetch()');
}
}
/**
* Fetches the first row of the result
*
* @return array Returns an array of result
*/
public function fetch_first()
{
if ($this->_executed == false || !isset($this->_query))
$this->execute();
if (is_object($this->_result)) {
$this->_executed = false;
$results = $this->_result->fetch_array(MYSQLI_ASSOC);
return $results;
} else {
$this->error('Unable to perform fetch_first()');
}
}
/**
* This is an alias function for fetch_first()
*
* @return array
*/
public function fetch_array()
{
return $this->fetch_first();
}
/**
* This function returns the last query. Useful for troubleshooting the code.
*
* @return string Last query
*/
public function last_query()
{
if ($this->_dryrun == true)
return $this->_query;
else
return $this->_last_query;
}
/**
* Remove dangerous input
*
* @param string $string The string needs to be sanitized
*
* @return string Returns the sanitized string
*/
public function escape($string)
{
if (get_magic_quotes_runtime())
$string = stripslashes($string);
return @$this->_mysqli->real_escape_string($string);
}
/**
* Inserts data into table and ignore.
*
* @param string $table Name of the table
* @param array $data The array which contains the coulumn name and values to be
* inserted.
*
* @return integer Returns the inserted id. ( mysqli->insert_id)
*/
public function insert_ignore($table, $data)
{
return $this->insert($table, $data, 1);
}
/**
* Inserts data into table.
*
* @param string $table Name of the table
* @param array $data The array which contains the coulumn name and values to be
* inserted.
*
* @return integer Returns the inserted id. ( mysqli->insert_id)
*/
public function insert($table, $data, $ignore=0)
{
if (isset($this->table_prefix))
$table = $this->table_prefix . $table;
foreach ($data as $key => $value) {
$keys[] = "`$key`";
if (strpos($value, '()') == true OR is_numeric($value))
$values[] = "$value";
else
$values[] = "'$value'";
}
$ignore_string = $ignore ? 'IGNORE ' : '';
$this->_query = "INSERT " . $ignore_string . "INTO " . $table . " (" . implode(', ', $keys) . ") VALUES (" . implode(', ', $values) . ");";
$this->execute();
return $this->_mysqli->insert_id;
}
/**
* Update query. Must provider where() before calling this query.
*
* @param $table string Name of the table
* @param $data string Array containing the data to be updated
* @param $skipWhere bool If set to false which is default, the
* UPDATE query will stop if WHERE clause is
* not provided. This is for security reasons
* as UPDATE query can be dangerouse if WHERE
* condition is not provided. To skip this
* check, set this as TRUE
*
* @return Database
*
*/
public function update($table, $data, $skipWhere = false)
{
if (count($this->array_wherein) == 0 AND count($this->array_where) == 0 AND $skipWhere == false) {
$this->error('You must provider WHERE clause for UPDATE query. Add "false" as the 3rd parameter on UPDATE() to skip this.');
return $this;
}
if (isset($this->table_prefix))
$table = $this->table_prefix . $table;
foreach ($data as $key => $val) {
if (strpos($val, '()') == true OR is_numeric($val))
$valstr[] = "`$key`" . " = $val";
else
$valstr[] = "`$key`" . " = '$val'";
}
$this->_query = "UPDATE " . $table . " SET " . implode(', ', $valstr);
if (count($this->array_where) > 0) {
$this->_query .= " WHERE ";
$this->_query .= implode(" ", $this->array_where);
}
$this->execute();
return $this;
}
/**
* Write the LIKE portion of the query using the connector AND
*
* @param $title string or array Can either be a string or array. This is the
* title portion of LIKE
* @param $match string Required only if $title is a string. This is the matching
* portion
* @param $place string This enables you to control where the wildcard (%) is
* placed. Options are "both", "before", and "after". Default is "both"
*
* @return object Returns the current instance
*/
public function like($title, $match = null, $place = 'both')
{
$this->_like($title, $match, $place, 'AND ');
return $this;
}
/**
* Permits to write the LIKE portion of the query using the connector OR
*
* @param $title string or array Can either be a string or array. This is the
* title portion of LIKE
* @param $match string Required only if $title is a string. This is the matching
* portion
* @param $place string This enables you to control where the wildcard (%) is
* placed. Options are "both", "before", and "after". Default is "both"
*
* @return object Returns the current instance
*/
public function or_like($title, $match = null, $place = 'both')
{
$this->_like($title, $match, $place, 'OR ');
return $this;
}
/**
* Builds _like
*
* @return object Returns the current instance
*/
protected function _like($title, $match, $place = 'both', $type)
{
// If $title is an array, we need to process it
if (is_array($title)) {
foreach ($title as $key => $value) {
$this->_like($key, $value, $place, $type);
}
} else {
$prefix = (count($this->array_where) == 0) ? '' : $type;
$match = $this->escape($match);
if ($place == 'both') {
if ($this->isReservedWord($title))
$this->array_where[] = "$prefix`$title` LIKE '%$match%'";
else
$this->array_where[] = "$prefix$title LIKE '%$match%'";
}
if ($place == 'before') {
if ($this->isReservedWord($title))
$this->array_where[] = "$prefix`$title` LIKE '%$match'";
else
$this->array_where[] = "$prefix$title LIKE '%$match'";
}
if ($place == 'after') {
if ($this->isReservedWord($title))
$this->array_where[] = "$prefix`$title` LIKE '$match%'";
else
$this->array_where[] = "$prefix$title LIKE '$match%'";
}
if ($place == 'none') {
if ($this->isReservedWord($title))
$this->array_where[] = "$prefix`$title` LIKE '$match'";
else
$this->array_where[] = "$prefix$title LIKE '$match'";
}
return $this;
}
}
/**
* Throw an error message
*
* @param null $msg
*/
private function error($msg = null)
{
// If debug is not enabled, do not proceed
if (!$this->debug)
return;
if (!$msg) {
$msg = 'MySQL Error has occured';
}
$this->error = mysqli_error($this->_mysqli);
echo '<table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
<tr><th colspan=2>Database Error</th></tr>
<tr><td align="right" valign="top">Message:</td><td> ' . $msg . '</td></tr> ';
if (!empty($this->error))
echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>' . $this->error . '</td></tr>';
if (!empty($this->_query))
echo '<tr><td align="right">Query:</td><td>' . $this->_query . '</td></tr>';
$debug = array_reverse(debug_backtrace());
echo '<tr><td align="right">Trace:</td><td>';
foreach ($debug as $issues) {
echo $issues['file'] . ' at line ' . $issues['line'] . '<br>';
}
echo '</td></tr>';
echo '</table>';
unset($this->error);
if ($this->die_on_error == true)
die();
}
/**
* SELECT_MAX Portion of the query
*
* Writes a "SELECT MAX(field)" portion for your query. You can optionally
* include a second parameter to rename the resulting field.
*
* @param string $field The field name
* @param string $name AS field
*
* @return object Returns the current instance
*/
public function select_max($field, $name = null)
{
if ($name == null)
$name = $field;
if ($this->isReservedWord($field))
$this->array_select[0] = "MAX(`$field`) AS $name ";
else
$this->array_select[0] = "MAX($field) AS $name ";
return $this;
}
/**
* SELECT_MIN Portion of the query
*
* Writes a "SELECT MIN(field)" portion for your query. You can optionally
* include a second parameter to rename the resulting field.
*
* @return object Returns the current instance
*/
public function select_min($field, $name = null)
{
if ($name == null)
$name = $field;
if ($this->isReservedWord($field))
$this->array_select[0] = "MIN(`$field`) AS $name ";
else
$this->array_select[0] = "MIN($field) AS $name ";
return $this;
}
/**
* SELECT_AVG Portion of the query
*
* Writes a "SELECT AVG(field)" portion for your query. You can optionally
* include a second parameter to rename the resulting field.
*
* @return object Returns the current instance
*/
public function select_avg($field, $name = null)
{
if ($name == null)
$name = $field;
if ($this->isReservedWord($field))
$this->array_select[0] = "AVG(`$field`) AS $name ";
else
$this->array_select[0] = "AVG($field) AS $name ";
return $this;
}
/**
* SELECT_SUM Portion of the query
*
* Writes a "SELECT SUM(field)" portion for your query. You can optionally
* include a second parameter to rename the resulting field.
*
* @return object Returns the current instance
*/
public function select_sum($field, $name = null)
{
if ($name == null)
$name = $field;
if ($this->isReservedWord($field))
$this->array_select[0] = "SUM(`$field`) AS $name ";
else
$this->array_select[0] = "SUM($field) AS $name ";
return $this;
}
/**
* WHERE IN
*/
public function where_in($key = null, $values = null)
{
$this->_where_in($key, $values);
return $this ;
}
/**
* WHERE OR
*/
public function or_where_in($key = null, $values = null)
{
$this->_where_in($key, $values, false, 'OR ');
return $this;
}
/**
* WHERE NOT IN
*/
public function where_not_in($key = null, $values = null)
{
$this->_where_in($key, $values, true);
return $this;
}
/**
* WHERE NOT IN OR
*/
public function or_where_not_in($key = null, $values = null)
{
$this->_where_in($key, $values, true, 'OR ');
return $this;
}
/**
* WHERE IN process
*
* Called by where_in, where_in_or, where_not_in, where_not_in_or
*
* @return object Returns the current instance
*/
protected function _where_in($key = null, $values = null, $not = false, $type = 'AND ')
{
if ($key === null OR $values === null) {
return;
}
if (!is_array($values)) {
$values = array( $values );
}
$not = ($not) ? ' NOT' : '';
foreach ($values as $value) {
$this->array_wherein[] = "'" . $this->escape($value) . "'";
}
$prefix = (count($this->array_where) == 0) ? '' : $type;
if ($this->isReservedWord($key))
$where_in = $prefix . "`$key`" . $not . " IN (" . implode(", ", $this->array_wherein) . ") ";
else
$where_in = $prefix . "$key" . $not . " IN (" . implode(", ", $this->array_wherein) . ") ";
$this->array_where[] = $where_in;
$this->array_wherein = array();
return $this;
}