Permalink
Browse files

rev 0.10

Signed-off-by: Alexander Dorofeev <aka.spin@gmail.com>
  • Loading branch information...
akaspin committed Jul 28, 2012
1 parent 02c9b9d commit a438d085a27d09b9dbcf2ba8fce8a99e541c005b
View
@@ -1,4 +1,4 @@
- 16.07.2012, Version 0.9.0 (Stable) * Upgrage dependencies. * Optimised view parsing (Speed and Fault-tolerant). * `couchView*'` -> `couchView*_` 27.04.2012, Version 0.8.0 (Stable) * Code refactorings. 03.04.2012, Version 0.8.0 (Stable) * Update conduit to 0.3/0.4. Mass code changes. 13.03.2012, Version 0.7.6 (Stable) * Refactorings and cleanups. * Path-independent `couch'`. 09.03.2012, Version 0.7.1 (Stable) * Add `couchPrefix` to `CouchConnection`. 09.03.2012, Version 0.7.0 (Stable) * Rename `mkParam` to `viewQP` and add param name to arguments. * Add view query helpers. 09.03.2012, Version 0.6.0 (Stable) * Remove `couchPutView_` * Rename `couchPutView'` to `couchPutView` * `couchViewPost` and `couchViewPost'` * Remove `quoteQueryParam` * `mkParam` * Update deps constraints 15.02.2012, Version 0.5.3 (Stable) * Fix views with "update_seq" 04.02.2012, Version 0.5.1 (Stable) * Docs. * Quote query parameter helper. 03.02.2012, Version 0.5.0 (Stable) * Expose parser-independent methods in main module. * Expose Implicit methods. * Remove ambiguous database parameter from connection. * Add unambiguous database parameter in all methods. 03.02.2012, Version 0.4.2 * Update dependencies contraints. 24.01.2012, Version 0.4.1 * `couchSecureDB` lost db argument. 24.01.2012, Version 0.4.0 * Detailed error type. * Throw exception on `304`. * Database API lost db argument. * `couchViewPut*` -> `couchPutView` 22.01.2012, Version 0.3.0.1 * Constraints in cabal 19.01.2012, Version 0.3.0.0 * Fix for new http-conduit types 16.01.2012, Version 0.2.1.1 * Fixes for couchViewPut and couchSecureDB. 16.01.2012, Version 0.2.1 * Secure DB. 16.01.2012, Version 0.2.0.1 * Avoid escaping path in couch' 16.01.2012, Version 0.2.0 * Low-lewel API * "Don't care" versions of couchPut. * Brain-free couchRev'. * All database methods ignores DB in connection. * couchPutDB' renamed to couchPutDB_. * Database replication. 14.01.2012, Version 0.1.3.0 * Safe version of couchViewPut 14.01.2012, Version 0.1.2.0 * Authentification 11.01.2012, Version 0.1.1.0
+ 28.07.2012, Version 0.10.0 (Stable) * Generalize query creation. * Move query parameters to `Database.CouchDB.Conduit.View.Query`. * Generalize row fields extraction. 16.07.2012, Version 0.9.0 (Stable) * Upgrage dependencies. * Optimised view parsing (Speed and Fault-tolerant). * `couchView*'` -> `couchView*_` 27.04.2012, Version 0.8.0 (Stable) * Code refactorings. 03.04.2012, Version 0.8.0 (Stable) * Update conduit to 0.3/0.4. Mass code changes. 13.03.2012, Version 0.7.6 (Stable) * Refactorings and cleanups. * Path-independent `couch'`. 09.03.2012, Version 0.7.1 (Stable) * Add `couchPrefix` to `CouchConnection`. 09.03.2012, Version 0.7.0 (Stable) * Rename `mkParam` to `viewQP` and add param name to arguments. * Add view query helpers. 09.03.2012, Version 0.6.0 (Stable) * Remove `couchPutView_` * Rename `couchPutView'` to `couchPutView` * `couchViewPost` and `couchViewPost'` * Remove `quoteQueryParam` * `mkParam` * Update deps constraints 15.02.2012, Version 0.5.3 (Stable) * Fix views with "update_seq" 04.02.2012, Version 0.5.1 (Stable) * Docs. * Quote query parameter helper. 03.02.2012, Version 0.5.0 (Stable) * Expose parser-independent methods in main module. * Expose Implicit methods. * Remove ambiguous database parameter from connection. * Add unambiguous database parameter in all methods. 03.02.2012, Version 0.4.2 * Update dependencies contraints. 24.01.2012, Version 0.4.1 * `couchSecureDB` lost db argument. 24.01.2012, Version 0.4.0 * Detailed error type. * Throw exception on `304`. * Database API lost db argument. * `couchViewPut*` -> `couchPutView` 22.01.2012, Version 0.3.0.1 * Constraints in cabal 19.01.2012, Version 0.3.0.0 * Fix for new http-conduit types 16.01.2012, Version 0.2.1.1 * Fixes for couchViewPut and couchSecureDB. 16.01.2012, Version 0.2.1 * Secure DB. 16.01.2012, Version 0.2.0.1 * Avoid escaping path in couch' 16.01.2012, Version 0.2.0 * Low-lewel API * "Don't care" versions of couchPut. * Brain-free couchRev'. * All database methods ignores DB in connection. * couchPutDB' renamed to couchPutDB_. * Database replication. 14.01.2012, Version 0.1.3.0 * Safe version of couchViewPut 14.01.2012, Version 0.1.2.0 * Authentification 11.01.2012, Version 0.1.1.0
* API Changes. `couchViewPut` moved to Database.CouchDB.Design and lost
language argument.
* Tests. 10.01.2012, Version 0.1.0.1
View
@@ -1,5 +1,5 @@
name: couchdb-conduit
-version: 0.9.0
+version: 0.10.0
cabal-version: >= 1.8
build-type: Simple
stability: Testing
@@ -54,7 +54,8 @@ library
Database.CouchDB.Conduit.Generic,
Database.CouchDB.Conduit.Implicit,
Database.CouchDB.Conduit.LowLevel,
- Database.CouchDB.Conduit.View
+ Database.CouchDB.Conduit.View,
+ Database.CouchDB.Conduit.View.Query
other-modules:
Database.CouchDB.Conduit.Internal.Doc,
Database.CouchDB.Conduit.Internal.Parser,
@@ -98,5 +99,6 @@ test-suite test
Database.CouchDB.Conduit.Test.Explicit,
Database.CouchDB.Conduit.Test.Generic,
Database.CouchDB.Conduit.Test.Util,
- Database.CouchDB.Conduit.Test.View
+ Database.CouchDB.Conduit.Test.View,
+ Database.CouchDB.Conduit.Test.Query
@@ -2,6 +2,10 @@
{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables, NoMonomorphismRestriction #-}
-- | Higher-level functions to interact with CouchDB views.
+--
+-- To automate creation of CouchDB Query Options see
+-- "Database.CouchDB.Conduit.View.Query"
+--
-- To manipulate views in design documents see
-- "Database.CouchDB.Conduit.Design"
@@ -14,54 +18,18 @@ module Database.CouchDB.Conduit.View
couchViewPost,
couchViewPost_,
rowValue,
-
- -- * View query parameters
- -- $view_query #view_query#
- viewQpUnit,
- viewQp,
- viewQpBS,
- viewQpInt,
- viewQpTrue,
- viewQpFalse,
-
- -- ** Rows
- -- $view_query_rows #view_query_rows#
- viewQpDescending,
- viewQpLimit,
- viewQpSkip,
- viewQpStartId,
- viewQpEndId,
-
- -- ** Map/Reduce
- -- $view_query_reduce #view_query_reduce#
- viewQpGroup,
- viewQpGroupLevel,
- viewQpReduceOff,
- viewQpReduceOn,
-
- -- ** Keys
- -- $view_query_keys #view_query_keys#
- viewQpKey,
- viewQpStartKey,
- viewQpEndKey,
- viewQpKeys,
-
- -- ** Control
- -- $view_query_control #view_query_control#
- viewQpIncludeDocs,
- viewQpInclusiveEnd
-)
- where
+ rowDoc,
+ rowField,
+) where
import Control.Exception.Lifted (throw)
-import Data.Monoid (mconcat)
import qualified Data.ByteString as B
-import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Char8 as BS8
+import qualified Data.Text as T
import qualified Data.HashMap.Lazy as M
-import qualified Data.HashMap.Strict as MS
import qualified Data.Aeson as A
+import Data.String.Conversions (cs, (<>))
import Data.Attoparsec
import qualified Data.Vector.Generic as V
@@ -79,180 +47,6 @@ import qualified Network.HTTP.Types as HT
import Database.CouchDB.Conduit.Internal.Connection
import Database.CouchDB.Conduit.LowLevel (couch, protect')
------------------------------------------------------------------------------
--- View query parameters
------------------------------------------------------------------------------
-
--- $view_query
--- For details see
--- <http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options>. Note,
--- because all options must be a proper URL encoded JSON, construction of
--- complex parameters can be very tedious. To simplify this, use following
--- functions.
-
--- | Not query parameter. Returns empty 'MS.HashMap'. Aeson will convert
--- this to @\{\}@ (JSON unit). This useful for @startkey@ and @endkey@.
-viewQpUnit :: MS.HashMap B.ByteString Bool
-viewQpUnit = MS.empty
-
--- | Make complex view query parameter.
---
--- > viewQP "key" (["a", "b"] :: [String])
--- > ("key", Just "[\"a\",\"b\"]")
---
--- It't just convert lazy 'BL.ByteString' from 'A.encode' to strict
--- 'B.ByteString'. For more efficient use specific functions.
-viewQp :: A.ToJSON a =>
- B.ByteString -- ^ Query parameter name
- -> a -- ^ Parameter
- -> HT.QueryItem
-viewQp n v = (n, Just $ mconcat . BL.toChunks . A.encode $ v)
-
--- | Make quoted 'B.ByteString' query parameter.
-viewQpBS :: B.ByteString -> B.ByteString -> HT.QueryItem
-viewQpBS n v = (n, Just $ "\"" `B.append` v `B.append` "\"")
-
--- | Make 'Int' query parameter.
-viewQpInt :: B.ByteString -> Int -> HT.QueryItem
-viewQpInt n v = (n, Just $ BS8.pack . show $ v)
-
--- | Make @...=true@ query parameter.
-viewQpTrue :: B.ByteString -> HT.QueryItem
-viewQpTrue n = (n, Just "true")
-
--- | Make @...=true@ query parameter.
-viewQpFalse :: B.ByteString -> HT.QueryItem
-viewQpFalse n = (n, Just "false")
-
--- $view_query_rows
--- Helpers for sorting and limiting rows.
-
--- $view_query_reduce
--- Helpers for Map/Reduce.
-
--- $view_query_keys
--- Helpers for quering by keys.
-
--- $view_query_control
--- Helpers for view behaviour.
-
----------------------------
--- Boolean query parameters
----------------------------
-
--- | Turn on descending sort of view results.
--- Shorthand for @viewQpTrue \"descending\"@.
-viewQpDescending :: HT.QueryItem
-viewQpDescending = viewQpTrue "descending"
-
--- | Turn on grouping.
--- Shorthand for @viewQpTrue \"group\"@.
-viewQpGroup :: HT.QueryItem
-viewQpGroup = viewQpTrue "group"
-
--- | Turn on inclusion docs in view results.
--- Shorthand for @viewQpTrue \"include_docs\"@.
-viewQpIncludeDocs :: HT.QueryItem
-viewQpIncludeDocs = viewQpTrue "include_docs"
-
--- | Turn off inclusion @endkey@ in view results.
--- Shorthand for @viewQpFalse \"inclusive_end\"@.
-viewQpInclusiveEnd :: HT.QueryItem
-viewQpInclusiveEnd = viewQpFalse "inclusive_end"
-
--- | Force off reduce if a reduce function is defined.
--- Shorthand for @viewQpFalse \"reduce\"@.
-viewQpReduceOff :: HT.QueryItem
-viewQpReduceOff = viewQpFalse "reduce"
-
--- | Force on reduce if a reduce function is not defined.
--- Shorthand for @viewQpTrue \"reduce\"@.
-viewQpReduceOn :: HT.QueryItem
-viewQpReduceOn = viewQpTrue "reduce"
-
---------------
--- Pure string
---------------
-
--- | Document id to start with.
--- Shorthand for @viewQpBS \"startkey_docid\"@.
-viewQpStartId ::
- Path -- ^ Document ID.
- -> HT.QueryItem
-viewQpStartId = viewQpBS "startkey_docid"
-
--- | Last document id to include in the output.
--- Shorthand for @viewQpBS \"endkey_docid\"@.
-viewQpEndId ::
- Path -- ^ Document ID.
- -> HT.QueryItem
-viewQpEndId = viewQpBS "endkey_docid"
-
---------------
--- Numeric
---------------
-
--- | Limit view rows.
--- Shorthand for @viewQpInt \"limit\"@.
-viewQpLimit ::
- Int -- ^ Max number of rows.
- -> HT.QueryItem
-viewQpLimit = viewQpInt "limit"
-
--- | Skip view rows.
--- Shorthand for @viewQpInt \"skip\"@.
-viewQpSkip ::
- Int -- ^ Number of rows to skip.
- -> HT.QueryItem
-viewQpSkip = viewQpInt "skip"
-
--- | Set grouping level.
--- Shorthand for @viewQpInt \"group_level\"@.
-viewQpGroupLevel ::
- Int -- ^ Grouping level.
- -> HT.QueryItem
-viewQpGroupLevel = viewQpInt "group_level"
-
---------------
--- Complex
---------------
-
--- | Make @key=...@ query parameter.
--- Shorthand for @viewQp \"key\"@.
-viewQpKey :: A.ToJSON a =>
- a -- ^ Key
- -> HT.QueryItem
-viewQpKey = viewQp "key"
-
--- | Row key to start with.
--- Shorthand for @viewQp "startkey"@
-viewQpStartKey :: A.ToJSON a =>
- a -- ^ Start Key value
- -> HT.QueryItem
-viewQpStartKey = viewQp "startkey"
-
--- | Row key to end with.
--- Shorthand for @viewQp "endkey"@
-viewQpEndKey :: A.ToJSON a =>
- a -- ^ Start Key value
- -> HT.QueryItem
-viewQpEndKey = viewQp "endkey"
-
-
--- | Make @keys=...@ query parameter.
--- Shorthand for @viewQp \"keys\"@.
---
--- Use it only with 'couchView' and 'couchView''. For large sets of @keys@
--- use 'couchViewPost' and 'couchViewPost''
-viewQpKeys :: A.ToJSON a =>
- a -- ^ Keys. Must be list or cortege.
- -> HT.QueryItem
-viewQpKeys = viewQp "keys"
-
------------------------------------------------------------------------------
--- Running
------------------------------------------------------------------------------
-
-- $run
-- In contrast to the functions of access to documents that are loaded into
-- memory entirely. 'couchView' and 'couchView'' combines the incredible power
@@ -317,7 +111,7 @@ couchView_ db design view q sink = do
--
-- > runCouch def $ do
-- > src <- couchViewPost "mydb" "mydesign" "myview"
--- > [("group", Just "true")]
+-- > (mkQuery [QPGroup])
-- > ["key1", "key2", "key3"]
-- > src $$ CL.mapM_ (liftIO . print)
couchViewPost :: (MonadCouch m, A.ToJSON a) =>
@@ -352,11 +146,20 @@ couchViewPost_ db design view q ks sink = do
-- | Conduit for extract \"value\" field from CouchDB view row.
rowValue :: Monad m => Conduit A.Object m A.Value
-rowValue = CL.mapM (\v -> case M.lookup "value" v of
- (Just o) -> return o
- _ -> throw $ CouchInternalError $ BS8.pack
- ("View row does not contain value: " ++ show v))
+rowValue = rowField "value"
+
+-- | Conduit for extract \"doc\" field from CouchDB view row.
+-- Use only with @include_docs=true@ query parameter.
+rowDoc :: Monad m => Conduit A.Object m A.Value
+rowDoc = rowField "doc"
+-- | Extract field from view row
+rowField :: Monad m => T.Text -> Conduit A.Object m A.Value
+rowField f = CL.mapM (\v -> case M.lookup f v of
+ (Just o) -> return o
+ _ -> throw $ CouchInternalError $
+ "View row does not contain \"" <> cs f <> "\": " <> cs (show v)
+ )
-----------------------------------------------------------------------------
-- Internal
-----------------------------------------------------------------------------
Oops, something went wrong.

0 comments on commit a438d08

Please sign in to comment.