获取实例

@param string $config_name
@return DbConnection
@throws Exception
484054
public static function instance($config_name)
    {
        if (empty(self::$instance[$config_name])) {
            $config = self::$config[$config_name];
            self::$instance[$config_name] = new \Beanbun\Lib\DbConnection($config);
        }
        return self::$instance[$config_name];
    }
388111
static function getConnection($db_name=null) {
		if (null === $db_name) {
			$keys = array_keys(self::$parameters);
			$db_name = reset($keys);
		}

		if (!@$db_name) {
			return null;
		}

		return self::connect($db_name);
	}
1224128
static function getConnection($connection_name = null) {
		if (null === $connection_name) {
			$keys = array_keys(self::$parameters);
			$connection_name = reset($keys);
		}

		if (!@$connection_name) {
			return null;
		}

		return self::connect($connection_name);
	}
1072763
static function getParameter($connection_name, $key) {
		// don't reveal passwords through this interface
		if ('password' === $key) {
			throw new RuntimeException('DB::password is private');
		}

		if (!array_key_exists($connection_name, self::$parameters)) {
			throw new RuntimeException("Configuration for database '$connection_name' not loaded");
		}

		return @self::$parameters[$connection_name][$key];
	}
472020
public static SystemConfiguration get(final String _name)
        throws CacheReloadException
    {
        final Cache<String, SystemConfiguration> cache = InfinispanCache.get().<String, SystemConfiguration>getCache(
                        SystemConfiguration.NAMECACHE);
        if (!cache.containsKey(_name)) {
            SystemConfiguration.getSystemConfigurationFromDB(SystemConfiguration.SQL_NAME, _name);
        }
        return cache.get(_name);
    }
404396
protected function connect() {
        $db = Config::config()['user']['database'];

        if ($db['enabled']) {
            switch ($db['driver']) {
                case 'pdo' :
                    $options = [Pdo::ATTR_STATEMENT_CLASS => ['\Gcs\Framework\Core\Pdo\PdoStatement', []]];

                    switch ($db['type']) {
                        case 'mysql':
                            try {
                                $this->db = new Pdo('mysql:host=' . $db['hostname'] . ';dbname=' . $db['database'], $db['username'], $db['password'], $options);
                                //self::$sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                $this->db->exec('SET NAMES ' . strtoupper($db['charset']));
                            }
                            catch (\PDOException $e) {
                                throw new MissingDatabaseException($e->getMessage() . ' / ' . $e->getCode() . ' / ' . $e->getFile());
                            }
                            break;

                        case 'pgsql':
                            try {
                                $this->db = new Pdo('mysql:host=' . $db['hostname'] . ';dbname=' . $db['database'], $db['username'], $db['password'], $options);
                                //self::$sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                $this->db->exec('SET NAMES ' . strtoupper($db['charset']));
                            }
                            catch (\PDOException $e) {
                                throw new MissingDatabaseException($e->getMessage() . ' / ' . $e->getCode());
                            }
                            break;

                        default :
                            throw new MissingDatabaseException("Can't connect to SQL Database because the driver is not supported");
                            break;
                    }
                    break;

                default :
                    throw new MissingDatabaseException("Can't connect to SQL Database because the API is unrecognized");
                    break;
            }

            return $this->db;
        }
        else {
            return null;
        }
    }
851266
public static function get($name, array $config)
    {
        $driverClass = "Rad\\Database\\Driver\\" . ucfirst($name);

        if (!class_exists($driverClass)) {
            throw new Exception(sprintf('Driver "%s" does not exist.', $driverClass));
        }

        return new $driverClass($config);
    }
1329207
protected static function _setupDb($connection_name = self::DEFAULT_CONNECTION)
        {
            if (!Arrays::exists($connection_name, self::$_db) || !is_object(self::$_db[$connection_name])) {
                self::_setupDbConfig($connection_name);

                $db = new PDO(
                    self::$_config[$connection_name]['connection_string'],
                    self::$_config[$connection_name]['username'],
                    self::$_config[$connection_name]['password'],
                    self::$_config[$connection_name]['driver_options']
                );

                $db->setAttribute(PDO::ATTR_ERRMODE, self::$_config[$connection_name]['error_mode']);

                self::set_db($db, $connection_name);
            }
        }
744636
public static function for_table($table_name, $connection_name = parent::DEFAULT_CONNECTION)
        {
            self::_setupDb($connection_name);

            return new self($table_name, [], $connection_name);
        }
757706
public function getConnection($name = null) {
        if (is_null($name)) {
            $name = $this->defaultConnectionName;
        }

        if (isset($this->connectionMap[$name])) {
            return $this->connectionMap[$name];
        }

        if (!isset($this->connectionConfigs[$name])) {
            throw new SqlException("未定义的数据库连接，连接名称：{$name}");
        }

        $connConfig = $this->connectionConfigs[$name];
        $conn = $this->buildConnection($connConfig);

        $this->connectionMap[$name] = $conn;
        return $conn;
    }
280086
public static function setConnectionConfig()
	{
		if (!static::$_dbConnectionConfig) {
			$config = static::_getDbConnectionConfig();
			if (!is_array($config) || empty($config))
				throw new \ErrorException(
					'Not set configuration of database in file '
					.APPPATH.'config/database.php. '
					.'See example '.__DIR__.'database.php.config_example'
				);
			static::_setDbConnectionConfig($config);
			static::$_dbConnectionConfig = $config;
		}
	}
1200387
public static function config($config_block)
    {
        if (!empty(self::$config)) {
            return self::$config[$config_block];
        }

        $config_file = "database.php";
        $path = __DIR__ . "/../config/" .$config_file;

        $env =  (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : '');

        if ($config_block == "default" && $env) {
            $config_block = $env;
        }

        if (file_exists($path)) {
            $config = (include $path);
            if (isset($config[$config_block])) {
                return $config[$config_block];
            } else {
                throw new \Exception("Database config section '{$config_block}' not exists.");
            }

            return $config['default'];
        } else {
            throw new \Exception("Database config not found.");
        }

    }
591575
public function statement($name, $config = [])
    {
        $defaults = ['dialect' => $this];
        $config += $defaults;

        if (!isset($this->_classes[$name])) {
            throw new SqlException("Unsupported statement `'{$name}'`.");
        }
        $statement = $this->_classes[$name];
        return new $statement($config);
    }
1242096
public static function initPdoDatabase($config, $name)
    {
        try {
            $pdo = static::createPdoConnectionByConfig($config);
        } catch (PDOException $e) {
            throw new Exception('Cannon instantiate database connection: ' . $e->getMessage());
        }

        $pdo->uniqueName = $config['host'] . "-" . $config['db'];

        static::checkDebugBar($pdo, $name);

        return new RepositoryPDO($pdo, $name);
    }
151579
static function getParameter($db_name, $key) {
		// don't reveal passwords through this interface
		if ('password' === $key) {
			throw new Exception('DB::password is private');
		}

		if (!array_key_exists($db_name, self::$parameters)) {
			throw new Exception("Configuration for database '$db_name' not loaded");
		}

		return @self::$parameters[$db_name][$key];
	}
1161492
public static function build(string $connectionName = null) : Connection
    {
        $configs = Application::current()->config('database');
        $connectionName = $connectionName ?: $configs['default'];

        if (!isset($configs[$connectionName])) {
            throw new \Exception("Unknown database connection: '$connectionName'");
        }

        if (!isset(self::$connections[$connectionName])) {
            $connectionClass = $configs[$connectionName]['driver'];
            $options = $configs[$connectionName]['options'];
            self::$connections[$connectionName] = new $connectionClass($options);
        }

        return self::$connections[$connectionName];
    }
1368933
protected function getPDO($name)
    {
        if (!isset($this->config[$name]) || !is_array($this->config[$name])) {
            throw new \Exception('Config not found ! (config: ' . $name . ')');
        }

        $config = $this->config[$name];

        if (!isset($config['dsn'])) {
            throw new \Exception('"dsn" parameter is not defined !');
        }

        if (!isset($config['user'])) {
            throw new \Exception('"user" parameter is not defined !');
        }

        if (!isset($config['pass'])) {
            throw new \Exception('"pass" parameter is not defined !');
        }

        $config['options'] = isset($config['options']) ? $config['options'] : array();

        $options = array();
        foreach ($config['options'] as $name => $value) {
            if (0 === strpos($name, 'PDO::')) {
                $options[constant($name)] = $value;
            }
        }

        $connection = new \PDO($config['dsn'], $config['user'], $config['pass'], $options);
        $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

        return $connection;
    }