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.

115 lines
2.3 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. */
  9. abstract class DbStatement
  10. {
  11. /**
  12. * @var DbDriver
  13. */
  14. protected $driver;
  15. /**
  16. * @var string
  17. */
  18. protected $request;
  19. protected $params = array();
  20. protected $result;
  21. public function __construct($driver, $request)
  22. {
  23. $this->driver = $driver;
  24. $this->request = $request;
  25. }
  26. /**
  27. * @param array $params
  28. * @return bool
  29. */
  30. public function execute($params = null)
  31. {
  32. if (is_array($params)) {
  33. foreach ($params as $param => &$value) {
  34. $this->bindParam($param, $value);
  35. }
  36. }
  37. return $this->driverExecute($this->assemble());
  38. }
  39. public function __destruct()
  40. {
  41. $this->close();
  42. }
  43. /**
  44. * @param mixed $style
  45. * @return array
  46. */
  47. public function fetchAll($style = Db::FETCH_OBJ)
  48. {
  49. $data = array();
  50. while ($row = $this->fetch($style)) {
  51. $data[] = $row;
  52. }
  53. return $data;
  54. }
  55. /**
  56. * @param string $field
  57. * @return array Value of queried field for all matching rows
  58. */
  59. public function fetchColumn($field)
  60. {
  61. $data = array();
  62. while ($row = $this->fetch(Db::FETCH_ASSOC)) {
  63. $data[] = $row[$field];
  64. }
  65. return $data;
  66. }
  67. /**
  68. * @param string $field
  69. * @return mixed Value of queried filed for first matching row
  70. */
  71. public function fetchField($field)
  72. {
  73. $row = $this->fetch(Db::FETCH_ASSOC);
  74. if (isset($row[$field])) {
  75. return $row[$field];
  76. }
  77. return false;
  78. }
  79. /* Abstract methods */
  80. abstract public function bindParam($param, &$value);
  81. abstract protected function assemble();
  82. abstract public function fetch($style = Db::FETCH_OBJ);
  83. abstract public function fetchObject($class = 'stdClass');
  84. abstract public function close();
  85. /**
  86. * @return int
  87. */
  88. abstract public function affectedRows();
  89. abstract public function numRows();
  90. /**
  91. * @param mixed $request
  92. * @return bool
  93. */
  94. abstract protected function driverExecute($request);
  95. }