改进Connection类的connect方法

This commit is contained in:
thinkphp
2016-04-28 17:18:09 +08:00
parent 7050d8b630
commit d8214cca48
3 changed files with 21 additions and 18 deletions

View File

@@ -14,9 +14,9 @@ namespace think;
class View
{
// 视图实例
protected static $instance = null;
protected static $instance;
// 模板引擎实例
public $engine = null;
public $engine;
// 模板变量
protected $data = [];
// 视图输出替换

View File

@@ -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)
{

View File

@@ -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');