Source for file MySQLSelectQueryResult.class.php

Documentation is available at MySQLSelectQueryResult.class.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc.mysql
  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: MySQLSelectQueryResult.class.php,v 1.13 2007/09/11 17:34:38 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/SelectQueryResult.interface.php");
  11.  
  12. /**
  13. * The MySQLSelectQueryResult interface provides the functionality common to a MySQL SELECT query result.
  14. *
  15. * For example, you can fetch associative arrays, advance the current row position, etc.
  16. *
  17. * @package harmoni.dbc.mysql
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: MySQLSelectQueryResult.class.php,v 1.13 2007/09/11 17:34:38 adamfranco Exp $
  23. */
  24. class MySQLSelectQueryResult
  25. implements SelectQueryResultInterface
  26. {
  27.  
  28.  
  29. /**
  30. * The index of the current row in the result.
  31. * The index of the current row in the result. The first row has an index of 0.
  32. * The last row has an index of getNumberOfRows() - 1.
  33. * @var integer $_currentRow The index of the current row in the result.
  34. */
  35. var $_currentRowIndex;
  36. /**
  37. * An array storing three arrays (associative,
  38. * numeric, and both) for the current row in the result.
  39. * @variable private array _currentRow
  40. */
  41. var $_currentRow;
  42.  
  43. /**
  44. * The resource id for this SELECT query.
  45. * The resource id for this SELECT query.
  46. * @var integer $_resourceId The resource id for this SELECT query.
  47. * @access private
  48. */
  49. var $_resourceId;
  50.  
  51.  
  52. /**
  53. * The link identifier for the database connection.
  54. * The link identifier for the database connection.
  55. * @param integer $_linkId The link identifier for the database connection.
  56. * @access private
  57. */
  58. var $_linkId;
  59. /**
  60. * This is an array consisting of all bound variables.
  61. * @var array _boundVars
  62. * @access private
  63. */
  64. var $_boundVars;
  65.  
  66. /**
  67. * Creates a new MySQLSelectQueryResult object.
  68. * Creates a new MySQLSelectQueryResult object.
  69. * @access public
  70. * @param integer $resourceId The resource id for this SELECT query.
  71. * @param integer $linkId The link identifier for the database connection.
  72. * @return object MySQLSelectQueryResult A new MySQLSelectQueryResult object.
  73. */
  74. function MySQLSelectQueryResult($resourceId, $linkId) {
  75. // ** parameter validation
  76. $resourceRule = ResourceValidatorRule::getRule();
  77. ArgumentValidator::validate($resourceId, $resourceRule, true);
  78. ArgumentValidator::validate($linkId, $resourceRule, true);
  79. // ** end of parameter validation
  80.  
  81. $this->_resourceId = $resourceId;
  82. $this->_linkId = $linkId;
  83. $this->_currentRowIndex = 0;
  84. $this->_currentRow = array();
  85. $this->_currentRow[BOTH] = array();
  86. $this->_currentRow[NUMERIC] = array();
  87. $this->_currentRow[ASSOC] = array();
  88. $this->_boundVars = array();
  89. // if we have at least one row in the result, fetch its array
  90. if ($this->hasMoreRows()) {
  91. $this->_currentRow[BOTH] = mysql_fetch_array($this->_resourceId, MYSQL_BOTH);
  92. foreach ($this->_currentRow[BOTH] as $key => $value)
  93. if (is_int($key))
  94. $this->_currentRow[NUMERIC][$key] = $value;
  95. else
  96. $this->_currentRow[ASSOC][$key] = $value;
  97. }
  98. }
  99.  
  100.  
  101. /**
  102. * Returns the resource id for this SELECT query.
  103. * Returns the resource id for this SELECT query. The resource id is returned
  104. * by the mysql_query() function.
  105. * @access public
  106. * @return integer The resource id for this SELECT query.
  107. ***/
  108. function getResourceId() {
  109. return $this->_resourceId;
  110. }
  111. /**
  112. * Indicates if there are any remaining rows returned by the SELECT query.
  113. * Indicates if there are any remaining rows returned by the SELECT query including
  114. * the current row.
  115. * @access public
  116. * @return boolean True, if there are some rows left; False, otherwise.
  117. ***/
  118. function hasNext() {
  119. return $this->hasMoreRows();
  120. }
  121.  
  122. /**
  123. * Returns an array that stores the current row in the result and advances
  124. * to the next row. The data
  125. * can be accessed through associative indices <b>as well as</b> numeric indices.
  126. * @access public
  127. * @param optional integer arrayType Specifies what type of an array to return.
  128. * Allowed values are: ASSOC, NUMERIC, and BOTH.
  129. * @return array An associative array of the current row.
  130. ***/
  131. function next() {
  132. $row = $this->getCurrentRow();
  133. $this->advanceRow();
  134. return $row;
  135. }
  136.  
  137. /**
  138. * Advances the current row position.
  139. * Advances the current row position. If there are no more rows left, then
  140. * it returns <code>false</code>.
  141. * @access public
  142. * @return boolean True, if successful; False, otherwise.
  143. */
  144. function advanceRow() {
  145. // if no rows left, cannot advance
  146. if (!$this->hasMoreRows())
  147. return false;
  148. // now, advance
  149. $this->_currentRowIndex++;
  150.  
  151. $this->_currentRow[BOTH] = mysql_fetch_array($this->_resourceId, MYSQL_BOTH);
  152. if (!is_array($this->_currentRow[BOTH]))
  153. return false;
  154. foreach ($this->_currentRow[BOTH] as $key => $value)
  155. if (is_int($key))
  156. $this->_currentRow[NUMERIC][$key] = $value;
  157. else
  158. $this->_currentRow[ASSOC][$key] = $value;
  159. // update the value of bound variables
  160. foreach (array_keys($this->_boundVars) as $i => $key) {
  161. if (is_int($key))
  162. $this->_boundVars[$key] = $this->_currentRow[NUMERIC][$key];
  163. else
  164. $this->_boundVars[$key] = $this->_currentRow[ASSOC][$key];
  165. }
  166. return true;
  167. }
  168.  
  169. /**
  170. * Indicates if there are any remaining rows returned by the SELECT query.
  171. * Indicates if there are any remaining rows returned by the SELECT query. The
  172. * current row does count as well.
  173. * @access public
  174. * @return boolean True, if there are some rows left; False, otherwise.
  175. ***/
  176. function hasMoreRows() {
  177. return ($this->_currentRowIndex < $this->getNumberOfRows());
  178. }
  179.  
  180. /**
  181. * Returns the specified field value in the current row.
  182. * Returns the specified field value in the current row.
  183. * @param mixed $field The name or index of the field, whose value will be returned.
  184. * @access public
  185. * @return mixed The value that was requested.
  186. ***/
  187. function field($field) {
  188. // ** parameter validation
  189. if (!array_key_exists($field, $this->_currentRow[BOTH])) {
  190. $str = "Invalid field to return from the SELECT query result.";
  191. throw new DatabaseException($str);
  192. }
  193. // ** end of parameter validation
  194.  
  195. return $this->_currentRow[BOTH][$field];
  196. }
  197. /**
  198. * Get the number of fields that were selected by the SELECT query.
  199. * Get the number of fields that were selected by the SELECT query.
  200. * @access public
  201. * @return integer The number of fields.
  202. ***/
  203. function getNumberOfFields() {
  204. return mysql_num_fields($this->_resourceId);
  205. }
  206.  
  207. /**
  208. * Returns an indexed array of all field names that were selected.
  209. * Returns an indexed array of all field names that were selected.
  210. * @access public
  211. * @return array An array of all field names that were selected.
  212. ***/
  213. function getFieldNames() {
  214. return array_keys($this->getCurrentRow(ASSOC));
  215. }
  216. /**
  217. * Returns an array that stores the current row in the result. The data
  218. * can be accessed through associative indices <b>as well as</b> numeric indices.
  219. * @access public
  220. * @param optional integer arrayType Specifies what type of an array to return.
  221. * Allowed values are: ASSOC, NUMERIC, and BOTH.
  222. * @return array An associative array of the current row.
  223. ***/
  224. function getCurrentRow($arrayType = BOTH) {
  225. // ** parameter validation
  226. $integerRule = IntegerValidatorRule::getRule();
  227. ArgumentValidator::validate($arrayType, $integerRule, true);
  228. // ** end of parameter validation
  229. $result = $this->_currentRow[$arrayType];
  230. if (is_null($result))
  231. $result = $this->_currentRow[BOTH];
  232. return $result;
  233. }
  234.  
  235.  
  236. /**
  237. * Returns the number of rows that the query processed.
  238. * Returns the number of rows that the query processed. For a SELECT query,
  239. * this would be the total number of rows selected. For a DELETE, UPDATE, or
  240. * INSERT query, this would be the number of rows that were affected.
  241. * @return integer Number of rows that were processed by the query.
  242. */
  243. function getNumberOfRows() {
  244. return mysql_num_rows($this->_resourceId);
  245. }
  246.  
  247.  
  248. /**
  249. * Moves the internal row pointer to the specified position. The range of
  250. * possible values is <code>0 - (getNumberOfRows()-1)</code>.
  251. * @param integer rowNumber The number of the row to move to.
  252. * @access public
  253. * @return boolean <code>true</code>, if operation was successful; <code>false</code>, otherwise.
  254. */
  255. function moveToRow($rowNumber) {
  256. // ** parameter validation
  257. $integerRule = IntegerValidatorRule::getRule();
  258. ArgumentValidator::validate($rowNumber, $integerRule, true);
  259. // ** end of parameter validation
  260. if (($rowNumber < 0) || ($rowNumber > $this->getNumberOfRows() - 1)) {
  261. $str = "\$rowNumber must be in the range 0..(getNumberOfRows()-1)";
  262. throw new DatabaseException($str);
  263. }
  264. $result = mysql_data_seek($this->_resourceId, $rowNumber);
  265. if ($result === true)
  266. $this->_currentRowIndex = $rowNumber;
  267. return $result;
  268. }
  269.  
  270. /**
  271. * Binds the field specified by the first argument to the variable given as
  272. * the second argument. The method stores a reference to the variable represented
  273. * by the second argument; whenever a new row is fetched, the value of the field
  274. * in the new row will be updated in the referenced variable. This enables the
  275. * user to avoid unnecessary calls to <code>getCurrentRow()</code> or
  276. * <code>field()</code>.
  277. * @access public
  278. * @param string field The field to bind. This could be either
  279. * a string value that would correspond to the field as returned by
  280. * <code>getFieldNames()</code>, or an integer (less than <code>getNumberOfFields()</code>)
  281. * corresponding to the index of the field.
  282. * @param ref mixed var The variable to be bound to the value of the field in
  283. * the current row.
  284. ***/
  285. function bindField($field, $var) {
  286. // ** parameter validation
  287. $orRule = OrValidatorRule::getRule(IntegerValidatorRule::getRule(), StringValidatorRule::getRule());
  288. ArgumentValidator::validate($field, $orRule, true);
  289. // ** end of parameter validation
  290. // add $var to the array of bound variables and update its value
  291. $this->_boundVars[$field] =$var;
  292. if (is_int($field))
  293. $var = $this->_currentRow[NUMERIC][$field];
  294. else
  295. $var = $this->_currentRow[ASSOC][$field];
  296. }
  297. /**
  298. * Unbinds the field that has been bound by <code>bindField()</code>.
  299. * @access public
  300. * @param string field The field to unbind. This could be either
  301. * a string value that would correspond to the field as returned by
  302. * <code>getFieldNames()</code>, or an integer (less than <code>getNumberOfFields()</code>)
  303. * corresponding to the index of the field.
  304. ***/
  305. function unbindField($field) {
  306. // ** parameter validation
  307. $orRule = OrValidatorRule::getRule(IntegerValidatorRule::getRule(), StringValidatorRule::getRule());
  308. ArgumentValidator::validate($field, $orRule, true);
  309. // ** end of parameter validation
  310. if (isset($this->_boundVars[$field])) {
  311. $this->_boundVars[$field] = null;
  312. unset($this->_boundVars[$field]);
  313. }
  314. }
  315. /**
  316. * Frees the memory for this result.
  317. * @access public
  318. * @return void
  319. */
  320. function free() {
  321. mysql_free_result($this->_resourceId);
  322. }
  323.  
  324. }
  325.  
  326. ?>

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