Get the global database connection.

@param string $name An optional name given to a connection in the DB::setConn() call.  If omitted,
the default connection is returned.
@return Database
551710
public static function get_conn($name = 'default')
    {
        if (isset(self::$connections[$name])) {
            return self::$connections[$name];
        }

        // lazy connect
        $config = static::getConfig($name);
        if ($config) {
            return static::connect($config, $name);
        }

        return null;
    }
12704
public function connection($name = null)
    {
        if ( ! isset($this->connections[$name]))
        {
            $this->connections[$name] = $this->createConnection($name);
        }

        return $this->connections[$name];
    }
1160939
public function getConnection(string $name = null)
    {
        if (empty($name)) {
            $name = $this->defaultConnection;
        }

        if (false === array_key_exists($name, $this->connections)) {
            return null;
        }

        return $this->connections[$name];
    }
1047963
protected function configuration($name)
    {
        $name = $name ?: $this->getDefaultConnection();

        // To get the database connection configuration, we will just pull each of the
        // connection configurations and get the configurations for the given name.
        // If the configuration doesn't exist, we'll throw an exception and bail.
        $connections = $this->container['config']['database.connections']->toArray();

        if (is_null($config = Arr::get($connections, $name))) {
            throw new InvalidArgumentException("Database [$name] not configured.");
        }

        return $config;
    }
1183810
private static function getConnectionConfiguration($name)
    {
        $databaseConfig = Configuration::get('database.connections');
        if (isset($databaseConfig[$name])) {
            $databaseConfig = $databaseConfig[$name];
        } else {
            $databaseConfig = null;
        }

        if ($databaseConfig === null) {
            throw new ConfigurationException("Database configuration for connection '" . $name . "' doesn't exists!");
        }

        if (! isset($databaseConfig['driver'])) {
            throw new ConfigurationException("Database configuration for connection '" . $name . "' is invalid!");
        }

        return $databaseConfig;
    }
241872
public static function connect($name = 'default') {
        if ($name instanceof DatabaseConnection) {
            return $name;
        }
        if (is_null($name)) {
            $name = 'default';
        }
        if (defined('ARTISAN_TASK_PID')) {
            $pid = '@' . @posix_getpid();
        } else {
            $pid = '';
        }
        $config = $pname = false;
        if (is_array($name)) {
            $tmpname = implode('_', $name) . $pid;
            if (isset (self::$dbs [ $tmpname ])) {
                return self::$dbs [ $tmpname ];
            }
            $config = $name;
            $pname  = $tmpname;
        } else if (is_string($name)) {
            $pname = $name . $pid;
            if (isset (self::$dbs [ $pname ])) {
                return self::$dbs [ $pname ];
            }
            $config = App::cfgLoader()->loadDatabaseConfig($name);
        } else if ($name instanceof DatabaseConfiguration) {
            $config = $name;
            $pname  = $config->__toString() . $pid;
        }
        if ($config) {
            $dialect = DatabaseDialect::getDialect($config);
            if ($dialect) {
                $db                   = new DatabaseConnection($dialect, $name);
                $db->name             = $pname;
                self::$dbs [ $pname ] = $db;

                return $db;
            }
        }
        throw new DialectException('cannot connect the database server!');
    }
772103
public static function connection($name = null)
    {
        self::$use = $name ? $name : self::$use;
        return self::get(self::$use);
    }
1229918
public function getConnectionConfig($name = null) {
		if (is_null($name)) {
			$name = $this->defaultConfigName;
		}

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

		return null;
	}
191123
public function getDatabase($connectionName = 'default')
    {
        if ($this->databases->containsKey($connectionName)) {
            return $this->databases->get($connectionName);
        }

        $configuration = $this->getConfiguration($connectionName);
        $database = $this->getConnection($connectionName);

        $database->DBOpen(
            $configuration->getDatabase(),
            $configuration->getUser(),
            $configuration->getPassword()
        );

        $this->databases->set($connectionName, $database);

        return $this->databases->get($connectionName);
    }
401892
public static function getGlobal(string $name)
    {
        if (isset(self::$globalOptions[$name])) {
            return self::$globalOptions[$name];
        } else {
            return null;
        }
    }
924834
public function acquire( $name = 'db' )
	{
		try
		{
			$adapter = $this->_config->get( 'resource/' . $name . '/adapter', 'mysql' );

			if( !isset( $this->_connections[$name] ) || empty( $this->_connections[$name] ) )
			{
				if( !isset( $this->_count[$name] ) ) {
					$this->_count[$name] = 0;
				}

				$limit = $this->_config->get( 'resource/' . $name . '/limit', -1 );

				if( $limit >= 0 && $this->_count[$name] >= $limit ) {
					throw new MW_DB_Exception( sprintf( 'Maximum number of connections (%1$d) exceeded', $limit ) );
				}

				$this->_connections[$name] = array( $this->_createConnection( $name, $adapter ) );
				$this->_count[$name]++;
			}

			switch( $adapter )
			{
				case 'sqlite':
				case 'sqlite3':
					// SQLite uses page locking which prevents a second connection from
					// reading from tables which are already in use. Fortunately, it is
					// possible withing the same connection to do the update.
					return $this->_connections[$name][0];
			}

			return array_pop( $this->_connections[$name] );

		}
		catch( PDOException $e ) {
			throw new MW_DB_Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
		}
	}
859211
public static function connection($name)
    {
        $schema = static::$app['db']->connection($name)->getSchemaBuilder();
        $schema->blueprintResolver(function ($table, $callback) {
            return new Blueprint($table, $callback);
        });

        return $schema;
    }
1278083
public static function connection(?string $connectionName=null): Connection {
        if ($connectionName == null) {
            $connectionName = get_property("database.default");
            if ($connectionName == null) {
                throw new RuntimeException("No default database was configured !!");
            }
        }
        if (!isset(self::$connections[$connectionName])) {
            $connectionsConfig = get_property("database.connections", []);
            if (!isset($connectionsConfig[$connectionName])) {
                throw new RuntimeException("Database connection with name \"$connectionName\" doesnt exist !!");
            }

            $connectionConfig = $connectionsConfig[$connectionName];
            $connectionDriver = $connectionConfig["driver"];
            $connectionQueryBuilder = null;
            switch ($connectionDriver) {
                case "pgsql":
                    $connectionQueryBuilder = new PostgresQueryBuilder();
                    break;
                default:
                    $builderClasses = get_property("database.builders", []);
                    if (isset($builderClasses[$connectionDriver])) {
                        $connectionQueryBuilder = $builderClasses[$connectionDriver];
                    }
                    else {
                        throw new RuntimeException("Database driver \"$connectionDriver\" not supported !!. Consider adding a builder class for the driver.");
                    }
                    break;
            }
            $connectionDsn = $connectionDriver;
            $connectionDsn .= ":host=" . $connectionConfig["host"];
            $connectionDsn .= ";port=" . $connectionConfig["port"];
            $connectionDsn .= ";dbname=" . $connectionConfig["database"];
            $connectionPdo = new PDO($connectionDsn, $connectionConfig["username"], $connectionConfig["password"]);
            if (!empty($connectionConfig["emulate_prepared_statements"])) {
                $connectionPdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
            }
            self::$connections[$connectionName] = new Connection($connectionPdo, $connectionQueryBuilder, $connectionConfig);
        }
        return self::$connections[$connectionName];
    }
548966
public static function using($name){
    if (empty(self::$connections[$name])) throw new \Exception("[SQL] Unknown connection named '$name'.");
    return self::$connections[$name];
  }
1251032
public function getConnection($name = ''): QueryBuilderInterface
	{
		// If the parameter is a string, use it as an array index
		if (is_scalar($name) && isset($this->connections[$name]))
		{
			return $this->connections[$name];
		}
		else if (empty($name) && ! empty($this->connections)) // Otherwise, return the last one
		{
			return end($this->connections);
		}

		// You should actually connect before trying to get a connection...
		throw new InvalidArgumentException('The specified connection does not exist');
	}
1300756
public static function get($name = 'default')
    {
        if (!isset(self::$connections[$name])) {
            return null;
        }

        $connector = self::$connections[$name];

        return $connector->connection();
    }
629327
protected function getConnection($connection = null)
    {
        $database = $this->app->make('db');

        $connection = $connection ?: $database->getDefaultConnection();

        return $database->connection($connection);
    }