Browse files

Merge /home/gokr/frukost/Divan into hacks

  • Loading branch information...
2 parents 2ef84b6 + e276bd0 commit a6486a4c728a6898cfafa25415ba20ffc79faa17 @gokr gokr committed Nov 16, 2009
Showing with 48 additions and 3 deletions.
  1. +1 −1 src/CouchDatabase.cs
  2. +46 −1 src/CouchGenericViewResult.cs
  3. +1 −1 src/CouchViewDefinition.cs
View
2 src/CouchDatabase.cs
@@ -270,7 +270,7 @@ public ICouchDocument WriteDocument(ICouchDocument document, bool batch)
}
JObject result =
Request(document.Id).Query(batch ? "?batch=ok" : null).Data(CouchDocument.WriteJson(document)).Put().Check("Failed to write document").Result();
- document.Id = result["id"].Value<string>(); // Not really neeed
+ document.Id = result["id"].Value<string>(); // Not really needed
document.Rev = result["rev"].Value<string>();
return document;
View
47 src/CouchGenericViewResult.cs
@@ -13,6 +13,21 @@ namespace Divan
public class CouchGenericViewResult : CouchViewResult
{
/// <summary>
+ /// Return all found values of given type
+ /// </summary>
+ /// <typeparam name="T">Type of value.</typeparam>
+ /// <returns>All found values.</returns>
+ public IList<T> Values<T>() where T : new()
+ {
+ var list = new List<T>();
+ foreach (JToken row in Rows())
+ {
+ list.Add(row["value"].Value<T>());
+ }
+ return list;
+ }
+
+ /// <summary>
/// Return all found values as documents of given type
/// </summary>
/// <typeparam name="T">Type of value.</typeparam>
@@ -22,6 +37,16 @@ public IList<T> ValueDocuments<T>() where T : ICanJson, new()
return RetrieveDocuments<T>("value");
}
+ /// <summary>
+ /// Return all ids in value as documents of given type.
+ /// </summary>
+ /// <typeparam name="T">Type of value.</typeparam>
+ /// <returns>All found documents.</returns>
+ public IList<T> ValueDocumentsWithIds<T>() where T : ICouchDocument, new()
+ {
+ return RetrieveDocumentsWithIds<T>("value");
+ }
+
public IList<T> ValueDocuments<T>(Func<T> ctor)
{
return RetrieveArbitraryDocuments<T>("value", ctor);
@@ -100,7 +125,27 @@ protected virtual IList<T> RetrieveDocuments<T>(string docOrValue) where T : ICa
}
return list;
}
-
+
+ protected virtual IList<T> RetrieveDocumentsWithIds<T>(string docOrValue) where T : ICouchDocument, new()
+ {
+ var list = new List<T>();
+ var found = new Dictionary<string, T>();
+ foreach (JToken row in Rows())
+ {
+ var ids = row[docOrValue].Value<JArray>();
+ foreach (JToken id in ids) {
+ var stringId = id.Value<string>();
+ if (!found.ContainsKey(stringId)) {
+ var doc = new T();
+ doc.Id = stringId;
+ found[stringId] = doc;
+ list.Add(doc);
+ }
+ }
+ }
+ return list;
+ }
+
protected virtual T RetrieveDocument<T>(string docOrValue) where T : ICanJson, new()
{
foreach (JToken row in Rows())
View
2 src/CouchViewDefinition.cs
@@ -76,7 +76,7 @@ public void ReadJson(JObject obj)
/// <summary>
/// Utility methods to make queries shorter.
/// </summary>
- public IList<T> Key<T>(string key) where T : ICouchDocument, new()
+ public IList<T> Key<T>(object key) where T : ICouchDocument, new()
{
return Query().Key(key).IncludeDocuments().GetResult().Documents<T>();
}

0 comments on commit a6486a4

Please sign in to comment.