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.

102 lines
2.4 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. /**
  18. * Fetches single row
  19. *
  20. * @param mixed $style
  21. * @return mixed
  22. */
  23. public function fetch($style = Db::FETCH_OBJ)
  24. {
  25. if (!$this->result) {
  26. return false;
  27. }
  28. $row = false;
  29. switch ($style) {
  30. case Db::FETCH_OBJ:
  31. $row = $this->result->fetch_object();
  32. break;
  33. case Db::FETCH_NUM:
  34. $row = $this->result->fetch_array(MYSQLI_NUM);
  35. break;
  36. case Db::FETCH_ASSOC:
  37. $row = $this->result->fetch_assoc();
  38. break;
  39. case Db::FETCH_BOTH:
  40. $row = $this->result->fetch_array(MYSQLI_BOTH);
  41. break;
  42. default:
  43. throw new GeneralException('Invalid fetch mode "' . $style . '" specified');
  44. }
  45. return $row;
  46. }
  47. /**
  48. * @param string $class
  49. */
  50. public function fetchObject($class = 'stdClass')
  51. {
  52. return $this->result->fetch_object($class);
  53. }
  54. public function close()
  55. {
  56. if ($this->result !== null) {
  57. $this->result->close();
  58. $this->result = null;
  59. }
  60. }
  61. public function affectedRows()
  62. {
  63. return $this->driver->getConnection()->affected_rows;
  64. }
  65. public function numRows()
  66. {
  67. if ($this->result) {
  68. return $this->result->num_rows;
  69. }
  70. return false;
  71. }
  72. protected function driverExecute($sql)
  73. {
  74. /**
  75. * @var MySQLi
  76. */
  77. $mysqli = $this->driver->getConnection();
  78. if (DEBUG) {
  79. $profiler = Profiler::getInstance()->profilerCommand('MySQL', $sql);
  80. $result = $mysqli->query($sql);
  81. $profiler->end();
  82. } else {
  83. $result = $mysqli->query($sql);
  84. }
  85. if ($result === false) {
  86. $message = $mysqli->error . "\nQuery: \"" . $sql . '"';
  87. throw new GeneralException($message, $mysqli->errno);
  88. }
  89. if ($result instanceof MySQLi_Result) {
  90. $this->result = $result;
  91. }
  92. return true;
  93. }
  94. }