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.

126 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. 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. */
  60. public function fetchColumn($field)
  61. {
  62. $data = array();
  63. while ($row = $this->fetch(Db::FETCH_ASSOC)) {
  64. $data[] = $row[$field];
  65. }
  66. return $data;
  67. }
  68. /**
  69. * @param string $field
  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. /**
  80. * @return array
  81. */
  82. public function fetchPairs()
  83. {
  84. $data = array();
  85. while ($row = $this->fetch(Db::FETCH_NUM)) {
  86. $data[$row[0]] = $row[1];
  87. }
  88. return $data;
  89. }
  90. /* Abstract methods */
  91. abstract public function bindParam($param, &$value);
  92. abstract protected function assemble();
  93. abstract public function fetch($style = Db::FETCH_OBJ);
  94. abstract public function fetchObject($class = 'stdClass');
  95. abstract public function close();
  96. /**
  97. * @return int
  98. */
  99. abstract public function affectedRows();
  100. abstract public function numRows();
  101. /**
  102. * @return bool
  103. */
  104. abstract protected function driverExecute($request);
  105. }