New optional "strict" mode for databases.
This commit is contained in:
parent
bc61eacf03
commit
103d7e5c62
|
@ -39,6 +39,8 @@ http://github.com/geir/mongo-java-driver/tree/master.
|
|||
|
||||
== Release Notes
|
||||
|
||||
Added "strict" db attribute.
|
||||
|
||||
I plan to remove the auto-generation of _id primary keys.
|
||||
|
||||
If you ran tests using code before release
|
||||
|
@ -52,6 +54,8 @@ type
|
|||
|
||||
= To Do
|
||||
|
||||
* Add way to ask db if it is master or slave.
|
||||
|
||||
* Tests that prove that this driver's ObjectID and Geir's Java version do the
|
||||
same thing. (I've done so manually.)
|
||||
|
||||
|
|
|
@ -27,12 +27,20 @@ module XGen
|
|||
SYSTEM_INDEX_COLLECTION = "system.indexes"
|
||||
SYSTEM_COMMAND_COLLECTION = "$cmd"
|
||||
|
||||
# Strict mode means that trying to access a collection that does not
|
||||
# exist will raise an error. Strict mode is off (false) by default.
|
||||
attr_writer :strict
|
||||
|
||||
# Returns the value of the +strict+ flag.
|
||||
def strict?; @strict; end
|
||||
|
||||
attr_reader :name, :socket
|
||||
|
||||
def initialize(db_name, host, port)
|
||||
raise "Invalid DB name" if !db_name || (db_name && db_name.length > 0 && db_name.include?("."))
|
||||
@name, @host, @port = db_name, host, port
|
||||
@socket = TCPSocket.new(@host, @port)
|
||||
@strict = false
|
||||
end
|
||||
|
||||
def collection_names
|
||||
|
@ -47,9 +55,18 @@ module XGen
|
|||
query(SYSTEM_NAMESPACE_COLLECTION, Query.new(selector))
|
||||
end
|
||||
|
||||
# Create a collection. If +strict+ is false, will return existing or
|
||||
# new collection. If +strict+ is true, will raise an error if
|
||||
# collection +name+ does not already exist.
|
||||
def create_collection(name, options={})
|
||||
# First check existence
|
||||
return Collection.new(self, name) if collection_names.include?(name)
|
||||
if collection_names.include?(full_coll_name(name))
|
||||
if strict?
|
||||
raise "Collection #{name} already exists. Currently in strict mode."
|
||||
else
|
||||
return Collection.new(self, name)
|
||||
end
|
||||
end
|
||||
|
||||
# Create new collection
|
||||
oh = OrderedHash.new
|
||||
|
@ -66,11 +83,17 @@ module XGen
|
|||
Admin.new(self)
|
||||
end
|
||||
|
||||
# Return a collection. If +strict+ is false, will return existing or
|
||||
# new collection. If +strict+ is true, will raise an error if
|
||||
# collection +name+ already exists.
|
||||
def collection(name)
|
||||
# We do not implement the Java driver's optional strict mode, which
|
||||
# throws an exception if the collection does not exist.
|
||||
return Collection.new(self, name) if collection_names.include?(full_coll_name(name))
|
||||
if strict?
|
||||
raise "Collection #{name} doesn't exist. Currently in strict mode."
|
||||
else
|
||||
create_collection(name)
|
||||
end
|
||||
end
|
||||
|
||||
def drop_collection(name)
|
||||
coll = collection(name)
|
||||
|
|
|
@ -203,4 +203,47 @@ class DBAPITest < Test::Unit::TestCase
|
|||
assert_equal 1, rows.length
|
||||
assert_equal regex, rows[0]['b']
|
||||
end
|
||||
|
||||
def test_strict
|
||||
assert !@db.strict?
|
||||
@db.strict = true
|
||||
assert @db.strict?
|
||||
end
|
||||
|
||||
def test_strict_access_collection
|
||||
@db.strict = true
|
||||
begin
|
||||
@db.collection('does-not-exist')
|
||||
fail "expected exception"
|
||||
rescue => ex
|
||||
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
||||
ensure
|
||||
@db.strict = false
|
||||
@db.drop_collection('does-not-exist')
|
||||
end
|
||||
end
|
||||
|
||||
def test_strict_create_collection
|
||||
@db.drop_collection('foobar')
|
||||
@db.strict = true
|
||||
|
||||
begin
|
||||
@db.create_collection('foobar')
|
||||
assert true
|
||||
rescue => ex
|
||||
fail "did not expect exception \"#{ex}\""
|
||||
end
|
||||
|
||||
# Now the collection exists. This time we should see an exception.
|
||||
begin
|
||||
@db.create_collection('foobar')
|
||||
fail "expected exception"
|
||||
rescue => ex
|
||||
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
||||
ensure
|
||||
@db.strict = false
|
||||
@db.drop_collection('foobar')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue