Select.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Select
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. require_once 'Zend/Db/Adapter/Abstract.php';
  26. /**
  27. * @see Zend_Db_Expr
  28. */
  29. require_once 'Zend/Db/Expr.php';
  30. /**
  31. * Class for SQL SELECT generation and results.
  32. *
  33. * @category Zend
  34. * @package Zend_Db
  35. * @subpackage Select
  36. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Db_Select
  40. {
  41. const DISTINCT = 'distinct';
  42. const COLUMNS = 'columns';
  43. const FROM = 'from';
  44. const UNION = 'union';
  45. const WHERE = 'where';
  46. const GROUP = 'group';
  47. const HAVING = 'having';
  48. const ORDER = 'order';
  49. const LIMIT_COUNT = 'limitcount';
  50. const LIMIT_OFFSET = 'limitoffset';
  51. const FOR_UPDATE = 'forupdate';
  52. const INNER_JOIN = 'inner join';
  53. const LEFT_JOIN = 'left join';
  54. const RIGHT_JOIN = 'right join';
  55. const FULL_JOIN = 'full join';
  56. const CROSS_JOIN = 'cross join';
  57. const NATURAL_JOIN = 'natural join';
  58. const SQL_WILDCARD = '*';
  59. const SQL_SELECT = 'SELECT';
  60. const SQL_UNION = 'UNION';
  61. const SQL_UNION_ALL = 'UNION ALL';
  62. const SQL_FROM = 'FROM';
  63. const SQL_WHERE = 'WHERE';
  64. const SQL_DISTINCT = 'DISTINCT';
  65. const SQL_GROUP_BY = 'GROUP BY';
  66. const SQL_ORDER_BY = 'ORDER BY';
  67. const SQL_HAVING = 'HAVING';
  68. const SQL_FOR_UPDATE = 'FOR UPDATE';
  69. const SQL_AND = 'AND';
  70. const SQL_AS = 'AS';
  71. const SQL_OR = 'OR';
  72. const SQL_ON = 'ON';
  73. const SQL_ASC = 'ASC';
  74. const SQL_DESC = 'DESC';
  75. const REGEX_COLUMN_EXPR = '/^([\w]*\(([^\(\)]|(?1))*\))$/';
  76. /**
  77. * Bind variables for query
  78. *
  79. * @var array
  80. */
  81. protected $_bind = array();
  82. /**
  83. * Zend_Db_Adapter_Abstract object.
  84. *
  85. * @var Zend_Db_Adapter_Abstract
  86. */
  87. protected $_adapter;
  88. /**
  89. * The initial values for the $_parts array.
  90. * NOTE: It is important for the 'FOR_UPDATE' part to be last to ensure
  91. * meximum compatibility with database adapters.
  92. *
  93. * @var array
  94. */
  95. protected static $_partsInit = array(
  96. self::DISTINCT => false,
  97. self::COLUMNS => array(),
  98. self::UNION => array(),
  99. self::FROM => array(),
  100. self::WHERE => array(),
  101. self::GROUP => array(),
  102. self::HAVING => array(),
  103. self::ORDER => array(),
  104. self::LIMIT_COUNT => null,
  105. self::LIMIT_OFFSET => null,
  106. self::FOR_UPDATE => false
  107. );
  108. /**
  109. * Specify legal join types.
  110. *
  111. * @var array
  112. */
  113. protected static $_joinTypes = array(
  114. self::INNER_JOIN,
  115. self::LEFT_JOIN,
  116. self::RIGHT_JOIN,
  117. self::FULL_JOIN,
  118. self::CROSS_JOIN,
  119. self::NATURAL_JOIN,
  120. );
  121. /**
  122. * Specify legal union types.
  123. *
  124. * @var array
  125. */
  126. protected static $_unionTypes = array(
  127. self::SQL_UNION,
  128. self::SQL_UNION_ALL
  129. );
  130. /**
  131. * The component parts of a SELECT statement.
  132. * Initialized to the $_partsInit array in the constructor.
  133. *
  134. * @var array
  135. */
  136. protected $_parts = array();
  137. /**
  138. * Tracks which columns are being select from each table and join.
  139. *
  140. * @var array
  141. */
  142. protected $_tableCols = array();
  143. /**
  144. * Class constructor
  145. *
  146. * @param Zend_Db_Adapter_Abstract $adapter
  147. */
  148. public function __construct(Zend_Db_Adapter_Abstract $adapter)
  149. {
  150. $this->_adapter = $adapter;
  151. $this->_parts = self::$_partsInit;
  152. }
  153. /**
  154. * Get bind variables
  155. *
  156. * @return array
  157. */
  158. public function getBind()
  159. {
  160. return $this->_bind;
  161. }
  162. /**
  163. * Set bind variables
  164. *
  165. * @param mixed $bind
  166. * @return Zend_Db_Select
  167. */
  168. public function bind($bind)
  169. {
  170. $this->_bind = $bind;
  171. return $this;
  172. }
  173. /**
  174. * Makes the query SELECT DISTINCT.
  175. *
  176. * @param bool $flag Whether or not the SELECT is DISTINCT (default true).
  177. * @return Zend_Db_Select This Zend_Db_Select object.
  178. */
  179. public function distinct($flag = true)
  180. {
  181. $this->_parts[self::DISTINCT] = (bool) $flag;
  182. return $this;
  183. }
  184. /**
  185. * Adds a FROM table and optional columns to the query.
  186. *
  187. * The first parameter $name can be a simple string, in which case the
  188. * correlation name is generated automatically. If you want to specify
  189. * the correlation name, the first parameter must be an associative
  190. * array in which the key is the correlation name, and the value is
  191. * the physical table name. For example, array('alias' => 'table').
  192. * The correlation name is prepended to all columns fetched for this
  193. * table.
  194. *
  195. * The second parameter can be a single string or Zend_Db_Expr object,
  196. * or else an array of strings or Zend_Db_Expr objects.
  197. *
  198. * The first parameter can be null or an empty string, in which case
  199. * no correlation name is generated or prepended to the columns named
  200. * in the second parameter.
  201. *
  202. * @param array|string|Zend_Db_Expr $name The table name or an associative array
  203. * relating correlation name to table name.
  204. * @param array|string|Zend_Db_Expr $cols The columns to select from this table.
  205. * @param string $schema The schema name to specify, if any.
  206. * @return Zend_Db_Select This Zend_Db_Select object.
  207. */
  208. public function from($name, $cols = '*', $schema = null)
  209. {
  210. return $this->_join(self::FROM, $name, null, $cols, $schema);
  211. }
  212. /**
  213. * Specifies the columns used in the FROM clause.
  214. *
  215. * The parameter can be a single string or Zend_Db_Expr object,
  216. * or else an array of strings or Zend_Db_Expr objects.
  217. *
  218. * @param array|string|Zend_Db_Expr $cols The columns to select from this table.
  219. * @param string $correlationName Correlation name of target table. OPTIONAL
  220. * @return Zend_Db_Select This Zend_Db_Select object.
  221. */
  222. public function columns($cols = '*', $correlationName = null)
  223. {
  224. if ($correlationName === null && count($this->_parts[self::FROM])) {
  225. $correlationNameKeys = array_keys($this->_parts[self::FROM]);
  226. $correlationName = current($correlationNameKeys);
  227. }
  228. if (!array_key_exists($correlationName, $this->_parts[self::FROM])) {
  229. /**
  230. * @see Zend_Db_Select_Exception
  231. */
  232. require_once 'Zend/Db/Select/Exception.php';
  233. throw new Zend_Db_Select_Exception("No table has been specified for the FROM clause");
  234. }
  235. $this->_tableCols($correlationName, $cols);
  236. return $this;
  237. }
  238. /**
  239. * Adds a UNION clause to the query.
  240. *
  241. * The first parameter has to be an array of Zend_Db_Select or
  242. * sql query strings.
  243. *
  244. * <code>
  245. * $sql1 = $db->select();
  246. * $sql2 = "SELECT ...";
  247. * $select = $db->select()
  248. * ->union(array($sql1, $sql2))
  249. * ->order("id");
  250. * </code>
  251. *
  252. * @param array $select Array of select clauses for the union.
  253. * @return Zend_Db_Select This Zend_Db_Select object.
  254. */
  255. public function union($select = array(), $type = self::SQL_UNION)
  256. {
  257. if (!is_array($select)) {
  258. require_once 'Zend/Db/Select/Exception.php';
  259. throw new Zend_Db_Select_Exception(
  260. "union() only accepts an array of Zend_Db_Select instances of sql query strings."
  261. );
  262. }
  263. if (!in_array($type, self::$_unionTypes)) {
  264. require_once 'Zend/Db/Select/Exception.php';
  265. throw new Zend_Db_Select_Exception("Invalid union type '{$type}'");
  266. }
  267. foreach ($select as $target) {
  268. $this->_parts[self::UNION][] = array($target, $type);
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Adds a JOIN table and columns to the query.
  274. *
  275. * The $name and $cols parameters follow the same logic
  276. * as described in the from() method.
  277. *
  278. * @param array|string|Zend_Db_Expr $name The table name.
  279. * @param string $cond Join on this condition.
  280. * @param array|string $cols The columns to select from the joined table.
  281. * @param string $schema The database name to specify, if any.
  282. * @return Zend_Db_Select This Zend_Db_Select object.
  283. */
  284. public function join($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
  285. {
  286. return $this->joinInner($name, $cond, $cols, $schema);
  287. }
  288. /**
  289. * Add an INNER JOIN table and colums to the query
  290. * Rows in both tables are matched according to the expression
  291. * in the $cond argument. The result set is comprised
  292. * of all cases where rows from the left table match
  293. * rows from the right table.
  294. *
  295. * The $name and $cols parameters follow the same logic
  296. * as described in the from() method.
  297. *
  298. * @param array|string|Zend_Db_Expr $name The table name.
  299. * @param string $cond Join on this condition.
  300. * @param array|string $cols The columns to select from the joined table.
  301. * @param string $schema The database name to specify, if any.
  302. * @return Zend_Db_Select This Zend_Db_Select object.
  303. */
  304. public function joinInner($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
  305. {
  306. return $this->_join(self::INNER_JOIN, $name, $cond, $cols, $schema);
  307. }
  308. /**
  309. * Add a LEFT OUTER JOIN table and colums to the query
  310. * All rows from the left operand table are included,
  311. * matching rows from the right operand table included,
  312. * and the columns from the right operand table are filled
  313. * with NULLs if no row exists matching the left table.
  314. *
  315. * The $name and $cols parameters follow the same logic
  316. * as described in the from() method.
  317. *
  318. * @param array|string|Zend_Db_Expr $name The table name.
  319. * @param string $cond Join on this condition.
  320. * @param array|string $cols The columns to select from the joined table.
  321. * @param string $schema The database name to specify, if any.
  322. * @return Zend_Db_Select This Zend_Db_Select object.
  323. */
  324. public function joinLeft($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
  325. {
  326. return $this->_join(self::LEFT_JOIN, $name, $cond, $cols, $schema);
  327. }
  328. /**
  329. * Add a RIGHT OUTER JOIN table and colums to the query.
  330. * Right outer join is the complement of left outer join.
  331. * All rows from the right operand table are included,
  332. * matching rows from the left operand table included,
  333. * and the columns from the left operand table are filled
  334. * with NULLs if no row exists matching the right table.
  335. *
  336. * The $name and $cols parameters follow the same logic
  337. * as described in the from() method.
  338. *
  339. * @param array|string|Zend_Db_Expr $name The table name.
  340. * @param string $cond Join on this condition.
  341. * @param array|string $cols The columns to select from the joined table.
  342. * @param string $schema The database name to specify, if any.
  343. * @return Zend_Db_Select This Zend_Db_Select object.
  344. */
  345. public function joinRight($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
  346. {
  347. return $this->_join(self::RIGHT_JOIN, $name, $cond, $cols, $schema);
  348. }
  349. /**
  350. * Add a FULL OUTER JOIN table and colums to the query.
  351. * A full outer join is like combining a left outer join
  352. * and a right outer join. All rows from both tables are
  353. * included, paired with each other on the same row of the
  354. * result set if they satisfy the join condition, and otherwise
  355. * paired with NULLs in place of columns from the other table.
  356. *
  357. * The $name and $cols parameters follow the same logic
  358. * as described in the from() method.
  359. *
  360. * @param array|string|Zend_Db_Expr $name The table name.
  361. * @param string $cond Join on this condition.
  362. * @param array|string $cols The columns to select from the joined table.
  363. * @param string $schema The database name to specify, if any.
  364. * @return Zend_Db_Select This Zend_Db_Select object.
  365. */
  366. public function joinFull($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
  367. {
  368. return $this->_join(self::FULL_JOIN, $name, $cond, $cols, $schema);
  369. }
  370. /**
  371. * Add a CROSS JOIN table and colums to the query.
  372. * A cross join is a cartesian product; there is no join condition.
  373. *
  374. * The $name and $cols parameters follow the same logic
  375. * as described in the from() method.
  376. *
  377. * @param array|string|Zend_Db_Expr $name The table name.
  378. * @param array|string $cols The columns to select from the joined table.
  379. * @param string $schema The database name to specify, if any.
  380. * @return Zend_Db_Select This Zend_Db_Select object.
  381. */
  382. public function joinCross($name, $cols = self::SQL_WILDCARD, $schema = null)
  383. {
  384. return $this->_join(self::CROSS_JOIN, $name, null, $cols, $schema);
  385. }
  386. /**
  387. * Add a NATURAL JOIN table and colums to the query.
  388. * A natural join assumes an equi-join across any column(s)
  389. * that appear with the same name in both tables.
  390. * Only natural inner joins are supported by this API,
  391. * even though SQL permits natural outer joins as well.
  392. *
  393. * The $name and $cols parameters follow the same logic
  394. * as described in the from() method.
  395. *
  396. * @param array|string|Zend_Db_Expr $name The table name.
  397. * @param array|string $cols The columns to select from the joined table.
  398. * @param string $schema The database name to specify, if any.
  399. * @return Zend_Db_Select This Zend_Db_Select object.
  400. */
  401. public function joinNatural($name, $cols = self::SQL_WILDCARD, $schema = null)
  402. {
  403. return $this->_join(self::NATURAL_JOIN, $name, null, $cols, $schema);
  404. }
  405. /**
  406. * Adds a WHERE condition to the query by AND.
  407. *
  408. * If a value is passed as the second param, it will be quoted
  409. * and replaced into the condition wherever a question-mark
  410. * appears. Array values are quoted and comma-separated.
  411. *
  412. * <code>
  413. * // simplest but non-secure
  414. * $select->where("id = $id");
  415. *
  416. * // secure (ID is quoted but matched anyway)
  417. * $select->where('id = ?', $id);
  418. *
  419. * // alternatively, with named binding
  420. * $select->where('id = :id');
  421. * </code>
  422. *
  423. * Note that it is more correct to use named bindings in your
  424. * queries for values other than strings. When you use named
  425. * bindings, don't forget to pass the values when actually
  426. * making a query:
  427. *
  428. * <code>
  429. * $db->fetchAll($select, array('id' => 5));
  430. * </code>
  431. *
  432. * @param string $cond The WHERE condition.
  433. * @param mixed $value OPTIONAL The value to quote into the condition.
  434. * @param int $type OPTIONAL The type of the given value
  435. * @return Zend_Db_Select This Zend_Db_Select object.
  436. */
  437. public function where($cond, $value = null, $type = null)
  438. {
  439. $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, true);
  440. return $this;
  441. }
  442. /**
  443. * Adds a WHERE condition to the query by OR.
  444. *
  445. * Otherwise identical to where().
  446. *
  447. * @param string $cond The WHERE condition.
  448. * @param mixed $value OPTIONAL The value to quote into the condition.
  449. * @param int $type OPTIONAL The type of the given value
  450. * @return Zend_Db_Select This Zend_Db_Select object.
  451. *
  452. * @see where()
  453. */
  454. public function orWhere($cond, $value = null, $type = null)
  455. {
  456. $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, false);
  457. return $this;
  458. }
  459. /**
  460. * Adds grouping to the query.
  461. *
  462. * @param array|string $spec The column(s) to group by.
  463. * @return Zend_Db_Select This Zend_Db_Select object.
  464. */
  465. public function group($spec)
  466. {
  467. if (!is_array($spec)) {
  468. $spec = array($spec);
  469. }
  470. foreach ($spec as $val) {
  471. if (preg_match(self::REGEX_COLUMN_EXPR, (string) $val)) {
  472. $val = new Zend_Db_Expr($val);
  473. }
  474. $this->_parts[self::GROUP][] = $val;
  475. }
  476. return $this;
  477. }
  478. /**
  479. * Adds a HAVING condition to the query by AND.
  480. *
  481. * If a value is passed as the second param, it will be quoted
  482. * and replaced into the condition wherever a question-mark
  483. * appears. See {@link where()} for an example
  484. *
  485. * @param string $cond The HAVING condition.
  486. * @param mixed $value OPTIONAL The value to quote into the condition.
  487. * @param int $type OPTIONAL The type of the given value
  488. * @return Zend_Db_Select This Zend_Db_Select object.
  489. */
  490. public function having($cond, $value = null, $type = null)
  491. {
  492. if ($value !== null) {
  493. $cond = $this->_adapter->quoteInto($cond, $value, $type);
  494. }
  495. if ($this->_parts[self::HAVING]) {
  496. $this->_parts[self::HAVING][] = self::SQL_AND . " ($cond)";
  497. } else {
  498. $this->_parts[self::HAVING][] = "($cond)";
  499. }
  500. return $this;
  501. }
  502. /**
  503. * Adds a HAVING condition to the query by OR.
  504. *
  505. * Otherwise identical to orHaving().
  506. *
  507. * @param string $cond The HAVING condition.
  508. * @param mixed $value OPTIONAL The value to quote into the condition.
  509. * @param int $type OPTIONAL The type of the given value
  510. * @return Zend_Db_Select This Zend_Db_Select object.
  511. *
  512. * @see having()
  513. */
  514. public function orHaving($cond, $value = null, $type = null)
  515. {
  516. if ($value !== null) {
  517. $cond = $this->_adapter->quoteInto($cond, $value, $type);
  518. }
  519. if ($this->_parts[self::HAVING]) {
  520. $this->_parts[self::HAVING][] = self::SQL_OR . " ($cond)";
  521. } else {
  522. $this->_parts[self::HAVING][] = "($cond)";
  523. }
  524. return $this;
  525. }
  526. /**
  527. * Adds a row order to the query.
  528. *
  529. * @param mixed $spec The column(s) and direction to order by.
  530. * @return Zend_Db_Select This Zend_Db_Select object.
  531. */
  532. public function order($spec)
  533. {
  534. if (!is_array($spec)) {
  535. $spec = array($spec);
  536. }
  537. // force 'ASC' or 'DESC' on each order spec, default is ASC.
  538. foreach ($spec as $val) {
  539. if ($val instanceof Zend_Db_Expr) {
  540. $expr = $val->__toString();
  541. if (empty($expr)) {
  542. continue;
  543. }
  544. $this->_parts[self::ORDER][] = $val;
  545. } else {
  546. if (empty($val)) {
  547. continue;
  548. }
  549. $direction = self::SQL_ASC;
  550. if (preg_match('/(.*\W)(' . self::SQL_ASC . '|' . self::SQL_DESC . ')\b/si', $val, $matches)) {
  551. $val = trim($matches[1]);
  552. $direction = $matches[2];
  553. }
  554. if (preg_match(self::REGEX_COLUMN_EXPR, (string) $val)) {
  555. $val = new Zend_Db_Expr($val);
  556. }
  557. $this->_parts[self::ORDER][] = array($val, $direction);
  558. }
  559. }
  560. return $this;
  561. }
  562. /**
  563. * Sets a limit count and offset to the query.
  564. *
  565. * @param int $count OPTIONAL The number of rows to return.
  566. * @param int $offset OPTIONAL Start returning after this many rows.
  567. * @return Zend_Db_Select This Zend_Db_Select object.
  568. */
  569. public function limit($count = null, $offset = null)
  570. {
  571. $this->_parts[self::LIMIT_COUNT] = (int) $count;
  572. $this->_parts[self::LIMIT_OFFSET] = (int) $offset;
  573. return $this;
  574. }
  575. /**
  576. * Sets the limit and count by page number.
  577. *
  578. * @param int $page Limit results to this page number.
  579. * @param int $rowCount Use this many rows per page.
  580. * @return Zend_Db_Select This Zend_Db_Select object.
  581. */
  582. public function limitPage($page, $rowCount)
  583. {
  584. $page = ($page > 0) ? $page : 1;
  585. $rowCount = ($rowCount > 0) ? $rowCount : 1;
  586. $this->_parts[self::LIMIT_COUNT] = (int) $rowCount;
  587. $this->_parts[self::LIMIT_OFFSET] = (int) $rowCount * ($page - 1);
  588. return $this;
  589. }
  590. /**
  591. * Makes the query SELECT FOR UPDATE.
  592. *
  593. * @param bool $flag Whether or not the SELECT is FOR UPDATE (default true).
  594. * @return Zend_Db_Select This Zend_Db_Select object.
  595. */
  596. public function forUpdate($flag = true)
  597. {
  598. $this->_parts[self::FOR_UPDATE] = (bool) $flag;
  599. return $this;
  600. }
  601. /**
  602. * Get part of the structured information for the current query.
  603. *
  604. * @param string $part
  605. * @return mixed
  606. * @throws Zend_Db_Select_Exception
  607. */
  608. public function getPart($part)
  609. {
  610. $part = strtolower($part);
  611. if (!array_key_exists($part, $this->_parts)) {
  612. require_once 'Zend/Db/Select/Exception.php';
  613. throw new Zend_Db_Select_Exception("Invalid Select part '$part'");
  614. }
  615. return $this->_parts[$part];
  616. }
  617. /**
  618. * Executes the current select object and returns the result
  619. *
  620. * @param integer $fetchMode OPTIONAL
  621. * @param mixed $bind An array of data to bind to the placeholders.
  622. * @return PDO_Statement|Zend_Db_Statement
  623. */
  624. public function query($fetchMode = null, $bind = array())
  625. {
  626. if (!empty($bind)) {
  627. $this->bind($bind);
  628. }
  629. $stmt = $this->_adapter->query($this);
  630. if ($fetchMode == null) {
  631. $fetchMode = $this->_adapter->getFetchMode();
  632. }
  633. $stmt->setFetchMode($fetchMode);
  634. return $stmt;
  635. }
  636. /**
  637. * Converts this object to an SQL SELECT string.
  638. *
  639. * @return string|null This object as a SELECT string. (or null if a string cannot be produced.)
  640. */
  641. public function assemble()
  642. {
  643. $sql = self::SQL_SELECT;
  644. foreach (array_keys(self::$_partsInit) as $part) {
  645. $method = '_render' . ucfirst($part);
  646. if (method_exists($this, $method)) {
  647. $sql = $this->$method($sql);
  648. }
  649. }
  650. return $sql;
  651. }
  652. /**
  653. * Clear parts of the Select object, or an individual part.
  654. *
  655. * @param string $part OPTIONAL
  656. * @return Zend_Db_Select
  657. */
  658. public function reset($part = null)
  659. {
  660. if ($part == null) {
  661. $this->_parts = self::$_partsInit;
  662. } elseif (array_key_exists($part, self::$_partsInit)) {
  663. $this->_parts[$part] = self::$_partsInit[$part];
  664. }
  665. return $this;
  666. }
  667. /**
  668. * Gets the Zend_Db_Adapter_Abstract for this
  669. * particular Zend_Db_Select object.
  670. *
  671. * @return Zend_Db_Adapter_Abstract
  672. */
  673. public function getAdapter()
  674. {
  675. return $this->_adapter;
  676. }
  677. /**
  678. * Populate the {@link $_parts} 'join' key
  679. *
  680. * Does the dirty work of populating the join key.
  681. *
  682. * The $name and $cols parameters follow the same logic
  683. * as described in the from() method.
  684. *
  685. * @param null|string $type Type of join; inner, left, and null are currently supported
  686. * @param array|string|Zend_Db_Expr $name Table name
  687. * @param string $cond Join on this condition
  688. * @param array|string $cols The columns to select from the joined table
  689. * @param string $schema The database name to specify, if any.
  690. * @return Zend_Db_Select This Zend_Db_Select object
  691. * @throws Zend_Db_Select_Exception
  692. */
  693. protected function _join($type, $name, $cond, $cols, $schema = null)
  694. {
  695. if (!in_array($type, self::$_joinTypes) && $type != self::FROM) {
  696. /**
  697. * @see Zend_Db_Select_Exception
  698. */
  699. require_once 'Zend/Db/Select/Exception.php';
  700. throw new Zend_Db_Select_Exception("Invalid join type '$type'");
  701. }
  702. if (count($this->_parts[self::UNION])) {
  703. require_once 'Zend/Db/Select/Exception.php';
  704. throw new Zend_Db_Select_Exception("Invalid use of table with " . self::SQL_UNION);
  705. }
  706. if (empty($name)) {
  707. $correlationName = $tableName = '';
  708. } elseif (is_array($name)) {
  709. // Must be array($correlationName => $tableName) or array($ident, ...)
  710. foreach ($name as $_correlationName => $_tableName) {
  711. if (is_string($_correlationName)) {
  712. // We assume the key is the correlation name and value is the table name
  713. $tableName = $_tableName;
  714. $correlationName = $_correlationName;
  715. } else {
  716. // We assume just an array of identifiers, with no correlation name
  717. $tableName = $_tableName;
  718. $correlationName = $this->_uniqueCorrelation($tableName);
  719. }
  720. break;
  721. }
  722. } elseif ($name instanceof Zend_Db_Expr|| $name instanceof Zend_Db_Select) {
  723. $tableName = $name;
  724. $correlationName = $this->_uniqueCorrelation('t');
  725. } elseif (preg_match('/^(.+)\s+AS\s+(.+)$/i', $name, $m)) {
  726. $tableName = $m[1];
  727. $correlationName = $m[2];
  728. } else {
  729. $tableName = $name;
  730. $correlationName = $this->_uniqueCorrelation($tableName);
  731. }
  732. // Schema from table name overrides schema argument
  733. if (!is_object($tableName) && false !== strpos($tableName, '.')) {
  734. list($schema, $tableName) = explode('.', $tableName);
  735. }
  736. $lastFromCorrelationName = null;
  737. if (!empty($correlationName)) {
  738. if (array_key_exists($correlationName, $this->_parts[self::FROM])) {
  739. /**
  740. * @see Zend_Db_Select_Exception
  741. */
  742. require_once 'Zend/Db/Select/Exception.php';
  743. throw new Zend_Db_Select_Exception("You cannot define a correlation name '$correlationName' more than once");
  744. }
  745. if ($type == self::FROM) {
  746. // append this from after the last from joinType
  747. $tmpFromParts = $this->_parts[self::FROM];
  748. $this->_parts[self::FROM] = array();
  749. // move all the froms onto the stack
  750. while ($tmpFromParts) {
  751. $currentCorrelationName = key($tmpFromParts);
  752. if ($tmpFromParts[$currentCorrelationName]['joinType'] != self::FROM) {
  753. break;
  754. }
  755. $lastFromCorrelationName = $currentCorrelationName;
  756. $this->_parts[self::FROM][$currentCorrelationName] = array_shift($tmpFromParts);
  757. }
  758. } else {
  759. $tmpFromParts = array();
  760. }
  761. $this->_parts[self::FROM][$correlationName] = array(
  762. 'joinType' => $type,
  763. 'schema' => $schema,
  764. 'tableName' => $tableName,
  765. 'joinCondition' => $cond
  766. );
  767. while ($tmpFromParts) {
  768. $currentCorrelationName = key($tmpFromParts);
  769. $this->_parts[self::FROM][$currentCorrelationName] = array_shift($tmpFromParts);
  770. }
  771. }
  772. // add to the columns from this joined table
  773. if ($type == self::FROM && $lastFromCorrelationName == null) {
  774. $lastFromCorrelationName = true;
  775. }
  776. $this->_tableCols($correlationName, $cols, $lastFromCorrelationName);
  777. return $this;
  778. }
  779. /**
  780. * Handle JOIN... USING... syntax
  781. *
  782. * This is functionality identical to the existing JOIN methods, however
  783. * the join condition can be passed as a single column name. This method
  784. * then completes the ON condition by using the same field for the FROM
  785. * table and the JOIN table.
  786. *
  787. * <code>
  788. * $select = $db->select()->from('table1')
  789. * ->joinUsing('table2', 'column1');
  790. *
  791. * // SELECT * FROM table1 JOIN table2 ON table1.column1 = table2.column2
  792. * </code>
  793. *
  794. * These joins are called by the developer simply by adding 'Using' to the
  795. * method name. E.g.
  796. * * joinUsing
  797. * * joinInnerUsing
  798. * * joinFullUsing
  799. * * joinRightUsing
  800. * * joinLeftUsing
  801. *
  802. * @return Zend_Db_Select This Zend_Db_Select object.
  803. */
  804. public function _joinUsing($type, $name, $cond, $cols = '*', $schema = null)
  805. {
  806. if (empty($this->_parts[self::FROM])) {
  807. require_once 'Zend/Db/Select/Exception.php';
  808. throw new Zend_Db_Select_Exception("You can only perform a joinUsing after specifying a FROM table");
  809. }
  810. $join = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM]), true);
  811. $from = $this->_adapter->quoteIdentifier($this->_uniqueCorrelation($name), true);
  812. $joinCond = array();
  813. foreach ((array)$cond as $fieldName) {
  814. $cond1 = $from . '.' . $fieldName;
  815. $cond2 = $join . '.' . $fieldName;
  816. $joinCond[] = $cond1 . ' = ' . $cond2;
  817. }
  818. $cond = implode(' '.self::SQL_AND.' ', $joinCond);
  819. return $this->_join($type, $name, $cond, $cols, $schema);
  820. }
  821. /**
  822. * Generate a unique correlation name
  823. *
  824. * @param string|array $name A qualified identifier.
  825. * @return string A unique correlation name.
  826. */
  827. private function _uniqueCorrelation($name)
  828. {
  829. if (is_array($name)) {
  830. $k = key($name);
  831. $c = is_string($k) ? $k : end($name);
  832. } else {
  833. // Extract just the last name of a qualified table name
  834. $dot = strrpos($name,'.');
  835. $c = ($dot === false) ? $name : substr($name, $dot+1);
  836. }
  837. for ($i = 2; array_key_exists($c, $this->_parts[self::FROM]); ++$i) {
  838. $c = $name . '_' . (string) $i;
  839. }
  840. return $c;
  841. }
  842. /**
  843. * Adds to the internal table-to-column mapping array.
  844. *
  845. * @param string $tbl The table/join the columns come from.
  846. * @param array|string $cols The list of columns; preferably as
  847. * an array, but possibly as a string containing one column.
  848. * @param bool|string True if it should be prepended, a correlation name if it should be inserted
  849. * @return void
  850. */
  851. protected function _tableCols($correlationName, $cols, $afterCorrelationName = null)
  852. {
  853. if (!is_array($cols)) {
  854. $cols = array($cols);
  855. }
  856. if ($correlationName == null) {
  857. $correlationName = '';
  858. }
  859. $columnValues = array();
  860. foreach (array_filter($cols) as $alias => $col) {
  861. $currentCorrelationName = $correlationName;
  862. if (is_string($col)) {
  863. // Check for a column matching "<column> AS <alias>" and extract the alias name
  864. if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
  865. $col = $m[1];
  866. $alias = $m[2];
  867. }
  868. // Check for columns that look like functions and convert to Zend_Db_Expr
  869. if (preg_match(self::REGEX_COLUMN_EXPR, (string) $col)) {
  870. $col = new Zend_Db_Expr($col);
  871. } elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
  872. $currentCorrelationName = $m[1];
  873. $col = $m[2];
  874. }
  875. }
  876. $columnValues[] = array($currentCorrelationName, $col, is_string($alias) ? $alias : null);
  877. }
  878. if ($columnValues) {
  879. // should we attempt to prepend or insert these values?
  880. if ($afterCorrelationName === true || is_string($afterCorrelationName)) {
  881. $tmpColumns = $this->_parts[self::COLUMNS];
  882. $this->_parts[self::COLUMNS] = array();
  883. } else {
  884. $tmpColumns = array();
  885. }
  886. // find the correlation name to insert after
  887. if (is_string($afterCorrelationName)) {
  888. while ($tmpColumns) {
  889. $this->_parts[self::COLUMNS][] = $currentColumn = array_shift($tmpColumns);
  890. if ($currentColumn[0] == $afterCorrelationName) {
  891. break;
  892. }
  893. }
  894. }
  895. // apply current values to current stack
  896. foreach ($columnValues as $columnValue) {
  897. array_push($this->_parts[self::COLUMNS], $columnValue);
  898. }
  899. // finish ensuring that all previous values are applied (if they exist)
  900. while ($tmpColumns) {
  901. array_push($this->_parts[self::COLUMNS], array_shift($tmpColumns));
  902. }
  903. }
  904. }
  905. /**
  906. * Internal function for creating the where clause
  907. *
  908. * @param string $condition
  909. * @param mixed $value optional
  910. * @param string $type optional
  911. * @param boolean $bool true = AND, false = OR
  912. * @return string clause
  913. */
  914. protected function _where($condition, $value = null, $type = null, $bool = true)
  915. {
  916. if (count($this->_parts[self::UNION])) {
  917. require_once 'Zend/Db/Select/Exception.php';
  918. throw new Zend_Db_Select_Exception("Invalid use of where clause with " . self::SQL_UNION);
  919. }
  920. if ($value !== null) {
  921. $condition = $this->_adapter->quoteInto($condition, $value, $type);
  922. }
  923. $cond = "";
  924. if ($this->_parts[self::WHERE]) {
  925. if ($bool === true) {
  926. $cond = self::SQL_AND . ' ';
  927. } else {
  928. $cond = self::SQL_OR . ' ';
  929. }
  930. }
  931. return $cond . "($condition)";
  932. }
  933. /**
  934. * @return array
  935. */
  936. protected function _getDummyTable()
  937. {
  938. return array();
  939. }
  940. /**
  941. * Return a quoted schema name
  942. *
  943. * @param string $schema The schema name OPTIONAL
  944. * @return string|null
  945. */
  946. protected function _getQuotedSchema($schema = null)
  947. {
  948. if ($schema === null) {
  949. return null;
  950. }
  951. return $this->_adapter->quoteIdentifier($schema, true) . '.';
  952. }
  953. /**
  954. * Return a quoted table name
  955. *
  956. * @param string $tableName The table name
  957. * @param string $correlationName The correlation name OPTIONAL
  958. * @return string
  959. */
  960. protected function _getQuotedTable($tableName, $correlationName = null)
  961. {
  962. return $this->_adapter->quoteTableAs($tableName, $correlationName, true);
  963. }
  964. /**
  965. * Render DISTINCT clause
  966. *
  967. * @param string $sql SQL query
  968. * @return string
  969. */
  970. protected function _renderDistinct($sql)
  971. {
  972. if ($this->_parts[self::DISTINCT]) {
  973. $sql .= ' ' . self::SQL_DISTINCT;
  974. }
  975. return $sql;
  976. }
  977. /**
  978. * Render DISTINCT clause
  979. *
  980. * @param string $sql SQL query
  981. * @return string|null
  982. */
  983. protected function _renderColumns($sql)
  984. {
  985. if (!count($this->_parts[self::COLUMNS])) {
  986. return null;
  987. }
  988. $columns = array();
  989. foreach ($this->_parts[self::COLUMNS] as $columnEntry) {
  990. list($correlationName, $column, $alias) = $columnEntry;
  991. if ($column instanceof Zend_Db_Expr) {
  992. $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true);
  993. } else {
  994. if ($column == self::SQL_WILDCARD) {
  995. $column = new Zend_Db_Expr(self::SQL_WILDCARD);
  996. $alias = null;
  997. }
  998. if (empty($correlationName)) {
  999. $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true);
  1000. } else {
  1001. $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true);
  1002. }
  1003. }
  1004. }
  1005. return $sql . ' ' . implode(', ', $columns);
  1006. }
  1007. /**
  1008. * Render FROM clause
  1009. *
  1010. * @param string $sql SQL query
  1011. * @return string
  1012. */
  1013. protected function _renderFrom($sql)
  1014. {
  1015. /*
  1016. * If no table specified, use RDBMS-dependent solution
  1017. * for table-less query. e.g. DUAL in Oracle.
  1018. */
  1019. if (empty($this->_parts[self::FROM])) {
  1020. $this->_parts[self::FROM] = $this->_getDummyTable();
  1021. }
  1022. $from = array();
  1023. foreach ($this->_parts[self::FROM] as $correlationName => $table) {
  1024. $tmp = '';
  1025. $joinType = ($table['joinType'] == self::FROM) ? self::INNER_JOIN : $table['joinType'];
  1026. // Add join clause (if applicable)
  1027. if (! empty($from)) {
  1028. $tmp .= ' ' . strtoupper($joinType) . ' ';
  1029. }
  1030. $tmp .= $this->_getQuotedSchema($table['schema']);
  1031. $tmp .= $this->_getQuotedTable($table['tableName'], $correlationName);
  1032. // Add join conditions (if applicable)
  1033. if (!empty($from) && ! empty($table['joinCondition'])) {
  1034. $tmp .= ' ' . self::SQL_ON . ' ' . $table['joinCondition'];
  1035. }
  1036. // Add the table name and condition add to the list
  1037. $from[] = $tmp;
  1038. }
  1039. // Add the list of all joins
  1040. if (!empty($from)) {
  1041. $sql .= ' ' . self::SQL_FROM . ' ' . implode("\n", $from);
  1042. }
  1043. return $sql;
  1044. }
  1045. /**
  1046. * Render UNION query
  1047. *
  1048. * @param string $sql SQL query
  1049. * @return string
  1050. */
  1051. protected function _renderUnion($sql)
  1052. {
  1053. if ($this->_parts[self::UNION]) {
  1054. $parts = count($this->_parts[self::UNION]);
  1055. foreach ($this->_parts[self::UNION] as $cnt => $union) {
  1056. list($target, $type) = $union;
  1057. if ($target instanceof Zend_Db_Select) {
  1058. $target = $target->assemble();
  1059. }
  1060. $sql .= $target;
  1061. if ($cnt < $parts - 1) {
  1062. $sql .= ' ' . $type . ' ';
  1063. }
  1064. }
  1065. }
  1066. return $sql;
  1067. }
  1068. /**
  1069. * Render WHERE clause
  1070. *
  1071. * @param string $sql SQL query
  1072. * @return string
  1073. */
  1074. protected function _renderWhere($sql)
  1075. {
  1076. if ($this->_parts[self::FROM] && $this->_parts[self::WHERE]) {
  1077. $sql .= ' ' . self::SQL_WHERE . ' ' . implode(' ', $this->_parts[self::WHERE]);
  1078. }
  1079. return $sql;
  1080. }
  1081. /**
  1082. * Render GROUP clause
  1083. *
  1084. * @param string $sql SQL query
  1085. * @return string
  1086. */
  1087. protected function _renderGroup($sql)
  1088. {
  1089. if ($this->_parts[self::FROM] && $this->_parts[self::GROUP]) {
  1090. $group = array();
  1091. foreach ($this->_parts[self::GROUP] as $term) {
  1092. $group[] = $this->_adapter->quoteIdentifier($term, true);
  1093. }
  1094. $sql .= ' ' . self::SQL_GROUP_BY . ' ' . implode(",\n\t", $group);
  1095. }
  1096. return $sql;
  1097. }
  1098. /**
  1099. * Render HAVING clause
  1100. *
  1101. * @param string $sql SQL query
  1102. * @return string
  1103. */
  1104. protected function _renderHaving($sql)
  1105. {
  1106. if ($this->_parts[self::FROM] && $this->_parts[self::HAVING]) {
  1107. $sql .= ' ' . self::SQL_HAVING . ' ' . implode(' ', $this->_parts[self::HAVING]);
  1108. }
  1109. return $sql;
  1110. }
  1111. /**
  1112. * Render ORDER clause
  1113. *
  1114. * @param string $sql SQL query
  1115. * @return string
  1116. */
  1117. protected function _renderOrder($sql)
  1118. {
  1119. if ($this->_parts[self::ORDER]) {
  1120. $order = array();
  1121. foreach ($this->_parts[self::ORDER] as $term) {
  1122. if (is_array($term)) {
  1123. if(is_numeric($term[0]) && strval(intval($term[0])) == $term[0]) {
  1124. $order[] = (int)trim($term[0]) . ' ' . $term[1];
  1125. } else {
  1126. $order[] = $this->_adapter->quoteIdentifier($term[0], true) . ' ' . $term[1];
  1127. }
  1128. } elseif (is_numeric($term) && strval(intval($term)) == $term) {
  1129. $order[] = (int)trim($term);
  1130. } else {
  1131. $order[] = $this->_adapter->quoteIdentifier($term, true);
  1132. }
  1133. }
  1134. $sql .= ' ' . self::SQL_ORDER_BY . ' ' . implode(', ', $order);
  1135. }
  1136. return $sql;
  1137. }
  1138. /**
  1139. * Render LIMIT OFFSET clause
  1140. *
  1141. * @param string $sql SQL query
  1142. * @return string
  1143. */
  1144. protected function _renderLimitoffset($sql)
  1145. {
  1146. $count = 0;
  1147. $offset = 0;
  1148. if (!empty($this->_parts[self::LIMIT_OFFSET])) {
  1149. $offset = (int) $this->_parts[self::LIMIT_OFFSET];
  1150. $count = PHP_INT_MAX;
  1151. }
  1152. if (!empty($this->_parts[self::LIMIT_COUNT])) {
  1153. $count = (int) $this->_parts[self::LIMIT_COUNT];
  1154. }
  1155. /*
  1156. * Add limits clause
  1157. */
  1158. if ($count > 0) {
  1159. $sql = trim($this->_adapter->limit($sql, $count, $offset));
  1160. }
  1161. return $sql;
  1162. }
  1163. /**
  1164. * Render FOR UPDATE clause
  1165. *
  1166. * @param string $sql SQL query
  1167. * @return string
  1168. */
  1169. protected function _renderForupdate($sql)
  1170. {
  1171. if ($this->_parts[self::FOR_UPDATE]) {
  1172. $sql .= ' ' . self::SQL_FOR_UPDATE;
  1173. }
  1174. return $sql;
  1175. }
  1176. /**
  1177. * Turn magic function calls into non-magic function calls
  1178. * for joinUsing syntax
  1179. *
  1180. * @param string $method
  1181. * @param array $args OPTIONAL Zend_Db_Table_Select query modifier
  1182. * @return Zend_Db_Select
  1183. * @throws Zend_Db_Select_Exception If an invalid method is called.
  1184. */
  1185. public function __call($method, array $args)
  1186. {
  1187. $matches = array();
  1188. /**
  1189. * Recognize methods for Has-Many cases:
  1190. * findParent<Class>()
  1191. * findParent<Class>By<Rule>()
  1192. * Use the non-greedy pattern repeat modifier e.g. \w+?
  1193. */
  1194. if (preg_match('/^join([a-zA-Z]*?)Using$/', $method, $matches)) {
  1195. $type = strtolower($matches[1]);
  1196. if ($type) {
  1197. $type .= ' join';
  1198. if (!in_array($type, self::$_joinTypes)) {
  1199. require_once 'Zend/Db/Select/Exception.php';
  1200. throw new Zend_Db_Select_Exception("Unrecognized method '$method()'");
  1201. }
  1202. if (in_array($type, array(self::CROSS_JOIN, self::NATURAL_JOIN))) {
  1203. require_once 'Zend/Db/Select/Exception.php';
  1204. throw new Zend_Db_Select_Exception("Cannot perform a joinUsing with method '$method()'");
  1205. }
  1206. } else {
  1207. $type = self::INNER_JOIN;
  1208. }
  1209. array_unshift($args, $type);
  1210. return call_user_func_array(array($this, '_joinUsing'), $args);
  1211. }
  1212. require_once 'Zend/Db/Select/Exception.php';
  1213. throw new Zend_Db_Select_Exception("Unrecognized method '$method()'");
  1214. }
  1215. /**
  1216. * Implements magic method.
  1217. *
  1218. * @return string This object as a SELECT string.
  1219. */
  1220. public function __toString()
  1221. {
  1222. try {
  1223. $sql = $this->assemble();
  1224. } catch (Exception $e) {
  1225. trigger_error($e->getMessage(), E_USER_WARNING);
  1226. $sql = '';
  1227. }
  1228. return (string)$sql;
  1229. }
  1230. }