thinkphp6框架分析—数据库—PDO

1、tp已经集成了pdo,那么如何使用框架方法调起pdo了?

use think\facade\Db;
$instance = Db::connect(); //如果有多个数据库,可填写数据库配置标识(database中配置的键名,非数据库名),默认为mysql

$instance->execute("SELECT 1");     //如果是【数据】增删改查直接运行
$instance->getPdo()->exec($sql);    //如果是【数据表】的增删查改,需要通过excute或query获取pdo实例,在执行exec。这里必须是exec

Db::connect()只创建了对象,还没有链接数据库 ,query()或execute()才会链接

connect()内部执行分析,最终返回返回\think\db\connector\Mysql对象

#第1步【\think\DbManager】
//$name  连接配置标识,$force 强制重新连接
public function connect(string $name = null, bool $force = false)
{
	return $this->instance($name, $force);  //$this是Db对象,通过facade实现
}

#第2步【\think\DbManager】
protected function instance(string $name = null, bool $force = false): ConnectionInterface
{
	if (empty($name)) {
		$name = $this->getConfig('default', 'mysql');  //默认获取database的mysql
	}
	if ($force || !isset($this->instance[$name])) {    
		$this->instance[$name] = $this->createConnection($name);  //容器为空,执行该行代码
	}
	return $this->instance[$name];
}

#第3步【\think\DbManager】
protected function createConnection(string $name): ConnectionInterface
{
	$config = $this->getConnectionConfig($name);     //获取database中对应标识的配置数组     
	$type = !empty($config['type']) ? $config['type'] : 'mysql';
	if (false !== strpos($type, '\\')) {                //结果为false
		$class = $type;
	} else {
        //执行这里,class为【\think\db\connector\Mysql】,该类继承PDOConnection类, PDOConnection又继承Connection
		$class = '\\think\\db\\connector\\' . ucfirst($type);   
	}

	$connection = new $class($config);    //触发Connection中的构造函数,返回Mysql对象,详见第4步
	$connection->setDb($this);            //调用Connection的setDb方法设置db属性;$this是Db对象,通过facade实现
	if ($this->cache) {
		$connection->setCache($this->cache);
	}
	return $connection;            //返回\think\db\connector\Mysql对象
}

#第4步【\think\db\Connection类】
public function __construct(array $config = [])
{
	if (!empty($config)) {
		$this->config = array_merge($this->config, $config);
	}
	//创建Builder对象,这里$this为实例化对象本身,即【\think\db\connector\Mysql】,这里调用父类【\think\db\PDOConnection】的getBuilderClass()
	$class = $this->getBuilderClass();  //详见第4.1步;类为【\think\db\builder\Mysql】

	$this->builder = new $class($this);  //\think\db\builder\Mysql为基本mysql语句
}

	#第4.1步【\think\db\PDOConnection类】
	public function getBuilderClass(): string
	{
		//获取本类的config属性,这里本类重写了父类【\think\db\Connection】的config属性,builder='',详见第6步;
		return $this->getConfig('builder') ?: '\\think\\db\\builder\\' . ucfirst($this->getConfig('type'));
		//$this->getConfig('builder')为null 最终返回 \\think\\db\\builder\\Mysql
	}

		#第4.1.1步【\think\db\PDOConnection类】
		public function getConfig(string $config = '')
		{
			if ('' === $config) {
				return $this->config;  
			}
			return $this->config[$config] ?? null;  //返回null
		}

execute()内部分析

#第1步 Mysql->execute();实际调用【\think\db\PDOConnection】
//$sql  sql指令; $bind 参数绑定
public function execute(string $sql, array $bind = []): int
{
	return $this->pdoExecute($this->newQuery(), $sql, $bind, true);
}

    #第1.1 获取查询对象
	public function newQuery(){
		$class = $this->getQueryClass(); //$this为Mysql,调用【\think\db\PDOConnection】中的方法
		$query = new $class($this);    //由1.2可知 返回为【think\db\Query】
        ... //省略部分代码
		return $query;
	}

    #第1.2 【\think\db\PDOConnection】
    public function getQueryClass(): string
    {
        return $this->getConfig('query') ?: Query::class;  //返回后者【think\db\Query】
    }

#第2步
//$query查询对象;$sql sql指令;$bind 参数绑定; $origin是否原生查询
protected function pdoExecute(BaseQuery $query, string $sql, array $bind = [], bool $origin = false): int
{
	if ($origin) {
		$query->parseOptions();
	}

	$this->queryPDOStatement($query->master(true), $sql, $bind);    //重点

	if (!$origin && !empty($this->config['deploy']) && !empty($this->config['read_master'])) {
		$this->readMaster = true;
	}

	$this->numRows = $this->PDOStatement->rowCount();

	if ($query->getOptions('cache')) {
		// 清理缓存数据
		$cacheItem = $this->parseCache($query, $query->getOptions('cache'));
		$key       = $cacheItem->getKey();
		$tag       = $cacheItem->getTag();

		if (isset($key) && $this->cache->has($key)) {
			$this->cache->delete($key);
		} elseif (!empty($tag) && method_exists($this->cache, 'tag')) {
			$this->cache->tag($tag)->clear();
		}
	}

	return $this->numRows;
}

#第3步 执行sql前,判断是否在主服务器读操作,是否为存储过程调用【\think\db\PDOConnection】【可忽略】
protected function queryPDOStatement(BaseQuery $query, string $sql, array $bind = []): PDOStatement
{
	$options   = $query->getOptions();
	$master    = !empty($options['master']) ? true : false;
	$procedure = !empty($options['procedure']) ? true : in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);

	return $this->getPDOStatement($sql, $bind, $master, $procedure);
}

#第4步  执行查询但只返回PDOStatement对象 【\think\db\PDOConnection】
//$sql sql指令;$bind参数绑定;$master是否在主服务器读操作;$procedure是否为存储过程调用
public function getPDOStatement(string $sql, array $bind = [], bool $master = false, bool $procedure = false): PDOStatement
{
	try {
		$this->initConnect($this->readMaster ?: $master);  //【重点】
		// 记录SQL语句
		$this->queryStr = $sql;
		$this->bind     = $bind;

		$this->db->updateQueryTimes();
		$this->queryStartTime = microtime(true);

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

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

		// 执行查询
		$this->PDOStatement->execute();			//【重点】

		// SQL监控
		if (!empty($this->config['trigger_sql'])) {
			$this->trigger('', $master);
		}

		$this->reConnectTimes = 0;

		return $this->PDOStatement;
	} catch (\Throwable | \Exception $e) {
		if ($this->transTimes > 0) {
			// 事务活动中时不应该进行重试,应直接中断执行,防止造成污染。
			if ($this->isBreak($e)) {
				// 尝试对事务计数进行重置
				$this->transTimes = 0;
			}
		} else {
			if ($this->reConnectTimes < 4 && $this->isBreak($e)) {
				++$this->reConnectTimes;
				return $this->close()->getPDOStatement($sql, $bind, $master, $procedure);
			}
		}

		if ($e instanceof \PDOException) {
			throw new PDOException($e, $this->config, $this->getLastsql());
		} else {
			throw $e;
		}
	}
}

#第5步  初始化数据库连接【\think\db\PDOConnection】
protected function initConnect(bool $master = true): void
{
	if (!empty($this->config['deploy'])) {
		// 采用分布式数据库
		if ($master || $this->transTimes) {
			if (!$this->linkWrite) {
				$this->linkWrite = $this->multiConnect(true);
			}

			$this->linkID = $this->linkWrite;
		} else {
			if (!$this->linkRead) {
				$this->linkRead = $this->multiConnect(false);
			}

			$this->linkID = $this->linkRead;
		}
	} elseif (!$this->linkID) {
		// 默认单数据库
		$this->linkID = $this->connect();  //【重点】
	}
}

#第6步  连接数据库方法【\think\db\PDOConnection】
public function connect(array $config = [], $linkNum = 0, $autoConnection = false): PDO
{
	if (isset($this->links[$linkNum])) {
		return $this->links[$linkNum];
	}

	if (empty($config)) {
		$config = $this->config;
	} else {
		$config = array_merge($this->config, $config);
	}

	// 连接参数
	if (isset($config['params']) && is_array($config['params'])) {
		$params = $config['params'] + $this->params;
	} else {
		$params = $this->params;
	}

	// 记录当前字段属性大小写设置
	$this->attrCase = $params[PDO::ATTR_CASE];

	if (!empty($config['break_match_str'])) {
		$this->breakMatchStr = array_merge($this->breakMatchStr, (array) $config['break_match_str']);
	}

	try {
		if (empty($config['dsn'])) {
			$config['dsn'] = $this->parseDsn($config);
		}

		$startTime = microtime(true);

		$this->links[$linkNum] = $this->createPdo($config['dsn'], $config['username'], $config['password'], $params); //链接数据库

		// SQL监控
		if (!empty($config['trigger_sql'])) {
			$this->trigger('CONNECT:[ UseTime:' . number_format(microtime(true) - $startTime, 6) . 's ] ' . $config['dsn']);
		}

		return $this->links[$linkNum];
	} catch (\PDOException $e) {
		if ($autoConnection) {
			$this->db->log($e->getMessage(), 'error');
			return $this->connect($autoConnection, $linkNum);
		} else {
			throw $e;
		}
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值