Retrieves a database connection.

@param string $connectionName
@return \PDO
@throws DatabaseException
768796
public function getConnection(string $connectionName): \PDO
    {
        if (!isset($this->connections[$connectionName])) {
            throw new DatabaseException(sprintf('Connection (%s) not found in pool.', $connectionName));
        }
        return $this->connections[$connectionName];
    }
1074373
public function getDatabase($databaseName = null) {
		if ($databaseName === null) {
			$databaseName = $this->dbname;
		}

		if (! isset($this->databases[$databaseName])) {
			$this->databases[$databaseName] = $this->getClient()->selectDB($databaseName);
		}

		return $this->databases[$databaseName];
	}
315731
public function getDatabaseSchemata($dbName)
    {
        $sql = "SELECT
            default_character_set_name,
            default_collation_name
            FROM information_schema.SCHEMATA
            WHERE schema_name = %s;";
        $sql = sprintf($sql, $this->quote($dbName));
        $row = $this->pdo->query($sql)->fetch();

        return $row;
    }
357831
public static function getConnection()
    {
        // DataBase constants
        require_once dirname(dirname(dirname(dirname(__DIR__)))).'/config/bdd.php';

        // Checks if Pdo connection doesn't exist
        if (is_null(self::$pdo)) {

            // Creates the first parameters for the new pdo concerning the database
            $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME;

            // Sets & stores the pdo options as the last parameters for the new pdo
            $options = [
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            ];
            // Creates & stores the pdo connection
            $pdo = new PDO($dsn, DB_USER, DB_PASS, $options);

            // Sets the encoding characters
            $pdo->exec('SET NAMES UTF8');

            // Stores the pdo connection
            self::$pdo = $pdo;

            // Returns the connection
            return $pdo;
        } else {

            // Return the pdo connection
            return self::$pdo;
        }
    }
1015533
protected function buildConnectionURI($databaseName = ''): string
    {
        $servers = $this->getConfig()->getServers();
        $options = $this->getConfig()->getOptions();

        $host = $this->getConfig()->getHost();
        $port = $this->getConfig()->getPort();
        $username = $this->getConfig()->getUsername();
        $password = $this->getConfig()->getPassword();

        if( \count($servers) > 0 ){
            $host = \array_reduce($servers, function($carry, $data){
                $carry .= ($carry === '' ? '' : ',').$data['host'].':'.$data['port'];
                return $carry;
            }, '');
            $port = false;
        }

        return implode('', [
            'mongodb://',
            ($username ?: ''),
            ($password ? ":{$password}" : ''),
            ($username ? '@' : ''),
            $host,
            ($port !== 27017 && $port !== false ? ":{$port}" : ''),
            ($databaseName ? "/{$databaseName}" : ''),
            (\count($options) > 0 ? '?'.\http_build_query($options) : ''),
        ]);
    }
1320933
public function getConnection()
    {
        if (! $this->connection) {
            list($dns, $username, $password) = $this->connectionString();

            $this->setConnection(new \PDO($dns, $username, $password));
        }

        return $this->connection;
    }
611589
public function resolve($connectionName)
    {
        $config = $this->databaseConfigs[$connectionName];
        $driver = $config['driver'];

        return $this->factories[$driver]->build($config);
    }
282352
public function getConnection($connectionName = null)
    {
        /** @var Configuration $configuration */
        $configuration = $this->get('configuration');
        $connectionName = null !== $connectionName ?: $configuration['connection_name'];
        
        return $this->getConnectionManager()->getConnection($connectionName);
    }
481367
public function makeInsert(string $connectionName = ''): InsertQueryBuilder
    {
        $connection = $this->provideConnection($connectionName);
        $statementBuilder = new InsertStatementBuilder;
        return new InsertQueryBuilder($connection, $statementBuilder);
    }
874086
public function getConnection() {
        if (is_null($this->conn) || !($this->conn)) {
            $dsn = 'dblib:host=' . $this->host . ':1433;dbname=' . $this->db . ';charset=utf8';
            $this->conn = new \PDO($dsn, $this->user, $this->pass);

            if (is_bool($this->conn)) {
                throw new \Exception('unable to connect to db with provided credentials');
            }
        }

        return $this->conn;
    }
861995
public static function &getDatabase($jname)
	{
		static $instances;
		if (!isset($instances)) {
			$instances = array();
		}
		//only create a new database instance if it has not been created before
		if (!isset($instances[$jname])) {
			/**
			 * TODO: MUST BE CHANGED! as do not rely on joomla_int
			 */
			if ($jname == 'joomla_int') {
				$db = self::getDBO();
			} else {
				//get config values
				$params = static::getParams($jname);
				//prepare the data for creating a database connection
				$host = $params->get('database_host');
				$user = $params->get('database_user');
				$password = $params->get('database_password');
				$database = $params->get('database_name');
				$prefix = $params->get('database_prefix', '');
				$driver = $params->get('database_type');
				$charset = $params->get('database_charset', 'utf8');
				//added extra code to prevent error when $driver is incorrect

				$options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);

				$db = DatabaseFactory::getInstance()->getDriver($driver, $options);

				if ($driver != 'sqlite') {
					//add support for UTF8
					$db->setQuery('SET names ' . $db->quote($charset));
					$db->execute();
				}

				//get the debug configuration setting
				$db->setDebug(Config::get()->get('debug'));
			}
			$instances[$jname] = $db;
		}
		return $instances[$jname];
	}
462049
public function getTableColumns($tableName = null, $connectionName = null)
    {
        $tableName = $this->validateTableName($tableName);

        return $this->getSchema($connectionName)->listTableColumns($tableName);
    }
1041594
public function tableExists($tableName, $connectionName = 'default')
    {
        try {
            $db = ConnectionManager::get($connectionName);
            $collection = $db->schemaCollection();
            $tables = $collection->listTables();
            if (in_array($tableName, $tables)) {
                return true;
            }
        } catch (PDOException $connectionError) {
            return false;
        }
        return false;
    }
293854
public function getTables($dbName = '')
    {
        $sql    = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
        $pdo    = $this->query($sql, [], false, true);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $info   = [];

        foreach ($result as $key => $val) {
            $info[$key] = current($val);
        }

        return $info;
    }
693439
public static function getConnection( $hostname, $dbname ) {
		if ( isset( self::$dbConnections[ $hostname ] ) ) {
			$conn = self::$dbConnections[ $hostname ];
		} else {
			$section = new kfLogSection( __METHOD__ );
			try {
				$conn = new LoggedPDO(
					'mysql:host=' . $hostname . ';dbname=' . $dbname . '_p;charset=utf8',
					kfDbUsername(),
					kfDbPassword()
				);
			    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
			} catch ( Exception $e ) {
				throw new Exception( "Connection to '$hostname' failed: " . $e->getMessage() );
			}
			self::$dbConnections[ $hostname ] = $conn;
			return $conn;
		}

		// Re-used connection, switch database first.
		self::selectDB( $conn, $dbname );

		return $conn;
	}
908217
public function connect(): DatabaseConnection
    {
        if (null !== $this->pdo) {
            return $this;
        }

        try {
            $pdoCreator = $this->getPdoCreator();
            $this->pdo  = $pdoCreator($this->configuration);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            if ($this->configuration->hasInitialQuery()) {
                $this->pdo->query($this->configuration->getInitialQuery());
            }
        } catch (PDOException $pdoe) {
            throw new DatabaseException($pdoe->getMessage(), $pdoe);
        }

        return $this;
    }
545436
protected function makeCommand($connection, $connectionName)
    {
        $cmd[] = 'mysqldump -u ' . $connection['username'];
        $cmd[] = '--password=' . $connection['password'];
        $cmd[] = $connection['database'];
        $cmd[] = ' -h ' . $connection['host'];
        if ( ! empty($connection['port'])) {
            $cmd[] = '-P ' . $connection['port'];
        }
        $cmd[] = '> ' . $this->filename($connectionName);
        return implode(' ', $cmd);
    }