00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_DB_active_record extends CI_DB_driver {
00030
00031 var $ar_select = array();
00032 var $ar_distinct = FALSE;
00033 var $ar_from = array();
00034 var $ar_join = array();
00035 var $ar_where = array();
00036 var $ar_like = array();
00037 var $ar_groupby = array();
00038 var $ar_having = array();
00039 var $ar_limit = FALSE;
00040 var $ar_offset = FALSE;
00041 var $ar_order = FALSE;
00042 var $ar_orderby = array();
00043 var $ar_set = array();
00044 var $ar_wherein = array();
00045 var $ar_aliased_tables = array();
00046 var $ar_store_array = array();
00047
00048
00049 var $ar_caching = FALSE;
00050 var $ar_cache_exists = array();
00051 var $ar_cache_select = array();
00052 var $ar_cache_from = array();
00053 var $ar_cache_join = array();
00054 var $ar_cache_where = array();
00055 var $ar_cache_like = array();
00056 var $ar_cache_groupby = array();
00057 var $ar_cache_having = array();
00058 var $ar_cache_orderby = array();
00059 var $ar_cache_set = array();
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 function select($select = '*', $escape = NULL)
00074 {
00075
00076 if (is_bool($escape))
00077 {
00078 $this->_protect_identifiers = $escape;
00079 }
00080
00081 if (is_string($select))
00082 {
00083 $select = explode(',', $select);
00084 }
00085
00086 foreach ($select as $val)
00087 {
00088 $val = trim($val);
00089
00090 if ($val != '')
00091 {
00092 $this->ar_select[] = $val;
00093
00094 if ($this->ar_caching === TRUE)
00095 {
00096 $this->ar_cache_select[] = $val;
00097 $this->ar_cache_exists[] = 'select';
00098 }
00099 }
00100 }
00101 return $this;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 function select_max($select = '', $alias = '')
00117 {
00118 return $this->_max_min_avg_sum($select, $alias, 'MAX');
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 function select_min($select = '', $alias = '')
00134 {
00135 return $this->_max_min_avg_sum($select, $alias, 'MIN');
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 function select_avg($select = '', $alias = '')
00151 {
00152 return $this->_max_min_avg_sum($select, $alias, 'AVG');
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 function select_sum($select = '', $alias = '')
00168 {
00169 return $this->_max_min_avg_sum($select, $alias, 'SUM');
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
00188 {
00189 if ( ! is_string($select) OR $select == '')
00190 {
00191 $this->display_error('db_invalid_query');
00192 }
00193
00194 $type = strtoupper($type);
00195
00196 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
00197 {
00198 show_error('Invalid function type: '.$type);
00199 }
00200
00201 if ($alias == '')
00202 {
00203 $alias = $this->_create_alias_from_table(trim($select));
00204 }
00205
00206 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
00207
00208 $this->ar_select[] = $sql;
00209
00210 if ($this->ar_caching === TRUE)
00211 {
00212 $this->ar_cache_select[] = $sql;
00213 $this->ar_cache_exists[] = 'select';
00214 }
00215
00216 return $this;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 function _create_alias_from_table($item)
00229 {
00230 if (strpos($item, '.') !== FALSE)
00231 {
00232 return end(explode('.', $item));
00233 }
00234
00235 return $item;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 function distinct($val = TRUE)
00250 {
00251 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
00252 return $this;
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 function from($from)
00267 {
00268 foreach ((array)$from as $val)
00269 {
00270
00271
00272 $this->_track_aliases($val);
00273
00274 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
00275
00276 if ($this->ar_caching === TRUE)
00277 {
00278 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
00279 $this->ar_cache_exists[] = 'from';
00280 }
00281 }
00282
00283 return $this;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 function join($table, $cond, $type = '')
00300 {
00301 if ($type != '')
00302 {
00303 $type = strtoupper(trim($type));
00304
00305 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
00306 {
00307 $type = '';
00308 }
00309 else
00310 {
00311 $type .= ' ';
00312 }
00313 }
00314
00315
00316
00317 $this->_track_aliases($table);
00318
00319
00320 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
00321 {
00322 $match[1] = $this->_protect_identifiers($match[1]);
00323 $match[3] = $this->_protect_identifiers($match[3]);
00324
00325 $cond = $match[1].$match[2].$match[3];
00326 }
00327
00328
00329 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
00330
00331 $this->ar_join[] = $join;
00332 if ($this->ar_caching === TRUE)
00333 {
00334 $this->ar_cache_join[] = $join;
00335 $this->ar_cache_exists[] = 'join';
00336 }
00337
00338 return $this;
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 function where($key, $value = NULL, $escape = TRUE)
00355 {
00356 return $this->_where($key, $value, 'AND ', $escape);
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 function or_where($key, $value = NULL, $escape = TRUE)
00373 {
00374 return $this->_where($key, $value, 'OR ', $escape);
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384 function orwhere($key, $value = NULL, $escape = TRUE)
00385 {
00386 return $this->or_where($key, $value, $escape);
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
00403 {
00404 if ( ! is_array($key))
00405 {
00406 $key = array($key => $value);
00407 }
00408
00409
00410 if ( ! is_bool($escape))
00411 {
00412 $escape = $this->_protect_identifiers;
00413 }
00414
00415 foreach ($key as $k => $v)
00416 {
00417 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
00418
00419 if (is_null($v) && ! $this->_has_operator($k))
00420 {
00421
00422 $k .= ' IS NULL';
00423 }
00424
00425 if ( ! is_null($v))
00426 {
00427 if ($escape === TRUE)
00428 {
00429 $k = $this->_protect_identifiers($k, FALSE, $escape);
00430
00431 $v = ' '.$this->escape($v);
00432 }
00433
00434 if ( ! $this->_has_operator($k))
00435 {
00436 $k .= ' =';
00437 }
00438 }
00439 else
00440 {
00441 $k = $this->_protect_identifiers($k, FALSE, $escape);
00442 }
00443
00444 $this->ar_where[] = $prefix.$k.$v;
00445
00446 if ($this->ar_caching === TRUE)
00447 {
00448 $this->ar_cache_where[] = $prefix.$k.$v;
00449 $this->ar_cache_exists[] = 'where';
00450 }
00451
00452 }
00453
00454 return $this;
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 function where_in($key = NULL, $values = NULL)
00471 {
00472 return $this->_where_in($key, $values);
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 function or_where_in($key = NULL, $values = NULL)
00489 {
00490 return $this->_where_in($key, $values, FALSE, 'OR ');
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 function where_not_in($key = NULL, $values = NULL)
00507 {
00508 return $this->_where_in($key, $values, TRUE);
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 function or_where_not_in($key = NULL, $values = NULL)
00525 {
00526 return $this->_where_in($key, $values, TRUE, 'OR ');
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
00544 {
00545 if ($key === NULL OR $values === NULL)
00546 {
00547 return;
00548 }
00549
00550 if ( ! is_array($values))
00551 {
00552 $values = array($values);
00553 }
00554
00555 $not = ($not) ? ' NOT' : '';
00556
00557 foreach ($values as $value)
00558 {
00559 $this->ar_wherein[] = $this->escape($value);
00560 }
00561
00562 $prefix = (count($this->ar_where) == 0) ? '' : $type;
00563
00564 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
00565
00566 $this->ar_where[] = $where_in;
00567 if ($this->ar_caching === TRUE)
00568 {
00569 $this->ar_cache_where[] = $where_in;
00570 $this->ar_cache_exists[] = 'where';
00571 }
00572
00573
00574 $this->ar_wherein = array();
00575 return $this;
00576 }
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 function like($field, $match = '', $side = 'both')
00592 {
00593 return $this->_like($field, $match, 'AND ', $side);
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 function not_like($field, $match = '', $side = 'both')
00610 {
00611 return $this->_like($field, $match, 'AND ', $side, 'NOT');
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627 function or_like($field, $match = '', $side = 'both')
00628 {
00629 return $this->_like($field, $match, 'OR ', $side);
00630 }
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 function or_not_like($field, $match = '', $side = 'both')
00646 {
00647 return $this->_like($field, $match, 'OR ', $side, 'NOT');
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657 function orlike($field, $match = '', $side = 'both')
00658 {
00659 return $this->or_like($field, $match, $side);
00660 }
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
00676 {
00677 if ( ! is_array($field))
00678 {
00679 $field = array($field => $match);
00680 }
00681
00682 foreach ($field as $k => $v)
00683 {
00684 $k = $this->_protect_identifiers($k);
00685
00686 $prefix = (count($this->ar_like) == 0) ? '' : $type;
00687
00688 $v = $this->escape_str($v);
00689
00690 if ($side == 'before')
00691 {
00692 $like_statement = $prefix." $k $not LIKE '%{$v}'";
00693 }
00694 elseif ($side == 'after')
00695 {
00696 $like_statement = $prefix." $k $not LIKE '{$v}%'";
00697 }
00698 else
00699 {
00700 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
00701 }
00702
00703 $this->ar_like[] = $like_statement;
00704 if ($this->ar_caching === TRUE)
00705 {
00706 $this->ar_cache_like[] = $like_statement;
00707 $this->ar_cache_exists[] = 'like';
00708 }
00709
00710 }
00711 return $this;
00712 }
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 function group_by($by)
00724 {
00725 if (is_string($by))
00726 {
00727 $by = explode(',', $by);
00728 }
00729
00730 foreach ($by as $val)
00731 {
00732 $val = trim($val);
00733
00734 if ($val != '')
00735 {
00736 $this->ar_groupby[] = $this->_protect_identifiers($val);
00737
00738 if ($this->ar_caching === TRUE)
00739 {
00740 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
00741 $this->ar_cache_exists[] = 'groupby';
00742 }
00743 }
00744 }
00745 return $this;
00746 }
00747
00748
00749
00750
00751
00752
00753
00754
00755 function groupby($by)
00756 {
00757 return $this->group_by($by);
00758 }
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772 function having($key, $value = '', $escape = TRUE)
00773 {
00774 return $this->_having($key, $value, 'AND ', $escape);
00775 }
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785 function orhaving($key, $value = '', $escape = TRUE)
00786 {
00787 return $this->or_having($key, $value, $escape);
00788 }
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801 function or_having($key, $value = '', $escape = TRUE)
00802 {
00803 return $this->_having($key, $value, 'OR ', $escape);
00804 }
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
00819 {
00820 if ( ! is_array($key))
00821 {
00822 $key = array($key => $value);
00823 }
00824
00825 foreach ($key as $k => $v)
00826 {
00827 $prefix = (count($this->ar_having) == 0) ? '' : $type;
00828
00829 if ($escape === TRUE)
00830 {
00831 $k = $this->_protect_identifiers($k);
00832 }
00833
00834 if ( ! $this->_has_operator($k))
00835 {
00836 $k .= ' = ';
00837 }
00838
00839 if ($v != '')
00840 {
00841 $v = ' '.$this->escape_str($v);
00842 }
00843
00844 $this->ar_having[] = $prefix.$k.$v;
00845 if ($this->ar_caching === TRUE)
00846 {
00847 $this->ar_cache_having[] = $prefix.$k.$v;
00848 $this->ar_cache_exists[] = 'having';
00849 }
00850 }
00851
00852 return $this;
00853 }
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 function order_by($orderby, $direction = '')
00866 {
00867 if (strtolower($direction) == 'random')
00868 {
00869 $orderby = '';
00870 $direction = $this->_random_keyword;
00871 }
00872 elseif (trim($direction) != '')
00873 {
00874 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
00875 }
00876
00877 $orderby_statement = $this->_protect_identifiers($orderby).$direction;
00878
00879 $this->ar_orderby[] = $orderby_statement;
00880 if ($this->ar_caching === TRUE)
00881 {
00882 $this->ar_cache_orderby[] = $orderby_statement;
00883 $this->ar_cache_exists[] = 'orderby';
00884 }
00885
00886 return $this;
00887 }
00888
00889
00890
00891
00892
00893
00894
00895
00896 function orderby($orderby, $direction = '')
00897 {
00898 return $this->order_by($orderby, $direction);
00899 }
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911 function limit($value, $offset = '')
00912 {
00913 $this->ar_limit = $value;
00914
00915 if ($offset != '')
00916 {
00917 $this->ar_offset = $offset;
00918 }
00919
00920 return $this;
00921 }
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932 function offset($offset)
00933 {
00934 $this->ar_offset = $offset;
00935 return $this;
00936 }
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949 function set($key, $value = '', $escape = TRUE)
00950 {
00951 $key = $this->_object_to_array($key);
00952
00953 if ( ! is_array($key))
00954 {
00955 $key = array($key => $value);
00956 }
00957
00958 foreach ($key as $k => $v)
00959 {
00960 if ($escape === FALSE)
00961 {
00962 $this->ar_set[$this->_protect_identifiers($k)] = $v;
00963 }
00964 else
00965 {
00966 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
00967 }
00968 }
00969
00970 return $this;
00971 }
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987 function get($table = '', $limit = null, $offset = null)
00988 {
00989 if ($table != '')
00990 {
00991 $this->_track_aliases($table);
00992 $this->from($table);
00993 }
00994
00995 if ( ! is_null($limit))
00996 {
00997 $this->limit($limit, $offset);
00998 }
00999
01000 $sql = $this->_compile_select();
01001
01002 $result = $this->query($sql);
01003 $this->_reset_select();
01004 return $result;
01005 }
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 function count_all_results($table = '')
01018 {
01019 if ($table != '')
01020 {
01021 $this->_track_aliases($table);
01022 $this->from($table);
01023 }
01024
01025 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
01026
01027 $query = $this->query($sql);
01028 $this->_reset_select();
01029
01030 if ($query->num_rows() == 0)
01031 {
01032 return '0';
01033 }
01034
01035 $row = $query->row();
01036 return $row->numrows;
01037 }
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052 function get_where($table = '', $where = null, $limit = null, $offset = null)
01053 {
01054 if ($table != '')
01055 {
01056 $this->from($table);
01057 }
01058
01059 if ( ! is_null($where))
01060 {
01061 $this->where($where);
01062 }
01063
01064 if ( ! is_null($limit))
01065 {
01066 $this->limit($limit, $offset);
01067 }
01068
01069 $sql = $this->_compile_select();
01070
01071 $result = $this->query($sql);
01072 $this->_reset_select();
01073 return $result;
01074 }
01075
01076
01077
01078
01079
01080
01081
01082
01083 function getwhere($table = '', $where = null, $limit = null, $offset = null)
01084 {
01085 return $this->get_where($table, $where, $limit, $offset);
01086 }
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100 function insert($table = '', $set = NULL)
01101 {
01102 if ( ! is_null($set))
01103 {
01104 $this->set($set);
01105 }
01106
01107 if (count($this->ar_set) == 0)
01108 {
01109 if ($this->db_debug)
01110 {
01111 return $this->display_error('db_must_use_set');
01112 }
01113 return FALSE;
01114 }
01115
01116 if ($table == '')
01117 {
01118 if ( ! isset($this->ar_from[0]))
01119 {
01120 if ($this->db_debug)
01121 {
01122 return $this->display_error('db_must_set_table');
01123 }
01124 return FALSE;
01125 }
01126
01127 $table = $this->ar_from[0];
01128 }
01129
01130 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
01131
01132 $this->_reset_write();
01133 return $this->query($sql);
01134 }
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
01150 {
01151
01152 $this->_merge_cache();
01153
01154 if ( ! is_null($set))
01155 {
01156 $this->set($set);
01157 }
01158
01159 if (count($this->ar_set) == 0)
01160 {
01161 if ($this->db_debug)
01162 {
01163 return $this->display_error('db_must_use_set');
01164 }
01165 return FALSE;
01166 }
01167
01168 if ($table == '')
01169 {
01170 if ( ! isset($this->ar_from[0]))
01171 {
01172 if ($this->db_debug)
01173 {
01174 return $this->display_error('db_must_set_table');
01175 }
01176 return FALSE;
01177 }
01178
01179 $table = $this->ar_from[0];
01180 }
01181
01182 if ($where != NULL)
01183 {
01184 $this->where($where);
01185 }
01186
01187 if ($limit != NULL)
01188 {
01189 $this->limit($limit);
01190 }
01191
01192 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
01193
01194 $this->_reset_write();
01195 return $this->query($sql);
01196 }
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209 function empty_table($table = '')
01210 {
01211 if ($table == '')
01212 {
01213 if ( ! isset($this->ar_from[0]))
01214 {
01215 if ($this->db_debug)
01216 {
01217 return $this->display_error('db_must_set_table');
01218 }
01219 return FALSE;
01220 }
01221
01222 $table = $this->ar_from[0];
01223 }
01224 else
01225 {
01226 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
01227 }
01228
01229 $sql = $this->_delete($table);
01230
01231 $this->_reset_write();
01232
01233 return $this->query($sql);
01234 }
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249 function truncate($table = '')
01250 {
01251 if ($table == '')
01252 {
01253 if ( ! isset($this->ar_from[0]))
01254 {
01255 if ($this->db_debug)
01256 {
01257 return $this->display_error('db_must_set_table');
01258 }
01259 return FALSE;
01260 }
01261
01262 $table = $this->ar_from[0];
01263 }
01264 else
01265 {
01266 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
01267 }
01268
01269 $sql = $this->_truncate($table);
01270
01271 $this->_reset_write();
01272
01273 return $this->query($sql);
01274 }
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
01291 {
01292
01293 $this->_merge_cache();
01294
01295 if ($table == '')
01296 {
01297 if ( ! isset($this->ar_from[0]))
01298 {
01299 if ($this->db_debug)
01300 {
01301 return $this->display_error('db_must_set_table');
01302 }
01303 return FALSE;
01304 }
01305
01306 $table = $this->ar_from[0];
01307 }
01308 elseif (is_array($table))
01309 {
01310 foreach($table as $single_table)
01311 {
01312 $this->delete($single_table, $where, $limit, FALSE);
01313 }
01314
01315 $this->_reset_write();
01316 return;
01317 }
01318 else
01319 {
01320 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
01321 }
01322
01323 if ($where != '')
01324 {
01325 $this->where($where);
01326 }
01327
01328 if ($limit != NULL)
01329 {
01330 $this->limit($limit);
01331 }
01332
01333 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
01334 {
01335 if ($this->db_debug)
01336 {
01337 return $this->display_error('db_del_must_use_where');
01338 }
01339
01340 return FALSE;
01341 }
01342
01343 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
01344
01345 if ($reset_data)
01346 {
01347 $this->_reset_write();
01348 }
01349
01350 return $this->query($sql);
01351 }
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364 function dbprefix($table = '')
01365 {
01366 if ($table == '')
01367 {
01368 $this->display_error('db_table_name_required');
01369 }
01370
01371 return $this->dbprefix.$table;
01372 }
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385 function _track_aliases($table)
01386 {
01387 if (is_array($table))
01388 {
01389 foreach ($table as $t)
01390 {
01391 $this->_track_aliases($t);
01392 }
01393 return;
01394 }
01395
01396
01397
01398 if (strpos($table, ',') !== FALSE)
01399 {
01400 return $this->_track_aliases(explode(',', $table));
01401 }
01402
01403
01404 if (strpos($table, " ") !== FALSE)
01405 {
01406
01407 $table = preg_replace('/ AS /i', ' ', $table);
01408
01409
01410 $table = trim(strrchr($table, " "));
01411
01412
01413 if ( ! in_array($table, $this->ar_aliased_tables))
01414 {
01415 $this->ar_aliased_tables[] = $table;
01416 }
01417 }
01418 }
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431 function _compile_select($select_override = FALSE)
01432 {
01433
01434 $this->_merge_cache();
01435
01436
01437
01438
01439
01440 if ($select_override !== FALSE)
01441 {
01442 $sql = $select_override;
01443 }
01444 else
01445 {
01446 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
01447
01448 if (count($this->ar_select) == 0)
01449 {
01450 $sql .= '*';
01451 }
01452 else
01453 {
01454
01455
01456
01457 foreach ($this->ar_select as $key => $val)
01458 {
01459 $this->ar_select[$key] = $this->_protect_identifiers($val);
01460 }
01461
01462 $sql .= implode(', ', $this->ar_select);
01463 }
01464 }
01465
01466
01467
01468
01469
01470 if (count($this->ar_from) > 0)
01471 {
01472 $sql .= "\nFROM ";
01473
01474 $sql .= $this->_from_tables($this->ar_from);
01475 }
01476
01477
01478
01479
01480
01481 if (count($this->ar_join) > 0)
01482 {
01483 $sql .= "\n";
01484
01485 $sql .= implode("\n", $this->ar_join);
01486 }
01487
01488
01489
01490
01491
01492 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
01493 {
01494 $sql .= "\n";
01495
01496 $sql .= "WHERE ";
01497 }
01498
01499 $sql .= implode("\n", $this->ar_where);
01500
01501
01502
01503
01504
01505 if (count($this->ar_like) > 0)
01506 {
01507 if (count($this->ar_where) > 0)
01508 {
01509 $sql .= "\nAND ";
01510 }
01511
01512 $sql .= implode("\n", $this->ar_like);
01513 }
01514
01515
01516
01517
01518
01519 if (count($this->ar_groupby) > 0)
01520 {
01521 $sql .= "\nGROUP BY ";
01522
01523 $sql .= implode(', ', $this->ar_groupby);
01524 }
01525
01526
01527
01528
01529
01530 if (count($this->ar_having) > 0)
01531 {
01532 $sql .= "\nHAVING ";
01533 $sql .= implode("\n", $this->ar_having);
01534 }
01535
01536
01537
01538
01539
01540 if (count($this->ar_orderby) > 0)
01541 {
01542 $sql .= "\nORDER BY ";
01543 $sql .= implode(', ', $this->ar_orderby);
01544
01545 if ($this->ar_order !== FALSE)
01546 {
01547 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
01548 }
01549 }
01550
01551
01552
01553
01554
01555 if (is_numeric($this->ar_limit))
01556 {
01557 $sql .= "\n";
01558 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
01559 }
01560
01561 return $sql;
01562 }
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575 function _object_to_array($object)
01576 {
01577 if ( ! is_object($object))
01578 {
01579 return $object;
01580 }
01581
01582 $array = array();
01583 foreach (get_object_vars($object) as $key => $val)
01584 {
01585
01586 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
01587 {
01588 $array[$key] = $val;
01589 }
01590 }
01591
01592 return $array;
01593 }
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605 function start_cache()
01606 {
01607 $this->ar_caching = TRUE;
01608 }
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620 function stop_cache()
01621 {
01622 $this->ar_caching = FALSE;
01623 }
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635 function flush_cache()
01636 {
01637 $this->_reset_run(
01638 array(
01639 'ar_cache_select' => array(),
01640 'ar_cache_from' => array(),
01641 'ar_cache_join' => array(),
01642 'ar_cache_where' => array(),
01643 'ar_cache_like' => array(),
01644 'ar_cache_groupby' => array(),
01645 'ar_cache_having' => array(),
01646 'ar_cache_orderby' => array(),
01647 'ar_cache_set' => array(),
01648 'ar_cache_exists' => array()
01649 )
01650 );
01651 }
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664 function _merge_cache()
01665 {
01666 if (count($this->ar_cache_exists) == 0)
01667 {
01668 return;
01669 }
01670
01671 foreach ($this->ar_cache_exists as $val)
01672 {
01673 $ar_variable = 'ar_'.$val;
01674 $ar_cache_var = 'ar_cache_'.$val;
01675
01676 if (count($this->$ar_cache_var) == 0)
01677 {
01678 continue;
01679 }
01680
01681 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
01682 }
01683
01684
01685
01686 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
01687 {
01688 $this->_track_aliases($this->ar_from);
01689 }
01690 }
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700
01701 function _reset_run($ar_reset_items)
01702 {
01703 foreach ($ar_reset_items as $item => $default_value)
01704 {
01705 if ( ! in_array($item, $this->ar_store_array))
01706 {
01707 $this->$item = $default_value;
01708 }
01709 }
01710 }
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720 function _reset_select()
01721 {
01722 $ar_reset_items = array(
01723 'ar_select' => array(),
01724 'ar_from' => array(),
01725 'ar_join' => array(),
01726 'ar_where' => array(),
01727 'ar_like' => array(),
01728 'ar_groupby' => array(),
01729 'ar_having' => array(),
01730 'ar_orderby' => array(),
01731 'ar_wherein' => array(),
01732 'ar_aliased_tables' => array(),
01733 'ar_distinct' => FALSE,
01734 'ar_limit' => FALSE,
01735 'ar_offset' => FALSE,
01736 'ar_order' => FALSE,
01737 );
01738
01739 $this->_reset_run($ar_reset_items);
01740 }
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752 function _reset_write()
01753 {
01754 $ar_reset_items = array(
01755 'ar_set' => array(),
01756 'ar_from' => array(),
01757 'ar_where' => array(),
01758 'ar_like' => array(),
01759 'ar_orderby' => array(),
01760 'ar_limit' => FALSE,
01761 'ar_order' => FALSE
01762 );
01763
01764 $this->_reset_run($ar_reset_items);
01765 }
01766
01767 }
01768
01769
01770