renameCollection

This commit is contained in:
Scott R. Parish 2010-01-21 19:12:06 -06:00
parent 539c98bbaa
commit e8f6141529
2 changed files with 29 additions and 15 deletions

View file

@ -33,7 +33,7 @@ module Database.MongoDB
Database, MongoDBCollectionInvalid, Database, MongoDBCollectionInvalid,
ColCreateOpt(..), ColCreateOpt(..),
collectionNames, createCollection, dropCollection, collectionNames, createCollection, dropCollection,
runCommand, validateCollection, renameCollection, runCommand, validateCollection,
-- * Collection -- * Collection
Collection, FieldSelector, FullCollection, Collection, FieldSelector, FullCollection,
NumToSkip, NumToReturn, Selector, NumToSkip, NumToReturn, Selector,
@ -137,23 +137,11 @@ colCreateOptToBson (CCOMax m) = ("max", toBson m)
-- exists. -- exists.
createCollection :: Connection -> FullCollection -> [ColCreateOpt] -> IO () createCollection :: Connection -> FullCollection -> [ColCreateOpt] -> IO ()
createCollection c col opts = do createCollection c col opts = do
let (db, col') = splitFullCol col (db, col') <- validateCollectionName col
dbcols <- collectionNames c db dbcols <- collectionNames c db
case col `List.elem` dbcols of case col `List.elem` dbcols of
True -> throwColInvalid $ "Collection already exists: " ++ show col True -> throwColInvalid $ "Collection already exists: " ++ show col
False -> return () False -> return ()
case s2L ".." `List.elem` (L.group col) of
True -> throwColInvalid $ "Collection can't contain \"..\": " ++ show col
False -> return ()
case (c2w '$') `L.elem` col &&
not (s2L "oplog.$mail" `L.isPrefixOf` col' ||
s2L "$cmd" `L.isPrefixOf` col') of
True -> throwColInvalid $ "Collection can't contain '$': " ++ show col
False -> return ()
case L.head col == (c2w '.') || L.last col == (c2w '.') of
True -> throwColInvalid $
"Collection can't start or end with '.': " ++ show col
False -> return ()
let cmd = ("create", toBson col') : List.map colCreateOptToBson opts let cmd = ("create", toBson col') : List.map colCreateOptToBson opts
_ <- runCommand c db $ toBsonDoc cmd _ <- runCommand c db $ toBsonDoc cmd
return () return ()
@ -165,6 +153,16 @@ dropCollection c col = do
_ <- runCommand c db $ toBsonDoc [("drop", toBson col')] _ <- runCommand c db $ toBsonDoc [("drop", toBson col')]
return () return ()
-- | Rename a collection--first /FullCollection/ argument is the
-- existing name, the second is the new name. At the moment this command
-- can also be used to move a collection between databases.
renameCollection :: Connection -> FullCollection -> FullCollection -> IO ()
renameCollection c col newName = do
_ <- validateCollectionName col
_ <- runCommand c (s2L "admin") $ toBsonDoc [("renameCollection", toBson col),
("to", toBson newName)]
return ()
-- | Return a string of validation info about the collection. -- | Return a string of validation info about the collection.
-- --
-- Example output (note this probably can/will change with different -- Example output (note this probably can/will change with different
@ -708,3 +706,20 @@ randNum Connection { cRand = nsRef } = atomicModifyIORef nsRef $ \ns ->
s2L :: String -> L8.ByteString s2L :: String -> L8.ByteString
s2L = L8.fromString s2L = L8.fromString
validateCollectionName :: FullCollection -> IO (Database, Collection)
validateCollectionName col = do
let (db, col') = splitFullCol col
case s2L ".." `List.elem` (L.group col) of
True -> throwColInvalid $ "Collection can't contain \"..\": " ++ show col
False -> return ()
case (c2w '$') `L.elem` col &&
not (s2L "oplog.$mail" `L.isPrefixOf` col' ||
s2L "$cmd" `L.isPrefixOf` col') of
True -> throwColInvalid $ "Collection can't contain '$': " ++ show col
False -> return ()
case L.head col == (c2w '.') || L.last col == (c2w '.') of
True -> throwColInvalid $
"Collection can't start or end with '.': " ++ show col
False -> return ()
return (db, col')

1
TODO
View file

@ -43,7 +43,6 @@ MongoDB
* modify * modify
* replace * replace
* repsert * repsert
* rename
- index operations - index operations
* ensureIndex / index existance caching * ensureIndex / index existance caching
- misc operations - misc operations