From d8214cca481a011069d22d5556a1f3b5533b6bd9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 28 Apr 2016 17:18:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BConnection=E7=B1=BB=E7=9A=84c?= =?UTF-8?q?onnect=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/View.php | 4 ++-- library/think/db/Builder.php | 8 +++++--- library/think/db/Connection.php | 27 ++++++++++++++------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/library/think/View.php b/library/think/View.php index d066916d..8ff888ab 100644 --- a/library/think/View.php +++ b/library/think/View.php @@ -14,9 +14,9 @@ namespace think; class View { // 视图实例 - protected static $instance = null; + protected static $instance; // 模板引擎实例 - public $engine = null; + public $engine; // 模板变量 protected $data = []; // 视图输出替换 diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index a3838499..23a5c8f4 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -17,8 +17,9 @@ use think\Exception; abstract class Builder { - // db对象实例 + // connection对象实例 protected $connection; + // 查询对象实例 protected $query; // 查询参数 @@ -26,7 +27,8 @@ abstract class Builder // 数据库表达式 protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL']; - // 查询表达式 + + // SQL表达式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%'; protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%'; protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%'; @@ -36,7 +38,7 @@ abstract class Builder /** * 架构函数 * @access public - * @param object $db 数据库对象实例 + * @param \think\db\Connection $connection 数据库连接对象实例 */ public function __construct($connection) { diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index aa4ce76d..be1570ea 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -24,7 +24,7 @@ use think\Log; abstract class Connection { // PDO操作实例 - protected $PDOStatement = null; + protected $PDOStatement; // 当前操作的数据表名 protected $table = ''; // 当前操作的数据对象名 @@ -32,7 +32,7 @@ abstract class Connection // 当前SQL指令 protected $queryStr = ''; // 最后插入ID - protected $lastInsID = null; + protected $lastInsID; // 返回或者影响记录数 protected $numRows = 0; // 事务指令数 @@ -42,7 +42,7 @@ abstract class Connection // 数据库连接ID 支持多个连接 protected $links = []; // 当前连接ID - protected $linkID = null; + protected $linkID; // 查询结果类型 protected $fetchType = PDO::FETCH_ASSOC; // 监听回调 @@ -102,9 +102,6 @@ abstract class Connection { if (!empty($config)) { $this->config = array_merge($this->config, $config); - if (is_array($this->config['params'])) { - $this->params = $this->config['params'] + $this->params; - } } $this->query = new Query($this); } @@ -178,19 +175,23 @@ abstract class Connection public function connect(array $config = [], $linkNum = 0, $autoConnection = false) { if (!isset($this->links[$linkNum])) { - if (!empty($config)) { - $this->config = $config; + if (empty($config)) { + $config = $this->config; } // 连接参数 - $params = $this->config['params'] + $this->params; + if (isset($config['params']) && is_array($config['params'])) { + $params = $config['params'] + $this->params; + } else { + $params = $this->params; + } try { - if (empty($this->config['dsn'])) { - $this->config['dsn'] = $this->parseDsn($this->config); + if (empty($config['dsn'])) { + $config['dsn'] = $this->parseDsn($config); } - $this->links[$linkNum] = new PDO($this->config['dsn'], $this->config['username'], $this->config['password'], $params); + $this->links[$linkNum] = new PDO($config['dsn'], $config['username'], $config['password'], $params); // 记录数据库连接信息 - APP_DEBUG && Log::record('[ DB ] CONNECT: ' . $this->config['dsn'], 'info'); + APP_DEBUG && Log::record('[ DB ] CONNECT: ' . $config['dsn'], 'info'); } catch (\PDOException $e) { if ($autoConnection) { Log::record($e->getMessage(), 'error');