|
@@ -20,7 +20,7 @@ class Mongo extends Base
|
|
|
if (empty($setting['timeout'])) {
|
|
|
$setting['timeout'] = 1000;
|
|
|
}
|
|
|
- $handle = new Manager('mongodb://' . $setting['host'] . ':' . $setting['port'], array('username' => $setting['user'], 'password' => $setting['pwd'], 'connectTimeoutMS' => $setting['timeout']));
|
|
|
+ $handle = new Manager('mongodb://' . $setting['host'] . ':' . $setting['port'], ['username' => $setting['user'], 'password' => $setting['pwd'], 'connectTimeoutMS' => $setting['timeout']]);
|
|
|
Debug::add('mongodb ' . $setting['host'] . ' connected', $setting['type']);
|
|
|
return $handle;
|
|
|
} catch (\PDOException $e) {
|
|
@@ -30,15 +30,15 @@ class Mongo extends Base
|
|
|
public function index($config, $state = 0)
|
|
|
{
|
|
|
return;
|
|
|
- $command = array('listIndexes' => $config['table']);
|
|
|
+ $command = ['listIndexes' => $config['table']];
|
|
|
$result = $this->read->executeCommand($this->db, new Command($command));
|
|
|
foreach ($result as $k => $v) {
|
|
|
if ($v->name != '_id_') {
|
|
|
- $command = array('dropIndexes' => $config['table'], 'index' => $v->name);
|
|
|
+ $command = ['dropIndexes' => $config['table'], 'index' => $v->name];
|
|
|
$this->read->executeCommand($this->db, new Command($command));
|
|
|
}
|
|
|
}
|
|
|
- $index = array();
|
|
|
+ $index = [];
|
|
|
foreach ($config['index'] as $k => $v) {
|
|
|
$t = false;
|
|
|
if (strpos($v, '.')) {
|
|
@@ -48,29 +48,28 @@ class Mongo extends Base
|
|
|
}
|
|
|
}
|
|
|
$v = explode(',', $v);
|
|
|
- $key = array();
|
|
|
+ $key = [];
|
|
|
foreach ($v as $v1) {
|
|
|
$key[$v1] = 1;
|
|
|
}
|
|
|
- $index[] = array
|
|
|
- (
|
|
|
+ $index[] = [
|
|
|
'name' => $k,
|
|
|
'key' => $key,
|
|
|
'background' => false,
|
|
|
'unique' => $t
|
|
|
- );
|
|
|
+ ];
|
|
|
}
|
|
|
- $command = array('createIndexes' => $config['table'],'indexes' => $index);
|
|
|
+ $command = ['createIndexes' => $config['table'],'indexes' => $index];
|
|
|
$this->read->executeCommand($this->db, new Command($command));
|
|
|
}
|
|
|
public function load($table, $param, $set, $field, $lock)
|
|
|
{
|
|
|
$param = $this->param($param);
|
|
|
- $options = array();
|
|
|
+ $options = [];
|
|
|
if (isset($set['order'])) {
|
|
|
if (is_string($set['order'])) {
|
|
|
$temp = explode(',', $set['order']);
|
|
|
- $set['order'] = array();
|
|
|
+ $set['order'] = [];
|
|
|
foreach ($temp as $k => $v) {
|
|
|
$t = explode(' ', $v);
|
|
|
if ($t[0] == 'id') {
|
|
@@ -96,7 +95,7 @@ class Mongo extends Base
|
|
|
}
|
|
|
if (isset($set['col']) && $set['col'] && $set['col'] != '*') {
|
|
|
$temp = explode(',', $set['col']);
|
|
|
- $total = array();
|
|
|
+ $total = [];
|
|
|
foreach ($temp as $k => $v) {
|
|
|
if (strstr($v, 'sum(')) {
|
|
|
if (strstr($v, ' as')) {
|
|
@@ -107,24 +106,24 @@ class Mongo extends Base
|
|
|
$k = $v;
|
|
|
}
|
|
|
$v = str_replace(array('sum(', ')'), '', $v);
|
|
|
- $total[$k] = array('$sum' => '$' . $v);
|
|
|
+ $total[$k] = ['$sum' => '$' . $v];
|
|
|
} else {
|
|
|
$options['projection'][$v] = true;
|
|
|
}
|
|
|
}
|
|
|
if ($total && empty($set['group'])) {
|
|
|
- $set['group'] = array();
|
|
|
+ $set['group'] = [];
|
|
|
foreach ($param as $k => $v) {
|
|
|
$set['group'][] = $k;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (isset($set['group'])) {
|
|
|
- $pipeline = array();
|
|
|
+ $pipeline = [];
|
|
|
if ($param) {
|
|
|
- $pipeline[] = array('$match' => $param);
|
|
|
+ $pipeline[] = ['$match' => $param];
|
|
|
}
|
|
|
- $group = array();
|
|
|
+ $group = [];
|
|
|
if ($set['group'] == 'null') {
|
|
|
$group = null;
|
|
|
} else {
|
|
@@ -135,26 +134,26 @@ class Mongo extends Base
|
|
|
$group[$v] = '$' . $v;
|
|
|
}
|
|
|
}
|
|
|
- $group = array('_id' => $group);
|
|
|
+ $group = ['_id' => $group];
|
|
|
if (isset($total) && $total) {
|
|
|
$group = array_merge($group, $total);
|
|
|
} else {
|
|
|
- $group['count'] = array('$sum' => 1);
|
|
|
+ $group['count'] = ['$sum' => 1];
|
|
|
}
|
|
|
if (isset($options['projection'])) {
|
|
|
foreach ($options['projection'] as $k => $v) {
|
|
|
- $group[$k] = array('$push' => '$' . $k);
|
|
|
+ $group[$k] = ['$push' => '$' . $k];
|
|
|
}
|
|
|
}
|
|
|
- $pipeline[] = array('$group' => $group);
|
|
|
+ $pipeline[] = ['$group' => $group];
|
|
|
if (isset($options['sort'])) {
|
|
|
- $pipeline[] = array('$sort' => $options['sort']);
|
|
|
+ $pipeline[] = ['$sort' => $options['sort']];
|
|
|
}
|
|
|
if (isset($options['skip'])) {
|
|
|
- $pipeline[] = array('$skip' => $options['skip']);
|
|
|
+ $pipeline[] = ['$skip' => $options['skip']];
|
|
|
}
|
|
|
if (isset($options['limit'])) {
|
|
|
- $pipeline[] = array('$limit' => $options['limit']);
|
|
|
+ $pipeline[] = ['$limit' => $options['limit']];
|
|
|
}
|
|
|
$options = array('aggregate' => $table,'pipeline' => $pipeline,'cursor' => new \stdClass());
|
|
|
$command = new Command($options);
|
|
@@ -164,13 +163,13 @@ class Mongo extends Base
|
|
|
$result = $this->read->executeQuery($this->db . '.' . $table, $query);
|
|
|
}
|
|
|
if (Debug::$shell) {
|
|
|
- $this->log(array('table' => $this->db . '.' . $table, 'param' => $param, 'option' => $options, 'result' => $result));
|
|
|
+ $this->log(['table' => $this->db . '.' . $table, 'param' => $param, 'option' => $options, 'result' => $result]);
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
public function select($table, $param, $set, $field, $lock)
|
|
|
{
|
|
|
- $result = array();
|
|
|
+ $result = [];
|
|
|
$data = $this->load($table, $param, $set, $field, $lock);
|
|
|
foreach ($data as $k => $v) {
|
|
|
$result[] = $this->handle($v);
|
|
@@ -179,7 +178,7 @@ class Mongo extends Base
|
|
|
}
|
|
|
public function find($table, $param, $set, $field, $lock)
|
|
|
{
|
|
|
- $result = array();
|
|
|
+ $result = [];
|
|
|
$data = $this->load($table, $param, $set, $field, $lock);
|
|
|
foreach ($data as $k => $v) {
|
|
|
$result = $this->handle($v);
|
|
@@ -196,13 +195,13 @@ class Mongo extends Base
|
|
|
$result = $data[0]->count;
|
|
|
}
|
|
|
if (Debug::$shell) {
|
|
|
- $this->log(array('table' => $this->db . '.' . $table, 'param' => $param, 'result' => $result));
|
|
|
+ $this->log(['table' => $this->db . '.' . $table, 'param' => $param, 'result' => $result]);
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
public function insert($table, $data, $field)
|
|
|
{
|
|
|
- $insert = array();
|
|
|
+ $insert = [];
|
|
|
foreach ($data as $k => $v) {
|
|
|
if ($field && empty($field[$k]) && strpos('id,cdate', $k) === false) {
|
|
|
continue;
|
|
@@ -230,7 +229,7 @@ class Mongo extends Base
|
|
|
$id = $id['oid'];
|
|
|
$result = $this->update->executeBulkWrite($this->db . '.' . $table, $bulk);
|
|
|
if (Debug::$shell) {
|
|
|
- $this->log(array('table' => $this->db . '.' . $table, 'insert' => $insert, 'result' => $id));
|
|
|
+ $this->log(['table' => $this->db . '.' . $table, 'insert' => $insert, 'result' => $id]);
|
|
|
}
|
|
|
if ($result->getInsertedCount() >= 1) {
|
|
|
return $id;
|
|
@@ -239,7 +238,7 @@ class Mongo extends Base
|
|
|
}
|
|
|
public function update($table, $param, $data, $field)
|
|
|
{
|
|
|
- $update = array();
|
|
|
+ $update = [];
|
|
|
foreach ($data as $k => $v) {
|
|
|
if ($field && empty($field[$k]) && strpos('id,cdate', $k) === false) {
|
|
|
continue;
|
|
@@ -253,14 +252,14 @@ class Mongo extends Base
|
|
|
}*/
|
|
|
$update[$k] = $v;
|
|
|
}
|
|
|
- $update = array('$set' => $update);
|
|
|
+ $update = ['$set' => $update];
|
|
|
$param = $this->param($param);
|
|
|
$bulk = new BulkWrite;
|
|
|
- $bulk->update($param, $update, array('multi' => true, 'upsert' => false));
|
|
|
+ $bulk->update($param, $update, ['multi' => true, 'upsert' => false]);
|
|
|
$result = $this->update->executeBulkWrite($this->db . '.' . $table, $bulk);
|
|
|
$result = $result->getModifiedCount();
|
|
|
if (Debug::$shell) {
|
|
|
- $this->log(array('table' => $this->db . '.' . $table, 'param' => $param, 'update' => $update, 'result' => $result));
|
|
|
+ $this->log(['table' => $this->db . '.' . $table, 'param' => $param, 'update' => $update, 'result' => $result]);
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
@@ -272,13 +271,13 @@ class Mongo extends Base
|
|
|
$result = $this->update->executeBulkWrite($this->db . '.' . $table, $bulk);
|
|
|
$result = $result->getDeletedCount();
|
|
|
if (Debug::$shell) {
|
|
|
- $this->log(array('table' => $this->db . '.' . $table, 'param' => $param, 'result' => $result));
|
|
|
+ $this->log(['table' => $this->db . '.' . $table, 'param' => $param, 'result' => $result]);
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
private function param($param)
|
|
|
{
|
|
|
- $result = array();
|
|
|
+ $result = [];
|
|
|
if ($param) {
|
|
|
if (is_array($param)) {
|
|
|
foreach ($param as $k => $v) {
|
|
@@ -289,7 +288,7 @@ class Mongo extends Base
|
|
|
$k = '_id';
|
|
|
}
|
|
|
if ($k == 'or' || $k == 'and') {
|
|
|
- $where = array();
|
|
|
+ $where = [];
|
|
|
foreach ($v as $k1 => $v1) {
|
|
|
if (strpos($k1, '#')) {
|
|
|
$k1 = trim($k1, '#');
|
|
@@ -342,7 +341,7 @@ class Mongo extends Base
|
|
|
foreach ($value as $k => $v) {
|
|
|
$value[$k] = $this->value($key, $v);
|
|
|
}
|
|
|
- $value = array('$' . $method => $value);
|
|
|
+ $value = ['$' . $method => $value];
|
|
|
break;
|
|
|
case '>':
|
|
|
$value = array('$gt' => $this->value($key, $value));
|