1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809 |
- <?php
- /**
- * CodeIgniter
- *
- * An open source application development framework for PHP
- *
- * This content is released under the MIT License (MIT)
- *
- * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @package CodeIgniter
- * @author EllisLab Dev Team
- * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
- * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
- * @license https://opensource.org/licenses/MIT MIT License
- * @link https://codeigniter.com
- * @since Version 1.0.0
- * @filesource
- */
- defined('BASEPATH') OR exit('No direct script access allowed');
-
- /**
- * Query Builder Class
- *
- * This is the platform-independent base Query Builder implementation class.
- *
- * @package CodeIgniter
- * @subpackage Drivers
- * @category Database
- * @author EllisLab Dev Team
- * @link https://codeigniter.com/user_guide/database/
- */
-
- abstract class CI_DB_query_builder extends CI_DB_driver {
-
- /**
- * Return DELETE SQL flag
- *
- * @var bool
- */
- protected $return_delete_sql = FALSE;
-
- /**
- * Reset DELETE data flag
- *
- * @var bool
- */
- protected $reset_delete_data = FALSE;
-
- /**
- * QB SELECT data
- *
- * @var array
- */
- protected $qb_select = array();
-
- /**
- * QB DISTINCT flag
- *
- * @var bool
- */
- protected $qb_distinct = FALSE;
-
- /**
- * QB FROM data
- *
- * @var array
- */
- protected $qb_from = array();
-
- /**
- * QB JOIN data
- *
- * @var array
- */
- protected $qb_join = array();
-
- /**
- * QB WHERE data
- *
- * @var array
- */
- protected $qb_where = array();
-
- /**
- * QB GROUP BY data
- *
- * @var array
- */
- protected $qb_groupby = array();
-
- /**
- * QB HAVING data
- *
- * @var array
- */
- protected $qb_having = array();
-
- /**
- * QB keys
- *
- * @var array
- */
- protected $qb_keys = array();
-
- /**
- * QB LIMIT data
- *
- * @var int
- */
- protected $qb_limit = FALSE;
-
- /**
- * QB OFFSET data
- *
- * @var int
- */
- protected $qb_offset = FALSE;
-
- /**
- * QB ORDER BY data
- *
- * @var array
- */
- protected $qb_orderby = array();
-
- /**
- * QB data sets
- *
- * @var array
- */
- protected $qb_set = array();
-
- /**
- * QB data set for update_batch()
- *
- * @var array
- */
- protected $qb_set_ub = array();
-
- /**
- * QB aliased tables list
- *
- * @var array
- */
- protected $qb_aliased_tables = array();
-
- /**
- * QB WHERE group started flag
- *
- * @var bool
- */
- protected $qb_where_group_started = FALSE;
-
- /**
- * QB WHERE group count
- *
- * @var int
- */
- protected $qb_where_group_count = 0;
-
- // Query Builder Caching variables
-
- /**
- * QB Caching flag
- *
- * @var bool
- */
- protected $qb_caching = FALSE;
-
- /**
- * QB Cache exists list
- *
- * @var array
- */
- protected $qb_cache_exists = array();
-
- /**
- * QB Cache SELECT data
- *
- * @var array
- */
- protected $qb_cache_select = array();
-
- /**
- * QB Cache FROM data
- *
- * @var array
- */
- protected $qb_cache_from = array();
-
- /**
- * QB Cache JOIN data
- *
- * @var array
- */
- protected $qb_cache_join = array();
-
- /**
- * QB Cache aliased tables list
- *
- * @var array
- */
- protected $qb_cache_aliased_tables = array();
-
- /**
- * QB Cache WHERE data
- *
- * @var array
- */
- protected $qb_cache_where = array();
-
- /**
- * QB Cache GROUP BY data
- *
- * @var array
- */
- protected $qb_cache_groupby = array();
-
- /**
- * QB Cache HAVING data
- *
- * @var array
- */
- protected $qb_cache_having = array();
-
- /**
- * QB Cache ORDER BY data
- *
- * @var array
- */
- protected $qb_cache_orderby = array();
-
- /**
- * QB Cache data sets
- *
- * @var array
- */
- protected $qb_cache_set = array();
-
- /**
- * QB No Escape data
- *
- * @var array
- */
- protected $qb_no_escape = array();
-
- /**
- * QB Cache No Escape data
- *
- * @var array
- */
- protected $qb_cache_no_escape = array();
-
- // --------------------------------------------------------------------
-
- /**
- * Select
- *
- * Generates the SELECT portion of the query
- *
- * @param string
- * @param mixed
- * @return CI_DB_query_builder
- */
- public function select($select = '*', $escape = NULL)
- {
- if (is_string($select))
- {
- $select = explode(',', $select);
- }
-
- // If the escape value was not set, we will base it on the global setting
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- foreach ($select as $val)
- {
- $val = trim($val);
-
- if ($val !== '')
- {
- $this->qb_select[] = $val;
- $this->qb_no_escape[] = $escape;
-
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_select[] = $val;
- $this->qb_cache_exists[] = 'select';
- $this->qb_cache_no_escape[] = $escape;
- }
- }
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Select Max
- *
- * Generates a SELECT MAX(field) portion of a query
- *
- * @param string the field
- * @param string an alias
- * @return CI_DB_query_builder
- */
- public function select_max($select = '', $alias = '')
- {
- return $this->_max_min_avg_sum($select, $alias, 'MAX');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Select Min
- *
- * Generates a SELECT MIN(field) portion of a query
- *
- * @param string the field
- * @param string an alias
- * @return CI_DB_query_builder
- */
- public function select_min($select = '', $alias = '')
- {
- return $this->_max_min_avg_sum($select, $alias, 'MIN');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Select Average
- *
- * Generates a SELECT AVG(field) portion of a query
- *
- * @param string the field
- * @param string an alias
- * @return CI_DB_query_builder
- */
- public function select_avg($select = '', $alias = '')
- {
- return $this->_max_min_avg_sum($select, $alias, 'AVG');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Select Sum
- *
- * Generates a SELECT SUM(field) portion of a query
- *
- * @param string the field
- * @param string an alias
- * @return CI_DB_query_builder
- */
- public function select_sum($select = '', $alias = '')
- {
- return $this->_max_min_avg_sum($select, $alias, 'SUM');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * SELECT [MAX|MIN|AVG|SUM]()
- *
- * @used-by select_max()
- * @used-by select_min()
- * @used-by select_avg()
- * @used-by select_sum()
- *
- * @param string $select Field name
- * @param string $alias
- * @param string $type
- * @return CI_DB_query_builder
- */
- protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
- {
- if ( ! is_string($select) OR $select === '')
- {
- $this->display_error('db_invalid_query');
- }
-
- $type = strtoupper($type);
-
- if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
- {
- show_error('Invalid function type: '.$type);
- }
-
- if ($alias === '')
- {
- $alias = $this->_create_alias_from_table(trim($select));
- }
-
- $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias));
-
- $this->qb_select[] = $sql;
- $this->qb_no_escape[] = NULL;
-
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_select[] = $sql;
- $this->qb_cache_exists[] = 'select';
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Determines the alias name based on the table
- *
- * @param string $item
- * @return string
- */
- protected function _create_alias_from_table($item)
- {
- if (strpos($item, '.') !== FALSE)
- {
- $item = explode('.', $item);
- return end($item);
- }
-
- return $item;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * DISTINCT
- *
- * Sets a flag which tells the query string compiler to add DISTINCT
- *
- * @param bool $val
- * @return CI_DB_query_builder
- */
- public function distinct($val = TRUE)
- {
- $this->qb_distinct = is_bool($val) ? $val : TRUE;
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * From
- *
- * Generates the FROM portion of the query
- *
- * @param mixed $from can be a string or array
- * @return CI_DB_query_builder
- */
- public function from($from)
- {
- foreach ((array) $from as $val)
- {
- if (strpos($val, ',') !== FALSE)
- {
- foreach (explode(',', $val) as $v)
- {
- $v = trim($v);
- $this->_track_aliases($v);
-
- $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
-
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_from[] = $v;
- $this->qb_cache_exists[] = 'from';
- }
- }
- }
- else
- {
- $val = trim($val);
-
- // Extract any aliases that might exist. We use this information
- // in the protect_identifiers to know whether to add a table prefix
- $this->_track_aliases($val);
-
- $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
-
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_from[] = $val;
- $this->qb_cache_exists[] = 'from';
- }
- }
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * JOIN
- *
- * Generates the JOIN portion of the query
- *
- * @param string
- * @param string the join condition
- * @param string the type of join
- * @param string whether not to try to escape identifiers
- * @return CI_DB_query_builder
- */
- public function join($table, $cond, $type = '', $escape = NULL)
- {
- if ($type !== '')
- {
- $type = strtoupper(trim($type));
-
- if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
- {
- $type = '';
- }
- else
- {
- $type .= ' ';
- }
- }
-
- // Extract any aliases that might exist. We use this information
- // in the protect_identifiers to know whether to add a table prefix
- $this->_track_aliases($table);
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- if ( ! $this->_has_operator($cond))
- {
- $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
- }
- elseif ($escape === FALSE)
- {
- $cond = ' ON '.$cond;
- }
- else
- {
- // Split multiple conditions
- if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE))
- {
- $conditions = array();
- $joints = $joints[0];
- array_unshift($joints, array('', 0));
-
- for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--)
- {
- $joints[$i][1] += strlen($joints[$i][0]); // offset
- $conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
- $pos = $joints[$i][1] - strlen($joints[$i][0]);
- $joints[$i] = $joints[$i][0];
- }
- }
- else
- {
- $conditions = array($cond);
- $joints = array('');
- }
-
- $cond = ' ON ';
- for ($i = 0, $c = count($conditions); $i < $c; $i++)
- {
- $operator = $this->_get_operator($conditions[$i]);
- $cond .= $joints[$i];
- $cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)".preg_quote($operator)."(.*)/i", $conditions[$i], $match)
- ? $match[1].$this->protect_identifiers($match[2]).$operator.$this->protect_identifiers($match[3])
- : $conditions[$i];
- }
- }
-
- // Do we want to escape the table name?
- if ($escape === TRUE)
- {
- $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
- }
-
- // Assemble the JOIN statement
- $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
-
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_join[] = $join;
- $this->qb_cache_exists[] = 'join';
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * WHERE
- *
- * Generates the WHERE portion of the query.
- * Separates multiple calls with 'AND'.
- *
- * @param mixed
- * @param mixed
- * @param bool
- * @return CI_DB_query_builder
- */
- public function where($key, $value = NULL, $escape = NULL)
- {
- return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * OR WHERE
- *
- * Generates the WHERE portion of the query.
- * Separates multiple calls with 'OR'.
- *
- * @param mixed
- * @param mixed
- * @param bool
- * @return CI_DB_query_builder
- */
- public function or_where($key, $value = NULL, $escape = NULL)
- {
- return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * WHERE, HAVING
- *
- * @used-by where()
- * @used-by or_where()
- * @used-by having()
- * @used-by or_having()
- *
- * @param string $qb_key 'qb_where' or 'qb_having'
- * @param mixed $key
- * @param mixed $value
- * @param string $type
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
- {
- $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
-
- if ( ! is_array($key))
- {
- $key = array($key => $value);
- }
-
- // If the escape value was not set will base it on the global setting
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- foreach ($key as $k => $v)
- {
- $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
- ? $this->_group_get_type('')
- : $this->_group_get_type($type);
-
- if ($v !== NULL)
- {
- if ($escape === TRUE)
- {
- $v = $this->escape($v);
- }
-
- if ( ! $this->_has_operator($k))
- {
- $k .= ' = ';
- }
- }
- elseif ( ! $this->_has_operator($k))
- {
- // value appears not to have been set, assign the test to IS NULL
- $k .= ' IS NULL';
- }
- elseif (preg_match('/\s*(!?=|<>|\sIS(?:\s+NOT)?\s)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
- {
- $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
- }
-
- ${$qb_key} = array('condition' => $prefix.$k, 'value' => $v, 'escape' => $escape);
- $this->{$qb_key}[] = ${$qb_key};
- if ($this->qb_caching === TRUE)
- {
- $this->{$qb_cache_key}[] = ${$qb_key};
- $this->qb_cache_exists[] = substr($qb_key, 3);
- }
-
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * WHERE IN
- *
- * Generates a WHERE field IN('item', 'item') SQL query,
- * joined with 'AND' if appropriate.
- *
- * @param string $key The field to search
- * @param array $values The values searched on
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function where_in($key = NULL, $values = NULL, $escape = NULL)
- {
- return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * OR WHERE IN
- *
- * Generates a WHERE field IN('item', 'item') SQL query,
- * joined with 'OR' if appropriate.
- *
- * @param string $key The field to search
- * @param array $values The values searched on
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
- {
- return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * WHERE NOT IN
- *
- * Generates a WHERE field NOT IN('item', 'item') SQL query,
- * joined with 'AND' if appropriate.
- *
- * @param string $key The field to search
- * @param array $values The values searched on
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
- {
- return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * OR WHERE NOT IN
- *
- * Generates a WHERE field NOT IN('item', 'item') SQL query,
- * joined with 'OR' if appropriate.
- *
- * @param string $key The field to search
- * @param array $values The values searched on
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
- {
- return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Internal WHERE IN
- *
- * @used-by where_in()
- * @used-by or_where_in()
- * @used-by where_not_in()
- * @used-by or_where_not_in()
- *
- * @param string $key The field to search
- * @param array $values The values searched on
- * @param bool $not If the statement would be IN or NOT IN
- * @param string $type
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
- {
- if ($key === NULL OR $values === NULL)
- {
- return $this;
- }
-
- if ( ! is_array($values))
- {
- $values = array($values);
- }
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- $not = ($not) ? ' NOT' : '';
-
- if ($escape === TRUE)
- {
- $where_in = array();
- foreach ($values as $value)
- {
- $where_in[] = $this->escape($value);
- }
- }
- else
- {
- $where_in = array_values($values);
- }
-
- $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
- ? $this->_group_get_type('')
- : $this->_group_get_type($type);
-
- $where_in = array(
- 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
- 'value' => NULL,
- 'escape' => $escape
- );
-
- $this->qb_where[] = $where_in;
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_where[] = $where_in;
- $this->qb_cache_exists[] = 'where';
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * LIKE
- *
- * Generates a %LIKE% portion of the query.
- * Separates multiple calls with 'AND'.
- *
- * @param mixed $field
- * @param string $match
- * @param string $side
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function like($field, $match = '', $side = 'both', $escape = NULL)
- {
- return $this->_like($field, $match, 'AND ', $side, '', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * NOT LIKE
- *
- * Generates a NOT LIKE portion of the query.
- * Separates multiple calls with 'AND'.
- *
- * @param mixed $field
- * @param string $match
- * @param string $side
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function not_like($field, $match = '', $side = 'both', $escape = NULL)
- {
- return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * OR LIKE
- *
- * Generates a %LIKE% portion of the query.
- * Separates multiple calls with 'OR'.
- *
- * @param mixed $field
- * @param string $match
- * @param string $side
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function or_like($field, $match = '', $side = 'both', $escape = NULL)
- {
- return $this->_like($field, $match, 'OR ', $side, '', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * OR NOT LIKE
- *
- * Generates a NOT LIKE portion of the query.
- * Separates multiple calls with 'OR'.
- *
- * @param mixed $field
- * @param string $match
- * @param string $side
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
- {
- return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Internal LIKE
- *
- * @used-by like()
- * @used-by or_like()
- * @used-by not_like()
- * @used-by or_not_like()
- *
- * @param mixed $field
- * @param string $match
- * @param string $type
- * @param string $side
- * @param string $not
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
- {
- if ( ! is_array($field))
- {
- $field = array($field => $match);
- }
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
- // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
- $side = strtolower($side);
-
- foreach ($field as $k => $v)
- {
- $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
- ? $this->_group_get_type('') : $this->_group_get_type($type);
-
- if ($escape === TRUE)
- {
- $v = $this->escape_like_str($v);
- }
-
- switch ($side)
- {
- case 'none':
- $v = "'{$v}'";
- break;
- case 'before':
- $v = "'%{$v}'";
- break;
- case 'after':
- $v = "'{$v}%'";
- break;
- case 'both':
- default:
- $v = "'%{$v}%'";
- break;
- }
-
- // some platforms require an escape sequence definition for LIKE wildcards
- if ($escape === TRUE && $this->_like_escape_str !== '')
- {
- $v .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
- }
-
- $qb_where = array('condition' => "{$prefix} {$k} {$not} LIKE {$v}", 'value' => NULL, 'escape' => $escape);
- $this->qb_where[] = $qb_where;
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_where[] = $qb_where;
- $this->qb_cache_exists[] = 'where';
- }
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Starts a query group.
- *
- * @param string $not (Internal use only)
- * @param string $type (Internal use only)
- * @return CI_DB_query_builder
- */
- public function group_start($not = '', $type = 'AND ')
- {
- $type = $this->_group_get_type($type);
-
- $this->qb_where_group_started = TRUE;
- $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
- $where = array(
- 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
- 'value' => NULL,
- 'escape' => FALSE
- );
-
- $this->qb_where[] = $where;
- if ($this->qb_caching)
- {
- $this->qb_cache_where[] = $where;
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Starts a query group, but ORs the group
- *
- * @return CI_DB_query_builder
- */
- public function or_group_start()
- {
- return $this->group_start('', 'OR ');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Starts a query group, but NOTs the group
- *
- * @return CI_DB_query_builder
- */
- public function not_group_start()
- {
- return $this->group_start('NOT ', 'AND ');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Starts a query group, but OR NOTs the group
- *
- * @return CI_DB_query_builder
- */
- public function or_not_group_start()
- {
- return $this->group_start('NOT ', 'OR ');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Ends a query group
- *
- * @return CI_DB_query_builder
- */
- public function group_end()
- {
- $this->qb_where_group_started = FALSE;
- $where = array(
- 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
- 'value' => NULL,
- 'escape' => FALSE
- );
-
- $this->qb_where[] = $where;
- if ($this->qb_caching)
- {
- $this->qb_cache_where[] = $where;
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Group_get_type
- *
- * @used-by group_start()
- * @used-by _like()
- * @used-by _wh()
- * @used-by _where_in()
- *
- * @param string $type
- * @return string
- */
- protected function _group_get_type($type)
- {
- if ($this->qb_where_group_started)
- {
- $type = '';
- $this->qb_where_group_started = FALSE;
- }
-
- return $type;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * GROUP BY
- *
- * @param string $by
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function group_by($by, $escape = NULL)
- {
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- if (is_string($by))
- {
- $by = ($escape === TRUE)
- ? explode(',', $by)
- : array($by);
- }
-
- foreach ($by as $val)
- {
- $val = trim($val);
-
- if ($val !== '')
- {
- $val = array('field' => $val, 'escape' => $escape);
-
- $this->qb_groupby[] = $val;
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_groupby[] = $val;
- $this->qb_cache_exists[] = 'groupby';
- }
- }
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * HAVING
- *
- * Separates multiple calls with 'AND'.
- *
- * @param string $key
- * @param string $value
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function having($key, $value = NULL, $escape = NULL)
- {
- return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * OR HAVING
- *
- * Separates multiple calls with 'OR'.
- *
- * @param string $key
- * @param string $value
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function or_having($key, $value = NULL, $escape = NULL)
- {
- return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * ORDER BY
- *
- * @param string $orderby
- * @param string $direction ASC, DESC or RANDOM
- * @param bool $escape
- * @return CI_DB_query_builder
- */
- public function order_by($orderby, $direction = '', $escape = NULL)
- {
- $direction = strtoupper(trim($direction));
-
- if ($direction === 'RANDOM')
- {
- $direction = '';
-
- // Do we have a seed value?
- $orderby = ctype_digit((string) $orderby)
- ? sprintf($this->_random_keyword[1], $orderby)
- : $this->_random_keyword[0];
- }
- elseif (empty($orderby))
- {
- return $this;
- }
- elseif ($direction !== '')
- {
- $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
- }
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- if ($escape === FALSE)
- {
- $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
- }
- else
- {
- $qb_orderby = array();
- foreach (explode(',', $orderby) as $field)
- {
- $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
- ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
- : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
- }
- }
-
- $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
- if ($this->qb_caching === TRUE)
- {
- $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
- $this->qb_cache_exists[] = 'orderby';
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * LIMIT
- *
- * @param int $value LIMIT value
- * @param int $offset OFFSET value
- * @return CI_DB_query_builder
- */
- public function limit($value, $offset = 0)
- {
- is_null($value) OR $this->qb_limit = (int) $value;
- empty($offset) OR $this->qb_offset = (int) $offset;
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Sets the OFFSET value
- *
- * @param int $offset OFFSET value
- * @return CI_DB_query_builder
- */
- public function offset($offset)
- {
- empty($offset) OR $this->qb_offset = (int) $offset;
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * LIMIT string
- *
- * Generates a platform-specific LIMIT clause.
- *
- * @param string $sql SQL Query
- * @return string
- */
- protected function _limit($sql)
- {
- return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').(int) $this->qb_limit;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * The "set" function.
- *
- * Allows key/value pairs to be set for inserting or updating
- *
- * @param mixed
- * @param string
- * @param bool
- * @return CI_DB_query_builder
- */
- public function set($key, $value = '', $escape = NULL)
- {
- $key = $this->_object_to_array($key);
-
- if ( ! is_array($key))
- {
- $key = array($key => $value);
- }
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- foreach ($key as $k => $v)
- {
- $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
- ? $this->escape($v) : $v;
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Get SELECT query string
- *
- * Compiles a SELECT query string and returns the sql.
- *
- * @param string the table name to select from (optional)
- * @param bool TRUE: resets QB values; FALSE: leave QB values alone
- * @return string
- */
- public function get_compiled_select($table = '', $reset = TRUE)
- {
- if ($table !== '')
- {
- $this->_track_aliases($table);
- $this->from($table);
- }
-
- $select = $this->_compile_select();
-
- if ($reset === TRUE)
- {
- $this->_reset_select();
- }
-
- return $select;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Get
- *
- * Compiles the select statement based on the other functions called
- * and runs the query
- *
- * @param string the table
- * @param string the limit clause
- * @param string the offset clause
- * @return CI_DB_result
- */
- public function get($table = '', $limit = NULL, $offset = NULL)
- {
- if ($table !== '')
- {
- $this->_track_aliases($table);
- $this->from($table);
- }
-
- if ( ! empty($limit))
- {
- $this->limit($limit, $offset);
- }
-
- $result = $this->query($this->_compile_select());
- $this->_reset_select();
- return $result;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * "Count All Results" query
- *
- * Generates a platform-specific query string that counts all records
- * returned by an Query Builder query.
- *
- * @param string
- * @param bool the reset clause
- * @return int
- */
- public function count_all_results($table = '', $reset = TRUE)
- {
- if ($table !== '')
- {
- $this->_track_aliases($table);
- $this->from($table);
- }
-
- // ORDER BY usage is often problematic here (most notably
- // on Microsoft SQL Server) and ultimately unnecessary
- // for selecting COUNT(*) ...
- $qb_orderby = $this->qb_orderby;
- $qb_cache_orderby = $this->qb_cache_orderby;
- $this->qb_orderby = $this->qb_cache_orderby = array();
-
- $result = ($this->qb_distinct === TRUE OR ! empty($this->qb_groupby) OR ! empty($this->qb_cache_groupby) OR $this->qb_limit OR $this->qb_offset)
- ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
- : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
-
- if ($reset === TRUE)
- {
- $this->_reset_select();
- }
- else
- {
- $this->qb_orderby = $qb_orderby;
- $this->qb_cache_orderby = $qb_cache_orderby;
- }
-
- if ($result->num_rows() === 0)
- {
- return 0;
- }
-
- $row = $result->row();
- return (int) $row->numrows;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * get_where()
- *
- * Allows the where clause, limit and offset to be added directly
- *
- * @param string $table
- * @param string $where
- * @param int $limit
- * @param int $offset
- * @return CI_DB_result
- */
- public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
- {
- if ($table !== '')
- {
- $this->from($table);
- }
-
- if ($where !== NULL)
- {
- $this->where($where);
- }
-
- if ( ! empty($limit))
- {
- $this->limit($limit, $offset);
- }
-
- $result = $this->query($this->_compile_select());
- $this->_reset_select();
- return $result;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Insert_Batch
- *
- * Compiles batch insert strings and runs the queries
- *
- * @param string $table Table to insert into
- * @param array $set An associative array of insert values
- * @param bool $escape Whether to escape values and identifiers
- * @return int Number of rows inserted or FALSE on failure
- */
- public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
- {
- if ($set === NULL)
- {
- if (empty($this->qb_set))
- {
- return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
- }
- }
- else
- {
- if (empty($set))
- {
- return ($this->db_debug) ? $this->display_error('insert_batch() called with no data') : FALSE;
- }
-
- $this->set_insert_batch($set, '', $escape);
- }
-
- if (strlen($table) === 0)
- {
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- $table = $this->qb_from[0];
- }
-
- // Batch this baby
- $affected_rows = 0;
- for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
- {
- if ($this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size))))
- {
- $affected_rows += $this->affected_rows();
- }
- }
-
- $this->_reset_write();
- return $affected_rows;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Insert batch statement
- *
- * Generates a platform-specific insert string from the supplied data.
- *
- * @param string $table Table name
- * @param array $keys INSERT keys
- * @param array $values INSERT values
- * @return string
- */
- protected function _insert_batch($table, $keys, $values)
- {
- return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
- *
- * @param mixed
- * @param string
- * @param bool
- * @return CI_DB_query_builder
- */
- public function set_insert_batch($key, $value = '', $escape = NULL)
- {
- $key = $this->_object_to_array_batch($key);
-
- if ( ! is_array($key))
- {
- $key = array($key => $value);
- }
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- $keys = array_keys($this->_object_to_array(reset($key)));
- sort($keys);
-
- foreach ($key as $row)
- {
- $row = $this->_object_to_array($row);
- if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
- {
- // batch function above returns an error on an empty array
- $this->qb_set[] = array();
- return;
- }
-
- ksort($row); // puts $row in the same order as our keys
-
- if ($escape !== FALSE)
- {
- $clean = array();
- foreach ($row as $value)
- {
- $clean[] = $this->escape($value);
- }
-
- $row = $clean;
- }
-
- $this->qb_set[] = '('.implode(',', $row).')';
- }
-
- foreach ($keys as $k)
- {
- $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Get INSERT query string
- *
- * Compiles an insert query and returns the sql
- *
- * @param string the table to insert into
- * @param bool TRUE: reset QB values; FALSE: leave QB values alone
- * @return string
- */
- public function get_compiled_insert($table = '', $reset = TRUE)
- {
- if ($this->_validate_insert($table) === FALSE)
- {
- return FALSE;
- }
-
- $sql = $this->_insert(
- $this->protect_identifiers(
- $this->qb_from[0], TRUE, NULL, FALSE
- ),
- array_keys($this->qb_set),
- array_values($this->qb_set)
- );
-
- if ($reset === TRUE)
- {
- $this->_reset_write();
- }
-
- return $sql;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Insert
- *
- * Compiles an insert string and runs the query
- *
- * @param string the table to insert data into
- * @param array an associative array of insert values
- * @param bool $escape Whether to escape values and identifiers
- * @return bool TRUE on success, FALSE on failure
- */
- public function insert($table = '', $set = NULL, $escape = NULL)
- {
- if ($set !== NULL)
- {
- $this->set($set, '', $escape);
- }
-
- if ($this->_validate_insert($table) === FALSE)
- {
- return FALSE;
- }
-
- $sql = $this->_insert(
- $this->protect_identifiers(
- $this->qb_from[0], TRUE, $escape, FALSE
- ),
- array_keys($this->qb_set),
- array_values($this->qb_set)
- );
-
- $this->_reset_write();
- return $this->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Validate Insert
- *
- * This method is used by both insert() and get_compiled_insert() to
- * validate that the there data is actually being set and that table
- * has been chosen to be inserted into.
- *
- * @param string the table to insert data into
- * @return string
- */
- protected function _validate_insert($table = '')
- {
- if (count($this->qb_set) === 0)
- {
- return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
- }
-
- if ($table !== '')
- {
- $this->qb_from[0] = $table;
- }
- elseif ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- return TRUE;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Replace
- *
- * Compiles an replace into string and runs the query
- *
- * @param string the table to replace data into
- * @param array an associative array of insert values
- * @return bool TRUE on success, FALSE on failure
- */
- public function replace($table = '', $set = NULL)
- {
- if ($set !== NULL)
- {
- $this->set($set);
- }
-
- if (count($this->qb_set) === 0)
- {
- return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
- }
-
- if ($table === '')
- {
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- $table = $this->qb_from[0];
- }
-
- $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set));
-
- $this->_reset_write();
- return $this->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Replace statement
- *
- * Generates a platform-specific replace string from the supplied data
- *
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
- */
- protected function _replace($table, $keys, $values)
- {
- return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * FROM tables
- *
- * Groups tables in FROM clauses if needed, so there is no confusion
- * about operator precedence.
- *
- * Note: This is only used (and overridden) by MySQL and CUBRID.
- *
- * @return string
- */
- protected function _from_tables()
- {
- return implode(', ', $this->qb_from);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Get UPDATE query string
- *
- * Compiles an update query and returns the sql
- *
- * @param string the table to update
- * @param bool TRUE: reset QB values; FALSE: leave QB values alone
- * @return string
- */
- public function get_compiled_update($table = '', $reset = TRUE)
- {
- // Combine any cached components with the current statements
- $this->_merge_cache();
-
- if ($this->_validate_update($table) === FALSE)
- {
- return FALSE;
- }
-
- $sql = $this->_update($this->qb_from[0], $this->qb_set);
-
- if ($reset === TRUE)
- {
- $this->_reset_write();
- }
-
- return $sql;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * UPDATE
- *
- * Compiles an update string and runs the query.
- *
- * @param string $table
- * @param array $set An associative array of update values
- * @param mixed $where
- * @param int $limit
- * @return bool TRUE on success, FALSE on failure
- */
- public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
- {
- // Combine any cached components with the current statements
- $this->_merge_cache();
-
- if ($set !== NULL)
- {
- $this->set($set);
- }
-
- if ($this->_validate_update($table) === FALSE)
- {
- return FALSE;
- }
-
- if ($where !== NULL)
- {
- $this->where($where);
- }
-
- if ( ! empty($limit))
- {
- $this->limit($limit);
- }
-
- $sql = $this->_update($this->qb_from[0], $this->qb_set);
- $this->_reset_write();
- return $this->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Validate Update
- *
- * This method is used by both update() and get_compiled_update() to
- * validate that data is actually being set and that a table has been
- * chosen to be update.
- *
- * @param string the table to update data on
- * @return bool
- */
- protected function _validate_update($table)
- {
- if (count($this->qb_set) === 0)
- {
- return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
- }
-
- if ($table !== '')
- {
- $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
- }
- elseif ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- return TRUE;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Update_Batch
- *
- * Compiles an update string and runs the query
- *
- * @param string the table to retrieve the results from
- * @param array an associative array of update values
- * @param string the where key
- * @return int number of rows affected or FALSE on failure
- */
- public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
- {
- // Combine any cached components with the current statements
- $this->_merge_cache();
-
- if ($index === NULL)
- {
- return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
- }
-
- if ($set === NULL)
- {
- if (empty($this->qb_set_ub))
- {
- return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
- }
- }
- else
- {
- if (empty($set))
- {
- return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE;
- }
-
- $this->set_update_batch($set, $index);
- }
-
- if (strlen($table) === 0)
- {
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- $table = $this->qb_from[0];
- }
-
- // Batch this baby
- $affected_rows = 0;
- for ($i = 0, $total = count($this->qb_set_ub); $i < $total; $i += $batch_size)
- {
- if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set_ub, $i, $batch_size), $index)))
- {
- $affected_rows += $this->affected_rows();
- }
-
- $this->qb_where = array();
- }
-
- $this->_reset_write();
- return $affected_rows;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Update_Batch statement
- *
- * Generates a platform-specific batch update string from the supplied data
- *
- * @param string $table Table name
- * @param array $values Update data
- * @param string $index WHERE key
- * @return string
- */
- protected function _update_batch($table, $values, $index)
- {
- $ids = array();
- foreach ($values as $key => $val)
- {
- $ids[] = $val[$index]['value'];
-
- foreach (array_keys($val) as $field)
- {
- if ($field !== $index)
- {
- $final[$val[$field]['field']][] = 'WHEN '.$val[$index]['field'].' = '.$val[$index]['value'].' THEN '.$val[$field]['value'];
- }
- }
- }
-
- $cases = '';
- foreach ($final as $k => $v)
- {
- $cases .= $k." = CASE \n"
- .implode("\n", $v)."\n"
- .'ELSE '.$k.' END, ';
- }
-
- $this->where($val[$index]['field'].' IN('.implode(',', $ids).')', NULL, FALSE);
-
- return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
- *
- * @param array
- * @param string
- * @param bool
- * @return CI_DB_query_builder
- */
- public function set_update_batch($key, $index = '', $escape = NULL)
- {
- $key = $this->_object_to_array_batch($key);
-
- if ( ! is_array($key))
- {
- // @todo error
- }
-
- is_bool($escape) OR $escape = $this->_protect_identifiers;
-
- foreach ($key as $k => $v)
- {
- $index_set = FALSE;
- $clean = array();
- foreach ($v as $k2 => $v2)
- {
- if ($k2 === $index)
- {
- $index_set = TRUE;
- }
-
- $clean[$k2] = array(
- 'field' => $this->protect_identifiers($k2, FALSE, $escape),
- 'value' => ($escape === FALSE ? $v2 : $this->escape($v2))
- );
- }
-
- if ($index_set === FALSE)
- {
- return $this->display_error('db_batch_missing_index');
- }
-
- $this->qb_set_ub[] = $clean;
- }
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Empty Table
- *
- * Compiles a delete string and runs "DELETE FROM table"
- *
- * @param string the table to empty
- * @return bool TRUE on success, FALSE on failure
- */
- public function empty_table($table = '')
- {
- if ($table === '')
- {
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- $table = $this->qb_from[0];
- }
- else
- {
- $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
- }
-
- $sql = $this->_delete($table);
- $this->_reset_write();
- return $this->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Truncate
- *
- * Compiles a truncate string and runs the query
- * If the database does not support the truncate() command
- * This function maps to "DELETE FROM table"
- *
- * @param string the table to truncate
- * @return bool TRUE on success, FALSE on failure
- */
- public function truncate($table = '')
- {
- if ($table === '')
- {
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- $table = $this->qb_from[0];
- }
- else
- {
- $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
- }
-
- $sql = $this->_truncate($table);
- $this->_reset_write();
- return $this->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Truncate statement
- *
- * Generates a platform-specific truncate string from the supplied data
- *
- * If the database does not support the truncate() command,
- * then this method maps to 'DELETE FROM table'
- *
- * @param string the table name
- * @return string
- */
- protected function _truncate($table)
- {
- return 'TRUNCATE '.$table;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Get DELETE query string
- *
- * Compiles a delete query string and returns the sql
- *
- * @param string the table to delete from
- * @param bool TRUE: reset QB values; FALSE: leave QB values alone
- * @return string
- */
- public function get_compiled_delete($table = '', $reset = TRUE)
- {
- $this->return_delete_sql = TRUE;
- $sql = $this->delete($table, '', NULL, $reset);
- $this->return_delete_sql = FALSE;
- return $sql;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Delete
- *
- * Compiles a delete string and runs the query
- *
- * @param mixed the table(s) to delete from. String or array
- * @param mixed the where clause
- * @param mixed the limit clause
- * @param bool
- * @return mixed
- */
- public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
- {
- // Combine any cached components with the current statements
- $this->_merge_cache();
-
- if ($table === '')
- {
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
-
- $table = $this->qb_from[0];
- }
- elseif (is_array($table))
- {
- empty($where) && $reset_data = FALSE;
-
- foreach ($table as $single_table)
- {
- $this->delete($single_table, $where, $limit, $reset_data);
- }
-
- return;
- }
- else
- {
- $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
- }
-
- if ($where !== '')
- {
- $this->where($where);
- }
-
- if ( ! empty($limit))
- {
- $this->limit($limit);
- }
-
- if (count($this->qb_where) === 0)
- {
- return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
- }
-
- $sql = $this->_delete($table);
- if ($reset_data)
- {
- $this->_reset_write();
- }
-
- return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Delete statement
- *
- * Generates a platform-specific delete string from the supplied data
- *
- * @param string the table name
- * @return string
- */
- protected function _delete($table)
- {
- return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
- .($this->qb_limit !== FALSE ? ' LIMIT '.$this->qb_limit : '');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * DB Prefix
- *
- * Prepends a database prefix if one exists in configuration
- *
- * @param string the table
- * @return string
- */
- public function dbprefix($table = '')
- {
- if ($table === '')
- {
- $this->display_error('db_table_name_required');
- }
-
- return $this->dbprefix.$table;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Set DB Prefix
- *
- * Set's the DB Prefix to something new without needing to reconnect
- *
- * @param string the prefix
- * @return string
- */
- public function set_dbprefix($prefix = '')
- {
- return $this->dbprefix = $prefix;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Track Aliases
- *
- * Used to track SQL statements written with aliased tables.
- *
- * @param string The table to inspect
- * @return string
- */
- protected function _track_aliases($table)
- {
- if (is_array($table))
- {
- foreach ($table as $t)
- {
- $this->_track_aliases($t);
- }
- return;
- }
-
- // Does the string contain a comma? If so, we need to separate
- // the string into discreet statements
- if (strpos($table, ',') !== FALSE)
- {
- return $this->_track_aliases(explode(',', $table));
- }
-
- // if a table alias is used we can recognize it by a space
- if (strpos($table, ' ') !== FALSE)
- {
- // if the alias is written with the AS keyword, remove it
- $table = preg_replace('/\s+AS\s+/i', ' ', $table);
-
- // Grab the alias
- $table = trim(strrchr($table, ' '));
-
- // Store the alias, if it doesn't already exist
- if ( ! in_array($table, $this->qb_aliased_tables, TRUE))
- {
- $this->qb_aliased_tables[] = $table;
- if ($this->qb_caching === TRUE && ! in_array($table, $this->qb_cache_aliased_tables, TRUE))
- {
- $this->qb_cache_aliased_tables[] = $table;
- $this->qb_cache_exists[] = 'aliased_tables';
- }
- }
- }
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Compile the SELECT statement
- *
- * Generates a query string based on which functions were used.
- * Should not be called directly.
- *
- * @param bool $select_override
- * @return string
- */
- protected function _compile_select($select_override = FALSE)
- {
- // Combine any cached components with the current statements
- $this->_merge_cache();
-
- // Write the "select" portion of the query
- if ($select_override !== FALSE)
- {
- $sql = $select_override;
- }
- else
- {
- $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
-
- if (count($this->qb_select) === 0)
- {
- $sql .= '*';
- }
- else
- {
- // Cycle through the "select" portion of the query and prep each column name.
- // The reason we protect identifiers here rather than in the select() function
- // is because until the user calls the from() function we don't know if there are aliases
- foreach ($this->qb_select as $key => $val)
- {
- $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
- $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
- }
-
- $sql .= implode(', ', $this->qb_select);
- }
- }
-
- // Write the "FROM" portion of the query
- if (count($this->qb_from) > 0)
- {
- $sql .= "\nFROM ".$this->_from_tables();
- }
-
- // Write the "JOIN" portion of the query
- if (count($this->qb_join) > 0)
- {
- $sql .= "\n".implode("\n", $this->qb_join);
- }
-
- $sql .= $this->_compile_wh('qb_where')
- .$this->_compile_group_by()
- .$this->_compile_wh('qb_having')
- .$this->_compile_order_by(); // ORDER BY
-
- // LIMIT
- if ($this->qb_limit !== FALSE OR $this->qb_offset)
- {
- return $this->_limit($sql."\n");
- }
-
- return $sql;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Compile WHERE, HAVING statements
- *
- * Escapes identifiers in WHERE and HAVING statements at execution time.
- *
- * Required so that aliases are tracked properly, regardless of whether
- * where(), or_where(), having(), or_having are called prior to from(),
- * join() and dbprefix is added only if needed.
- *
- * @param string $qb_key 'qb_where' or 'qb_having'
- * @return string SQL statement
- */
- protected function _compile_wh($qb_key)
- {
- if (count($this->$qb_key) > 0)
- {
- for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
- {
- // Is this condition already compiled?
- if (is_string($this->{$qb_key}[$i]))
- {
- continue;
- }
- elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
- {
- $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'].(isset($this->{$qb_key}[$i]['value']) ? ' '.$this->{$qb_key}[$i]['value'] : '');
- continue;
- }
-
- // Split multiple conditions
- $conditions = preg_split(
- '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
- $this->{$qb_key}[$i]['condition'],
- -1,
- PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
- );
-
- for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
- {
- if (($op = $this->_get_operator($conditions[$ci])) === FALSE
- OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
- {
- continue;
- }
-
- // $matches = array(
- // 0 => '(test <= foo)', /* the whole thing */
- // 1 => '(', /* optional */
- // 2 => 'test', /* the field name */
- // 3 => ' <= ', /* $op */
- // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
- // 5 => ')' /* optional */
- // );
-
- if ( ! empty($matches[4]))
- {
- $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
- $matches[4] = ' '.$matches[4];
- }
-
- $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
- .' '.trim($matches[3]).$matches[4].$matches[5];
- }
-
- $this->{$qb_key}[$i] = implode('', $conditions).(isset($this->{$qb_key}[$i]['value']) ? ' '.$this->{$qb_key}[$i]['value'] : '');
- }
-
- return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
- .implode("\n", $this->$qb_key);
- }
-
- return '';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Compile GROUP BY
- *
- * Escapes identifiers in GROUP BY statements at execution time.
- *
- * Required so that aliases are tracked properly, regardless of whether
- * group_by() is called prior to from(), join() and dbprefix is added
- * only if needed.
- *
- * @return string SQL statement
- */
- protected function _compile_group_by()
- {
- if (count($this->qb_groupby) > 0)
- {
- for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
- {
- // Is it already compiled?
- if (is_string($this->qb_groupby[$i]))
- {
- continue;
- }
-
- $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE OR $this->_is_literal($this->qb_groupby[$i]['field']))
- ? $this->qb_groupby[$i]['field']
- : $this->protect_identifiers($this->qb_groupby[$i]['field']);
- }
-
- return "\nGROUP BY ".implode(', ', $this->qb_groupby);
- }
-
- return '';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Compile ORDER BY
- *
- * Escapes identifiers in ORDER BY statements at execution time.
- *
- * Required so that aliases are tracked properly, regardless of whether
- * order_by() is called prior to from(), join() and dbprefix is added
- * only if needed.
- *
- * @return string SQL statement
- */
- protected function _compile_order_by()
- {
- if (empty($this->qb_orderby))
- {
- return '';
- }
-
- for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
- {
- if (is_string($this->qb_orderby[$i]))
- {
- continue;
- }
-
- if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
- {
- $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
- }
-
- $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
- }
-
- return "\nORDER BY ".implode(', ', $this->qb_orderby);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Object to Array
- *
- * Takes an object as input and converts the class variables to array key/vals
- *
- * @param object
- * @return array
- */
- protected function _object_to_array($object)
- {
- if ( ! is_object($object))
- {
- return $object;
- }
-
- $array = array();
- foreach (get_object_vars($object) as $key => $val)
- {
- // There are some built in keys we need to ignore for this conversion
- if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
- {
- $array[$key] = $val;
- }
- }
-
- return $array;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Object to Array
- *
- * Takes an object as input and converts the class variables to array key/vals
- *
- * @param object
- * @return array
- */
- protected function _object_to_array_batch($object)
- {
- if ( ! is_object($object))
- {
- return $object;
- }
-
- $array = array();
- $out = get_object_vars($object);
- $fields = array_keys($out);
-
- foreach ($fields as $val)
- {
- // There are some built in keys we need to ignore for this conversion
- if ($val !== '_parent_name')
- {
- $i = 0;
- foreach ($out[$val] as $data)
- {
- $array[$i++][$val] = $data;
- }
- }
- }
-
- return $array;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Start Cache
- *
- * Starts QB caching
- *
- * @return CI_DB_query_builder
- */
- public function start_cache()
- {
- $this->qb_caching = TRUE;
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Stop Cache
- *
- * Stops QB caching
- *
- * @return CI_DB_query_builder
- */
- public function stop_cache()
- {
- $this->qb_caching = FALSE;
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Flush Cache
- *
- * Empties the QB cache
- *
- * @return CI_DB_query_builder
- */
- public function flush_cache()
- {
- $this->_reset_run(array(
- 'qb_cache_select' => array(),
- 'qb_cache_from' => array(),
- 'qb_cache_join' => array(),
- 'qb_cache_where' => array(),
- 'qb_cache_groupby' => array(),
- 'qb_cache_having' => array(),
- 'qb_cache_orderby' => array(),
- 'qb_cache_set' => array(),
- 'qb_cache_exists' => array(),
- 'qb_cache_no_escape' => array(),
- 'qb_cache_aliased_tables' => array()
- ));
-
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Merge Cache
- *
- * When called, this function merges any cached QB arrays with
- * locally called ones.
- *
- * @return void
- */
- protected function _merge_cache()
- {
- if (count($this->qb_cache_exists) === 0)
- {
- return;
- }
- elseif (in_array('select', $this->qb_cache_exists, TRUE))
- {
- $qb_no_escape = $this->qb_cache_no_escape;
- }
-
- foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
- {
- $qb_variable = 'qb_'.$val;
- $qb_cache_var = 'qb_cache_'.$val;
- $qb_new = $this->$qb_cache_var;
-
- for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
- {
- if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
- {
- $qb_new[] = $this->{$qb_variable}[$i];
- if ($val === 'select')
- {
- $qb_no_escape[] = $this->qb_no_escape[$i];
- }
- }
- }
-
- $this->$qb_variable = $qb_new;
- if ($val === 'select')
- {
- $this->qb_no_escape = $qb_no_escape;
- }
- }
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Is literal
- *
- * Determines if a string represents a literal value or a field name
- *
- * @param string $str
- * @return bool
- */
- protected function _is_literal($str)
- {
- $str = trim($str);
-
- if (empty($str) OR ctype_digit($str) OR (string) (float) $str === $str OR in_array(strtoupper($str), array('TRUE', 'FALSE'), TRUE))
- {
- return TRUE;
- }
-
- static $_str;
-
- if (empty($_str))
- {
- $_str = ($this->_escape_char !== '"')
- ? array('"', "'") : array("'");
- }
-
- return in_array($str[0], $_str, TRUE);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Reset Query Builder values.
- *
- * Publicly-visible method to reset the QB values.
- *
- * @return CI_DB_query_builder
- */
- public function reset_query()
- {
- $this->_reset_select();
- $this->_reset_write();
- return $this;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Resets the query builder values. Called by the get() function
- *
- * @param array An array of fields to reset
- * @return void
- */
- protected function _reset_run($qb_reset_items)
- {
- foreach ($qb_reset_items as $item => $default_value)
- {
- $this->$item = $default_value;
- }
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Resets the query builder values. Called by the get() function
- *
- * @return void
- */
- protected function _reset_select()
- {
- $this->_reset_run(array(
- 'qb_select' => array(),
- 'qb_from' => array(),
- 'qb_join' => array(),
- 'qb_where' => array(),
- 'qb_groupby' => array(),
- 'qb_having' => array(),
- 'qb_orderby' => array(),
- 'qb_aliased_tables' => array(),
- 'qb_no_escape' => array(),
- 'qb_distinct' => FALSE,
- 'qb_limit' => FALSE,
- 'qb_offset' => FALSE
- ));
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Resets the query builder "write" values.
- *
- * Called by the insert() update() insert_batch() update_batch() and delete() functions
- *
- * @return void
- */
- protected function _reset_write()
- {
- $this->_reset_run(array(
- 'qb_set' => array(),
- 'qb_set_ub' => array(),
- 'qb_from' => array(),
- 'qb_join' => array(),
- 'qb_where' => array(),
- 'qb_orderby' => array(),
- 'qb_keys' => array(),
- 'qb_limit' => FALSE
- ));
- }
-
- }
|