DB_active_rec.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. <?php die('This file is not really here!');
  2. /**
  3. * ------------- DO NOT UPLOAD THIS FILE TO LIVE SERVER ---------------------
  4. * -------------------------------------------------------------------------------
  5. * phpStorm Helper file for Active Record MySQL code completion
  6. * -------------------------------------------------------------------------------
  7. * Right click on system/core/database/DB_active_rec.php and Mark As Plain Text
  8. * -------------------------------------------------------------------------------
  9. *
  10. * PHP version 5
  11. *
  12. * LICENSE: GPL http://www.gnu.org/copyleft/gpl.html
  13. *
  14. * Created 2/03/12, 2:06 PM
  15. *
  16. * @category
  17. * @package CodeIgniter Active Record DB_active_rec.php
  18. * @author Jeff Behnke
  19. * @copyright 2009-11 Valid-Webs.com
  20. * @license GPL http://www.gnu.org/copyleft/gpl.html
  21. * @version 2012.02.03
  22. */
  23. /**
  24. * CodeIgniter
  25. *
  26. * An open source application development framework for PHP 5.1.6 or newer
  27. *
  28. * @package CodeIgniter
  29. * @author ExpressionEngine Dev Team
  30. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  31. * @license http://codeigniter.com/user_guide/license.html
  32. * @link http://codeigniter.com
  33. * @since Version 1.0
  34. * @filesource
  35. */
  36. // ------------------------------------------------------------------------
  37. /**
  38. * Active Record Class
  39. *
  40. * This is the platform-independent base Active Record implementation class.
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Drivers
  44. * @category Database
  45. * @author ExpressionEngine Dev Team
  46. * @link http://codeigniter.com/user_guide/database/
  47. *
  48. * @method int insert_id() insert_id() The insert ID number when performing database inserts.
  49. * @method int affected_rows() affected_rows() The number of affected rows, when doing "write" type queries (insert, update, etc.).
  50. * @method int count_all() count_all(string $table) Determine the number of rows in a particular table.
  51. */
  52. class CI_DB_active_record extends CI_DB_driver
  53. {
  54. var $ar_select = array();
  55. var $ar_distinct = FALSE;
  56. var $ar_from = array();
  57. var $ar_join = array();
  58. var $ar_where = array();
  59. var $ar_like = array();
  60. var $ar_groupby = array();
  61. var $ar_having = array();
  62. var $ar_keys = array();
  63. var $ar_limit = FALSE;
  64. var $ar_offset = FALSE;
  65. var $ar_order = FALSE;
  66. var $ar_orderby = array();
  67. var $ar_set = array();
  68. var $ar_wherein = array();
  69. var $ar_aliased_tables = array();
  70. var $ar_store_array = array();
  71. // Active Record Caching variables
  72. var $ar_caching = FALSE;
  73. var $ar_cache_exists = array();
  74. var $ar_cache_select = array();
  75. var $ar_cache_from = array();
  76. var $ar_cache_join = array();
  77. var $ar_cache_where = array();
  78. var $ar_cache_like = array();
  79. var $ar_cache_groupby = array();
  80. var $ar_cache_having = array();
  81. var $ar_cache_orderby = array();
  82. var $ar_cache_set = array();
  83. var $ar_no_escape = array();
  84. var $ar_cache_no_escape = array();
  85. // --------------------------------------------------------------------
  86. /**
  87. * Select
  88. *
  89. * Generates the SELECT portion of the query
  90. *
  91. * @param string $select
  92. * @param null $escape
  93. *
  94. * @return CI_DB_active_record|CI_DB_result
  95. */
  96. public function select($select = '*', $escape = NULL)
  97. {
  98. if (is_string($select))
  99. {
  100. $select = explode(',', $select);
  101. }
  102. foreach ($select as $val)
  103. {
  104. $val = trim($val);
  105. if ($val != '')
  106. {
  107. $this->ar_select[] = $val;
  108. $this->ar_no_escape[] = $escape;
  109. if ($this->ar_caching === TRUE)
  110. {
  111. $this->ar_cache_select[] = $val;
  112. $this->ar_cache_exists[] = 'select';
  113. $this->ar_cache_no_escape[] = $escape;
  114. }
  115. }
  116. }
  117. return $this;
  118. }
  119. // --------------------------------------------------------------------
  120. /**
  121. * Select Max
  122. *
  123. * Generates a SELECT MAX(field) portion of a query
  124. *
  125. * @param string $select the field
  126. * @param string $alias an alias
  127. *
  128. * @return CI_DB_active_record|CI_DB_result
  129. */
  130. public function select_max($select = '', $alias = '')
  131. {
  132. $this->_max_min_avg_sum($select, $alias, 'MAX');
  133. return $this;
  134. }
  135. // --------------------------------------------------------------------
  136. /**
  137. * Select Min
  138. *
  139. * Generates a SELECT MIN(field) portion of a query
  140. *
  141. * @param string $select the field
  142. * @param string $alias an alias
  143. *
  144. * @return CI_DB_active_record|CI_DB_result
  145. */
  146. public function select_min($select = '', $alias = '')
  147. {
  148. $this->_max_min_avg_sum($select, $alias, 'MIN');
  149. return $this;
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * Select Average
  154. *
  155. * Generates a SELECT AVG(field) portion of a query
  156. *
  157. * @param string $select the field
  158. * @param string $alias an alias
  159. *
  160. * @return CI_DB_active_record|CI_DB_result
  161. */
  162. public function select_avg($select = '', $alias = '')
  163. {
  164. $this->_max_min_avg_sum($select, $alias, 'AVG');
  165. return $this;
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Select Sum
  170. *
  171. * Generates a SELECT SUM(field) portion of a query
  172. *
  173. * @param string $select the field
  174. * @param string $alias an alias
  175. *
  176. * @return CI_DB_active_record|CI_DB_result
  177. */
  178. public function select_sum($select = '', $alias = '')
  179. {
  180. return $this->_max_min_avg_sum($select, $alias, 'SUM');
  181. }
  182. // --------------------------------------------------------------------
  183. /**
  184. * Processing Function for the four functions above:
  185. *
  186. * select_max()
  187. * select_min()
  188. * select_avg()
  189. * select_sum()
  190. *
  191. * @param string $select the string field
  192. * @param string $alias an string alias
  193. * @param string $type
  194. *
  195. * @return CI_DB_active_record|CI_DB_result
  196. */
  197. protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
  198. {
  199. if (!is_string($select) OR $select == '')
  200. {
  201. $this->display_error('db_invalid_query');
  202. }
  203. $type = strtoupper($type);
  204. if (!in_array($type, array(
  205. 'MAX',
  206. 'MIN',
  207. 'AVG',
  208. 'SUM'
  209. ))
  210. )
  211. {
  212. show_error('Invalid function type: ' . $type);
  213. }
  214. if ($alias == '')
  215. {
  216. $alias = $this->_create_alias_from_table(trim($select));
  217. }
  218. $sql = $type . '(' . $this->_protect_identifiers(trim($select)) . ') AS ' . $alias;
  219. $this->ar_select[] = $sql;
  220. if ($this->ar_caching === TRUE)
  221. {
  222. $this->ar_cache_select[] = $sql;
  223. $this->ar_cache_exists[] = 'select';
  224. }
  225. return $this;
  226. }
  227. // --------------------------------------------------------------------
  228. /**
  229. * Determines the alias name based on the table
  230. *
  231. * @param string $item
  232. *
  233. * @return string
  234. */
  235. protected function _create_alias_from_table($item)
  236. {
  237. if (strpos($item, '.') !== FALSE)
  238. {
  239. return end(explode('.', $item));
  240. }
  241. return $item;
  242. }
  243. // --------------------------------------------------------------------
  244. /**
  245. * DISTINCT
  246. *
  247. * Sets a flag which tells the query string compiler to add DISTINCT
  248. *
  249. * @param bool $val
  250. *
  251. * @return CI_DB_active_record|CI_DB_result
  252. */
  253. public function distinct($val = TRUE)
  254. {
  255. $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
  256. return $this;
  257. }
  258. // --------------------------------------------------------------------
  259. /**
  260. * From
  261. *
  262. * Generates the FROM portion of the query
  263. *
  264. * @param mixed $from can be a string or array
  265. *
  266. * @return CI_DB_active_record|CI_DB_result
  267. */
  268. public function from($from)
  269. {
  270. foreach ((array) $from as $val)
  271. {
  272. if (strpos($val, ',') !== FALSE)
  273. {
  274. foreach (explode(',', $val) as $v)
  275. {
  276. $v = trim($v);
  277. $this->_track_aliases($v);
  278. $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
  279. if ($this->ar_caching === TRUE)
  280. {
  281. $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
  282. $this->ar_cache_exists[] = 'from';
  283. }
  284. }
  285. }
  286. else
  287. {
  288. $val = trim($val);
  289. // Extract any aliases that might exist. We use this information
  290. // in the _protect_identifiers to know whether to add a table prefix
  291. $this->_track_aliases($val);
  292. $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
  293. if ($this->ar_caching === TRUE)
  294. {
  295. $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
  296. $this->ar_cache_exists[] = 'from';
  297. }
  298. }
  299. }
  300. return $this;
  301. }
  302. // --------------------------------------------------------------------
  303. /**
  304. * Join
  305. *
  306. * Generates the JOIN portion of the query
  307. *
  308. * @param string $table
  309. * @param string $cond the join condition
  310. * @param string $type the type of join
  311. *
  312. * @return CI_DB_active_record|CI_DB_result
  313. */
  314. public function join($table, $cond, $type = '')
  315. {
  316. if ($type != '')
  317. {
  318. $type = strtoupper(trim($type));
  319. if (!in_array($type, array(
  320. 'LEFT',
  321. 'RIGHT',
  322. 'OUTER',
  323. 'INNER',
  324. 'LEFT OUTER',
  325. 'RIGHT OUTER'
  326. ))
  327. )
  328. {
  329. $type = '';
  330. }
  331. else
  332. {
  333. $type .= ' ';
  334. }
  335. }
  336. // Extract any aliases that might exist. We use this information
  337. // in the _protect_identifiers to know whether to add a table prefix
  338. $this->_track_aliases($table);
  339. // Strip apart the condition and protect the identifiers
  340. if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
  341. {
  342. $match[1] = $this->_protect_identifiers($match[1]);
  343. $match[3] = $this->_protect_identifiers($match[3]);
  344. $cond = $match[1] . $match[2] . $match[3];
  345. }
  346. // Assemble the JOIN statement
  347. $join = $type . 'JOIN ' . $this->_protect_identifiers($table, TRUE, NULL, FALSE) . ' ON ' . $cond;
  348. $this->ar_join[] = $join;
  349. if ($this->ar_caching === TRUE)
  350. {
  351. $this->ar_cache_join[] = $join;
  352. $this->ar_cache_exists[] = 'join';
  353. }
  354. return $this;
  355. }
  356. // --------------------------------------------------------------------
  357. /**
  358. * Where
  359. *
  360. * Generates the WHERE portion of the query. Separates
  361. * multiple calls with AND
  362. *
  363. * @param mixed $key
  364. * @param null $value
  365. * @param bool $escape
  366. *
  367. * @return CI_DB_active_record|CI_DB_result
  368. */
  369. public function where($key, $value = NULL, $escape = TRUE)
  370. {
  371. return $this->_where($key, $value, 'AND ', $escape);
  372. }
  373. // --------------------------------------------------------------------
  374. /**
  375. * OR Where
  376. *
  377. * Generates the WHERE portion of the query. Separates
  378. * multiple calls with OR
  379. *
  380. * @param mixed $key
  381. * @param null $value
  382. * @param bool $escape
  383. *
  384. * @return CI_DB_active_record|CI_DB_result
  385. */
  386. public function or_where($key, $value = NULL, $escape = TRUE)
  387. {
  388. return $this->_where($key, $value, 'OR ', $escape);
  389. }
  390. // --------------------------------------------------------------------
  391. /**
  392. * Where
  393. *
  394. * Called by where() or or_where()
  395. *
  396. * @param mixed $key
  397. * @param null $value
  398. * @param string $type
  399. * @param null $escape
  400. *
  401. * @return CI_DB_active_record|CI_DB_result
  402. */
  403. protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
  404. {
  405. if (!is_array($key))
  406. {
  407. $key = array($key => $value);
  408. }
  409. // If the escape value was not set will will base it on the global setting
  410. if (!is_bool($escape))
  411. {
  412. $escape = $this->_protect_identifiers;
  413. }
  414. foreach ($key as $k => $v)
  415. {
  416. $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
  417. if (is_null($v) && !$this->_has_operator($k))
  418. {
  419. // value appears not to have been set, assign the test to IS NULL
  420. $k .= ' IS NULL';
  421. }
  422. if (!is_null($v))
  423. {
  424. if ($escape === TRUE)
  425. {
  426. $k = $this->_protect_identifiers($k, FALSE, $escape);
  427. $v = ' ' . $this->escape($v);
  428. }
  429. if (!$this->_has_operator($k))
  430. {
  431. $k .= ' = ';
  432. }
  433. }
  434. else
  435. {
  436. $k = $this->_protect_identifiers($k, FALSE, $escape);
  437. }
  438. $this->ar_where[] = $prefix . $k . $v;
  439. if ($this->ar_caching === TRUE)
  440. {
  441. $this->ar_cache_where[] = $prefix . $k . $v;
  442. $this->ar_cache_exists[] = 'where';
  443. }
  444. }
  445. return $this;
  446. }
  447. // --------------------------------------------------------------------
  448. /**
  449. * Where_in
  450. *
  451. * Generates a WHERE field IN ('item', 'item') SQL query joined with
  452. * AND if appropriate
  453. *
  454. * @param string $key The field to search
  455. * @param array $values The values searched on
  456. *
  457. * @return CI_DB_active_record|CI_DB_result
  458. */
  459. public function where_in($key = NULL, $values = NULL)
  460. {
  461. return $this->_where_in($key, $values);
  462. }
  463. // --------------------------------------------------------------------
  464. /**
  465. * Where_in_or
  466. *
  467. * Generates a WHERE field IN ('item', 'item') SQL query joined with
  468. * OR if appropriate
  469. *
  470. * @param string $key The field to search
  471. * @param array $values The values searched on
  472. *
  473. * @return CI_DB_active_record|CI_DB_result
  474. */
  475. public function or_where_in($key = NULL, $values = NULL)
  476. {
  477. return $this->_where_in($key, $values, FALSE, 'OR ');
  478. }
  479. // --------------------------------------------------------------------
  480. /**
  481. * Where_not_in
  482. *
  483. * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
  484. * with AND if appropriate
  485. *
  486. * @param string $key The field to search
  487. * @param array $values The values searched on
  488. *
  489. * @return CI_DB_active_record|CI_DB_result
  490. */
  491. public function where_not_in($key = NULL, $values = NULL)
  492. {
  493. return $this->_where_in($key, $values, TRUE);
  494. }
  495. // --------------------------------------------------------------------
  496. /**
  497. * Where_not_in_or
  498. *
  499. * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
  500. * with OR if appropriate
  501. *
  502. * @param string $key The field to search
  503. * @param array $values The values searched on
  504. *
  505. * @return CI_DB_active_record|CI_DB_result
  506. */
  507. public function or_where_not_in($key = NULL, $values = NULL)
  508. {
  509. return $this->_where_in($key, $values, TRUE, 'OR ');
  510. }
  511. // --------------------------------------------------------------------
  512. /**
  513. * Where_in
  514. *
  515. * Called by where_in, where_in_or, where_not_in, where_not_in_or
  516. *
  517. * @param string $key The field to search
  518. * @param array $values The values searched on
  519. * @param boolean $not If the statement would be IN or NOT IN
  520. * @param string $type
  521. *
  522. * @return CI_DB_active_record|CI_DB_result
  523. */
  524. protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
  525. {
  526. if ($key === NULL OR $values === NULL)
  527. {
  528. return null;
  529. }
  530. if (!is_array($values))
  531. {
  532. $values = array($values);
  533. }
  534. $not = ($not) ? ' NOT' : '';
  535. foreach ($values as $value)
  536. {
  537. $this->ar_wherein[] = $this->escape($value);
  538. }
  539. $prefix = (count($this->ar_where) == 0) ? '' : $type;
  540. $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
  541. $this->ar_where[] = $where_in;
  542. if ($this->ar_caching === TRUE)
  543. {
  544. $this->ar_cache_where[] = $where_in;
  545. $this->ar_cache_exists[] = 'where';
  546. }
  547. // reset the array for multiple calls
  548. $this->ar_wherein = array();
  549. return $this;
  550. }
  551. // --------------------------------------------------------------------
  552. /**
  553. * Like
  554. *
  555. * Generates a %LIKE% portion of the query. Separates
  556. * multiple calls with AND
  557. *
  558. * @param string $field
  559. * @param string $match
  560. * @param string $side
  561. *
  562. * @return CI_DB_active_record|CI_DB_result
  563. */
  564. public function like($field, $match = '', $side = 'both')
  565. {
  566. $this->_like($field, $match, 'AND ', $side);
  567. return $this;
  568. }
  569. // --------------------------------------------------------------------
  570. /**
  571. * Not Like
  572. *
  573. * Generates a NOT LIKE portion of the query. Separates
  574. * multiple calls with AND
  575. *
  576. * @param string $field
  577. * @param string $match
  578. * @param string $side
  579. *
  580. * @return CI_DB_active_record|CI_DB_result
  581. */
  582. public function not_like($field, $match = '', $side = 'both')
  583. {
  584. $this->_like($field, $match, 'AND ', $side, 'NOT');
  585. return $this;
  586. }
  587. // --------------------------------------------------------------------
  588. /**
  589. * OR Like
  590. *
  591. * Generates a %LIKE% portion of the query. Separates
  592. * multiple calls with OR
  593. *
  594. * @param string $field
  595. * @param string $match
  596. * @param string $side
  597. *
  598. * @return CI_DB_active_record|CI_DB_result
  599. */
  600. public function or_like($field, $match = '', $side = 'both')
  601. {
  602. return $this->_like($field, $match, 'OR ', $side);
  603. }
  604. // --------------------------------------------------------------------
  605. /**
  606. * OR Not Like
  607. *
  608. * Generates a NOT LIKE portion of the query. Separates
  609. * multiple calls with OR
  610. *
  611. * @param string $field
  612. * @param mixed $match
  613. * @param string $side
  614. *
  615. * @return CI_DB_active_record|CI_DB_result
  616. */
  617. public function or_not_like($field, $match = '', $side = 'both')
  618. {
  619. return $this->_like($field, $match, 'OR ', $side, 'NOT');
  620. }
  621. // --------------------------------------------------------------------
  622. /**
  623. * Like
  624. *
  625. * Called by like() or orlike()
  626. *
  627. * @param string $field
  628. * @param string $match
  629. * @param string $type
  630. * @param string $side
  631. * @param string $not
  632. *
  633. * @return CI_DB_active_record|CI_DB_result
  634. */
  635. protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
  636. {
  637. if (!is_array($field))
  638. {
  639. $field = array($field => $match);
  640. }
  641. foreach ($field as $k => $v)
  642. {
  643. $k = $this->_protect_identifiers($k);
  644. $prefix = (count($this->ar_like) == 0) ? '' : $type;
  645. $v = $this->escape_like_str($v);
  646. if ($side == 'before')
  647. {
  648. $like_statement = $prefix . " $k $not LIKE '%{$v}'";
  649. }
  650. elseif ($side == 'after')
  651. {
  652. $like_statement = $prefix . " $k $not LIKE '{$v}%'";
  653. }
  654. else
  655. {
  656. $like_statement = $prefix . " $k $not LIKE '%{$v}%'";
  657. }
  658. // some platforms require an escape sequence definition for LIKE wildcards
  659. if ($this->_like_escape_str != '')
  660. {
  661. $like_statement = $like_statement . sprintf($this->_like_escape_str, $this->_like_escape_chr);
  662. }
  663. $this->ar_like[] = $like_statement;
  664. if ($this->ar_caching === TRUE)
  665. {
  666. $this->ar_cache_like[] = $like_statement;
  667. $this->ar_cache_exists[] = 'like';
  668. }
  669. }
  670. return $this;
  671. }
  672. // --------------------------------------------------------------------
  673. /**
  674. * GROUP BY
  675. *
  676. * @param string $by
  677. *
  678. * @return CI_DB_active_record|CI_DB_result
  679. */
  680. public function group_by($by)
  681. {
  682. if (is_string($by))
  683. {
  684. $by = explode(',', $by);
  685. }
  686. foreach ($by as $val)
  687. {
  688. $val = trim($val);
  689. if ($val != '')
  690. {
  691. $this->ar_groupby[] = $this->_protect_identifiers($val);
  692. if ($this->ar_caching === TRUE)
  693. {
  694. $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
  695. $this->ar_cache_exists[] = 'groupby';
  696. }
  697. }
  698. }
  699. return $this;
  700. }
  701. // --------------------------------------------------------------------
  702. /**
  703. * Sets the HAVING value
  704. *
  705. * Separates multiple calls with AND
  706. *
  707. * @param mixed $key
  708. * @param string $value
  709. * @param bool $escape
  710. *
  711. * @return CI_DB_active_record|CI_DB_result
  712. */
  713. public function having($key, $value = '', $escape = TRUE)
  714. {
  715. $this->_having($key, $value, 'AND ', $escape);
  716. return $this;
  717. }
  718. // --------------------------------------------------------------------
  719. /**
  720. * Sets the OR HAVING value
  721. *
  722. * Separates multiple calls with OR
  723. *
  724. * @param mixed $key
  725. * @param string $value
  726. * @param bool $escape
  727. *
  728. * @return CI_DB_active_record|CI_DB_result
  729. */
  730. public function or_having($key, $value = '', $escape = TRUE)
  731. {
  732. $this->_having($key, $value, 'OR ', $escape);
  733. return $this;
  734. }
  735. // --------------------------------------------------------------------
  736. /**
  737. * Sets the HAVING values
  738. *
  739. * Called by having() or or_having()
  740. *
  741. * @param mixed $key
  742. * @param string $value
  743. * @param string $type
  744. * @param bool $escape
  745. *
  746. * @return CI_DB_active_record|CI_DB_result
  747. */
  748. protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
  749. {
  750. if (!is_array($key))
  751. {
  752. $key = array($key => $value);
  753. }
  754. foreach ($key as $k => $v)
  755. {
  756. $prefix = (count($this->ar_having) == 0) ? '' : $type;
  757. if ($escape === TRUE)
  758. {
  759. $k = $this->_protect_identifiers($k);
  760. }
  761. if (!$this->_has_operator($k))
  762. {
  763. $k .= ' = ';
  764. }
  765. if ($v != '')
  766. {
  767. $v = ' ' . $this->escape($v);
  768. }
  769. $this->ar_having[] = $prefix . $k . $v;
  770. if ($this->ar_caching === TRUE)
  771. {
  772. $this->ar_cache_having[] = $prefix . $k . $v;
  773. $this->ar_cache_exists[] = 'having';
  774. }
  775. }
  776. return $this;
  777. }
  778. // --------------------------------------------------------------------
  779. /**
  780. * Sets the ORDER BY value
  781. *
  782. * @param string $orderby
  783. * @param string $direction: asc or desc
  784. *
  785. * @return CI_DB_active_record|CI_DB_result
  786. */
  787. public function order_by($orderby, $direction = '')
  788. {
  789. if (strtolower($direction) == 'random')
  790. {
  791. $orderby = ''; // Random results want or don't need a field name
  792. $direction = $this->_random_keyword;
  793. }
  794. elseif (trim($direction) != '')
  795. {
  796. $direction = (in_array(strtoupper(trim($direction)), array(
  797. 'ASC',
  798. 'DESC'
  799. ), TRUE)) ? ' ' . $direction : ' ASC';
  800. }
  801. if (strpos($orderby, ',') !== FALSE)
  802. {
  803. $temp = array();
  804. foreach (explode(',', $orderby) as $part)
  805. {
  806. $part = trim($part);
  807. if (!in_array($part, $this->ar_aliased_tables))
  808. {
  809. $part = $this->_protect_identifiers(trim($part));
  810. }
  811. $temp[] = $part;
  812. }
  813. $orderby = implode(', ', $temp);
  814. }
  815. else if ($direction != $this->_random_keyword)
  816. {
  817. $orderby = $this->_protect_identifiers($orderby);
  818. }
  819. $orderby_statement = $orderby . $direction;
  820. $this->ar_orderby[] = $orderby_statement;
  821. if ($this->ar_caching === TRUE)
  822. {
  823. $this->ar_cache_orderby[] = $orderby_statement;
  824. $this->ar_cache_exists[] = 'orderby';
  825. }
  826. return $this;
  827. }
  828. // --------------------------------------------------------------------
  829. /**
  830. * Sets the LIMIT value
  831. *
  832. * @param integer $value the offset value
  833. * @param string $offset
  834. *
  835. * @return CI_DB_active_record|CI_DB_result
  836. */
  837. public function limit($value, $offset = '')
  838. {
  839. $this->ar_limit = (int) $value;
  840. if ($offset != '')
  841. {
  842. $this->ar_offset = (int) $offset;
  843. }
  844. return $this;
  845. }
  846. // --------------------------------------------------------------------
  847. /**
  848. * Sets the OFFSET value
  849. *
  850. * @param integer $offset the offset value
  851. *
  852. * @return CI_DB_active_record|CI_DB_result
  853. */
  854. public function offset($offset)
  855. {
  856. $this->ar_offset = $offset;
  857. return $this;
  858. }
  859. // --------------------------------------------------------------------
  860. /**
  861. * The "set" function. Allows key/value pairs to be set for inserting or updating
  862. *
  863. * @param mixed $key
  864. * @param string $value
  865. * @param boolean $escape
  866. *
  867. * @return CI_DB_active_record|CI_DB_result
  868. */
  869. public function set($key, $value = '', $escape = TRUE)
  870. {
  871. $key = $this->_object_to_array($key);
  872. if (!is_array($key))
  873. {
  874. $key = array($key => $value);
  875. }
  876. foreach ($key as $k => $v)
  877. {
  878. if ($escape === FALSE)
  879. {
  880. $this->ar_set[$this->_protect_identifiers($k)] = $v;
  881. }
  882. else
  883. {
  884. $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
  885. }
  886. }
  887. return $this;
  888. }
  889. // --------------------------------------------------------------------
  890. /**
  891. * Get
  892. *
  893. * Compiles the select statement based on the other functions called
  894. * and runs the query
  895. *
  896. * @param string $table the table
  897. * @param string $limit the limit clause
  898. * @param string $offset the offset clause
  899. *
  900. * @return mixed $result
  901. */
  902. public function get($table = '', $limit = null, $offset = null)
  903. {
  904. if ($table != '')
  905. {
  906. $this->_track_aliases($table);
  907. $this->from($table);
  908. }
  909. if (!is_null($limit))
  910. {
  911. $this->limit($limit, $offset);
  912. }
  913. $sql = $this->_compile_select();
  914. $result = $this->query($sql);
  915. $this->_reset_select();
  916. return $result;
  917. }
  918. /**
  919. * "Count All Results" query
  920. *
  921. * Generates a platform-specific query string that counts all records
  922. * returned by an Active Record query.
  923. *
  924. * @param string $table
  925. *
  926. * @return int
  927. */
  928. public function count_all_results($table = '')
  929. {
  930. if ($table != '')
  931. {
  932. $this->_track_aliases($table);
  933. $this->from($table);
  934. }
  935. $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
  936. $query = $this->query($sql);
  937. $this->_reset_select();
  938. if ($query->num_rows() == 0)
  939. {
  940. return 0;
  941. }
  942. $row = $query->row();
  943. return (int) $row->numrows;
  944. }
  945. // --------------------------------------------------------------------
  946. /**
  947. * Get_Where
  948. *
  949. * Allows the where clause, limit and offset to be added directly
  950. *
  951. * @param string $table
  952. * @param null $where the string where clause
  953. * @param null $limit the string limit clause
  954. * @param null $offset the string offset clause
  955. *
  956. * @return CI_DB_active_record|CI_DB_result
  957. */
  958. public function get_where($table = '', $where = null, $limit = null, $offset = null)
  959. {
  960. if ($table != '')
  961. {
  962. $this->from($table);
  963. }
  964. if (!is_null($where))
  965. {
  966. $this->where($where);
  967. }
  968. if (!is_null($limit))
  969. {
  970. $this->limit($limit, $offset);
  971. }
  972. $sql = $this->_compile_select();
  973. $result = $this->query($sql);
  974. $this->_reset_select();
  975. return $result;
  976. }
  977. // --------------------------------------------------------------------
  978. /**
  979. * Insert_Batch
  980. *
  981. * Compiles batch insert strings and runs the queries
  982. *
  983. * @param string $table the table to retrieve the results from
  984. * @param array $set an associative array of insert values
  985. *
  986. * @return CI_DB_active_record|CI_DB_result
  987. */
  988. public function insert_batch($table = '', $set = NULL)
  989. {
  990. if (!is_null($set))
  991. {
  992. $this->set_insert_batch($set);
  993. }
  994. if (count($this->ar_set) == 0)
  995. {
  996. if ($this->db_debug)
  997. {
  998. //No valid data array. Folds in cases where keys and values did not match up
  999. return $this->display_error('db_must_use_set');
  1000. }
  1001. return FALSE;
  1002. }
  1003. if ($table == '')
  1004. {
  1005. if (!isset($this->ar_from[0]))
  1006. {
  1007. if ($this->db_debug)
  1008. {
  1009. return $this->display_error('db_must_set_table');
  1010. }
  1011. return FALSE;
  1012. }
  1013. $table = $this->ar_from[0];
  1014. }
  1015. // Batch this baby
  1016. for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
  1017. {
  1018. $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
  1019. //echo $sql;
  1020. $this->query($sql);
  1021. }
  1022. $this->_reset_write();
  1023. return TRUE;
  1024. }
  1025. // --------------------------------------------------------------------
  1026. /**
  1027. * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
  1028. *
  1029. * @param mixed $key
  1030. * @param string $value
  1031. * @param boolean $escape
  1032. *
  1033. * @return CI_DB_active_record|CI_DB_result
  1034. */
  1035. public function set_insert_batch($key, $value = '', $escape = TRUE)
  1036. {
  1037. $key = $this->_object_to_array_batch($key);
  1038. if (!is_array($key))
  1039. {
  1040. $key = array($key => $value);
  1041. }
  1042. $keys = array_keys(current($key));
  1043. sort($keys);
  1044. foreach ($key as $row)
  1045. {
  1046. if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
  1047. {
  1048. // batch function above returns an error on an empty array
  1049. $this->ar_set[] = array();
  1050. return;
  1051. }
  1052. ksort($row); // puts $row in the same order as our keys
  1053. if ($escape === FALSE)
  1054. {
  1055. $this->ar_set[] = '(' . implode(',', $row) . ')';
  1056. }
  1057. else
  1058. {
  1059. $clean = array();
  1060. foreach ($row as $value)
  1061. {
  1062. $clean[] = $this->escape($value);
  1063. }
  1064. $this->ar_set[] = '(' . implode(',', $clean) . ')';
  1065. }
  1066. }
  1067. foreach ($keys as $k)
  1068. {
  1069. $this->ar_keys[] = $this->_protect_identifiers($k);
  1070. }
  1071. return $this;
  1072. }
  1073. // --------------------------------------------------------------------
  1074. /**
  1075. * Insert
  1076. *
  1077. * Compiles an insert string and runs the query
  1078. *
  1079. * @param string $table the table to insert data into
  1080. * @param array $set an associative array of insert values
  1081. *
  1082. * @return CI_DB_active_record|CI_DB_result
  1083. */
  1084. function insert($table = '', $set = NULL)
  1085. {
  1086. if (!is_null($set))
  1087. {
  1088. $this->set($set);
  1089. }
  1090. if (count($this->ar_set) == 0)
  1091. {
  1092. if ($this->db_debug)
  1093. {
  1094. return $this->display_error('db_must_use_set');
  1095. }
  1096. return FALSE;
  1097. }
  1098. if ($table == '')
  1099. {
  1100. if (!isset($this->ar_from[0]))
  1101. {
  1102. if ($this->db_debug)
  1103. {
  1104. return $this->display_error('db_must_set_table');
  1105. }
  1106. return FALSE;
  1107. }
  1108. $table = $this->ar_from[0];
  1109. }
  1110. $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
  1111. $this->_reset_write();
  1112. return $this->query($sql);
  1113. }
  1114. // --------------------------------------------------------------------
  1115. /**
  1116. * Replace
  1117. *
  1118. * Compiles an replace into string and runs the query
  1119. *
  1120. * @param string $table the table to replace data into
  1121. * @param array $set an associative array of insert values
  1122. *
  1123. * @return CI_DB_active_record|CI_DB_result
  1124. */
  1125. public function replace($table = '', $set = NULL)
  1126. {
  1127. if (!is_null($set))
  1128. {
  1129. $this->set($set);
  1130. }
  1131. if (count($this->ar_set) == 0)
  1132. {
  1133. if ($this->db_debug)
  1134. {
  1135. return $this->display_error('db_must_use_set');
  1136. }
  1137. return FALSE;
  1138. }
  1139. if ($table == '')
  1140. {
  1141. if (!isset($this->ar_from[0]))
  1142. {
  1143. if ($this->db_debug)
  1144. {
  1145. return $this->display_error('db_must_set_table');
  1146. }
  1147. return FALSE;
  1148. }
  1149. $table = $this->ar_from[0];
  1150. }
  1151. $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
  1152. $this->_reset_write();
  1153. return $this->query($sql);
  1154. }
  1155. // --------------------------------------------------------------------
  1156. /**
  1157. * Update
  1158. *
  1159. * Compiles an update string and runs the query
  1160. *
  1161. * @param string $table the table to retrieve the results from
  1162. * @param array $set an associative array of update values
  1163. * @param mixed $where the where clause
  1164. * @param bool $limit
  1165. *
  1166. * @return CI_DB_active_record|CI_DB_result
  1167. */
  1168. public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
  1169. {
  1170. // Combine any cached components with the current statements
  1171. $this->_merge_cache();
  1172. if (!is_null($set))
  1173. {
  1174. $this->set($set);
  1175. }
  1176. if (count($this->ar_set) == 0)
  1177. {
  1178. if ($this->db_debug)
  1179. {
  1180. return $this->display_error('db_must_use_set');
  1181. }
  1182. return FALSE;
  1183. }
  1184. if ($table == '')
  1185. {
  1186. if (!isset($this->ar_from[0]))
  1187. {
  1188. if ($this->db_debug)
  1189. {
  1190. return $this->display_error('db_must_set_table');
  1191. }
  1192. return FALSE;
  1193. }
  1194. $table = $this->ar_from[0];
  1195. }
  1196. if ($where != NULL)
  1197. {
  1198. $this->where($where);
  1199. }
  1200. if ($limit != NULL)
  1201. {
  1202. $this->limit($limit);
  1203. }
  1204. $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
  1205. $this->_reset_write();
  1206. return $this->query($sql);
  1207. }
  1208. // --------------------------------------------------------------------
  1209. /**
  1210. * Update_Batch
  1211. *
  1212. * Compiles an update string and runs the query
  1213. *
  1214. * @param string $table the table to retrieve the results from
  1215. * @param array $set an associative array of update values
  1216. * @param string $index the where key
  1217. *
  1218. * @return CI_DB_active_record|CI_DB_result
  1219. */
  1220. public function update_batch($table = '', $set = NULL, $index = NULL)
  1221. {
  1222. // Combine any cached components with the current statements
  1223. $this->_merge_cache();
  1224. if (is_null($index))
  1225. {
  1226. if ($this->db_debug)
  1227. {
  1228. return $this->display_error('db_must_use_index');
  1229. }
  1230. return FALSE;
  1231. }
  1232. if (!is_null($set))
  1233. {
  1234. $this->set_update_batch($set, $index);
  1235. }
  1236. if (count($this->ar_set) == 0)
  1237. {
  1238. if ($this->db_debug)
  1239. {
  1240. return $this->display_error('db_must_use_set');
  1241. }
  1242. return FALSE;
  1243. }
  1244. if ($table == '')
  1245. {
  1246. if (!isset($this->ar_from[0]))
  1247. {
  1248. if ($this->db_debug)
  1249. {
  1250. return $this->display_error('db_must_set_table');
  1251. }
  1252. return FALSE;
  1253. }
  1254. $table = $this->ar_from[0];
  1255. }
  1256. // Batch this baby
  1257. for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
  1258. {
  1259. $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
  1260. $this->query($sql);
  1261. }
  1262. $this->_reset_write();
  1263. }
  1264. // --------------------------------------------------------------------
  1265. /**
  1266. * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
  1267. *
  1268. * @param array $key
  1269. * @param string $index
  1270. * @param boolean $escape
  1271. *
  1272. * @return CI_DB_active_record|CI_DB_result
  1273. */
  1274. public function set_update_batch($key, $index = '', $escape = TRUE)
  1275. {
  1276. $key = $this->_object_to_array_batch($key);
  1277. if (!is_array($key))
  1278. {
  1279. // @todo error
  1280. }
  1281. foreach ($key as $k => $v)
  1282. {
  1283. $index_set = FALSE;
  1284. $clean = array();
  1285. foreach ($v as $k2 => $v2)
  1286. {
  1287. if ($k2 == $index)
  1288. {
  1289. $index_set = TRUE;
  1290. }
  1291. else
  1292. {
  1293. $not[] = $k . '-' . $v;
  1294. }
  1295. if ($escape === FALSE)
  1296. {
  1297. $clean[$this->_protect_identifiers($k2)] = $v2;
  1298. }
  1299. else
  1300. {
  1301. $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
  1302. }
  1303. }
  1304. if ($index_set == FALSE)
  1305. {
  1306. return $this->display_error('db_batch_missing_index');
  1307. }
  1308. $this->ar_set[] = $clean;
  1309. }
  1310. return $this;
  1311. }
  1312. // --------------------------------------------------------------------
  1313. /**
  1314. * Empty Table
  1315. *
  1316. * Compiles a delete string and runs "DELETE FROM table"
  1317. *
  1318. * @param string $table the table to empty
  1319. *
  1320. * @return CI_DB_active_record|CI_DB_result
  1321. */
  1322. public function empty_table($table = '')
  1323. {
  1324. if ($table == '')
  1325. {
  1326. if (!isset($this->ar_from[0]))
  1327. {
  1328. if ($this->db_debug)
  1329. {
  1330. return $this->display_error('db_must_set_table');
  1331. }
  1332. return FALSE;
  1333. }
  1334. $table = $this->ar_from[0];
  1335. }
  1336. else
  1337. {
  1338. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1339. }
  1340. $sql = $this->_delete($table);
  1341. $this->_reset_write();
  1342. return $this->query($sql);
  1343. }
  1344. // --------------------------------------------------------------------
  1345. /**
  1346. * Truncate
  1347. *
  1348. * Compiles a truncate string and runs the query
  1349. * If the database does not support the truncate() command
  1350. * This function maps to "DELETE FROM table"
  1351. *
  1352. * @param string $table the table to truncate
  1353. *
  1354. * @return CI_DB_active_record|CI_DB_result
  1355. */
  1356. public function truncate($table = '')
  1357. {
  1358. if ($table == '')
  1359. {
  1360. if (!isset($this->ar_from[0]))
  1361. {
  1362. if ($this->db_debug)
  1363. {
  1364. return $this->display_error('db_must_set_table');
  1365. }
  1366. return FALSE;
  1367. }
  1368. $table = $this->ar_from[0];
  1369. }
  1370. else
  1371. {
  1372. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1373. }
  1374. $sql = $this->_truncate($table);
  1375. $this->_reset_write();
  1376. return $this->query($sql);
  1377. }
  1378. // --------------------------------------------------------------------
  1379. /**
  1380. * Delete
  1381. *
  1382. * Compiles a delete string and runs the query
  1383. *
  1384. * @param mixed $table the table(s) to delete from. String or array
  1385. * @param mixed $where the where clause
  1386. * @param mixed $limit the limit clause
  1387. * @param boolean $reset_data
  1388. *
  1389. * @return CI_DB_active_record|CI_DB_result
  1390. */
  1391. public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
  1392. {
  1393. // Combine any cached components with the current statements
  1394. $this->_merge_cache();
  1395. if ($table == '')
  1396. {
  1397. if (!isset($this->ar_from[0]))
  1398. {
  1399. if ($this->db_debug)
  1400. {
  1401. return $this->display_error('db_must_set_table');
  1402. }
  1403. return FALSE;
  1404. }
  1405. $table = $this->ar_from[0];
  1406. }
  1407. elseif (is_array($table))
  1408. {
  1409. foreach ($table as $single_table)
  1410. {
  1411. $this->delete($single_table, $where, $limit, FALSE);
  1412. }
  1413. $this->_reset_write();
  1414. return;
  1415. }
  1416. else
  1417. {
  1418. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1419. }
  1420. if ($where != '')
  1421. {
  1422. $this->where($where);
  1423. }
  1424. if ($limit != NULL)
  1425. {
  1426. $this->limit($limit);
  1427. }
  1428. if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
  1429. {
  1430. if ($this->db_debug)
  1431. {
  1432. return $this->display_error('db_del_must_use_where');
  1433. }
  1434. return FALSE;
  1435. }
  1436. $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
  1437. if ($reset_data)
  1438. {
  1439. $this->_reset_write();
  1440. }
  1441. return $this->query($sql);
  1442. }
  1443. // --------------------------------------------------------------------
  1444. /**
  1445. * DB Prefix
  1446. *
  1447. * Prepends a database prefix if one exists in configuration
  1448. *
  1449. * @param string $table the table
  1450. *
  1451. * @return string
  1452. */
  1453. public function dbprefix($table = '')
  1454. {
  1455. if ($table == '')
  1456. {
  1457. $this->display_error('db_table_name_required');
  1458. }
  1459. return $this->dbprefix . $table;
  1460. }
  1461. // --------------------------------------------------------------------
  1462. /**
  1463. * Set DB Prefix
  1464. *
  1465. * Set's the DB Prefix to something new without needing to reconnect
  1466. *
  1467. * @param string $prefix the prefix
  1468. *
  1469. * @return string
  1470. */
  1471. public function set_dbprefix($prefix = '')
  1472. {
  1473. return $this->dbprefix = $prefix;
  1474. }
  1475. // --------------------------------------------------------------------
  1476. /**
  1477. * Track Aliases
  1478. *
  1479. * Used to track SQL statements written with aliased tables.
  1480. *
  1481. * @param string $table The table to inspect
  1482. *
  1483. * @return string
  1484. */
  1485. protected function _track_aliases($table)
  1486. {
  1487. if (is_array($table))
  1488. {
  1489. foreach ($table as $t)
  1490. {
  1491. $this->_track_aliases($t);
  1492. }
  1493. return;
  1494. }
  1495. // Does the string contain a comma? If so, we need to separate
  1496. // the string into discreet statements
  1497. if (strpos($table, ',') !== FALSE)
  1498. {
  1499. return $this->_track_aliases(explode(',', $table));
  1500. }
  1501. // if a table alias is used we can recognize it by a space
  1502. if (strpos($table, " ") !== FALSE)
  1503. {
  1504. // if the alias is written with the AS keyword, remove it
  1505. $table = preg_replace('/ AS /i', ' ', $table);
  1506. // Grab the alias
  1507. $table = trim(strrchr($table, " "));
  1508. // Store the alias, if it doesn't already exist
  1509. if (!in_array($table, $this->ar_aliased_tables))
  1510. {
  1511. $this->ar_aliased_tables[] = $table;
  1512. }
  1513. }
  1514. }
  1515. // --------------------------------------------------------------------
  1516. /**
  1517. * Compile the SELECT statement
  1518. *
  1519. * Generates a query string based on which functions were used.
  1520. * Should not be called directly. The get() function calls it.
  1521. *
  1522. * @param bool $select_override
  1523. *
  1524. * @return string
  1525. */
  1526. protected function _compile_select($select_override = FALSE)
  1527. {
  1528. // Combine any cached components with the current statements
  1529. $this->_merge_cache();
  1530. // ----------------------------------------------------------------
  1531. // Write the "select" portion of the query
  1532. if ($select_override !== FALSE)
  1533. {
  1534. $sql = $select_override;
  1535. }
  1536. else
  1537. {
  1538. $sql = (!$this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
  1539. if (count($this->ar_select) == 0)
  1540. {
  1541. $sql .= '*';
  1542. }
  1543. else
  1544. {
  1545. // Cycle through the "select" portion of the query and prep each column name.
  1546. // The reason we protect identifiers here rather then in the select() function
  1547. // is because until the user calls the from() function we don't know if there are aliases
  1548. foreach ($this->ar_select as $key => $val)
  1549. {
  1550. $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
  1551. $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
  1552. }
  1553. $sql .= implode(', ', $this->ar_select);
  1554. }
  1555. }
  1556. // ----------------------------------------------------------------
  1557. // Write the "FROM" portion of the query
  1558. if (count($this->ar_from) > 0)
  1559. {
  1560. $sql .= "\nFROM ";
  1561. $sql .= $this->_from_tables($this->ar_from);
  1562. }
  1563. // ----------------------------------------------------------------
  1564. // Write the "JOIN" portion of the query
  1565. if (count($this->ar_join) > 0)
  1566. {
  1567. $sql .= "\n";
  1568. $sql .= implode("\n", $this->ar_join);
  1569. }
  1570. // ----------------------------------------------------------------
  1571. // Write the "WHERE" portion of the query
  1572. if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
  1573. {
  1574. $sql .= "\nWHERE ";
  1575. }
  1576. $sql .= implode("\n", $this->ar_where);
  1577. // ----------------------------------------------------------------
  1578. // Write the "LIKE" portion of the query
  1579. if (count($this->ar_like) > 0)
  1580. {
  1581. if (count($this->ar_where) > 0)
  1582. {
  1583. $sql .= "\nAND ";
  1584. }
  1585. $sql .= implode("\n", $this->ar_like);
  1586. }
  1587. // ----------------------------------------------------------------
  1588. // Write the "GROUP BY" portion of the query
  1589. if (count($this->ar_groupby) > 0)
  1590. {
  1591. $sql .= "\nGROUP BY ";
  1592. $sql .= implode(', ', $this->ar_groupby);
  1593. }
  1594. // ----------------------------------------------------------------
  1595. // Write the "HAVING" portion of the query
  1596. if (count($this->ar_having) > 0)
  1597. {
  1598. $sql .= "\nHAVING ";
  1599. $sql .= implode("\n", $this->ar_having);
  1600. }
  1601. // ----------------------------------------------------------------
  1602. // Write the "ORDER BY" portion of the query
  1603. if (count($this->ar_orderby) > 0)
  1604. {
  1605. $sql .= "\nORDER BY ";
  1606. $sql .= implode(', ', $this->ar_orderby);
  1607. if ($this->ar_order !== FALSE)
  1608. {
  1609. $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
  1610. }
  1611. }
  1612. // ----------------------------------------------------------------
  1613. // Write the "LIMIT" portion of the query
  1614. if (is_numeric($this->ar_limit))
  1615. {
  1616. $sql .= "\n";
  1617. $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
  1618. }
  1619. return $sql;
  1620. }
  1621. // --------------------------------------------------------------------
  1622. /**
  1623. * Object to Array
  1624. *
  1625. * Takes an object as input and converts the class variables to array key/vals
  1626. *
  1627. * @param object $object
  1628. *
  1629. * @return array
  1630. */
  1631. public function _object_to_array($object)
  1632. {
  1633. if (!is_object($object))
  1634. {
  1635. return $object;
  1636. }
  1637. $array = array();
  1638. foreach (get_object_vars($object) as $key => $val)
  1639. {
  1640. // There are some built in keys we need to ignore for this conversion
  1641. if (!is_object($val) && !is_array($val) && $key != '_parent_name')
  1642. {
  1643. $array[$key] = $val;
  1644. }
  1645. }
  1646. return $array;
  1647. }
  1648. // --------------------------------------------------------------------
  1649. /**
  1650. * Object to Array
  1651. *
  1652. * Takes an object as input and converts the class variables to array key/vals
  1653. *
  1654. * @param object $object
  1655. *
  1656. * @return array
  1657. */
  1658. public function _object_to_array_batch($object)
  1659. {
  1660. if (!is_object($object))
  1661. {
  1662. return $object;
  1663. }
  1664. $array = array();
  1665. $out = get_object_vars($object);
  1666. $fields = array_keys($out);
  1667. foreach ($fields as $val)
  1668. {
  1669. // There are some built in keys we need to ignore for this conversion
  1670. if ($val != '_parent_name')
  1671. {
  1672. $i = 0;
  1673. foreach ($out[$val] as $data)
  1674. {
  1675. $array[$i][$val] = $data;
  1676. $i++;
  1677. }
  1678. }
  1679. }
  1680. return $array;
  1681. }
  1682. // --------------------------------------------------------------------
  1683. /**
  1684. * Start Cache
  1685. *
  1686. * Starts AR caching
  1687. *
  1688. * @return void
  1689. */
  1690. public function start_cache()
  1691. {
  1692. $this->ar_caching = TRUE;
  1693. }
  1694. // --------------------------------------------------------------------
  1695. /**
  1696. * Stop Cache
  1697. *
  1698. * Stops AR caching
  1699. *
  1700. * @return void
  1701. */
  1702. public function stop_cache()
  1703. {
  1704. $this->ar_caching = FALSE;
  1705. }
  1706. // --------------------------------------------------------------------
  1707. /**
  1708. * Flush Cache
  1709. *
  1710. * Empties the AR cache
  1711. *
  1712. * @access public
  1713. * @return void
  1714. */
  1715. public function flush_cache()
  1716. {
  1717. $this->_reset_run(array(
  1718. 'ar_cache_select' => array(),
  1719. 'ar_cache_from' => array(),
  1720. 'ar_cache_join' => array(),
  1721. 'ar_cache_where' => array(),
  1722. 'ar_cache_like' => array(),
  1723. 'ar_cache_groupby' => array(),
  1724. 'ar_cache_having' => array(),
  1725. 'ar_cache_orderby' => array(),
  1726. 'ar_cache_set' => array(),
  1727. 'ar_cache_exists' => array(),
  1728. 'ar_cache_no_escape' => array()
  1729. ));
  1730. }
  1731. // --------------------------------------------------------------------
  1732. /**
  1733. * Merge Cache
  1734. *
  1735. * When called, this function merges any cached AR arrays with
  1736. * locally called ones.
  1737. *
  1738. * @return void
  1739. */
  1740. protected function _merge_cache()
  1741. {
  1742. if (count($this->ar_cache_exists) == 0)
  1743. {
  1744. return;
  1745. }
  1746. foreach ($this->ar_cache_exists as $val)
  1747. {
  1748. $ar_variable = 'ar_' . $val;
  1749. $ar_cache_var = 'ar_cache_' . $val;
  1750. if (count($this->$ar_cache_var) == 0)
  1751. {
  1752. continue;
  1753. }
  1754. $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
  1755. }
  1756. // If we are "protecting identifiers" we need to examine the "from"
  1757. // portion of the query to determine if there are any aliases
  1758. if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
  1759. {
  1760. $this->_track_aliases($this->ar_from);
  1761. }
  1762. $this->ar_no_escape = $this->ar_cache_no_escape;
  1763. }
  1764. // --------------------------------------------------------------------
  1765. /**
  1766. * Resets the active record values. Called by the get() function
  1767. *
  1768. * @param array $ar_reset_items An array of fields to reset
  1769. *
  1770. * @return void
  1771. */
  1772. protected function _reset_run($ar_reset_items)
  1773. {
  1774. foreach ($ar_reset_items as $item => $default_value)
  1775. {
  1776. if (!in_array($item, $this->ar_store_array))
  1777. {
  1778. $this->$item = $default_value;
  1779. }
  1780. }
  1781. }
  1782. // --------------------------------------------------------------------
  1783. /**
  1784. * Resets the active record values. Called by the get() function
  1785. *
  1786. * @return void
  1787. */
  1788. protected function _reset_select()
  1789. {
  1790. $ar_reset_items = array(
  1791. 'ar_select' => array(),
  1792. 'ar_from' => array(),
  1793. 'ar_join' => array(),
  1794. 'ar_where' => array(),
  1795. 'ar_like' => array(),
  1796. 'ar_groupby' => array(),
  1797. 'ar_having' => array(),
  1798. 'ar_orderby' => array(),
  1799. 'ar_wherein' => array(),
  1800. 'ar_aliased_tables' => array(),
  1801. 'ar_no_escape' => array(),
  1802. 'ar_distinct' => FALSE,
  1803. 'ar_limit' => FALSE,
  1804. 'ar_offset' => FALSE,
  1805. 'ar_order' => FALSE,
  1806. );
  1807. $this->_reset_run($ar_reset_items);
  1808. }
  1809. // --------------------------------------------------------------------
  1810. /**
  1811. * Resets the active record "write" values.
  1812. *
  1813. * Called by the insert() update() insert_batch() update_batch() and delete() functions
  1814. *
  1815. * @return void
  1816. */
  1817. protected function _reset_write()
  1818. {
  1819. $ar_reset_items = array(
  1820. 'ar_set' => array(),
  1821. 'ar_from' => array(),
  1822. 'ar_where' => array(),
  1823. 'ar_like' => array(),
  1824. 'ar_orderby' => array(),
  1825. 'ar_keys' => array(),
  1826. 'ar_limit' => FALSE,
  1827. 'ar_order' => FALSE
  1828. );
  1829. $this->_reset_run($ar_reset_items);
  1830. }
  1831. }
  1832. /* End of file DB_active_rec.php */
  1833. /* Location: ./system/database/DB_active_rec.php */