-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathCouchDbClient.cs
More file actions
214 lines (178 loc) · 7.18 KB
/
Copy pathCouchDbClient.cs
File metadata and controls
214 lines (178 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Inspired by SharpCouch and tweaked for our specific needs
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
namespace SharpRepository.CouchDbRepository
{
/// <summary>
/// Used to return metadata about a document.
/// </summary>
public class DocInfo
{
public string Id;
public string Revision;
}
/// <summary>
/// A simple wrapper class for the CouchDB HTTP API. No
/// initialisation is necessary, just create an instance and
/// call the appropriate methods to interact with CouchDB.
/// All methods throw exceptions when things go wrong.
/// </summary>
public class CouchDbClient<T> where T : class, new()
{
private readonly string _server;
private readonly string _database;
public CouchDbClient()
{
}
public CouchDbClient(string server, string database)
{
_server = server;
_database = database;
}
public string Server
{
get { return _server; }
}
public string Database
{
get { return _database; }
}
public string DatabaseUrl
{
get { return _server + "/" + _database; }
}
/// <summary>
/// Get the document count for the given database.
/// </summary>
/// <returns>The number of documents in the database</returns>
public int CountDocuments()
{
// Get information about the database...
var result = CouchDbRequest.Execute(_server, _database, HttpMethod.Get);
var dictionary = JsonConvert.DeserializeObject<IDictionary<string, string>>(result);
// The document count is a field within...
return Int32.Parse(dictionary["doc_count"]);
}
/// <summary>
/// Get information on all the documents in the given database.
/// </summary>
/// <returns>An array of entitiy instances</returns>
public IList<T> GetAllDocuments()
{
var json = ExecMapReduce(_server, _database + "/_temp_view", "function (doc) {emit(doc._id, doc);}", null);
var res = JObject.Parse(json);
var rows = res["rows"];
return rows.Select(row => row["value"].ToObject<T>()).ToList();
}
public string ExecMapReduce(string url, string uri, string map, string reduce)
{
var viewdef = "{ \"map\":\"" + map + "\"";
if (reduce != null)
viewdef += ",\"reduce\":\"" + reduce + "\"";
viewdef += "}";
return CouchDbRequest.Execute(url, uri, HttpMethod.Post, viewdef, "application/json");
}
/// <summary>
/// Execute a temporary view and return the results.
/// </summary>
/// <param name="map">The javascript map function</param>
/// <param name="reduce">The javascript reduce function or
/// null if not required</param>
/// <param name="startkey">The startkey or null not to use</param>
/// <param name="endkey">The endkey or null not to use</param>
/// <returns>The result (JSON format)</returns>
public string ExecTempView(string map, string reduce ,string startkey, string endkey)
{
// Generate the JSON view definition from the supplied
// map and optional reduce functions...
var viewdef = "{ \"map\":\"" + map + "\"";
if (reduce != null)
viewdef += ",\"reduce\":\"" + reduce + "\"";
viewdef += "}";
var uri = _database + "/_temp_view";
if(startkey != null)
{
uri += "?startkey=" + Uri.EscapeDataString(startkey);
}
if(endkey != null)
{
if (startkey == null)
uri += "?";
else
uri += "&";
uri += "endkey=" + Uri.EscapeDataString(endkey);
}
return CouchDbRequest.Execute(_server, uri, HttpMethod.Post, viewdef, "application/json");
}
/// <summary>
/// Create a new document. If the document has no ID field,
/// it will be assigned one by the server.
/// </summary>
/// <param name="entity">The item to store in the database.</param>
public void CreateDocument(T entity, string id)
{
var item = JsonConvert.SerializeObject(entity);
// add the _id so that it is the same as the PK of the entity
// this is a crappy way of doing it I think, but just trying to get it to work for now
if (!String.IsNullOrEmpty(id))
{
item = "{\"_id\":\"" + id + "\"," + item.Substring(1);
}
CouchDbRequest.Execute(_server, _database, HttpMethod.Post, item, "application/json");
}
public string GetLatestRevision(string id)
{
var json = Get(id);
var obj = JObject.Parse(json);
return obj.Value<string>("_rev");
}
/// <summary>
/// Updates a document.
/// </summary>
/// <param name="entity">The item to store in the database.</param>
/// <param name="id">The document ID.</param>
public void UpdateDocument(T entity, string id)
{
var item = JsonConvert.SerializeObject(entity);
//needs the revision for updates and deletes, this isn't a great way because it's an extra API call, but it is what it is for now, just want to get it working then refactor
var rev = GetLatestRevision(id);
// add the _id so that it is the same as the PK of the entity
// this is a crappy way of doing it I think, but just trying to get it to work for now
item = "{\"_id\":\"" + id + "\",\"_rev\":\"" + rev + "\"," + item.Substring(1);
CouchDbRequest.Execute(_server, _database + "/" + id, HttpMethod.Put, item, "application/json");
}
/// <summary>
/// Get a document.
/// </summary>
/// <param name="id">The document ID.</param>
/// <returns>The document contents (JSON)</returns>
public T GetDocument(string id)
{
var json = Get(id);
if (String.IsNullOrEmpty(json))
{
return null;
}
return JsonConvert.DeserializeObject<T>(json);
}
private string Get(string id)
{
var uri = _database + "/" + id;
return CouchDbRequest.Execute(_server, uri, HttpMethod.Get);
}
/// <summary>
/// Delete a document.
/// </summary>
/// <param name="id">The document ID.</param>
public void DeleteDocument(string id)
{
//needs the revision for updates and deletes, this isn't a great way because it's an extra API call, but it is what it is for now, just want to get it working then refactor
var rev = GetLatestRevision(id);
CouchDbRequest.Execute(_server, _database + "/" + id + "?rev=" + rev, HttpMethod.Delete);
}
}
}