result_set.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. function __construct($sql, $limit_count = null, $limit_offset = null, $orderby_column = null, $orderby_direction = null)
  29. {
  30. $this->sql = $sql;
  31. $this->limit_count = $limit_count;
  32. $this->limit_offset = $limit_offset;
  33. $this->orderby_column = $orderby_column;
  34. $this->orderby_direction = $direction;
  35. }
  36. public function sql()
  37. {
  38. $sql = $this->sql;
  39. $column = $this->orderby_column;
  40. $direction = $this->orderby_direction;
  41. $offset = $this->limit_offset;
  42. $count = $this->limit_count;
  43. if (is_null($column) && is_null($count) && is_null($offset)) {
  44. return $sql;
  45. }
  46. if (strpos($sql, ' ORDER ') || strpos($sql, ' LIMIT ') || strpos($sql, ' OFFSET ')) {
  47. $sql = "SELECT * FROM ($sql) AS dat ";
  48. }else
  49. {
  50. $sql .= ' ';
  51. }
  52. if ($column) {
  53. $sql .= "ORDER BY $column $direction ";
  54. }
  55. if ($count) {
  56. $sql .= "LIMIT $count ";
  57. }
  58. if ($offset) {
  59. $sql .= "OFFSET $offset";
  60. }
  61. return $sql;
  62. }
  63. protected function handle()
  64. {
  65. if (is_null($this->handle)) {
  66. $this->handle = Database::query($this->sql());
  67. }
  68. return $this->handle;
  69. }
  70. public function count()
  71. {
  72. if ($this->count === false) {
  73. $sql = $this->sql();
  74. $sql = "SELECT COUNT(*) AS alpha FROM ($sql) AS dat ";
  75. $rs = Database :: query($sql);
  76. $data = Database::fetch_array($rs);
  77. $count = $data ? $data['alpha'] : 0;
  78. $this->count = (int) $count;
  79. }
  80. return $this->count;
  81. }
  82. /**
  83. *
  84. * @param int $count
  85. * @param int $from
  86. * @return ResultSet
  87. */
  88. public function limit($count, $from = 0)
  89. {
  90. $result = clone($this);
  91. $result->limit_offset = $from;
  92. $result->limit_count = $count;
  93. return $result;
  94. }
  95. /**
  96. *
  97. * @param int $column
  98. * @param int $dir
  99. * @return ResultSet
  100. */
  101. public function orderby($column, $dir = 'ASC')
  102. {
  103. $result = clone($this);
  104. $result->orderby_column = $column;
  105. $result->orderby_direction = $dir;
  106. return $result;
  107. }
  108. public function current()
  109. {
  110. return $this->current;
  111. }
  112. public function key()
  113. {
  114. return $this->index;
  115. }
  116. public function next()
  117. {
  118. $this->current = Database::fetch_assoc($this->handle());
  119. $this->index++;
  120. return $this->current;
  121. }
  122. public function rewind()
  123. {
  124. $this->handle = null;
  125. $this->current = false;
  126. $this->index = -1;
  127. $this->next();
  128. }
  129. public function valid()
  130. {
  131. return !empty($this->current);
  132. }
  133. function __clone()
  134. {
  135. $this->reset();
  136. }
  137. function reset()
  138. {
  139. $this->handle = null;
  140. $this->current = false;
  141. $this->index = -1;
  142. $this->count = false;
  143. }
  144. }