MySQLResult.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Swift Mailer MySQL Resultset Iterator
  4. * Please read the LICENSE file
  5. * @author Chris Corbyn <chris@w3style.co.uk>
  6. * @package Swift
  7. * @license GNU Lesser General Public License
  8. */
  9. require_once dirname(__FILE__) . "/../ClassLoader.php";
  10. Swift_ClassLoader::load("Swift_Iterator");
  11. /**
  12. * Swift Mailer MySQL Resultset Iterator.
  13. * Iterates over MySQL Resultset from mysql_query().
  14. * @package Swift
  15. * @author Chris Corbyn <chris@w3style.co.uk>
  16. */
  17. class Swift_Iterator_MySQLResult implements Swift_Iterator
  18. {
  19. /**
  20. * The MySQL resource.
  21. * @var resource
  22. */
  23. protected $resultSet;
  24. /**
  25. * The current row (array) in the resultset.
  26. * @var array
  27. */
  28. protected $currentRow = array(null, null);
  29. /**
  30. * The current array position.
  31. * @var int
  32. */
  33. protected $pos = -1;
  34. /**
  35. * The total number of rows in the resultset.
  36. * @var int
  37. */
  38. protected $numRows = 0;
  39. /**
  40. * Ctor.
  41. * @param resource The resultset iterate over.
  42. */
  43. public function __construct($rs)
  44. {
  45. $this->resultSet = $rs;
  46. $this->numRows = mysql_num_rows($rs);
  47. }
  48. /**
  49. * Get the resultset.
  50. * @return resource
  51. */
  52. public function getResultSet()
  53. {
  54. return $this->resultSet;
  55. }
  56. /**
  57. * Returns true if there is a value after the current one.
  58. * @return boolean
  59. */
  60. public function hasNext()
  61. {
  62. return (($this->pos + 1) < $this->numRows);
  63. }
  64. /**
  65. * Moves to the next array element if possible.
  66. * @return boolean
  67. */
  68. public function next()
  69. {
  70. if ($this->hasNext())
  71. {
  72. $this->currentRow = mysql_fetch_array($this->resultSet);
  73. $this->pos++;
  74. return true;
  75. }
  76. return false;
  77. }
  78. /**
  79. * Goes directly to the given element in the array if possible.
  80. * @param int Numeric position
  81. * @return boolean
  82. */
  83. public function seekTo($pos)
  84. {
  85. if ($pos >= 0 && $pos < $this->numRows)
  86. {
  87. mysql_data_seek($this->resultSet, $pos);
  88. $this->currentRow = mysql_fetch_array($this->resultSet);
  89. mysql_data_seek($this->resultSet, $pos);
  90. $this->pos = $pos;
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Returns the value at the current position, or NULL otherwise.
  97. * @return mixed.
  98. */
  99. public function getValue()
  100. {
  101. $row = $this->currentRow;
  102. if ($row[0] !== null)
  103. return new Swift_Address($row[0], isset($row[1]) ? $row[1] : null);
  104. else
  105. return null;
  106. }
  107. /**
  108. * Gets the current numeric position within the array.
  109. * @return int
  110. */
  111. public function getPosition()
  112. {
  113. return $this->pos;
  114. }
  115. }