You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage db
  7. * @since 2010-02-19
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. /**
  12. * @property MySQLiDriver $driver
  13. * @property MySQLi_Result $result
  14. */
  15. class MySQLiStatement extends DbStatement
  16. {
  17. public function fetch($style = Db::FETCH_OBJ)
  18. {
  19. if (! $this->result) {
  20. return false;
  21. }
  22. $row = false;
  23. switch ($style) {
  24. case Db::FETCH_OBJ:
  25. $row = $this->result->fetch_object();
  26. break;
  27. case Db::FETCH_NUM:
  28. $row = $this->result->fetch_array(MYSQLI_NUM);
  29. break;
  30. case Db::FETCH_ASSOC:
  31. $row = $this->result->fetch_assoc();
  32. break;
  33. case Db::FETCH_BOTH:
  34. $row = $this->result->fetch_array(MYSQLI_BOTH);
  35. break;
  36. default:
  37. throw new Exception('Invalid fetch mode "' . $style . '" specified');
  38. }
  39. return $row;
  40. }
  41. /**
  42. * @param string $class
  43. */
  44. public function fetchObject($class = 'stdClass')
  45. {
  46. return $this->result->fetch_object($class);
  47. }
  48. public function close()
  49. {
  50. if ($this->result !== null) {
  51. $this->result->close();
  52. $this->result = null;
  53. }
  54. }
  55. public function affectedRows()
  56. {
  57. return $this->driver->getConnection()->affected_rows;
  58. }
  59. public function numRows()
  60. {
  61. if ($this->result) {
  62. return $this->result->num_rows;
  63. }
  64. return false;
  65. }
  66. protected function driverExecute($sql)
  67. {
  68. /**
  69. * @var MySQLi
  70. */
  71. $mysqli = $this->driver->getConnection();
  72. $result = $mysqli->query($sql);
  73. if ($result === false) {
  74. throw new Exception($mysqli->error, $mysqli->errno);
  75. }
  76. if ($result instanceof MySQLi_Result) {
  77. $this->result = $result;
  78. }
  79. return true;
  80. }
  81. }