Source for file OracleSelectQueryResult.class.php

Documentation is available at OracleSelectQueryResult.class.php

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

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