Commits

Benoit Chesneau committed af765f8

include_docs is now possible, fix issue #20. now fetch_view return

[id] if value not given
{Id, Key, Value] -> if value only
{Id, key, Value, Doc} if doc is included.

  • Participants
  • Parent commits 8a65df8

Comments (0)

Files changed (4)

src/couchbeam_resource.erl

     "key"-> encode_value(V);
     "startkey" -> encode_value(V);
     "endkey" -> encode_value(V);
-    _ -> couchbeam_util:val(V)
+    _ -> 
+        couchbeam_util:quote_plus(V)
     end,
     V1.
 
     V1 = couchbeam:json_encode(V),
     couchbeam_util:quote_plus(binary_to_list(iolist_to_binary(V1))).
     
+    
 default_header(K, V, H) ->
     case proplists:is_defined(K, H) of
     true -> H;

src/couchbeam_util.erl

 quote_plus(Binary) when is_binary(Binary) ->
     quote_plus(binary_to_list(Binary));
 quote_plus(Float) when is_float(Float) ->
-    quote_plus(mochinum:digits(Float));
+    quote_plus(couchbeam_mochinum:digits(Float));
 quote_plus(String) ->
     quote_plus(String, []).
 

src/couchbeam_view.erl

         {Row1} = Row,
         Id = proplists:get_value(<<"id">>, Row1),
         Key = proplists:get_value(<<"key">>, Row1),
-        case proplists:get_value(<<"value">>, Row1) of
-            [] -> Id;
-            {Value} -> {Id, Key, {Value}};
-            Value when is_list(Value) -> {Id, Key, Value};
-            Value when is_integer(Value) -> {Id, Key, Value};
-            Value when is_binary(Value) -> {Id, Key, Value};
-            _ -> Id
+        Value = proplists:get_value(<<"value">>, Row1),
+        case proplists:get_value(<<"doc">>, Row1) of
+            undefined ->
+                case Value of 
+                    [] -> Id;
+                    _ -> {Id, Key, Value}
+                end;
+            Doc ->
+                {Id, Key, Value, Doc}
         end
     end || Row <- Rows],
     {TotalRows, Offset, Meta, Rows1};
 parse_view1(#view{view_cache=ViewCache, total_rows=_TotalRows,offset=_Offset, rows=_Rows, meta=_Meta}) ->
-    ViewCache.
+    ViewCache.
 %%! -pa ./ebin
 
 main(_) ->
-    etap:plan(12),
+    etap:plan(15),
     start_app(),
     case (catch test()) of
         ok ->
                 {[{<<"map">>,
                     <<"function (doc) {\n if (doc.type == \"test\") {\n emit(doc._id, doc);\n}\n}">>
                 }]}
+            },{<<"test2">>,
+                {[{<<"map">>,
+                    <<"function (doc) {\n if (doc.type == \"test2\") {\n emit(doc._id, null);\n}\n}">>
+                }]}
             }]}
-        }]},
+        }
+    ]},
+ 
     Doc = {[
         {<<"type">>, <<"test">>}
     ]},
         {<<"_id">>, <<"test3">>},
         {<<"type">>, <<"test">>}
     ]},
+    Doc4 = {[
+         {<<"_id">>, <<"test4">>},
+         {<<"type">>, <<"test2">>}
+    ]},
     couchbeam_db:save_doc(Db, Doc1),
     couchbeam_db:save_doc(Db, Doc2),
     couchbeam_db:save_doc(Db, Doc3),
+    couchbeam_db:save_doc(Db, Doc4),
     VResults = couchbeam_db:query_view(Db, {"couchbeam", "test"}, []),
     {T2, _, _, R2} = couchbeam_view:parse_view(VResults),
     etap:is(T2, 5, "view: total_rows in view ok"),
     etap:is(couchbeam_view:count(VResults5), 2, "view, keys : nb rows ok"),
     {Id, _, _} = couchbeam_view:first(VResults5),
     etap:is(Id, <<"test">>, "first key ok"),
+    VResults6 = couchbeam_db:query_view(Db, {"couchbeam", "test2"}, [{"key", <<"test4">>},{"include_docs", true}]),
+    etap:is(couchbeam_view:count(VResults6), 1, "view, include_docs: nb rows ok"),
+    
+    {_, _, _, [R31|_]} = couchbeam_view:parse_view(VResults6),
+    etap:ok(case R31 of
+        {_Id, _Key, _Value, _Doc} -> true;
+        _ -> false
+    end, "include_doc ok"),
+    {_, _, _, {Doc41}} = R31,
+    etap:is(proplists:get_value(<<"_id">>, Doc41), <<"test4">>, "include doc id ok"),
     ok.