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.

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