Db连接

@param string $host 数据库host
@param string $username 数据库用户名
@param string $password 数据库密码
@param string $dbName 数据库名
@param string $replicaSet replicaSet名称
@param string $engine 无用
@param bool $pConnect 无用

@return mixed
435521
public function connect($host, $username, $password, $dbName, $replicaSet = '', $engine = '', $pConnect = false)
    {
        $authString = "";
        if ($username && $password) {
            $authString = "{$username}:{$password}@";
        }

        $replicaSet && $replicaSet = '?replicaSet=' . $replicaSet;
        $dsn = "mongodb://{$authString}{$host}/{$dbName}{$replicaSet}";

        return new Manager($dsn);
    }
1064274
public function replicaSetConnect()
    {
        $this->dbName  = $this->config['database'];
        $this->typeMap = $this->config['type_map'];

        $this->config['params']['replicaSet'] = $this->config['database'];
        $manager                              = new Manager($this->buildUrl(), $this->config['params']);
        return $manager;
    }
448453
public function create($host, $port, $user, $password, $dbname)
    {
        $params = [
            ':host' => $host,
            ':port' => $port,
            ':user' => $user,
            ':password' => $password,
            ':dbname' => $dbname
        ];

        $query = 'SELECT sbas_id FROM sbas
                  WHERE host = :host AND port = :port AND `user` = :user AND pwd = :password AND dbname = :dbname';
        $statement = $this->connection->executeQuery($query, $params);

        if ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
            return $this->find((int) $row['sbas_id']);
        }

        $query = 'INSERT INTO sbas (ord, host, port, dbname, sqlengine, user, pwd)
              SELECT COALESCE(MAX(ord), 0) + 1 AS ord, :host AS host, :port AS port, :dbname AS dbname,
                     "MYSQL" AS sqlengine, :user AS user, :password AS pwd FROM sbas';

        $stmt = $this->connection->prepare($query);
        $stmt->execute($params);

        $stmt->closeCursor();

        return $this->find((int) $this->connection->lastInsertId());

    }
1184552
public function getCluster($name, $db = 0, $replicas = 128, $selectRandomSlave = true, $writeOnly = false)
    {
        if (!isset($this->_cluster[$name])) {
            $this->_cluster[$name] = $this->createCluster($name, $db, $replicas, $selectRandomSlave, $writeOnly);
        }
        return $this->_cluster[$name];
    }
615157
public function connect($host, $port, Connection $connection, $timeout = 5)
    {
        $deferred = new Deferred();
        $this->disconnect()
            ->then(function () use ($deferred, $host, $port, $connection, $timeout) {
                $this->establishConnection($host, $port, $connection, $timeout)
                    ->then(function () use ($deferred) {
                        $deferred->resolve($this);
                    })
                    ->otherwise(function (\Exception $error) use ($deferred) {
                        $deferred->reject($error);
                    });
            })
            ->otherwise(function () use ($deferred) {
                $deferred->reject($this);
            });

        return $deferred->promise();
    }
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;
    }
1182848
public function connect(string $host = null): self
    {
        // link is already active?
        if ($host && isset($this->links[$host])) {
            return $this;
        }

        // set type as single as default
        $type = Link::TYPE_SINGLE;

        // get config as array
        $config = $this->config->toArray();

        // get database directives from given config
        $database = ($config['database'] ?? []);

        // is master/slave active?
        if (true === $this->config->get('sharding')) {
            $master = ($database['master'] ?? []);
            $slaves = ($database['slaves'] ?? []);
            switch ($host) {
                // act: master as default
                case null:
                case Link::TYPE_MASTER:
                    $type = Link::TYPE_MASTER;
                    $database = array_merge($database, $master);
                    break;
                //  act: slave
                case Link::TYPE_SLAVE:
                    $type = Link::TYPE_SLAVE;
                    if (!empty($slaves)) {
                        $database = array_merge($database, Util::arrayRand($slaves));
                    }
                    break;
                default:
                    // given host is master's host?
                    if ($host == ($master['host'] ?? '')) {
                        $type = Link::TYPE_MASTER;
                        $database = array_merge($database, $master);
                    } else {
                        // or given host is slaves's host?
                        $type = Link::TYPE_SLAVE;
                        foreach ($slaves as $slave) {
                            if (isset($slave['host']) && $slave['host'] == $host) {
                                $database = array_merge($database, $slave);
                                break;
                            }
                        }
                    }
            }
        }

        // remove unused parts
        unset($config['database'], $database['master'], $database['slaves']);

        // merge configs
        $config = array_merge($config, $database);
        if (!isset($config['host'], $config['name'], $config['username'], $config['password'])) {
            throw new InvalidConfigException(
                'Please specify all needed credentials (host, name, username, password) for a link!');
        }

        // use host as a key for link stack
        $host = $config['host'];

        // create a new link if not exists
        if (!isset($this->links[$host])) {
            $link = new Link($this->database, new Config($config), $type, $host);
            $link->open();
            // add link to links stack
            $this->setLink($host, $link);
        }

        return $this;
    }
89844
public static function getConnection($connectionName = '')
    {
        if (!$connectionName) {
            $connectionName = static::$defaultConnection;
        }
        if (isset(static::$connections[$connectionName])) {
            return static::$connections[$connectionName];
        }
    }
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;
	}
77927
def create_mysql_mysqlconnector(username, password, host, port, database, **kwargs):  # pragma: no cover
    
    return create_engine(
        _create_mysql_mysqlconnector(username, password, host, port, database),
        **kwargs
    )
742575
public function connectPersistant( $host, $user, $pass, $database )
	{
		return $this->connectDatabase( "pconnect", $host, $user, $pass, $database );
	}
443726
public static function connection($name = 'default')
    {
        $databaseConfig = self::getConnectionConfiguration($name);

        /** @var Driver $driver */
        $driver = self::driver($name);

        $username = isset($databaseConfig['username']) ? $databaseConfig['username'] : null;
        $password = isset($databaseConfig['password']) ? $databaseConfig['password'] : null;
        $options = isset($databaseConfig['options']) ? $databaseConfig['options'] : array();

        // Create connection if we don't have it already
        if (! isset(self::$connections[$name])) {
            $connection = $driver->connect($databaseConfig, $username, $password, $options);

            self::$connections[$name] = $connection;
        }

        return self::$connections[$name];
    }
49546
public static function useProxy($host, $port, $username = null, $password = null) {
    Connect2PayCurrencyHelper::$proxy_host = $host;
    Connect2PayCurrencyHelper::$proxy_port = $port;
    Connect2PayCurrencyHelper::$proxy_username = $username;
    Connect2PayCurrencyHelper::$proxy_password = $password;
  }
403643
public function useProxy($host, $port, $username = null, $password = null) {
    $this->proxy_host = $host;
    $this->proxy_port = $port;
    $this->proxy_username = $username;
    $this->proxy_password = $password;
  }
1254463
public static function mysql($host, $database, $user, $password, $prefix = null)
    {
        if (is_null($prefix)) {
            $prefixing = 'false';
            $prefix = 'ysf';
        } else {
            $prefixing = 'true';
        }
        //
        $content = Database::set('mysql', $host, $database, $user, $password, $prefixing, $prefix);
        //
        file_put_contents(Application::$root.'config/database.php', $content, 0);
        //
        return true;
    }
655472
public function createDoctrineDBALConnection($driver, $host, $port, $dbname, $user, $password)
    {
        // TODO caching for connection

        $connectionFactory = $this->getDoctrineConnectionFactory();

        $connection = $connectionFactory->createConnection(array(
            'driver' => $driver,
            'host' => $host,
            'port' => $port,
            'dbname' => $dbname,
            'user' => $user,
            'password' => $password,
        ));

        return $connection;
    }
311156
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
	{
		if (!function_exists('ifx_connect')) return null;

		$dbs = $argDatabasename . "@" . $argHostname;
		putenv("INFORMIXSERVER=".trim($argHostname));
		$this->_connectionID = ifx_pconnect($dbs,$argUsername,$argPassword);
		if ($this->_connectionID === false) return false;
		#if ($argDatabasename) return $this->SelectDB($argDatabasename);
		return true;
	}