db 连接预处理
@param \PDO $pdo
192038
protected function initConnection($pdo)
    {
        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        if (null !== $this->emulatePrepare && constant('\PDO::ATTR_EMULATE_PREPARES')) {
            $this->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
        }

        if (null !== $this->charset) {
            $pdo->exec('SET NAMES ' . $pdo->quote($this->charset));
        }
    }
1042855
public static function getConection() : \PDO
    {
        if (self::$instance === null) {
            $con = self::findData();
            self::$instance = new \PDO("mysql:host=".$con['HOST'].";dbname=".$con['DBNAME'], $con['USER'], $con['PASS']);
            self::$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                
        }
        return self::$instance;
    }
680306
public function initConnection(Connection $connection)
    {
        /** @var $pdo PDO */
        $pdo = $connection->getWrappedConnection();
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
        $params = $connection->getParams();
        if (!isset($params['charset'])) {
            throw new RuntimeException("No connection charset specified");
        }
        $pdo->exec('SET NAMES ' . $pdo->quote($params['charset']));
    }
490158
protected function createConnection($pdo)
    {
        $params = [
            'user' => $this->config['username'],
            'password' => $this->config['password'],
            'pdo' => $pdo,
            'driverClass' => DoctrineDriver::class,
        ];
        $this->doctrineConnection = \Doctrine\DBAL\DriverManager::getConnection($params);
    }
180057
public function queryFastHandle(string $sql, $bind = []): \PDOStatement
    {
        if (!$bind) {
            return $this->connectWrite()->query($sql, \PDO::FETCH_ASSOC);
        }

        $stmt = $this->connectWrite()->prepare($sql);
        $ret = $stmt->execute($bind);

        //通常是参数绑定问题
        if (!$ret) {
            trigger_error('PDO语句参数绑定错误:' . $stmt->errorCode(), E_USER_ERROR);
        }
        return $stmt;
    }
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;
    }
1108507
private function open_pdo_connection() {
		try {
			$this->connection = in_array(
					$this->dbtype, ['sqlite3', 'pgsql'])
				? new \PDO($this->connection_string)
				: new \PDO(
					$this->connection_string,
					$this->dbuser, $this->dbpass);
			self::$logger->debug(sprintf(
				"SQL: connection opened: '%s'.",
				json_encode($this->verified_params)));
		} catch (\PDOException $e) {
			self::$logger->error(sprintf(
				"SQL: connection failed: '%s'.",
				json_encode($this->verified_params)));
			throw new SQLError(SQLError::CONNECTION_ERROR,
				$this->dbtype . " connection error.");
		}

		$this->connection->setAttribute(
			\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

		# set pragma for SQLite3
		if ($this->dbtype == 'sqlite3')
			$this->connection->exec("PRAGMA foreign_keys=ON");
	}
989039
public static function pdo(array $config = []): DB
    {
        [$hash, $config] = static::getKey($config, 'database');

        if (isset(static::$pool[$hash])) {
            return static::$pool[$hash];
        }

        $driver = $config['driver'] ?? '';

        if ($driver === 'develop' && !Debugger::$productionMode) {
            return static::$pool[$hash] = new Develop($config);
        }

        return static::$pool[$hash] = new DB($config);
    }
262260
public function query($sql, $bind = [], $master = false, $pdo = false)
    {
        $this->initConnect($master);

        if (!$this->linkID) {
            return false;
        }

        // 记录SQL语句
        $this->queryStr = $sql;

        $this->bind = $bind;

        Db::$queryTimes++;

        try {
            // 调试开始
            $this->debug(true);

            // 预处理
            $this->PDOStatement = $this->linkID->prepare($sql);

            // 是否为存储过程调用
            $procedure = in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);

            // 参数绑定
            if ($procedure) {
                $this->bindParam($bind);
            } else {
                $this->bindValue($bind);
            }

            // 执行查询
            $this->PDOStatement->execute();

            // 调试结束
            $this->debug(false, '', $master);

            // 返回结果集
            return $this->getResult($pdo, $procedure);
        } catch (\PDOException $e) {
            if ($this->isBreak($e)) {
                return $this->close()->query($sql, $bind, $master, $pdo);
            }

            throw new PDOException($e, $this->config, $this->getLastsql());
        } catch (\Throwable $e) {
            if ($this->isBreak($e)) {
                return $this->close()->query($sql, $bind, $master, $pdo);
            }

            throw $e;
        } catch (\Exception $e) {
            if ($this->isBreak($e)) {
                return $this->close()->query($sql, $bind, $master, $pdo);
            }

            throw $e;
        }
    }
374124
protected function getDatabaseDriver(PDO $pdo)
    {
        try {
            $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
        } catch (Exception $e) {
            $driver = null;
        }

        return $driver;
    }
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;
    }
78562
public function pdoWithoutDB()
    {
        $this->checkConfiguration();

        try {
            return new PDO(
                $this->buildDsn(false),
                $this->config['username'],
                $this->config['password']
            );
        } catch (Exception $e) {
            throw new RuntimeException(static::E_CONNECT);
        }
    }
830643
protected function connect(): PDO
	{
		// Connect to the database

		try
		{
			$pdo = new PDO($this->dsn, $this->username, $this->password, $this->getConnectionOptions());
		}
		catch(PDOException $e)
		{
			throw new RuntimeException(vsprintf('Failed to connect to the [ %s ] database. %s', [$this->name, $e->getMessage()]));
		}

		// Run queries

		foreach($this->onConnectQueries as $query)
		{
			$pdo->exec($query);
		}

		// Return PDO instance

		return $pdo;
	}
1371927
public function connect()
    {
        if (self::$profiling) {
            $timer = $this->initProfiler();
        }

        $this->connection = new PDO($this->dsn, $this->username, $this->password, $config = array());
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // we may want to setAttribute(PDO::ATTR_TIMEOUT ) to a few seconds (default is 60) in case the DB is locked
        // the matomo.php would stay waiting for the database... bad!
        // we delete the password from this object "just in case" it could be printed
        $this->password = '';

        if (!empty($this->charset)) {
            $sql = "SET NAMES '" . $this->charset . "'";
            $this->connection->exec($sql);
        }

        if (self::$profiling && isset($timer)) {
            $this->recordQueryProfile('connect', $timer);
        }
    }
929369
public static function initPDO(PDO $pdo): PDO
    {
        if (self::$debugbar instanceof StandardDebugBar) {
            $traceable_pdo = new TraceablePDO($pdo);
            self::$debugbar->addCollector(new PDOCollector($traceable_pdo));
        }

        return $pdo;
    }
544078
public static function build($pdo, $table)
    {
        $mapper = new CacheMapper(new NativeSerializer(), $pdo, $table);
        return new SqlCacheEngine($mapper);
    }
202620
function givePdo(\PDO $conn)
    {
        if ($this->pdo)
            throw new exImmutable;


        $this->pdo = $conn;
        return $this;
    }