result_set.class.php 4.3 KB

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