Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit c65c005

Browse files
Merge pull request #51 from sergeyklay/feature-exists
Added getName & exists
2 parents 28749e0 + 4a65080 commit c65c005

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

classes/mongo/database.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ class Mongo_Database {
7979
* @static array */
8080
protected static $instances = array();
8181

82-
public static $configs = array();
82+
/** Array of configuration sets
83+
* @var array */
84+
protected $configs = array();
8385

8486
/**
8587
* @var string default db to use
@@ -112,9 +114,9 @@ public static function instance($name = NULL, array $config = NULL, $override =
112114
{
113115
if ($config === NULL)
114116
{
115-
if (isset(self::$configs[$name]))
117+
if (isset($this->configs[$name]))
116118
{
117-
$config = self::$configs[$name];
119+
$config = $this->configs[$name];
118120
if ($config instanceof Closure) $config = $config($name);
119121
} else if (class_exists('Kohana'))
120122
{
@@ -138,7 +140,7 @@ public static function instance($name = NULL, array $config = NULL, $override =
138140
protected $_connected = FALSE;
139141

140142
/** The Mongo server connection
141-
* @var Mongo */
143+
* @var MongoClient|Mongo */
142144
protected $_connection;
143145

144146
/** The database instance for the database name chosen by the config
@@ -194,6 +196,9 @@ protected function __construct($name, array $config)
194196
// Save profiling option in a public variable
195197
$this->profiling = (isset($config['profiling']) && $config['profiling']);
196198

199+
// Store config
200+
$this->configs[$name] = $config;
201+
197202
// Store the database instance
198203
self::$instances[$name] = $this;
199204
}
@@ -324,7 +329,7 @@ public function execute_safe( $code, array $args = array(), $scope = array() )
324329
$result = $this->execute($code, $args);
325330
if( empty($result['ok']) )
326331
{
327-
throw new MongoException($result['errmsg'], $result['errno']);
332+
throw new MongoException($result['errmsg'], $result['code']);
328333
}
329334
return $result['retval'];
330335
}
@@ -461,11 +466,36 @@ public function profiler_stop($token)
461466
call_user_func($this->_stop_callback, $token);
462467
}
463468

464-
465-
/** @return Mongo */
469+
470+
/**
471+
* Get the MongoClient|Mongo instance directly
472+
* @return MongoClient|Mongo
473+
*/
466474
public function connection()
467475
{
468476
return $this->_connection;
469477
}
470478

479+
/**
480+
* Get the currently referenced database
481+
* @return string
482+
*/
483+
public function getName()
484+
{
485+
return $this->execute_safe('db.getName()');
486+
}
487+
488+
489+
/**
490+
* Checks if the given $collection exists in the currently referenced database
491+
* @param string $collection Collection name
492+
* @return bool
493+
*/
494+
public function exists($collection)
495+
{
496+
$result = $this->execute_safe("db.{$collection}.exists()");
497+
498+
return ( ! is_null($result));
499+
}
500+
471501
}

0 commit comments

Comments
 (0)