Source for file SelectQuery.class.php

Documentation is available at SelectQuery.class.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: SelectQuery.class.php,v 1.10 2007/09/05 21:38:59 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/SelectQuery.interface.php");
  11. require_once(HARMONI."DBHandler/Query.abstract.php");
  12.  
  13.  
  14. /**
  15. * A SelectQuery class provides the tools to build a SELECT query.
  16. *
  17. *
  18. * @package harmoni.dbc
  19. *
  20. * @copyright Copyright &copy; 2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  22. *
  23. * @version $Id: SelectQuery.class.php,v 1.10 2007/09/05 21:38:59 adamfranco Exp $
  24. */
  25.  
  26. class SelectQuery
  27. extends QueryAbstract
  28. implements SelectQueryInterface
  29. {
  30.  
  31.  
  32. /**
  33. * This array stores the tables in the FROM clause of the SELECT query.
  34. *
  35. * This array stores the tables in the FROM clause of the SELECT query along
  36. * with the join types, join conditions, and table alias.
  37. * @var string $_tables The tables in the FROM clause of the SELECT query.
  38. * @see SelectQuery::addTable()
  39. * @access private
  40. */
  41. var $_tables;
  42.  
  43.  
  44. /**
  45. * The list of columns we will be selecting.
  46. * The list of columns we will be selecting. This is an array of arrays.
  47. * Each element in the outer array specifies one column. The first element
  48. * of each inner array is the column name itself. The second element is
  49. * the alias of that column and is optional. The third and last element
  50. * is the name of the table where the column resides.
  51. * @var array $_columns The list of columns we will be selecting.
  52. * @access private
  53. */
  54. var $_columns;
  55.  
  56.  
  57. /**
  58. * This will store the condition in the WHERE clause. Each element of this
  59. * array stores 2 things: the condition itself, and the logical operator
  60. * to use to join with the previous condition.
  61. * @var array $_condition The condition in the WHERE clause.
  62. * @access private
  63. */
  64. var $_condition;
  65.  
  66.  
  67. /**
  68. * Will store the columns in the GROUP BY clause.
  69. *
  70. * Will store the columns in the GROUP BY clause.
  71. * @var array $_groupBy The columns in the GROUP BY clause.
  72. */
  73. var $_groupBy;
  74.  
  75.  
  76. /**
  77. * Will store the condition in the HAVING clause.
  78. *
  79. * Will store the condition in the HAVING clause.
  80. * @var array $_having The condition in the HAVING clause.
  81. */
  82. var $_having;
  83.  
  84.  
  85. /**
  86. * Will store the columns in the ORDER BY clause.
  87. *
  88. * Will store the columns in the OREDER BY clause. This is an array of arrays.
  89. * Each element in the outer arrays stores one entry in the ORDER BY clause.
  90. * Each inner array holds two elements. The first one is the name of the column
  91. * to order by. The second one specifies whether it is a ASCENDING or DESCENDING
  92. * order.
  93. * @var array $_orderBy The columns in the ORDER BY clause.
  94. */
  95. var $_orderBy;
  96.  
  97.  
  98. /**
  99. * Specifies whether distinct rows will be returned or not.
  100. *
  101. * Specifies whether distinct rows will be returned or not. If TRUE, only
  102. * unique rows will be returned by the query.
  103. * @var boolean $_distinct If true, then only unique rows will be returned.
  104. */
  105. var $_distinct;
  106.  
  107. /**
  108. * Stores the number of rows to return.
  109. *
  110. * Stores the number of rows to return.
  111. * @var integer $_numberOfRows The number of rows to return.
  112. */
  113. var $_numberOfRows;
  114.  
  115. /**
  116. * Stores the number of the row to start from.
  117. *
  118. * Stores the number of the row to start from.
  119. * @var integer $_startFromRow The number of the row to start from.
  120. */
  121. var $_startFromRow;
  122.  
  123.  
  124. /**
  125. * The constructor initializes the query object.
  126. *
  127. * The constructor initializes the query object.
  128. * @access public
  129. */
  130. function SelectQuery() {
  131. $this->reset();
  132. }
  133.  
  134.  
  135. /**
  136. * Adds a table to the FROM clause of the SELECT query.
  137. *
  138. * Adds a table to the FROM clause of the SELECT statement. At any moment,
  139. * a current set of tables is maintained in the object, so when a new one
  140. * is added, it is combined with the current set.
  141. * @param string $table The table to add to the FROM clause.
  142. * @param integer $joinType Specifies what type of join to perform between
  143. * the current set of tables and the table being added. Could be one of
  144. * the following: NO_JOIN, LEFT_JOIN, INNER_JOIN, RIGHT_JOIN.
  145. * @param string $joinCondition If a join is to be performed, then this
  146. * will indicate the join condition.
  147. * @param string alias An alias for this table.
  148. * @use NO_JOIN
  149. * @use LEFT_JOIN
  150. * @use INNER_JOIN
  151. * @use RIGHT_JOIN
  152. * @access public
  153. */
  154. function addTable($table, $joinType = NO_JOIN, $joinCondition = "", $alias = "") {
  155. // ** parameter validation
  156. $stringRule = StringValidatorRule::getRule();
  157. $integerRule = IntegerValidatorRule::getRule();
  158. ArgumentValidator::validate($table, $stringRule, true);
  159. ArgumentValidator::validate($joinType, $integerRule, true);
  160. ArgumentValidator::validate($joinCondition, $stringRule, true);
  161. ArgumentValidator::validate($alias, $stringRule, true);
  162. // ** end of parameter validation
  163. $newTable = array($table, $joinType, $joinCondition, $alias);
  164. $this->_tables[] = $newTable;
  165. }
  166.  
  167.  
  168. /**
  169. * *Deprecated* Sets the columns to select.
  170. * Sets the columns to select.
  171. * Note: addColumn() and setColumns() can be used together in any order.
  172. * However, calling setColumns() after addColumn() resets the list of columns.
  173. * @param array $column The columns to select. This is a one-dimensional array
  174. * of the column names. If you want aliases you have to include the alias
  175. * in the column name itself. For example: array("user_id AS id", "user_name AS name")
  176. * For a better approach, see addColumn().
  177. * @access public
  178. * @deprecated June 24, 2003 - Use addColumn() instead.
  179. * @see SelectQuery::addColumn()
  180. */
  181. function setColumns($columns) {
  182. // ** parameter validation
  183. $arrayRule = ArrayValidatorRule::getRule();
  184. ArgumentValidator::validate($columns, $arrayRule, true);
  185. // ** end of parameter validation
  186. // convert each string in the array to a 2-dimensional array
  187. // (for compatibility with addColumn) and store in $this->_columns
  188. $this->_columns = array();
  189. foreach ($columns as $column) {
  190. $arr = array();
  191. $arr[] = $column;
  192. $arr[] = null;
  193. $arr[] = null;
  194. $this->_columns[] = $arr;
  195. }
  196. }
  197.  
  198. /**
  199. * Adds a new column to the SELECT query.
  200. * Adds a new column to the SELECT query. This method is an alternative to the
  201. * setColumns() method. It adds one column at a time, and also provides
  202. * the ability to explicitly specify the alias of the column to select.
  203. * Note: addColumn() and setColumns() can be used together in any order.
  204. * However, calling setColumns() after addColumn() resets the list of columns.
  205. * @param string $column The name of the column.
  206. * @param optional string $alias The alias of the column.
  207. * @param optional string $table An optional name of the table where
  208. * the column resides.
  209. * will be used.
  210. * @access public
  211. * @see SelectQueryInterface::setColumns()
  212. */
  213. function addColumn($column, $alias = "", $table = "") {
  214. // ** parameter validation
  215. $stringRule = StringValidatorRule::getRule();
  216. $optionalRule = OptionalRule::getRule($stringRule);
  217. ArgumentValidator::validate($column, $stringRule, true);
  218. ArgumentValidator::validate($alias, $optionalRule, true);
  219. ArgumentValidator::validate($table, $optionalRule, true);
  220. // ** end of parameter validation
  221. $arr = array();
  222. $arr[] = $column;
  223. $arr[] = $alias;
  224. $arr[] = $table;
  225. $this->_columns[] = $arr;
  226. }
  227.  
  228.  
  229.  
  230. /**
  231. * *Deprecated* Specifies the condition in the WHERE clause.
  232. *
  233. * The query will return only rows that fulfil the condition. If this method
  234. * is never called, then the WHERE clause will not be included.
  235. * @param string condition The WHERE clause condition.
  236. * @deprecated July 07, 2003 - Use addWhere() instead.
  237. * @access public
  238. */
  239. function setWhere($condition) {
  240. // ** parameter validation
  241. $stringRule = StringValidatorRule::getRule();
  242. ArgumentValidator::validate($condition, $stringRule, true);
  243. // ** end of parameter validation
  244.  
  245. $this->_condition = array();
  246.  
  247. $arr = array();
  248. $arr[] = $condition;
  249. $arr[] = null;
  250. $this->_condition[] = $arr;
  251. }
  252.  
  253.  
  254. /**
  255. * Adds a new condition in the WHERE clause.
  256. *
  257. * The query will return only rows that fulfil the condition. If this method
  258. * is never called, then the WHERE clause will not be included.
  259. * @param string condition The WHERE clause condition to add.
  260. * @param integer logicalOperation The logical operation to use to connect
  261. * this WHERE condition with the previous WHERE conditions. Allowed values:
  262. * <code>_AND</code> and <code>_OR</code>.
  263. * @access public
  264. * @return void
  265. */
  266. function addWhere($condition, $logicalOperation = _AND) {
  267. // ** parameter validation
  268. $stringRule = StringValidatorRule::getRule();
  269. $integerRule = IntegerValidatorRule::getRule();
  270. $optionalRule = OptionalRule::getRule($integerRule);
  271. ArgumentValidator::validate($condition, $stringRule, true);
  272. ArgumentValidator::validate($logicalOperation, $optionalRule, true);
  273. // ** end of parameter validation
  274.  
  275. $arr = array();
  276. $arr[] = $condition;
  277. $arr[] = $logicalOperation;
  278. $this->_condition[] = $arr;
  279. }
  280.  
  281. /**
  282. * Resets the WHERE clause.
  283. * @access public
  284. ***/
  285. function resetWhere() {
  286. unset($this->_condition);
  287. $this->_condition = array();
  288. }
  289. /**
  290. * Sets the GROUP BY and HAVING clause.
  291. *
  292. * This method sets the GROUP BY clause of the SELECT statement. In addition,
  293. * if $condition is specified, it includes the HAVING clause. If the method is never
  294. * called, no GROUP BY or HAVING clause will be included.
  295. * @param array $columns An array of the columns to group by. Ideally, the
  296. * columns should be in the list provided by setColumns().
  297. * @param string $condition An optional condition to be included in the
  298. * HAVING clause.
  299. * @access public
  300. */
  301. function setGroupBy($columns, $condition = "") {
  302. // ** parameter validation
  303. $arrayRule = ArrayValidatorRule::getRule();
  304. $stringRule = StringValidatorRule::getRule();
  305. ArgumentValidator::validate($columns, $arrayRule, true);
  306. ArgumentValidator::validate($condition, $stringRule, true);
  307. // ** end of parameter validation
  308. $this->_groupBy = $columns;
  309. $this->_having = $condition;
  310. }
  311.  
  312.  
  313.  
  314. /**
  315. * Add a column to the ORDER BY clause.
  316. *
  317. * This method adds a column to the ORDER BY clause of the SELECT statement. If the method is never
  318. * called, no ORDER BY clause will be included. The order of the columns in the
  319. * clause will coincide with the order, in which they were added with this method.
  320. * @param string $column A column to order by.
  321. * @param integer $direction An optional parameter specifying ascending or descending
  322. * sorting order. Allowed values are: ASCENDING, DESCENDING.
  323. * @use ASCENDING
  324. * @use DESCENDING
  325. * @access public
  326. */
  327. function addOrderBy($column, $direction = ASCENDING) {
  328. // ** parameter validation
  329. $stringRule = StringValidatorRule::getRule();
  330. $integerRule = IntegerValidatorRule::getRule();
  331. ArgumentValidator::validate($column, $stringRule, true);
  332. ArgumentValidator::validate($direction, $integerRule, true);
  333. // ** end of parameter validation
  334.  
  335. $this->_orderBy[] = array($column, $direction);
  336. }
  337. /**
  338. * Specifies whether distinct rows will be returned.
  339. *
  340. * Use this method to specify whether the rows returned by the SELECT query
  341. * have to be distinct (i.e. only unique rows) or not. If the method is never
  342. * called, then the default value is not distinct.
  343. * @param boolean $distinct If true, then only unique rows will be returned.
  344. * @access public
  345. */
  346. function setDistinct($distinct) {
  347. // ** parameter validation
  348. $booleanRule = BooleanValidatorRule::getRule();
  349. ArgumentValidator::validate($distinct, $booleanRule, true);
  350. // ** end of parameter validation
  351.  
  352. $this->_distinct = $distinct;
  353. }
  354.  
  355. /**
  356. * Limits the number of rows to the specified number.
  357. *
  358. * Limits the number of rows returned by the SELECT query to the specified
  359. * number.
  360. * @param integer $numberOfRows The number of rows to return
  361. * @access public
  362. */
  363. function limitNumberOfRows($numberOfRows) {
  364. // ** parameter validation
  365. $integerRule = IntegerValidatorRule::getRule();
  366. ArgumentValidator::validate($numberOfRows, $integerRule, true);
  367. // ** end of parameter validation
  368.  
  369. $this->_numberOfRows = $numberOfRows;
  370. }
  371. /**
  372. * Starts the results from the specified row.
  373. *
  374. * Starts the results of the SELECT query from the specified row.
  375. * @param integer $startingRow The number of the starting row. Numbers
  376. * start with 1 for the first row, 2 for the second row, and so forth.
  377. * @access public
  378. */
  379. function startFromRow($startFromRow) {
  380. // ** parameter validation
  381. $integerRule = IntegerValidatorRule::getRule();
  382. ArgumentValidator::validate($startFromRow, $integerRule, true);
  383. // ** end of parameter validation
  384.  
  385. $this->_startFromRow = $startFromRow;
  386. }
  387. /**
  388. * Resets the query.
  389. * @access public
  390. */
  391. function reset() {
  392. parent::reset();
  393.  
  394. // an UPDATE query
  395. $this->_type = SELECT;
  396. // default query configuration:
  397. // no tables to select from
  398. $this->_tables = array();
  399.  
  400. // no columns to select
  401. $this->_columns = array();
  402.  
  403. // no WHERE condition, by default
  404. $this->_condition = array();
  405.  
  406. // no GROUP BY clause
  407. $this->_groupBy = array();
  408. $this->_having = "";
  409. // no ORDER BY clause
  410. $this->_orderBy = array();
  411. // no LIMIT clause
  412. $this->_numberOfRows = 0;
  413. $this->_startFromRow = 0;
  414. }
  415. }
  416. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:22 -0400 by phpDocumentor 1.3.0RC3