Skip to content

Commit

Permalink
Add support for new MongoDB driver (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcosca committed Dec 11, 2016
1 parent 7587ec1 commit b5e21f0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
36 changes: 29 additions & 7 deletions db/mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Mongo {
$dsn,
//! MongoDB object
$db,
//! Legacy flag
$legacy,
//! MongoDB log
$log;

Expand All @@ -63,7 +65,7 @@ function uuid() {
**/
function log($flag=TRUE) {
if ($flag) {
$cursor=$this->selectcollection('system.profile')->find();
$cursor=$this->db->selectcollection('system.profile')->find();
foreach (iterator_to_array($cursor) as $frame)
if (!preg_match('/\.system\..+$/',$frame['ns']))
$this->log.=date('r',$frame['ts']->sec).' ('.
Expand All @@ -76,7 +78,10 @@ function log($flag=TRUE) {
PHP_EOL;
} else {
$this->log=FALSE;
$this->setprofilinglevel(-1);
if ($legacy)
$this->db->setprofilinglevel(-1);
else
$this->db->command(['profile'=>-1]);
}
return $this->log;
}
Expand All @@ -87,8 +92,12 @@ function log($flag=TRUE) {
**/
function drop() {
$out=$this->db->drop();
if ($this->log!==FALSE)
$this->setprofilinglevel(2);
if ($this->log!==FALSE) {
if ($legacy)
$this->db->setprofilinglevel(2);
else
$this->db->command(['profile'=>2]);
}
return $out;
}

Expand All @@ -102,6 +111,14 @@ function __call($func,array $args) {
return call_user_func_array([$this->db,$func],$args);
}

/**
* Return TRUE if legacy driver is loaded
* @return bool
**/
function legacy() {
return $this->legacy;
}

//! Prohibit cloning
private function __clone() {
}
Expand All @@ -114,9 +131,14 @@ private function __clone() {
**/
function __construct($dsn,$dbname,array $options=NULL) {
$this->uuid=\Base::instance()->hash($this->dsn=$dsn);
$class=class_exists('\MongoClient')?'\MongoClient':'\Mongo';
$this->db=new \MongoDB(new $class($dsn,$options?:[]),$dbname);
$this->setprofilinglevel(2);
if ($this->legacy=class_exists('\MongoClient')) {
$this->db=new \MongoDB(new $class($dsn,$options?:[]),$dbname);

This comment has been minimized.

Copy link
@ptdev

ptdev Dec 12, 2016

Contributor

$class is not defined here, I guess the previous check was to also support the even older Mongo class, pre MongoClient.

This comment has been minimized.

Copy link
@bcosca

bcosca Dec 12, 2016

Author Collaborator

Thanks for reporting this. Fixed in latest commit. I would appreciate testing within the old driver context because I don't have that setup anymore.

This comment has been minimized.

Copy link
@ikkez

ikkez Dec 12, 2016

Member

still works fine with the old driver 👍

$this->db->setprofilinglevel(2);
}
else {
$this->db=(new \MongoDB\Client($dsn,$options?:[]))->$dbname;
$this->db->command(['profile'=>2]);
}
}

}
62 changes: 44 additions & 18 deletions db/mongo/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Mapper extends \DB\Cursor {
protected
//! MongoDB wrapper
$db,
//! Legacy flag
$legacy,
//! Mongo collection
$collection,
//! Mongo document
Expand Down Expand Up @@ -155,16 +157,26 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
$filter=$filter?:[];
$collection=$this->collection;
}
$this->cursor=$collection->find($filter,$fields?:[]);
if ($options['order'])
$this->cursor=$this->cursor->sort($options['order']);
if ($options['limit'])
$this->cursor=$this->cursor->limit($options['limit']);
if ($options['offset'])
$this->cursor=$this->cursor->skip($options['offset']);
$result=[];
while ($this->cursor->hasnext())
$result[]=$this->cursor->getnext();
if ($this->legacy) {
$this->cursor=$collection->find($filter,$fields?:[]);
if ($options['order'])
$this->cursor=$this->cursor->sort($options['order']);
if ($options['limit'])
$this->cursor=$this->cursor->limit($options['limit']);
if ($options['offset'])
$this->cursor=$this->cursor->skip($options['offset']);
$result=[];
while ($this->cursor->hasnext())
$result[]=$this->cursor->getnext();
}
else {
$this->cursor=$collection->find($filter,[
'sort'=>$options['order'],
'limit'=>$options['limit'],
'skip'=>$options['offset']
]);
$result=$this->cursor->toarray();
}
if ($options['group'])
$tmp->drop();
if ($fw->get('CACHE') && $ttl)
Expand Down Expand Up @@ -240,8 +252,14 @@ function insert() {
\Base::instance()->call($this->trigger['beforeinsert'],
[$this,['_id'=>$this->document['_id']]])===FALSE)
return $this->document;
$this->collection->insert($this->document);
$pkey=['_id'=>$this->document['_id']];
if ($this->legacy) {
$this->collection->insert($this->document);
$pkey=['_id'=>$this->document['_id']];
}
else {
$result=$this->collection->insertone($this->document);
$pkey=['_id'=>$result->getinsertedid()];
}
if (isset($this->trigger['afterinsert']))
\Base::instance()->call($this->trigger['afterinsert'],
[$this,$pkey]);
Expand All @@ -259,8 +277,11 @@ function update() {
\Base::instance()->call($this->trigger['beforeupdate'],
[$this,$pkey])===FALSE)
return $this->document;
$this->collection->update(
$pkey,$this->document,['upsert'=>TRUE]);
$upsert=['upsert'=>TRUE];
if ($this->legacy)
$this->collection->update($pkey,$this->document,$upsert);
else
$this->collection->replaceone($pkey,$this->document,$upsert);
if (isset($this->trigger['afterupdate']))
\Base::instance()->call($this->trigger['afterupdate'],
[$this,$pkey]);
Expand All @@ -273,15 +294,19 @@ function update() {
* @param $filter array
**/
function erase($filter=NULL) {
if ($filter)
return $this->collection->remove($filter);
if ($filter) {
return $this->legacy?
$this->collection->remove($filter):
$this->collection->deletemany($filter);
}
$pkey=['_id'=>$this->document['_id']];
if (isset($this->trigger['beforeerase']) &&
\Base::instance()->call($this->trigger['beforeerase'],
[$this,$pkey])===FALSE)
return FALSE;
$result=$this->collection->
remove(['_id'=>$this->document['_id']]);
$result=$this->legacy?
$this->collection->remove(['_id'=>$this->document['_id']]):
$this->collection->deleteone(['_id'=>$this->document['_id']]);
parent::erase();
if (isset($this->trigger['aftererase']))
\Base::instance()->call($this->trigger['aftererase'],
Expand Down Expand Up @@ -357,6 +382,7 @@ function getiterator() {
**/
function __construct(\DB\Mongo $db,$collection,$fields=NULL) {
$this->db=$db;
$this->legacy=$db->legacy();
$this->collection=$db->selectcollection($collection);
$this->fields=$fields;
$this->reset();
Expand Down

0 comments on commit b5e21f0

Please sign in to comment.