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.

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