/php/db/result/ODBCDatabaseResult.php

https://bitbucket.org/emilhdiaz/webics · PHP · 145 lines · 122 code · 22 blank · 1 comment · 7 complexity · 515d73d0296db4af88d023353392d323 MD5 · raw file

  1. <?php
  2. final class ODBCDatabaseResult extends Object implements DatabaseResult {
  3. private $stmt;
  4. private $row_i;
  5. private $field_i;
  6. private $num_rows;
  7. private $bind_results;
  8. final public function __construct($stmt) {
  9. parent::__construct();
  10. debug("Initializing ODBCDatabaseResult object # ".static::getClass()->getInstanceCount()."...", 6);
  11. $this->stmt = $stmt;
  12. $this->row_i = 0;
  13. $this->field_i = 1;
  14. while( odbc_fetch_into($stmt, &$counter) ) $this->num_rows++;
  15. $this->reset();
  16. }
  17. final public function numOfRows() {
  18. debug("Fetching number of rows...", 6);
  19. return $this->num_rows;
  20. //return odbc_num_rows($this->stmt);
  21. }
  22. final public function numOfFields() {
  23. debug("Fetching number of fields...", 6);
  24. return odbc_num_fields($this->stmt);
  25. }
  26. final public function fetchHeaders() {
  27. debug("Fetching header names ...", 6);
  28. $fields = $this->fetchFields();
  29. $field_names = array();
  30. foreach($fields as $field) {
  31. $field_names[] = $field->name;
  32. }
  33. return $field_names;
  34. }
  35. final public function fetchField() {
  36. debug("Fetching next field...", 6);
  37. if($this->field_i > $this->numOfFields()) return NULL;
  38. $field = new Std();
  39. $field->name = odbc_field_name($this->stmt, $this->field_i);
  40. $field->type = odbc_field_type($this->stmt, $this->field_i);
  41. $field->length = odbc_field_len($this->stmt, $this->field_i);
  42. $field->precision = odbc_field_precision($this->stmt, $this->field_i);
  43. $field->scale = odbc_field_scale($this->stmt, $this->field_i);
  44. $this->field_i++;
  45. return $field;
  46. }
  47. final public function fetchFields() {
  48. debug("Fetching all fields...", 6);
  49. $this->reset();
  50. $fields = array();
  51. for ($i = 0; $i < $this->numOfFields(); $i++) {
  52. $fields[] = $this->fetchField();
  53. }
  54. return $fields;
  55. }
  56. final public function fetchRow( $type = FETCH_OBJ ) {
  57. debug("Fetching next row ...", 6);
  58. if($this->row_i > $this->numOfRows()) return NULL;
  59. $row = NULL;
  60. switch( $type ) {
  61. case FETCH_ARRAY:
  62. $row = odbc_fetch_array($this->stmt, $this->row_i++);
  63. break;
  64. case FETCH_VALUES:
  65. $row = array_values( mysqli_fetch_assoc($this->stmt, $this->row_i++) );
  66. break;
  67. case FETCH_OBJ:
  68. $row = odbc_fetch_object($this->stmt, $this->row_i++);
  69. break;
  70. }
  71. if( !$row ) return NULL;
  72. $i = 0;
  73. foreach($row as $value) {
  74. $this->bind_results[$i] = $value;
  75. $i++;
  76. }
  77. return $row;
  78. }
  79. final public function fetchRows( $type = FETCH_OBJ ) {
  80. debug("Fetching all rows...", 6);
  81. $this->reset();
  82. $rows = array();
  83. for($i = 0; $i < $this->numOfRows(); $i++) {
  84. $rows[] = $this->fetchRow($type);
  85. }
  86. return $rows;
  87. }
  88. final public function fetch() {
  89. debug("Fetching data for ".$this->numOfRows()." rows", 6);
  90. $this->reset();
  91. if($this->numOfRows() > 1)
  92. return $this->fetchRows();
  93. else
  94. return $this->fetchRow();
  95. }
  96. final public function reset() {
  97. debug("Resetting result set pointers...", 6);
  98. $this->row_i = 1;
  99. $this->field_i = 1;
  100. }
  101. final public function free() {
  102. debug("Freeing result set resources ...", 6);
  103. odbc_free_result ($this->stmt); //always return true
  104. }
  105. final public function bindResults(array $bind_results) {
  106. debug("Binding output parameters ...", 6);
  107. $this->bind_results = $bind_results;
  108. }
  109. final public function each( $closure ) {
  110. }
  111. final public function first() {
  112. }
  113. final public function last() {
  114. }
  115. final public function __toString() {
  116. return print_r($this->fetch(), true);
  117. }
  118. final public function __destruct() {
  119. debug("Destroying ODBCDatabaseResult object # ".static::getClass()->getInstanceCount()."...", 6);
  120. $this->free();
  121. }
  122. }
  123. ?>