Select.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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]*\s*\(([^\(\)]|(?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. $col = trim(str_replace("\n", ' ', $col));
  865. if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
  866. $col = $m[1];
  867. $alias = $m[2];
  868. }
  869. // Check for columns that look like functions and convert to Zend_Db_Expr
  870. if (preg_match(self::REGEX_COLUMN_EXPR, (string) $col)) {
  871. $col = new Zend_Db_Expr($col);
  872. } elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
  873. $currentCorrelationName = $m[1];
  874. $col = $m[2];
  875. }
  876. }
  877. $columnValues[] = array($currentCorrelationName, $col, is_string($alias) ? $alias : null);
  878. }
  879. if ($columnValues) {
  880. // should we attempt to prepend or insert these values?
  881. if ($afterCorrelationName === true || is_string($afterCorrelationName)) {
  882. $tmpColumns = $this->_parts[self::COLUMNS];
  883. $this->_parts[self::COLUMNS] = array();
  884. } else {
  885. $tmpColumns = array();
  886. }
  887. // find the correlation name to insert after
  888. if (is_string($afterCorrelationName)) {
  889. while ($tmpColumns) {
  890. $this->_parts[self::COLUMNS][] = $currentColumn = array_shift($tmpColumns);
  891. if ($currentColumn[0] == $afterCorrelationName) {
  892. break;
  893. }
  894. }
  895. }
  896. // apply current values to current stack
  897. foreach ($columnValues as $columnValue) {
  898. array_push($this->_parts[self::COLUMNS], $columnValue);
  899. }
  900. // finish ensuring that all previous values are applied (if they exist)
  901. while ($tmpColumns) {
  902. array_push($this->_parts[self::COLUMNS], array_shift($tmpColumns));
  903. }
  904. }
  905. }
  906. /**
  907. * Internal function for creating the where clause
  908. *
  909. * @param string $condition
  910. * @param mixed $value optional
  911. * @param string $type optional
  912. * @param boolean $bool true = AND, false = OR
  913. * @return string clause
  914. */
  915. protected function _where($condition, $value = null, $type = null, $bool = true)
  916. {
  917. if (count($this->_parts[self::UNION])) {
  918. require_once 'Zend/Db/Select/Exception.php';
  919. throw new Zend_Db_Select_Exception("Invalid use of where clause with " . self::SQL_UNION);
  920. }
  921. if ($value !== null) {
  922. $condition = $this->_adapter->quoteInto($condition, $value, $type);
  923. }
  924. $cond = "";
  925. if ($this->_parts[self::WHERE]) {
  926. if ($bool === true) {
  927. $cond = self::SQL_AND . ' ';
  928. } else {
  929. $cond = self::SQL_OR . ' ';
  930. }
  931. }
  932. return $cond . "($condition)";
  933. }
  934. /**
  935. * @return array
  936. */
  937. protected function _getDummyTable()
  938. {
  939. return array();
  940. }
  941. /**
  942. * Return a quoted schema name
  943. *
  944. * @param string $schema The schema name OPTIONAL
  945. * @return string|null
  946. */
  947. protected function _getQuotedSchema($schema = null)
  948. {
  949. if ($schema === null) {
  950. return null;
  951. }
  952. return $this->_adapter->quoteIdentifier($schema, true) . '.';
  953. }
  954. /**
  955. * Return a quoted table name
  956. *
  957. * @param string $tableName The table name
  958. * @param string $correlationName The correlation name OPTIONAL
  959. * @return string
  960. */
  961. protected function _getQuotedTable($tableName, $correlationName = null)
  962. {
  963. return $this->_adapter->quoteTableAs($tableName, $correlationName, true);
  964. }
  965. /**
  966. * Render DISTINCT clause
  967. *
  968. * @param string $sql SQL query
  969. * @return string
  970. */
  971. protected function _renderDistinct($sql)
  972. {
  973. if ($this->_parts[self::DISTINCT]) {
  974. $sql .= ' ' . self::SQL_DISTINCT;
  975. }
  976. return $sql;
  977. }
  978. /**
  979. * Render DISTINCT clause
  980. *
  981. * @param string $sql SQL query
  982. * @return string|null
  983. */
  984. protected function _renderColumns($sql)
  985. {
  986. if (!count($this->_parts[self::COLUMNS])) {
  987. return null;
  988. }
  989. $columns = array();
  990. foreach ($this->_parts[self::COLUMNS] as $columnEntry) {
  991. list($correlationName, $column, $alias) = $columnEntry;
  992. if ($column instanceof Zend_Db_Expr) {
  993. $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true);
  994. } else {
  995. if ($column == self::SQL_WILDCARD) {
  996. $column = new Zend_Db_Expr(self::SQL_WILDCARD);
  997. $alias = null;
  998. }
  999. if (empty($correlationName)) {
  1000. $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true);
  1001. } else {
  1002. $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true);
  1003. }
  1004. }
  1005. }
  1006. return $sql . ' ' . implode(', ', $columns);
  1007. }
  1008. /**
  1009. * Render FROM clause
  1010. *
  1011. * @param string $sql SQL query
  1012. * @return string
  1013. */
  1014. protected function _renderFrom($sql)
  1015. {
  1016. /*
  1017. * If no table specified, use RDBMS-dependent solution
  1018. * for table-less query. e.g. DUAL in Oracle.
  1019. */
  1020. if (empty($this->_parts[self::FROM])) {
  1021. $this->_parts[self::FROM] = $this->_getDummyTable();
  1022. }
  1023. $from = array();
  1024. foreach ($this->_parts[self::FROM] as $correlationName => $table) {
  1025. $tmp = '';
  1026. $joinType = ($table['joinType'] == self::FROM) ? self::INNER_JOIN : $table['joinType'];
  1027. // Add join clause (if applicable)
  1028. if (! empty($from)) {
  1029. $tmp .= ' ' . strtoupper($joinType) . ' ';
  1030. }
  1031. $tmp .= $this->_getQuotedSchema($table['schema']);
  1032. $tmp .= $this->_getQuotedTable($table['tableName'], $correlationName);
  1033. // Add join conditions (if applicable)
  1034. if (!empty($from) && ! empty($table['joinCondition'])) {
  1035. $tmp .= ' ' . self::SQL_ON . ' ' . $table['joinCondition'];
  1036. }
  1037. // Add the table name and condition add to the list
  1038. $from[] = $tmp;
  1039. }
  1040. // Add the list of all joins
  1041. if (!empty($from)) {
  1042. $sql .= ' ' . self::SQL_FROM . ' ' . implode("\n", $from);
  1043. }
  1044. return $sql;
  1045. }
  1046. /**
  1047. * Render UNION query
  1048. *
  1049. * @param string $sql SQL query
  1050. * @return string
  1051. */
  1052. protected function _renderUnion($sql)
  1053. {
  1054. if ($this->_parts[self::UNION]) {
  1055. $parts = count($this->_parts[self::UNION]);
  1056. foreach ($this->_parts[self::UNION] as $cnt => $union) {
  1057. list($target, $type) = $union;
  1058. if ($target instanceof Zend_Db_Select) {
  1059. $target = $target->assemble();
  1060. }
  1061. $sql .= $target;
  1062. if ($cnt < $parts - 1) {
  1063. $sql .= ' ' . $type . ' ';
  1064. }
  1065. }
  1066. }
  1067. return $sql;
  1068. }
  1069. /**
  1070. * Render WHERE clause
  1071. *
  1072. * @param string $sql SQL query
  1073. * @return string
  1074. */
  1075. protected function _renderWhere($sql)
  1076. {
  1077. if ($this->_parts[self::FROM] && $this->_parts[self::WHERE]) {
  1078. $sql .= ' ' . self::SQL_WHERE . ' ' . implode(' ', $this->_parts[self::WHERE]);
  1079. }
  1080. return $sql;
  1081. }
  1082. /**
  1083. * Render GROUP clause
  1084. *
  1085. * @param string $sql SQL query
  1086. * @return string
  1087. */
  1088. protected function _renderGroup($sql)
  1089. {
  1090. if ($this->_parts[self::FROM] && $this->_parts[self::GROUP]) {
  1091. $group = array();
  1092. foreach ($this->_parts[self::GROUP] as $term) {
  1093. $group[] = $this->_adapter->quoteIdentifier($term, true);
  1094. }
  1095. $sql .= ' ' . self::SQL_GROUP_BY . ' ' . implode(",\n\t", $group);
  1096. }
  1097. return $sql;
  1098. }
  1099. /**
  1100. * Render HAVING clause
  1101. *
  1102. * @param string $sql SQL query
  1103. * @return string
  1104. */
  1105. protected function _renderHaving($sql)
  1106. {
  1107. if ($this->_parts[self::FROM] && $this->_parts[self::HAVING]) {
  1108. $sql .= ' ' . self::SQL_HAVING . ' ' . implode(' ', $this->_parts[self::HAVING]);
  1109. }
  1110. return $sql;
  1111. }
  1112. /**
  1113. * Render ORDER clause
  1114. *
  1115. * @param string $sql SQL query
  1116. * @return string
  1117. */
  1118. protected function _renderOrder($sql)
  1119. {
  1120. if ($this->_parts[self::ORDER]) {
  1121. $order = array();
  1122. foreach ($this->_parts[self::ORDER] as $term) {
  1123. if (is_array($term)) {
  1124. if(is_numeric($term[0]) && strval(intval($term[0])) == $term[0]) {
  1125. $order[] = (int)trim($term[0]) . ' ' . $term[1];
  1126. } else {
  1127. $order[] = $this->_adapter->quoteIdentifier($term[0], true) . ' ' . $term[1];
  1128. }
  1129. } elseif (is_numeric($term) && strval(intval($term)) == $term) {
  1130. $order[] = (int)trim($term);
  1131. } else {
  1132. $order[] = $this->_adapter->quoteIdentifier($term, true);
  1133. }
  1134. }
  1135. $sql .= ' ' . self::SQL_ORDER_BY . ' ' . implode(', ', $order);
  1136. }
  1137. return $sql;
  1138. }
  1139. /**
  1140. * Render LIMIT OFFSET clause
  1141. *
  1142. * @param string $sql SQL query
  1143. * @return string
  1144. */
  1145. protected function _renderLimitoffset($sql)
  1146. {
  1147. $count = 0;
  1148. $offset = 0;
  1149. if (!empty($this->_parts[self::LIMIT_OFFSET])) {
  1150. $offset = (int) $this->_parts[self::LIMIT_OFFSET];
  1151. $count = PHP_INT_MAX;
  1152. }
  1153. if (!empty($this->_parts[self::LIMIT_COUNT])) {
  1154. $count = (int) $this->_parts[self::LIMIT_COUNT];
  1155. }
  1156. /*
  1157. * Add limits clause
  1158. */
  1159. if ($count > 0) {
  1160. $sql = trim($this->_adapter->limit($sql, $count, $offset));
  1161. }
  1162. return $sql;
  1163. }
  1164. /**
  1165. * Render FOR UPDATE clause
  1166. *
  1167. * @param string $sql SQL query
  1168. * @return string
  1169. */
  1170. protected function _renderForupdate($sql)
  1171. {
  1172. if ($this->_parts[self::FOR_UPDATE]) {
  1173. $sql .= ' ' . self::SQL_FOR_UPDATE;
  1174. }
  1175. return $sql;
  1176. }
  1177. /**
  1178. * Turn magic function calls into non-magic function calls
  1179. * for joinUsing syntax
  1180. *
  1181. * @param string $method
  1182. * @param array $args OPTIONAL Zend_Db_Table_Select query modifier
  1183. * @return Zend_Db_Select
  1184. * @throws Zend_Db_Select_Exception If an invalid method is called.
  1185. */
  1186. public function __call($method, array $args)
  1187. {
  1188. $matches = array();
  1189. /**
  1190. * Recognize methods for Has-Many cases:
  1191. * findParent<Class>()
  1192. * findParent<Class>By<Rule>()
  1193. * Use the non-greedy pattern repeat modifier e.g. \w+?
  1194. */
  1195. if (preg_match('/^join([a-zA-Z]*?)Using$/', $method, $matches)) {
  1196. $type = strtolower($matches[1]);
  1197. if ($type) {
  1198. $type .= ' join';
  1199. if (!in_array($type, self::$_joinTypes)) {
  1200. require_once 'Zend/Db/Select/Exception.php';
  1201. throw new Zend_Db_Select_Exception("Unrecognized method '$method()'");
  1202. }
  1203. if (in_array($type, array(self::CROSS_JOIN, self::NATURAL_JOIN))) {
  1204. require_once 'Zend/Db/Select/Exception.php';
  1205. throw new Zend_Db_Select_Exception("Cannot perform a joinUsing with method '$method()'");
  1206. }
  1207. } else {
  1208. $type = self::INNER_JOIN;
  1209. }
  1210. array_unshift($args, $type);
  1211. return call_user_func_array(array($this, '_joinUsing'), $args);
  1212. }
  1213. require_once 'Zend/Db/Select/Exception.php';
  1214. throw new Zend_Db_Select_Exception("Unrecognized method '$method()'");
  1215. }
  1216. /**
  1217. * Implements magic method.
  1218. *
  1219. * @return string This object as a SELECT string.
  1220. */
  1221. public function __toString()
  1222. {
  1223. try {
  1224. $sql = $this->assemble();
  1225. } catch (Exception $e) {
  1226. trigger_error($e->getMessage(), E_USER_WARNING);
  1227. $sql = '';
  1228. }
  1229. return (string)$sql;
  1230. }
  1231. }