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.

62 lines
1.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php namespace Majestic\Model;
  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. class Db
  10. {
  11. const FETCH_ASSOC = 2;
  12. const FETCH_NUM = 3;
  13. const FETCH_BOTH = 4;
  14. const FETCH_OBJ = 5;
  15. /**
  16. * Databases connections
  17. *
  18. * @var array
  19. */
  20. static protected $connections = array();
  21. /**
  22. * Connect to database
  23. *
  24. * @param string $name Database name. If not set 'default' will be used.
  25. * @param array $config Configuration array.
  26. *
  27. * @return DbDriver
  28. * @throws \Majestic\Exception\InitializationException
  29. */
  30. static public function connect($name = 'default', $config = null)
  31. {
  32. if (!isset(self::$connections[$name])) {
  33. if (!$config) {
  34. if (!is_object(\Majestic\Config::get(__CLASS__))) {
  35. throw new \Majestic\Exception\InitializationException('Trying to get property of non-object');
  36. }
  37. $config = \Majestic\Config::get(__CLASS__)->$name;
  38. }
  39. if (!is_array($config)) {
  40. throw new \Majestic\Exception\InitializationException('Connection parameters must be an array');
  41. }
  42. $driver = '\Majestic\Model\MySQLiDriver';
  43. if (isset($config['driver'])) {
  44. $driver = $config['driver'];
  45. unset($config['driver']);
  46. }
  47. $connection = new $driver($config);
  48. if (!$connection instanceof DbDriver) {
  49. throw new \Majestic\Exception\InitializationException('Database driver must extends DbDriver');
  50. }
  51. self::$connections[$name] = $connection;
  52. }
  53. return self::$connections[$name];
  54. }
  55. }