databasesInfo, databaseNames

This commit is contained in:
Scott R. Parish 2010-01-22 06:23:39 -06:00
parent 6bf32ab9eb
commit 5ae3c4c2ae

View file

@ -29,6 +29,7 @@ module Database.MongoDB
Connection, Connection,
connect, connectOnPort, conClose, disconnect, dropDatabase, connect, connectOnPort, conClose, disconnect, dropDatabase,
serverInfo, serverInfo,
databasesInfo, databaseNames,
-- * Database -- * Database
Database, MongoDBCollectionInvalid, Database, MongoDBCollectionInvalid,
ColCreateOpt(..), ColCreateOpt(..),
@ -63,6 +64,7 @@ import qualified Data.ByteString.Lazy.UTF8 as L8
import Data.Int import Data.Int
import Data.IORef import Data.IORef
import qualified Data.List as List import qualified Data.List as List
import qualified Data.Map as Map
import Data.Maybe import Data.Maybe
import Data.Typeable import Data.Typeable
import Database.MongoDB.BSON as BSON import Database.MongoDB.BSON as BSON
@ -96,6 +98,19 @@ connectOnPort host port = do
conClose :: Connection -> IO () conClose :: Connection -> IO ()
conClose = hClose . cHandle conClose = hClose . cHandle
-- | Information about the databases on the server.
databasesInfo :: Connection -> IO BsonDoc
databasesInfo c = do
runCommand c (s2L "admin") $ toBsonDoc [("listDatabases", toBson (1::Int))]
-- | Return a list of database names on the server.
databaseNames :: Connection -> IO [Database]
databaseNames c = do
info <- databasesInfo c
let (BsonArray dbs) = fromJust $ Map.lookup (s2L "databases") info
names = catMaybes $ List.map (Map.lookup (s2L "name") . fromBson) dbs
return $ List.map fromBson (names::[BsonValue])
-- | Alias for 'conClose' -- | Alias for 'conClose'
disconnect :: Connection -> IO () disconnect :: Connection -> IO ()
disconnect = conClose disconnect = conClose