Source for file PostgreSQLSelectQueryResult.class.php

Documentation is available at PostgreSQLSelectQueryResult.class.php

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

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