@@ -22,10 +22,24 @@ class DB
2222 # collection that already exists, raises an error.
2323 #
2424 # Strict mode is disabled by default, but enabled (+true+) at any time.
25- attr_writer :strict
25+ #
26+ # @deprecated Support for strict mode has been deprecated and will be
27+ # removed in version 2.0 of the driver.
28+ def strict = ( value )
29+ unless ENV [ 'TEST_MODE' ]
30+ warn "Support for strict mode has been deprecated and will be " +
31+ "removed in version 2.0 of the driver."
32+ end
33+ @strict = value
34+ end
2635
2736 # Returns the value of the +strict+ flag.
28- def strict? ; @strict ; end
37+ #
38+ # @deprecated Support for strict mode has been deprecated and will be
39+ # removed in version 2.0 of the driver.
40+ def strict?
41+ @strict
42+ end
2943
3044 # The name of the database and the local write concern options.
3145 attr_reader :name , :write_concern
@@ -272,20 +286,19 @@ def collections_info(coll_name=nil)
272286 # @return [Mongo::Collection]
273287 def create_collection ( name , opts = { } )
274288 name = name . to_s
275- if collection_names . include? ( name )
276- if strict?
277- raise MongoDBError , "Collection #{ name } already exists. " +
278- "Currently in strict mode."
279- else
280- return Collection . new ( name , self , opts )
281- end
289+ if strict? && collection_names . include? ( name )
290+ raise MongoDBError , "Collection '#{ name } ' already exists. (strict=true)"
282291 end
283292
284- # Create a new collection.
285- oh = BSON ::OrderedHash . new
286- oh [ :create ] = name
287- doc = command ( oh . merge ( opts || { } ) )
288- return Collection . new ( name , self , :pk => @pk_factory ) if ok? ( doc )
293+ begin
294+ cmd = BSON ::OrderedHash . new
295+ cmd [ :create ] = name
296+ doc = command ( cmd . merge ( opts || { } ) )
297+ return Collection . new ( name , self , :pk => @pk_factory ) if ok? ( doc )
298+ rescue OperationFailure => e
299+ return Collection . new ( name , self , :pk => @pk_factory ) if e . message =~ /exists/
300+ raise e
301+ end
289302 raise MongoDBError , "Error creating collection: #{ doc . inspect } "
290303 end
291304
@@ -300,8 +313,7 @@ def create_collection(name, opts={})
300313 # @return [Mongo::Collection]
301314 def collection ( name , opts = { } )
302315 if strict? && !collection_names . include? ( name . to_s )
303- raise Mongo ::MongoDBError , "Collection #{ name } doesn't exist. " +
304- "Currently in strict mode."
316+ raise MongoDBError , "Collection '#{ name } ' doesn't exist. (strict=true)"
305317 else
306318 opts = opts . dup
307319 opts . merge! ( :pk => @pk_factory ) unless opts [ :pk ]
@@ -316,9 +328,12 @@ def collection(name, opts={})
316328 #
317329 # @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
318330 def drop_collection ( name )
319- return true unless collection_names . include? ( name . to_s )
320-
321- ok? ( command ( :drop => name ) )
331+ return false if strict? && !collection_names . include? ( name . to_s )
332+ begin
333+ ok? ( command ( :drop => name , :check_response => false ) )
334+ rescue OperationFailure => e
335+ false
336+ end
322337 end
323338
324339 # Run the getlasterror command with the specified replication options.
0 commit comments