result_set.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * ResultSet
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. * @deprecated
  8. */
  9. class ResultSet implements Countable, Iterator
  10. {
  11. /**
  12. *
  13. * @param string $sql
  14. * @return ResultSet
  15. */
  16. static function create($sql)
  17. {
  18. return new self($sql);
  19. }
  20. protected $sql = '';
  21. protected $handle = null;
  22. protected $current = false;
  23. protected $index = -1;
  24. protected $count = false;
  25. protected $limit_count = null;
  26. protected $limit_offset = null;
  27. protected $orderby_column = null;
  28. protected $orderby_direction = null;
  29. protected $return_type = null;
  30. function __construct($sql, $limit_count = null, $limit_offset = null, $orderby_column = null, $orderby_direction = null, $return_type = null)
  31. {
  32. $this->sql = $sql;
  33. $this->limit_count = $limit_count;
  34. $this->limit_offset = $limit_offset;
  35. $this->orderby_column = $orderby_column;
  36. $this->orderby_direction = $orderby_direction;
  37. $this->return_type = $return_type;
  38. }
  39. public function sql()
  40. {
  41. $sql = $this->sql;
  42. $column = $this->orderby_column;
  43. $direction = $this->orderby_direction;
  44. $offset = $this->limit_offset;
  45. $count = $this->limit_count;
  46. if (is_null($column) && is_null($count) && is_null($offset)) {
  47. return $sql;
  48. }
  49. if (strpos($sql, ' ORDER ') || strpos($sql, ' LIMIT ') || strpos($sql, ' OFFSET ')) {
  50. $sql = "SELECT * FROM ($sql) AS dat ";
  51. } else {
  52. $sql .= ' ';
  53. }
  54. if ($column) {
  55. $sql .= "ORDER BY $column $direction ";
  56. }
  57. if ($count) {
  58. $sql .= "LIMIT $count ";
  59. }
  60. if ($offset) {
  61. $sql .= "OFFSET $offset";
  62. }
  63. return $sql;
  64. }
  65. protected function handle()
  66. {
  67. if (is_null($this->handle)) {
  68. $this->handle = Database::query($this->sql());
  69. }
  70. return $this->handle;
  71. }
  72. public function count()
  73. {
  74. if ($this->count === false) {
  75. $sql = $this->sql();
  76. $sql = "SELECT COUNT(*) AS alpha FROM ($sql) AS dat ";
  77. $rs = Database :: query($sql);
  78. $data = Database::fetch_array($rs);
  79. $count = $data ? $data['alpha'] : 0;
  80. $this->count = (int) $count;
  81. }
  82. return $this->count;
  83. }
  84. public function first()
  85. {
  86. foreach ($this as $item) {
  87. return $item;
  88. }
  89. return null;
  90. }
  91. /**
  92. *
  93. * @param int $count
  94. * @param int $from
  95. * @return ResultSet
  96. */
  97. public function limit($count, $from = 0)
  98. {
  99. $result = clone($this);
  100. $result->limit_offset = $from;
  101. $result->limit_count = $count;
  102. return $result;
  103. }
  104. /**
  105. *
  106. * @param int $column
  107. * @param int $dir
  108. * @return ResultSet
  109. */
  110. public function orderby($column, $dir = 'ASC')
  111. {
  112. $result = clone($this);
  113. $result->orderby_column = $column;
  114. $result->orderby_direction = $dir;
  115. return $result;
  116. }
  117. public function return_type($value)
  118. {
  119. $result = clone($this);
  120. $result->return_type = $value;
  121. return $result;
  122. }
  123. public function current()
  124. {
  125. return $this->current;
  126. }
  127. public function key()
  128. {
  129. return $this->index;
  130. }
  131. public function next()
  132. {
  133. $data = Database::fetch_assoc($this->handle());
  134. if (!$data) {
  135. $this->current = $this->return_type ? null : array();
  136. } else if (empty($this->return_type)) {
  137. $this->current = $data;
  138. } else if ($this->return_type == 'object') {
  139. $this->current = (object) $data;
  140. } else {
  141. $this->current = new $this->return_type($data);
  142. }
  143. $this->index++;
  144. return $this->current;
  145. }
  146. public function rewind()
  147. {
  148. $this->handle = null;
  149. $this->current = false;
  150. $this->index = -1;
  151. $this->next();
  152. }
  153. public function valid()
  154. {
  155. return !empty($this->current);
  156. }
  157. function __clone()
  158. {
  159. $this->reset();
  160. }
  161. function reset()
  162. {
  163. $this->handle = null;
  164. $this->current = false;
  165. $this->index = -1;
  166. $this->count = false;
  167. }
  168. }