修正Mongo驱动解析配置逻辑

This commit is contained in:
huangdijia
2016-01-05 14:01:36 +08:00
parent c70761c971
commit 95462c1cc0
2 changed files with 32 additions and 32 deletions

View File

@@ -41,22 +41,23 @@ abstract class Driver
protected $_linkID = null; protected $_linkID = null;
// 数据库连接参数配置 // 数据库连接参数配置
protected $config = [ protected $config = [
'type' => '', // 数据库类型 'type' => '', // 数据库类型
'hostname' => '127.0.0.1', // 服务器地址 'hostname' => '127.0.0.1', // 服务器地址
'database' => '', // 数据库名 'database' => '', // 数据库名
'username' => '', // 用户名 'username' => '', // 用户名
'password' => '', // 密码 'password' => '', // 密码
'hostport' => '', // 端口 'hostport' => '', // 端口
'dsn' => '', // 'dsn' => '', //
'params' => [], // 数据库连接参数 'params' => [], // 数据库连接参数
'charset' => 'utf8', // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库编码默认采用utf8
'prefix' => '', // 数据库表前缀 'prefix' => '', // 数据库表前缀
'debug' => false, // 数据库调试模式 'debug' => false, // 数据库调试模式
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'rw_separate' => false, // 数据库读写是否分离 主从式有效 'rw_separate' => false, // 数据库读写是否分离 主从式有效
'master_num' => 1, // 读写分离后 主服务器数量 'master_num' => 1, // 读写分离后 主服务器数量
'slave_no' => '', // 指定从服务器序号 'slave_no' => '', // 指定从服务器序号
'db_like_fields' => '', //like字段自动替换为%%包裹 'db_like_fields' => '', // like字段自动替换为%%包裹
'debug' => false, // 是否调试
]; ];
// 数据库表达式 // 数据库表达式
protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN']; protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN'];

View File

@@ -48,7 +48,6 @@ class Mongo extends Driver
$this->config['params'] = []; $this->config['params'] = [];
} }
} }
$this->config = $config;
} }
/** /**
@@ -105,7 +104,7 @@ class Mongo extends Driver
$this->_mongo = $this->_linkID->selectDb($db); $this->_mongo = $this->_linkID->selectDb($db);
} }
// 当前MongoCollection对象 // 当前MongoCollection对象
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.getCollection(' . $collection . ')'; $this->queryStr = $this->_dbName . '.getCollection(' . $collection . ')';
} }
if ($this->_collectionName != $collection) { if ($this->_collectionName != $collection) {
@@ -217,7 +216,7 @@ class Mongo extends Driver
} }
$this->model = $options['model']; $this->model = $options['model'];
$this->executeTimes++; $this->executeTimes++;
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.insert('; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.insert(';
$this->queryStr .= $data ? json_encode($data) : '{}'; $this->queryStr .= $data ? json_encode($data) : '{}';
$this->queryStr .= ')'; $this->queryStr .= ')';
@@ -277,7 +276,7 @@ class Mongo extends Driver
*/ */
public function getMongoNextId($pk) public function getMongoNextId($pk)
{ {
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.find({},{' . $pk . ':1}).sort({' . $pk . ':-1}).limit(1)'; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.find({},{' . $pk . ':1}).sort({' . $pk . ':-1}).limit(1)';
} }
try { try {
@@ -310,7 +309,7 @@ class Mongo extends Driver
$this->model = $options['model']; $this->model = $options['model'];
$query = $this->parseWhere($options['where']); $query = $this->parseWhere($options['where']);
$set = $this->parseSet($data); $set = $this->parseSet($data);
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.update('; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.update(';
$this->queryStr .= $query ? json_encode($query) : '{}'; $this->queryStr .= $query ? json_encode($query) : '{}';
$this->queryStr .= ',' . json_encode($set) . ')'; $this->queryStr .= ',' . json_encode($set) . ')';
@@ -344,7 +343,7 @@ class Mongo extends Driver
$query = $this->parseWhere($options['where']); $query = $this->parseWhere($options['where']);
$this->model = $options['model']; $this->model = $options['model'];
$this->executeTimes++; $this->executeTimes++;
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.remove(' . json_encode($query) . ')'; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.remove(' . json_encode($query) . ')';
} }
try { try {
@@ -370,7 +369,7 @@ class Mongo extends Driver
} }
$this->model = $options['model']; $this->model = $options['model'];
$this->executeTimes++; $this->executeTimes++;
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.remove({})'; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.remove({})';
} }
try { try {
@@ -405,10 +404,10 @@ class Mongo extends Driver
} }
$this->model = $options['model']; $this->model = $options['model'];
$this->queryTimes++; $this->queryTimes++;
$query = $this->parseWhere(!empty($options['where']) ? $options['where'] : ''); $query = $this->parseWhere(!empty($options['where']) ? $options['where'] : array());
$field = $this->parseField(!empty($options['field']) ? $options['field'] : ''); $field = $this->parseField(!empty($options['field']) ? $options['field'] : '');
try { try {
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.find('; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.find(';
$this->queryStr .= $query ? json_encode($query) : '{}'; $this->queryStr .= $query ? json_encode($query) : '{}';
$this->queryStr .= $field ? ',' . json_encode($field) : ''; $this->queryStr .= $field ? ',' . json_encode($field) : '';
@@ -418,7 +417,7 @@ class Mongo extends Driver
$_cursor = $this->_collection->find($query, $field); $_cursor = $this->_collection->find($query, $field);
if ($options['order']) { if ($options['order']) {
$order = $this->parseOrder($options['order']); $order = $this->parseOrder($options['order']);
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr .= '.sort(' . json_encode($order) . ')'; $this->queryStr .= '.sort(' . json_encode($order) . ')';
} }
$_cursor = $_cursor->sort($order); $_cursor = $_cursor->sort($order);
@@ -438,12 +437,12 @@ class Mongo extends Driver
if (isset($options['limit'])) { if (isset($options['limit'])) {
list($offset, $length) = $this->parseLimit($options['limit']); list($offset, $length) = $this->parseLimit($options['limit']);
if (!empty($offset)) { if (!empty($offset)) {
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr .= '.skip(' . intval($offset) . ')'; $this->queryStr .= '.skip(' . intval($offset) . ')';
} }
$_cursor = $_cursor->skip(intval($offset)); $_cursor = $_cursor->skip(intval($offset));
} }
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr .= '.limit(' . intval($length) . ')'; $this->queryStr .= '.limit(' . intval($length) . ')';
} }
$_cursor = $_cursor->limit(intval($length)); $_cursor = $_cursor->limit(intval($length));
@@ -485,7 +484,7 @@ class Mongo extends Driver
$this->queryTimes++; $this->queryTimes++;
$query = $this->parseWhere(!empty($options['where'])?$options['where']:''); $query = $this->parseWhere(!empty($options['where'])?$options['where']:'');
$fields = $this->parseField(!empty($options['field'])?$options['field']:''); $fields = $this->parseField(!empty($options['field'])?$options['field']:'');
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne('; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne(';
$this->queryStr .= $query ? json_encode($query) : '{}'; $this->queryStr .= $query ? json_encode($query) : '{}';
$this->queryStr .= $fields ? ',' . json_encode($fields) : ''; $this->queryStr .= $fields ? ',' . json_encode($fields) : '';
@@ -519,7 +518,7 @@ class Mongo extends Driver
$this->model = $options['model']; $this->model = $options['model'];
$this->queryTimes++; $this->queryTimes++;
$query = $this->parseWhere($options['where']); $query = $this->parseWhere($options['where']);
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName; $this->queryStr = $this->_dbName . '.' . $this->_collectionName;
$this->queryStr .= $query ? '.find(' . json_encode($query) . ')' : ''; $this->queryStr .= $query ? '.find(' . json_encode($query) . ')' : '';
$this->queryStr .= '.count()'; $this->queryStr .= '.count()';
@@ -550,7 +549,7 @@ class Mongo extends Driver
$this->switchCollection($collection, '', false); $this->switchCollection($collection, '', false);
} }
$this->queryTimes++; $this->queryTimes++;
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne()'; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne()';
} }
try { try {
@@ -581,7 +580,7 @@ class Mongo extends Driver
*/ */
public function getTables() public function getTables()
{ {
if (!empty($this->config['debug'])) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.getCollenctionNames()'; $this->queryStr = $this->_dbName . '.getCollenctionNames()';
} }
$this->queryTimes++; $this->queryTimes++;