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.

83 lines
1.8 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-16
  8. */
  9. abstract class DbDriver
  10. {
  11. /**
  12. * Database connection
  13. *
  14. * @var object
  15. */
  16. protected $connection = null;
  17. /**
  18. * Configuration data
  19. *
  20. * @var array
  21. */
  22. protected $config = array();
  23. public function __construct($config)
  24. {
  25. $this->checkConfig($config);
  26. $this->config = $config;
  27. }
  28. protected function checkConfig($config)
  29. {
  30. $required = array('database', 'username', 'password', 'hostname');
  31. foreach ($required as $option) {
  32. if (!isset($config[$option])) {
  33. throw new GeneralException('Configuration must have a "' . $option . '".');
  34. }
  35. }
  36. }
  37. public function getConnection()
  38. {
  39. $this->connect();
  40. return $this->connection;
  41. }
  42. /**
  43. * @param mixed $request
  44. * @param mixed $params
  45. * @return DbStatement
  46. */
  47. public function query($request, $params = array())
  48. {
  49. $this->connect();
  50. if (!is_array($params)) {
  51. $params = array($params);
  52. }
  53. $stmt = $this->prepare($request);
  54. $stmt->execute($params);
  55. return $stmt;
  56. }
  57. /* Abstract methods */
  58. abstract public function insert($table, $data);
  59. abstract public function update($table, $data, $condition);
  60. abstract public function delete($table, $condition);
  61. abstract public function prepare($request);
  62. abstract public function getInsertId($table = null, $key = null);
  63. abstract public function isConnected();
  64. abstract public function disconnect();
  65. abstract protected function connect();
  66. }