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

Commit

Permalink
Merge pull request #51 from sergeyklay/feature-exists
Browse files Browse the repository at this point in the history
Added getName & exists
  • Loading branch information
colinmollenhour committed Aug 8, 2013
2 parents 28749e0 + 4a65080 commit c65c005
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions classes/mongo/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class Mongo_Database {
* @static array */
protected static $instances = array();

public static $configs = array();
/** Array of configuration sets
* @var array */
protected $configs = array();

/**
* @var string default db to use
Expand Down Expand Up @@ -112,9 +114,9 @@ public static function instance($name = NULL, array $config = NULL, $override =
{
if ($config === NULL)
{
if (isset(self::$configs[$name]))
if (isset($this->configs[$name]))
{
$config = self::$configs[$name];
$config = $this->configs[$name];
if ($config instanceof Closure) $config = $config($name);
} else if (class_exists('Kohana'))
{
Expand All @@ -138,7 +140,7 @@ public static function instance($name = NULL, array $config = NULL, $override =
protected $_connected = FALSE;

/** The Mongo server connection
* @var Mongo */
* @var MongoClient|Mongo */
protected $_connection;

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

// Store config
$this->configs[$name] = $config;

// Store the database instance
self::$instances[$name] = $this;
}
Expand Down Expand Up @@ -324,7 +329,7 @@ public function execute_safe( $code, array $args = array(), $scope = array() )
$result = $this->execute($code, $args);
if( empty($result['ok']) )
{
throw new MongoException($result['errmsg'], $result['errno']);
throw new MongoException($result['errmsg'], $result['code']);
}
return $result['retval'];
}
Expand Down Expand Up @@ -461,11 +466,36 @@ public function profiler_stop($token)
call_user_func($this->_stop_callback, $token);
}


/** @return Mongo */

/**
* Get the MongoClient|Mongo instance directly
* @return MongoClient|Mongo
*/
public function connection()
{
return $this->_connection;
}

/**
* Get the currently referenced database
* @return string
*/
public function getName()
{
return $this->execute_safe('db.getName()');
}


/**
* Checks if the given $collection exists in the currently referenced database
* @param string $collection Collection name
* @return bool
*/
public function exists($collection)
{
$result = $this->execute_safe("db.{$collection}.exists()");

return ( ! is_null($result));
}

}

0 comments on commit c65c005

Please sign in to comment.