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.9 KiB

11 years ago
  1. <?php namespace Majestic\Model;
  2. use Majestic\Exception\GeneralException;
  3. /**
  4. * @copyright NetMonsters <team@netmonsters.ru>
  5. * @link http://netmonsters.ru
  6. * @package Majestic
  7. * @subpackage db
  8. * @since 2010-02-16
  9. */
  10. abstract class DbDriver
  11. {
  12. /**
  13. * Database connection
  14. *
  15. * @var object
  16. */
  17. protected $connection = null;
  18. /**
  19. * Configuration data
  20. *
  21. * @var array
  22. */
  23. protected $config = array();
  24. public function __construct($config)
  25. {
  26. $this->checkConfig($config);
  27. $this->config = $config;
  28. }
  29. protected function checkConfig($config)
  30. {
  31. $required = array('database', 'username', 'password', 'hostname');
  32. foreach ($required as $option) {
  33. if (!isset($config[$option])) {
  34. throw new GeneralException('Configuration must have a "' . $option . '".');
  35. }
  36. }
  37. }
  38. public function getConnection()
  39. {
  40. $this->connect();
  41. return $this->connection;
  42. }
  43. /**
  44. * @param mixed $request
  45. * @param mixed $params
  46. * @return DbStatement
  47. */
  48. public function query($request, $params = array())
  49. {
  50. $this->connect();
  51. if (!is_array($params)) {
  52. $params = array($params);
  53. }
  54. $stmt = $this->prepare($request);
  55. $stmt->execute($params);
  56. return $stmt;
  57. }
  58. /* Abstract methods */
  59. abstract public function insert($table, $data);
  60. abstract public function update($table, $data, $condition);
  61. abstract public function delete($table, $condition);
  62. abstract public function prepare($request);
  63. abstract public function getInsertId($table = null, $key = null);
  64. abstract public function isConnected();
  65. abstract public function disconnect();
  66. abstract protected function connect();
  67. }