Ejemplo n.º 1
0
 /**
  * new Database('default');
  *
  * 支持 `new Database('mysqli://*****:*****@127.0.0.1/myqee/');` 的方式
  *
  * @param string $config_name 默认值为 `Database::DEFAULT_CONFIG_NAME`
  * @return  void
  */
 public function __construct($config_name = null)
 {
     if (null === $config_name) {
         $config_name = Database::DEFAULT_CONFIG_NAME;
     }
     if (is_array($config_name)) {
         $this->config = $config_name;
     } elseif (false !== strpos($config_name, '://')) {
         list($type) = explode('://', $config_name);
         $this->config = array('type' => $type, 'connection' => $config_name, 'table_prefix' => '', 'charset' => 'utf8', 'caching' => false, 'profiling' => true);
     } else {
         $this->config = Core::config('database.' . $config_name);
     }
     $this->config['charset'] = strtoupper($this->config['charset']);
     if (!isset($this->config['auto_change_charset'])) {
         $this->config['auto_change_charset'] = false;
     }
     if ($this->config['auto_change_charset']) {
         if (isset($this->config['data_charset'])) {
             $this->config['data_charset'] = strtoupper($this->config['data_charset']);
         } else {
             $this->config['data_charset'] = $this->config['charset'];
         }
     }
     $driver = $this->config['type'];
     if (!$driver) {
         $driver = 'MySQL';
     }
     $driver = 'Database_Driver_' . $driver;
     if (!class_exists($driver, true)) {
         throw new Exception('Database Driver:' . $driver . ' not found.');
     }
     if (!isset($this->config['connection'])) {
         throw new Exception('Database connection not set.');
     }
     if (is_string($this->config['connection'])) {
         $this->config['connection'] = Database::parse_dsn($this->config['connection']);
     }
     # 当前驱动
     $this->driver = new $driver($this->config);
     parent::__construct();
     # 增加自动关闭连接列队
     Core::add_close_connect_class('Database');
 }
Ejemplo n.º 2
0
 /**
  * Sets the initial columns to select from.
  *
  * @param   array  column list
  * @return  void
  */
 public function __construct($config_name = 'default')
 {
     if (\is_array($config_name)) {
         $this->config = $config_name;
     } else {
         $this->config = \Core::config('database.' . $config_name);
     }
     $this->config['charset'] = \strtoupper($this->config['charset']);
     if (!isset($this->config['auto_change_charset'])) {
         $this->config['auto_change_charset'] = false;
     }
     if ($this->config['auto_change_charset']) {
         if (isset($this->config['data_charset'])) {
             $this->config['data_charset'] = \strtoupper($this->config['data_charset']);
         } else {
             $this->config['data_charset'] = $this->config['charset'];
         }
     }
     $driver = $this->config['type'];
     if (!$driver) {
         $driver = 'MySQL';
     }
     $driver = '\\Database_Driver_' . $driver;
     if (!\class_exists($driver, true)) {
         throw new \Exception('Database Driver:' . $driver . ' not found.');
     }
     if (\is_string($this->config['connection'])) {
         $this->config['connection'] = static::parse_dsn($this->config['connection']);
     }
     # 当前驱动
     $this->driver = new $driver($this->config);
     parent::__construct();
     # 增加自动关闭连接列队
     \Core::add_close_connect_class('\\Database');
 }