创建PDO实例
@return \PDO
515384
public function createMysqlClientInstance()
    {
        $dbConfig = $this->engine->getConfigVar('database')['database']['log_db'];
        $names = (isset($dbConfig['charset']) && !empty($dbConfig['charset'])) ? $dbConfig['charset'] : 'utf8';
        $dsn = sprintf(
            "%s:host=%s;port=%s;dbname=%s",
            $dbConfig['driver'], $dbConfig['host'], $dbConfig['port'], $dbConfig['db_name']);
        $params = [
            \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$names}",
            \PDO::ATTR_PERSISTENT => false,
            \PDO::ATTR_TIMEOUT => isset($dbConfig['timeout']) ? $dbConfig['timeout'] : 10
        ];
        try {
            $pdo = @new \PDO($dsn, $dbConfig['user'], $dbConfig['password'], $params);
        } catch (\PDOException $e) {
            $errMsg = 'database log_db config connect failed: ' . $e->getMessage();
            $this->engine->logErr($errMsg);
            return null;
        }
        if (!$pdo) {
            return null;
        }
        return $pdo;
    }
840541
public function generateNewPDO() :\PDO
    {
        if($this->validateConfig()) {
            $dns = $this->driver . ':dbname=' . $this->database . ";host=" . $this->host;
            return new \PDO($dns, $this->user, $this->pass);
        }
        else {
            throw (new \Exception("Invalid or missing database connection parameters"));
        }
    }
1191681
protected function getResult($pdo = false, $procedure = false)
    {
        if ($pdo) {
            // 返回PDOStatement对象处理
            return $this->PDOStatement;
        }

        if ($procedure) {
            // 存储过程返回结果
            return $this->procedure();
        }

        $result = $this->PDOStatement->fetchAll($this->fetchType);

        $this->numRows = count($result);

        return $result;
    }
638751
public function get()
    {
        // Define columns
        $columns = count($this->columns)? $this->getColumnsString() : $this->table . '.*';

        // Define root
        $root = 'SELECT ' . $columns . ' FROM ' . $this->table;

        // Define joins
        $joins = count($this->joins)? $this->getJoinString() : '';

        // Define where conditions
        $where = $this->getWhereString();

        // Define where in conditions
        $whereIn = $this->getWhereInString();

        // Define group by
        $groupBy = ($this->groupBy)? 'GROUP BY ' . $this->groupBy : '';

        // Define order
        $orderBy = ($this->orderBy)? $this->getOrderByString() : '';

        // Define limit
        $limit = 'LIMIT ' . $this->offset . ',' . $this->limit;

        // Build query
        $this->query = implode(' ', [$root, $joins, $where, $whereIn, $groupBy, $orderBy, $limit]);

        // Debug
        if ($this->app->config('queries.log')) {
            file_put_contents($this->app->config('queries.log'), date('Y-m-d h:i:s') . ' - ' . $this->query . "\r\n", FILE_APPEND);
        }

        // Prepare statement
        $this->statement = $this->pdo->prepare($this->query);

        // Bind where values
        if (count($this->wheres)) {
            $this->bindWheres();
        }

        // Execute statement
        if (!$this->statement->execute()) {
            throw new \PDOException('Error reading from database', 500);
        }

        // Return results
        $result = $this->statement->fetchAll(\PDO::FETCH_ASSOC);

        // Reset
        $this->reset();

        // Return result
        return $result;
    }
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;
    }
660327
public function getConnect()
    {
        if (! $this->isConnected()) {
            $this->connect();
            if (! $this->conn->getPdo() instanceof PDO) {
                $this->conn->reconnect();
            }
        }
        return $this->conn;
    }
527397
public function connect() : object
    {
        if ($this->pdo) {
            return $this;
        }

        if (!isset($this->options["dsn"])) {
            throw new Exception("You can not connect, missing dsn.");
        }

        try {
            $this->pdo = new \PDO(
                $this->options["dsn"],
                $this->options["username"],
                $this->options["password"],
                $this->options["driver_options"]
            );
            $this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, $this->options['fetch_mode']);
            $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->options['emulate_prepares']);
        } catch (\PDOException $e) {
            if ($this->options["debug_connect"]) {
                throw $e;
            }
            throw new Exception("Could not connect to database, hiding connection details.");
        }

        return $this;
    }
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;
        }
    }
577296
public function get()
    {
        if (!$this->stmt) {
            $this->execute();
        }
        $res = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
        $this->rewind();
        $this->stmt = null;

        return $res;
    }
442925
public function getConnection()
    {
        // if we already have a PDO object, no need to re-connect.
        if ($this->_connection) {
            return $this->_connection;
        }

        $this->_pdoType = "sqlsrv";
        // get the dsn first, because some adapters alter the $_pdoType
        //$dsn = $this->_dsn();

        // check for PDO extension
        if (!extension_loaded('pdo')) {
            /**
             * @see Zend_Db_Adapter_Exception
             */
            throw new \Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
        }

        // check the PDO driver is available
        if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
            /**
             * @see Zend_Db_Adapter_Exception
             */
            throw new \Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
        }

        // create PDO connection
        $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);

        // add the persistence flag if we find it in our config array
        if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
            $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
        }

        try {
            $serverName = $this->_config["host"];
            $database   = $this->_config["dbname"];
            if (is_null($database)) {
                $database = 'master';
            }
            $uid = $this->_config['username'];
            $pwd = $this->_config['password'];
            if ($this->_config["port"] != "") {
                $serverName = $serverName . "," . $this->_config["port"];
            }

            $this->_connection = new PDO("sqlsrv:$serverName", $uid, $pwd, array('Database' => $database));

            if ($this->_connection === false) {
                die(self::FormatErrors(sqlsrv_errors()));
            }

            /*
            $this->_connection = new PDO(
                $dsn,
                $this->_config['username'],
                $this->_config['password'],
                $this->_config['driver_options']
            );
            */

            $this->_profiler->queryEnd($q);

            // set the PDO connection to perform case-folding on array keys, or not
            $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
            $this->_connection->setAttribute(PDO::SQLSRV_ENCODING_UTF8, true);

            // always use exceptions.
            $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            return $this->_connection;
        } catch (PDOException $e) {
            /**
             * @see Zend_Db_Adapter_Exception
             */
            throw new \Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
        }
    }
984112
protected function getPdoStatement()
    {
        $statement = $this->connection->prepare($this->getSql());
        if ($this->dataRowClass !== null) {
            $statement->setFetchMode(PDO::FETCH_CLASS, $this->dataRowClass);
        } else {
            $statement->setFetchMode(PDO::FETCH_OBJ);
        }
        return $statement;
    }
704519
protected function dbConnect() {
		try {
			$pdo = new PDO('mysql:host=' . conf::$db['host'] . ';dbname=' . conf::$db['name'], conf::$db['user'], conf::$db['pass']);
    		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			return $pdo;
		} catch(PDOException $e) {
			return false;
		}
	}
1328512
public function fetch()
	{
		if(count($this->select->getColumns()) === 0)
		{
			/** @var Column $column */
			$column = $this->factory->references('Column', '*');
			$this->select->column($column);
		}
		$query    = $this->factory->builder($this->select)->process();
		$queryObj = $this->pdo->query($query);
		if($queryObj)
		{
			return $queryObj->fetchAll(\PDO::FETCH_ASSOC);
		}

		return array();
	}
544817
public function connect()
    {
        $dsnString = "{$this->dbDriver}:host={$this->dbHostname};dbname={$this->dbName};";
        $dsnString .= "charset={$this->dbCharset}";
        /*
         * Try to connect to database
         */
        try {
            $this->dbConnection = new \PDO(
                $dsnString,
                "{$this->dbUsername}",
                "{$this->dbPassword}",
                $this->driverOptions
            );
        } catch (\PDOException $ex) {
            // Error during database connection, check params.
            throw new \InvalidArgumentException(
                __METHOD__.': '.$ex->getMessage()
            );
        }

        return $this->dbConnection;
    }
1301509
protected function opendb() {
    // Only do one open
    
    if($this->db) {
      return $this->db;
    }
    //echo "$this->host, $this->user, $this->password";
    try {
      $db = @new PDO($this->host, $this->user, $this->password);
    } catch(PDOException $e) {
      $this->errno = $e->getCode();
      $this->error = $e->getMessage();
      throw new SqlException(__METHOD__.  " Connection failed: $this->host", $this);
    }

    $this->db = $db; // set this right away so if we get an error below $this->db is valid

    return $db;
  }
735980
public function build()
    {
        if ($this->handler) {
            $handler = $this->handler;
        } elseif ($this->pdo || $this->dbCredentials) {
            $options = $this->dbCredentials;
            $options['table'] = $this->table;
            if ($this->pdo) {
                $options['pdo'] = $this->pdo;
            }
            $handler = new PdoHandler($options);
        } else {
            $handler = new FileHandler($this->locking);
        }
        return new Session($handler, $this->name, $this->savePath, $this->serializer);
    }
975364
public function dump()
	{
		$pdo = $this->pdo();

		$sql = '';

		foreach ($this->list_tables() as $table)
		{
			$query = $pdo->query("SELECT * FROM `{$table}`");

			while ($row = $query->fetch(\PDO::FETCH_NUM)) 
			{
				$values = array();

				foreach ($row as $column) 
				{
					$values[] = is_null($column) ? 'NULL' : $pdo->quote($column);
				}

				$sql .= "INSERT INTO `{$table}` VALUES (".join(',', $values). ");\n";
			}
		}

		return $sql;
	}