make field selector a list rather then a BsonDoc

Requiring callers to construct {foo: 1, bar: 1} style field selectors
is just dumb. Instead we'll let them pass in the field selector as a list
of strings and then construct the above form automatically for them.
This commit is contained in:
Scott R. Parish 2010-01-18 12:43:05 -06:00
parent 35ef4acc41
commit 9f57eca933

View file

@ -43,6 +43,7 @@ import Data.Binary.Put
import Data.Bits import Data.Bits
import Data.ByteString.Char8 hiding (find) import Data.ByteString.Char8 hiding (find)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
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
@ -131,7 +132,7 @@ toOpcode n = throw $ MongoDBInternalError $ "Got unexpected Opcode: " ++ show n
type Collection = String type Collection = String
type Selector = BsonDoc type Selector = BsonDoc
type FieldSelector = BsonDoc type FieldSelector = [L8.ByteString]
type RequestID = Int32 type RequestID = Int32
type NumToSkip = Int32 type NumToSkip = Int32
type NumToReturn = Int32 type NumToReturn = Int32
@ -194,7 +195,7 @@ insertMany c col docs = do
{- | Open a cursor to find documents. If you need full functionality, {- | Open a cursor to find documents. If you need full functionality,
see 'query' -} see 'query' -}
find :: Connection -> Collection -> Selector -> IO Cursor find :: Connection -> Collection -> Selector -> IO Cursor
find c col sel = query c col [] 0 0 sel Nothing find c col sel = query c col [] 0 0 sel []
{- | Perform a query and return the result as a lazy list. Be sure to {- | Perform a query and return the result as a lazy list. Be sure to
understand the comments about using the lazy list given for 'allDocs'. -} understand the comments about using the lazy list given for 'allDocs'. -}
@ -206,7 +207,7 @@ quickFind' :: Connection -> Collection -> Selector -> IO [BsonDoc]
quickFind' c col sel = find c col sel >>= allDocs' quickFind' c col sel = find c col sel >>= allDocs'
query :: Connection -> Collection -> [QueryOpt] -> NumToSkip -> NumToReturn -> query :: Connection -> Collection -> [QueryOpt] -> NumToSkip -> NumToReturn ->
Selector -> Maybe FieldSelector -> IO Cursor Selector -> FieldSelector -> IO Cursor
query c col opts nskip ret sel fsel = do query c col opts nskip ret sel fsel = do
let h = cHandle c let h = cHandle c
@ -217,8 +218,8 @@ query c col opts nskip ret sel fsel = do
putI32 ret putI32 ret
put sel put sel
case fsel of case fsel of
Nothing -> putNothing [] -> putNothing
Just _ -> put fsel _ -> put $ toBsonDoc $ List.zip fsel $ repeat $ BsonInt32 1
(reqID, msg) <- packMsg c OP_QUERY body (reqID, msg) <- packMsg c OP_QUERY body
L.hPut h msg L.hPut h msg