/dbal/src/drivers/mysqli/lmbMysqliRecordSet.class.php

https://github.com/kugu/limb · PHP · 168 lines · 127 code · 26 blank · 15 comment · 19 complexity · 1921bd329ab588ff9809f75624c14340 MD5 · raw file

  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. lmb_require('limb/dbal/src/drivers/lmbDbBaseRecordSet.class.php');
  10. lmb_require('limb/dbal/src/drivers/mysqli/lmbMysqliRecord.class.php');
  11. /**
  12. * class lmbMysqliRecordSet.
  13. *
  14. * @package dbal
  15. * @version $Id: lmbMysqliRecordSet.class.php 6891 2008-04-01 21:44:38Z pachanga $
  16. */
  17. class lmbMysqliRecordSet extends lmbDbBaseRecordSet
  18. {
  19. protected $query;
  20. protected $connection;
  21. protected $current;
  22. protected $valid;
  23. protected $key;
  24. function __construct($connection, $queryString)
  25. {
  26. $this->connection = $connection;
  27. $this->query = $queryString;
  28. }
  29. protected function _is_resource($res)
  30. {
  31. return ('mysqli_result' === get_class($res));
  32. }
  33. function freeQuery()
  34. {
  35. if(isset($this->queryId) && $this->_is_resource($this->queryId))
  36. {
  37. Mysqli_free_result($this->queryId);
  38. $this->queryId = null;
  39. }
  40. }
  41. function rewind()
  42. {
  43. if(isset($this->queryId) && $this->_is_resource($this->queryId) && mysqli_num_rows($this->queryId))
  44. {
  45. if(mysqli_data_seek($this->queryId, 0) === false)
  46. {
  47. $this->connection->_raiseError();
  48. }
  49. }
  50. elseif(!$this->queryId)
  51. {
  52. $query = $this->query;
  53. if(is_array($this->sort_params))
  54. {
  55. if(preg_match('~(?<=FROM).+\s+ORDER\s+BY\s+~i', $query))
  56. $query .= ',';
  57. else
  58. $query .= ' ORDER BY ';
  59. foreach($this->sort_params as $field => $order)
  60. $query .= $this->connection->quoteIdentifier($field) . " $order,";
  61. $query = rtrim($query, ',');
  62. }
  63. if($this->limit)
  64. {
  65. $query .= ' LIMIT ' .
  66. $this->offset . ',' .
  67. $this->limit;
  68. }
  69. $this->queryId = $this->connection->execute($query);
  70. }
  71. $this->key = 0;
  72. $this->next();
  73. }
  74. function next()
  75. {
  76. $this->current = new lmbMysqliRecord();
  77. $values = Mysqli_fetch_assoc($this->queryId);
  78. $this->current->import($values);
  79. $this->valid = is_array($values);
  80. $this->key++;
  81. }
  82. function valid()
  83. {
  84. return $this->valid;
  85. }
  86. function current()
  87. {
  88. return $this->current;
  89. }
  90. function key()
  91. {
  92. return $this->key;
  93. }
  94. function at($pos)
  95. {
  96. $query = $this->query;
  97. if(is_array($this->sort_params))
  98. {
  99. $query .= ' ORDER BY ';
  100. foreach($this->sort_params as $field => $order)
  101. $query .= $this->connection->quoteIdentifier($field) . " $order,";
  102. $query = rtrim($query, ',');
  103. }
  104. $queryId = $this->connection->execute($query . " LIMIT $pos, 1");
  105. $res = Mysqli_fetch_assoc($queryId);
  106. Mysqli_free_result($queryId);
  107. if($res)
  108. {
  109. $record = new lmbMysqliRecord();
  110. $record->import($res);
  111. return $record;
  112. }
  113. }
  114. function countPaginated()
  115. {
  116. if(is_null($this->queryId))
  117. $this->rewind();
  118. return Mysqli_num_rows($this->queryId);
  119. }
  120. function count()
  121. {
  122. if(!(preg_match("/^\s*SELECT\s+DISTINCT/is", $this->query) || preg_match('/\s+GROUP\s+BY\s+/is', $this->query)) &&
  123. preg_match("/^\s*SELECT\s+.+\s+FROM\s+/Uis", $this->query))
  124. {
  125. //optimization for non paginated queries
  126. if(!$this->limit && $this->queryId && $this->valid())
  127. return mysqli_num_rows($this->queryId);
  128. $rewritesql = preg_replace('/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ', $this->query);
  129. $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','', $rewritesql);
  130. $queryId = $this->connection->execute($rewritesql);
  131. $row = Mysqli_fetch_row($queryId);
  132. Mysqli_free_result($queryId);
  133. if(is_array($row))
  134. return $row[0];
  135. }
  136. // could not re-write the query, try a different method.
  137. $queryId = $this->connection->execute($this->query);
  138. $count = Mysqli_num_rows($queryId);
  139. Mysqli_free_result($queryId);
  140. return $count;
  141. }
  142. }