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.

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