Source for file UpdateQuery.class.php

Documentation is available at UpdateQuery.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: UpdateQuery.class.php,v 1.9 2007/09/05 21:38:59 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/UpdateQuery.interface.php");
  11.  
  12. /**
  13. * An UpdateQuery class provides the tools to build an UPDATE query.
  14. *
  15. *
  16. * @package harmoni.dbc
  17. *
  18. * @copyright Copyright &copy; 2005, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: UpdateQuery.class.php,v 1.9 2007/09/05 21:38:59 adamfranco Exp $
  22. */
  23.  
  24. class UpdateQuery
  25. extends QueryAbstract
  26. implements UpdateQueryInterface
  27. {
  28.  
  29. /**
  30. * @var string $_table The name of the table to update.
  31. * @access private
  32. */
  33. var $_table;
  34.  
  35. /**
  36. * @var array $_columns A list of the columns we will be updating.
  37. * @access private
  38. */
  39. var $_columns;
  40.  
  41. /**
  42. * @var array $_values List of values that will be assigned to the columns.
  43. * @access private
  44. */
  45. var $_values;
  46.  
  47.  
  48. /**
  49. * This will store the condition in the WHERE clause. Each element of this
  50. * array stores 2 things: the condition itself, and the logical operator
  51. * to use to join with the previous condition.
  52. * @var array $_condition The condition in the WHERE clause.
  53. * @access private
  54. */
  55. var $_condition;
  56.  
  57.  
  58. /**
  59. * This is the constructor for a MySQL UPDATE query.
  60. * @access public
  61. */
  62. function UpdateQuery() {
  63. $this->reset();
  64. }
  65.  
  66. /**
  67. * Sets the table to update.
  68. * @param string $table The table to insert into.
  69. * @access public
  70. */
  71. function setTable($table) {
  72. // ** parameter validation
  73. $stringRule = StringValidatorRule::getRule();
  74. ArgumentValidator::validate($table, $stringRule, true);
  75. // ** end of parameter validation
  76.  
  77. $this->_table = $table;
  78. }
  79.  
  80. /**
  81. * Sets the columns to update in the table.
  82. * @param array $table The columns to insert into the table.
  83. * @access public
  84. */
  85. function setColumns($columns) {
  86. // ** parameter validation
  87. $arrayRule = ArrayValidatorRule::getRule();
  88. ArgumentValidator::validate($columns, $arrayRule, true);
  89. // ** end of parameter validation
  90.  
  91. $this->_columns = $columns;
  92. }
  93.  
  94.  
  95. /**
  96. * Specifies the values that the will be assigned to the columns specified with setColumns().
  97. *
  98. * @param array The values that the will be assigned to the columns specified with setColumns().
  99. * @access public
  100. */
  101. function setValues($values) {
  102. // ** parameter validation
  103. $arrayRule = ArrayValidatorRule::getRule();
  104. ArgumentValidator::validate($values, $arrayRule, true);
  105. // ** end of parameter validation
  106.  
  107. $this->_values = $values;
  108. }
  109. /**
  110. * Add a column/value pair, if a value for the column exists, it will be
  111. * overwritten. The value will not have any new escaping or quotes added to it.
  112. *
  113. * @param string $column
  114. * @param string $value
  115. * @return void
  116. * @access public
  117. * @since 3/8/07
  118. */
  119. function addRawValue ( $column, $value ) {
  120. ArgumentValidator::validate($column, NonzeroLengthStringValidatorRule::getRule());
  121. ArgumentValidator::validate($value, NonzeroLengthStringValidatorRule::getRule());
  122. $key = array_search($column, $this->_columns);
  123. if ($key !== FALSE && is_int($key)) {
  124. $this->_values[$key] = $value;
  125. } else {
  126. $this->_columns[] = $column;
  127. $this->_values[] = $value;
  128. }
  129. }
  130. /**
  131. * Add a value, escaping it and surrounding it with quotes.
  132. *
  133. * @param string $column
  134. * @param string $value
  135. * @return void
  136. * @access public
  137. * @since 3/8/07
  138. */
  139. function addValue ( $column, $value ) {
  140. ArgumentValidator::validate($column, NonzeroLengthStringValidatorRule::getRule());
  141. ArgumentValidator::validate($value, StringValidatorRule::getRule());
  142. $this->addRawValue($column, "'".addslashes($value)."'");
  143. }
  144.  
  145. /**
  146. * *Deprecated* Specifies the condition in the WHERE clause.
  147. *
  148. * The query will execute only on rows that fulfil the condition. If this method
  149. * is never called, then the WHERE clause will not be included.
  150. * @param string The WHERE clause condition.
  151. * @access public
  152. * @deprecated July 09, 2003 - Use addWhere() instead.
  153. */
  154. function setWhere($condition) {
  155. // ** parameter validation
  156. $stringRule = StringValidatorRule::getRule();
  157. ArgumentValidator::validate($condition, $stringRule, true);
  158. // ** end of parameter validation
  159.  
  160. $arr = array();
  161. $arr[] = $condition;
  162. $arr[] = null;
  163. $this->_condition[] = $arr;
  164. }
  165.  
  166.  
  167.  
  168. /**
  169. * Adds a new condition in the WHERE clause.
  170. *
  171. * The query will execute only on rows that fulfil the condition. If this method
  172. * is never called, then the WHERE clause will not be included.
  173. * @param string condition The WHERE clause condition to add.
  174. * @param integer logicalOperation The logical operation to use to connect
  175. * this WHERE condition with the previous WHERE conditions. Allowed values:
  176. * <code>_AND</code> and <code>_OR</code>.
  177. * @access public
  178. * @return void
  179. */
  180. function addWhere($condition, $logicalOperation = _AND) {
  181. // ** parameter validation
  182. $stringRule = StringValidatorRule::getRule();
  183. $integerRule = IntegerValidatorRule::getRule();
  184. $optionalRule = OptionalRule::getRule($integerRule);
  185. ArgumentValidator::validate($condition, $stringRule, true);
  186. ArgumentValidator::validate($logicalOperation, $optionalRule, true);
  187. // ** end of parameter validation
  188.  
  189. $arr = array();
  190. $arr[] = $condition;
  191. $arr[] = $logicalOperation;
  192. $this->_condition[] = $arr;
  193. }
  194.  
  195.  
  196. /**
  197. * Resets the query.
  198. * @access public
  199. */
  200. function reset() {
  201. parent::reset();
  202.  
  203. // an UPDATE query
  204. $this->_type = UPDATE;
  205. // default query configuration:
  206. // no table to update
  207. $this->_table = "";
  208.  
  209. // no columns to update
  210. $this->_columns = array();
  211.  
  212. // one row of values with no values specified
  213. $this->_values = array();
  214. // no WHERE condition, by default
  215. $this->_condition = array();
  216. }
  217.  
  218. }
  219. ?>

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