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.

114 lines
2.2 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. /* 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. * @return bool
  92. */
  93. abstract protected function driverExecute($request);
  94. }