@@ -79,7 +79,9 @@ class Mongo_Database {
79
79
* @static array */
80
80
protected static $ instances = array ();
81
81
82
- public static $ configs = array ();
82
+ /** Array of configuration sets
83
+ * @var array */
84
+ protected $ configs = array ();
83
85
84
86
/**
85
87
* @var string default db to use
@@ -112,9 +114,9 @@ public static function instance($name = NULL, array $config = NULL, $override =
112
114
{
113
115
if ($ config === NULL )
114
116
{
115
- if (isset (self :: $ configs [$ name ]))
117
+ if (isset ($ this -> configs [$ name ]))
116
118
{
117
- $ config = self :: $ configs [$ name ];
119
+ $ config = $ this -> configs [$ name ];
118
120
if ($ config instanceof Closure) $ config = $ config ($ name );
119
121
} else if (class_exists ('Kohana ' ))
120
122
{
@@ -138,7 +140,7 @@ public static function instance($name = NULL, array $config = NULL, $override =
138
140
protected $ _connected = FALSE ;
139
141
140
142
/** The Mongo server connection
141
- * @var Mongo */
143
+ * @var MongoClient| Mongo */
142
144
protected $ _connection ;
143
145
144
146
/** The database instance for the database name chosen by the config
@@ -194,6 +196,9 @@ protected function __construct($name, array $config)
194
196
// Save profiling option in a public variable
195
197
$ this ->profiling = (isset ($ config ['profiling ' ]) && $ config ['profiling ' ]);
196
198
199
+ // Store config
200
+ $ this ->configs [$ name ] = $ config ;
201
+
197
202
// Store the database instance
198
203
self ::$ instances [$ name ] = $ this ;
199
204
}
@@ -324,7 +329,7 @@ public function execute_safe( $code, array $args = array(), $scope = array() )
324
329
$ result = $ this ->execute ($ code , $ args );
325
330
if ( empty ($ result ['ok ' ]) )
326
331
{
327
- throw new MongoException ($ result ['errmsg ' ], $ result ['errno ' ]);
332
+ throw new MongoException ($ result ['errmsg ' ], $ result ['code ' ]);
328
333
}
329
334
return $ result ['retval ' ];
330
335
}
@@ -461,11 +466,36 @@ public function profiler_stop($token)
461
466
call_user_func ($ this ->_stop_callback , $ token );
462
467
}
463
468
464
-
465
- /** @return Mongo */
469
+
470
+ /**
471
+ * Get the MongoClient|Mongo instance directly
472
+ * @return MongoClient|Mongo
473
+ */
466
474
public function connection ()
467
475
{
468
476
return $ this ->_connection ;
469
477
}
470
478
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
+
471
501
}
0 commit comments