Select.php 44 KB

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