@param string $site
@param string $db

@return \Acquia\Cloud\Api\Response\DatabaseName

@throws \Guzzle\Http\Exception\ClientErrorResponseException

@see http://cloudapi.acquia.com/#GET__sites__site_dbs__db-instance_route
323532
public function database($site, $db)
    {
        $variables = array(
            'site' => $site,
            'db' => $db,
        );
        $request = $this->get(array('{+base_path}/sites/{site}/dbs/{db}.json', $variables));
        return new Response\DatabaseName($request);
    }
166907
public function setDatabase($dbname)
    {
        $this->config->setDatabase($dbname);
        if ($this->connection) {
            try {
                $this->query('USE ' . $this->quote()->identifier($dbname));
            } catch (RuntimeException $exception) {
                /** @var \PDOException $prevException */
                $prevException = $exception->getPrevious();
                if (UnknownDatabaseException::isUnknownDatabase($prevException)) {
                    throw UnknownDatabaseException::createFromUnknownDatabase($dbname, $prevException);
                }

                throw $exception;
            }
        }

        return $this;
    }
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) : ''),
        ]);
    }
851886
public function addDatabaseCredentials($acquiaDbName, $localDbName, $username, $password = null, $host = 'localhost', $port = 3306)
    {
        $connString = $username;
        if ($password !== null) {
            $connString . ':' . $password;
        }

        $this->creds['databases'][$acquiaDbName] = array(
            'id'  => '1',
            'role' => $this->sitegroup, // Is this right? For local doesn't really matter.
            'name' => $localDbName,
            'user' => $username,
            'pass' => $password,
            'db_url_ha' => array(
                $host => "mysqli://$connString@$host:$port/mysiteprod"
            ),
            'db_cluster_id' => '1',
            'port' => $port,
        );

        return $this;
    }
25906
public function site($site)
    {
        $variables = array('site' => $site);
        $request = $this->get(array('{+base_path}/sites/{site}.json', $variables));
        return new Response\Site($request);
    }
719549
public static function drop_database($database, $db = null)
	{
		return \DB::query('DROP DATABASE '.\DB::quote_identifier($database, $db ? $db : static::$connection), \DB::DELETE)->execute($db ? $db : static::$connection);
	}
1042241
public static function getDbPropertyName($dbFieldName)
    {
        if (!isset(self::$cachedDbPropertyNames[$dbFieldName])) {
            self::$cachedDbPropertyNames[$dbFieldName] = Str::separatorToCamel($dbFieldName);
        }

        return self::$cachedDbPropertyNames[$dbFieldName];
    }
1090756
public function selectDatabase($database)
    {
        $mysqli = $this->_mysqli();

        if(!$mysqli->select_db($database))
            throw new Exception($mysqli->error, $mysqli->errno);

        $this->_selectedDatabase = $database;

        return $this;
    }
394369
public static function connect($dbType, $dbName, $serverName = '127.0.0.1', $port = '3306', $user = 'root', $password = '', $options = [], $cache = false) {
		self::$db = new Database ( $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache );
		try {
			self::$db->connect ();
		} catch ( \Exception $e ) {
			Logger::error ( "DAO", $e->getMessage () );
			throw new DAOException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
		}
	}
66841
public function create_database($dbhost, $dbuser, $dbpass, $dbname, array $dboptions=null) {
        $driverstatus = $this->driver_installed();

        if ($driverstatus !== true) {
            throw new dml_exception('dbdriverproblem', $driverstatus);
        }

        if (!empty($dboptions['dbsocket'])
                and (strpos($dboptions['dbsocket'], '/') !== false or strpos($dboptions['dbsocket'], '\\') !== false)) {
            $dbsocket = $dboptions['dbsocket'];
        } else {
            $dbsocket = ini_get('mysqli.default_socket');
        }
        if (empty($dboptions['dbport'])) {
            $dbport = (int)ini_get('mysqli.default_port');
        } else {
            $dbport = (int)$dboptions['dbport'];
        }
        // verify ini.get does not return nonsense
        if (empty($dbport)) {
            $dbport = 3306;
        }
        ob_start();
        $conn = new mysqli($dbhost, $dbuser, $dbpass, '', $dbport, $dbsocket); // Connect without db
        $dberr = ob_get_contents();
        ob_end_clean();
        $errorno = @$conn->connect_errno;

        if ($errorno !== 0) {
            throw new dml_connection_exception($dberr);
        }

        // Normally a check would be done before setting utf8mb4, but the database can be created
        // before the enviroment checks are done. We'll proceed with creating the database and then do checks next.
        $charset = 'utf8mb4';
        if (isset($dboptions['dbcollation']) and (strpos($dboptions['dbcollation'], 'utf8_') === 0
                || strpos($dboptions['dbcollation'], 'utf8mb4_') === 0)) {
            $collation = $dboptions['dbcollation'];
            $collationinfo = explode('_', $dboptions['dbcollation']);
            $charset = reset($collationinfo);
        } else {
            $collation = 'utf8mb4_unicode_ci';
        }

        $result = $conn->query("CREATE DATABASE $dbname DEFAULT CHARACTER SET $charset DEFAULT COLLATE ".$collation);

        $conn->close();

        if (!$result) {
            throw new dml_exception('cannotcreatedb');
        }

        return true;
    }
832341
public function configure( DbAdapter $db )
    {
        $sm         = $this->getServiceLocator();
        $platform   = $db->getPlatform();
        $driver     = $db->getDriver();
        $matches    = array();
        $fulldomain = strtolower( $this->getDomain() );
        $config     = $driver->getConnection()
                             ->getConnectionParameters();

        /// TODO: remove this
        if ( isset( $_SERVER['GRIDGUYZ_DOMAIN'] ) )
        {
            $domain     = $_SERVER['GRIDGUYZ_DOMAIN'];
            $subdomain  = isset( $_SERVER['GRIDGUYZ_SUBDOMAIN'] )
                        ? $_SERVER['GRIDGUYZ_SUBDOMAIN']
                        : '';
        }
        elseif ( preg_match( '/^[\da-fA-F:]+/', $fulldomain ) ||    // probably ipv6
                 preg_match( '/^\d+(\.\d+){3}$/', $fulldomain ) )   // ipv4
        {
            $subdomain  = '';
            $domain     = $fulldomain;
        }
        else
        {
            $fullLength = strlen( $fulldomain );

            if ( empty( $config['validDomains'] ) )
            {
                $validDomains = array( 'localhost' );
            }
            else
            {
                $validDomains = array_map(
                    'strtolower',
                    (array) $config['validDomains']
                );
            }

            if ( ! empty( $config['defaultDomain'] ) )
            {
                $defaultDomain = strtolower( $config['defaultDomain'] );

                if ( ! in_array( $defaultDomain, $validDomains ) )
                {
                    array_unshift( $validDomains, $defaultDomain );
                }
            }

            foreach ( $validDomains as $validDomain )
            {
                if ( $fulldomain == $validDomain )
                {
                    $subdomain  = '';
                    $domain     = $fulldomain;
                    break;
                }

                $length = $fullLength - strlen( $validDomain ) - 1;

                if ( $length > 0 &&
                     ( '.' . $validDomain ) == substr( $fulldomain, $length ) )
                {
                    $subdomain  = substr( $fulldomain, 0, $length );
                    $domain     = $validDomain;
                    break;
                }
            }
        }

        if ( ! isset( $subdomain ) || ! isset( $domain ) )
        {
            if ( preg_match( '/^(.+)\.([a-z0-9-]+\.[a-z]+)$/', $fulldomain, $matches ) )
            {
                $subdomain  = $matches[1];
                $domain     = $matches[2];
            }
            else
            {
                $subdomain  = '';
                $domain     = $fulldomain;
            }
        }

        $query = $db->query( '
            SELECT *
              FROM ' . $platform->quoteIdentifier( 'subdomain' ) . '
             WHERE LOWER( ' . $platform->quoteIdentifier( 'subdomain' ) . ' )
                 = LOWER( ' . $driver->formatParameterName( 'subdomain' ) . ' )
        ' );

        $result = $query->execute( array(
            'subdomain' => $subdomain
        ) );

        if ( $result->getAffectedRows() > 0 )
        {
            $schema = $db->getCurrentSchema();

            foreach ( $result as $data )
            {
                $info = new SiteInfo( array(
                    'schema'        => $schema,
                    'domain'        => $domain,
                    'subdomain'     => $subdomain,
                    'subdomainId'   => $data['id'],
                    'fulldomain'    => $fulldomain,
                    'scheme'        => $this->getScheme(),
                    'port'          => $this->getPort(),
                ) );

                $sm->setService( 'SiteInfo', $info );
                return $db;
            }
        }
        else if ( $domain )
        {
            $sm->setService(
                'RedirectToDomain',
                new RedirectionService(
                    $this->getScheme(),
                    $domain,
                    $this->getPort(),
                    'sub-domain not found',
                    false
                )
            );
        }
        else
        {
            if ( empty( $config['defaultDomain'] ) )
            {
                throw new Exception\InvalidArgumentException(
                    'Domain not found, and default domain not set'
                );
            }
            else
            {
                $sm->setService(
                    'RedirectToDomain',
                    new RedirectionService(
                        $this->getScheme(),
                        $config['defaultDomain'],
                        $this->getPort(),
                        'sub-domain not found',
                        false
                    )
                );
            }
        }

        return $db;
    }
1258104
public function getDbInfo($db)
    {
        $info = ArrayHelper::getValue($this->dbInfo, $db, null);
        if (!$info) {
            throw new UserException('Database with name ' . $db . ' not configured for dump.');
        }

        return $info;
    }
457320
public static function databaseName($project, $instance, $database)
    {
        return self::getDatabaseNameTemplate()->render([
            'project' => $project,
            'instance' => $instance,
            'database' => $database,
        ]);
    }
604116
private function convertDbToApi($db)
    {
        $result = new AResponse();
        if ($db) {
            /* extract DB data */
            $custId = $db[QBGetCustomer::A_ID];
            $email = $db[QBGetCustomer::A_EMAIL];
            $nameFirst = $db[QBGetCustomer::A_NAME_FIRST];
            $nameLast = $db[QBGetCustomer::A_NAME_LAST];
            $mlmId = $db[QBGetCustomer::A_MLM_ID];
            $path = $db[QBGetCustomer::A_PATH];
            $country = $db[QBGetCustomer::A_COUNTRY];

            /* prepare response data */
            $pathFull = $path . $custId . Cfg::DTPS;

            /* compose response data */
            $result->setId($custId);
            $result->setEmail($email);
            $result->setNameFirst($nameFirst);
            $result->setNameLast($nameLast);
            $result->setMlmId($mlmId);
            $result->setCountry($country);
            $result->setPathFull($pathFull);
        }
        return $result;
    }
670513
public static function getInstance($db)
    {
        if (!isset(self::$instances[$db])) {
            $config = config('database.' . $db);
            if (!$config) {
                throw new RuntimeException('Not found database config [' . $db . '], Please checking file config/database.php');
            }

            switch ($config['driver']) {
                case 'mysql':
                    self::$instances[$db] = new MysqlConnection($config);
                    break;
            }
        }

        return self::$instances[$db];
    }
798129
public function getRoot(Site $site)
    {
        try {
            $query = $this->createQueryBuilder('s')
                    ->andWhere('s._site = :site')
                    ->andWhere('s._parent is null')
                    ->setMaxResults(1)
                    ->setParameters([
                        'site' => $site,
                    ]);

            return $query->getQuery()->getSingleResult();
        } catch (NoResultException $e) {
            return;
        } catch (\Exception $e) {
            return;
        }
    }
1325499
public static function getDSN($db_hostname = null, $db_driver = null, $db_port = null, $db_schema = null): string
    {
        try {
            // Check whether $db_hostname is set
            if (!$db_hostname) {
                // if not, try to use self::$db_params['db_hostname']
                if (!isset(self::$db_params['db_hostname'])) {
                    throw new \Exception('Database host not set');
                } else {
                    $db_hostname = self::$db_params['db_hostname'];
                }
            }

            // Check whether $db_driver is set
            if (!$db_driver) {
                // if not, try to use self::$db_params['db_driver']
                if (!isset(self::$db_params['db_driver'])) {
                    throw new \Exception('Database driver not set');
                } else {
                    $db_driver = self::$db_params['db_driver'];
                }
            }

            // Check whether $db_schema is set
            if (!$db_schema) {
                // if not, try to use self::$db_params['db_schema']
                if (!isset(self::$db_params['db_schema'])) {
                    throw new \Exception('Database name not set');
                } else {
                    $db_schema = self::$db_params['db_schema'];
                }
            }

            // Check whether $db_port is set
            if (!$db_port) {
                // if not, try to use self::$db_params['db_port']
                if (!isset(self::$db_params['db_port'])) {
                    // throw new \Exception('Database port not set');
                    $db_port = "";
                } else {
                    $db_port = self::$db_params['db_port'];
                }
            }

            // Make DNS
            if ($db_driver === 'mysql') {
                $dsn =  $db_driver .
                        ":host=" .
                        $db_hostname;

                $db_port = empty($db_port) ? "" : (":" . $db_port) ;

                $dsn .= $db_port .
                        ";dbname=" .
                        $db_schema;
            } elseif ($db_driver === 'sqlite') {
                // In case database type is sqlite,
                // 'dbname' param stores path to the database file.
                $dsn =  $db_driver . ':'. $db_schema;
            } else {
                throw new \Exception("Database type '". self::$db_params['db_driver'] . "' does not support yet.");
            }

            return $dsn;
        } catch (\Exception $e) {
            die('Error: ' . $e->getMessage());
        }

        return false;
    }