My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SharpCouch - a simple wrapper class for the CouchDB HTTP API
// Copyright 2007 Ciaran Gultnieks
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Collections.Generic;
using LitJson;

namespace SharpCouch
{

/// <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 DB
{
public DB()
{
}

/// <summary>
/// Get a list of database on the server.
/// </summary>
/// <param name="server">The server URL</param>
/// <returns>A string array containing the database names
/// </returns>
public string[] GetDatabases(string server)
{
string result=DoRequest(server+"/_all_dbs","GET");

List<string> list=new List<string>();
if(result!="[]")
{
JsonData d=JsonMapper.ToObject(result);
foreach(JsonData db in d)
list.Add(db.ToString());
}
return(list.ToArray());
}

/// <summary>
/// Get the document count for the given database.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
/// <returns>The number of documents in the database</returns>
public int CountDocuments(string server,string db)
{
// Get information about the database...
string result=DoRequest(server+"/"+db,"GET");

// The document count is a field within...
JsonData d=JsonMapper.ToObject(result);
int count=int.Parse(d["doc_count"].ToString());
return count;
}

/// <summary>
/// Get information on all the documents in the given database.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
/// <returns>An array of DocInfo instances</returns>
public DocInfo[] GetAllDocuments(string server,string db)
{
string result=DoRequest(server+"/"+db+"/_all_docs","GET");

List<DocInfo> list=new List<DocInfo>();

JsonData d=JsonMapper.ToObject(result);
foreach(JsonData row in d["rows"])
{
DocInfo doc=new DocInfo();
doc.ID=row["id"].ToString();
doc.Revision=(row["value"])["rev"].ToString();
list.Add(doc);
}
return list.ToArray();
}

/// <summary>
/// Create a new database.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
public void CreateDatabase(string server,string db)
{
string result=DoRequest(server+"/"+db,"PUT");
if(result!="{\"ok\":true}")
throw new ApplicationException("Failed to create database: "+result);
}

/// <summary>
/// Delete a database
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The name of the database to delete</param>
public void DeleteDatabase(string server,string db)
{
string result=DoRequest(server+"/"+db,"DELETE");
if(result!="{\"ok\":true}")
throw new ApplicationException("Failed to delete database: "+result);
}

/// <summary>
/// Execute a temporary view and return the results.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
/// <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 server,string db,string map,string reduce,string startkey,string endkey)
{
// Generate the JSON view definition from the supplied
// map and optional reduce functions...
string viewdef="{ \"map\":\""+map+"\"";
if(reduce!=null)
viewdef+=",\"reduce\":\""+reduce+"\"";
viewdef+="}";

string url=server+"/"+db+"/_temp_view";
if(startkey!=null)
{
url+="?startkey="+HttpUtility.UrlEncode(startkey);
}
if(endkey!=null)
{
if(startkey==null) url+="?"; else url+="&";
url+="endkey="+HttpUtility.UrlEncode(endkey);
}
return DoRequest(url,"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="server">The server URL</param>
/// <param name="db">The database name</param>
/// <param name="content">The document contents (JSON).</param>
public void CreateDocument(string server,string db,string content)
{
DoRequest(server+"/"+db,"POST",content,"application/json");
}

/// <summary>
/// Get a document.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
/// <param name="docid">The document ID.</param>
/// <returns>The document contents (JSON)</returns>
public string GetDocument(string server,string db,string docid)
{
return DoRequest(server+"/"+db+"/"+docid,"GET");
}

/// <summary>
/// Get a document.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
/// <param name="docid">The document ID.</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 document contents (JSON)</returns>
public string GetDocument(string server,string db,string docid,string startkey,string endkey)
{
string url=server+"/"+db+"/"+docid;
if(startkey!=null)
{
url+="?startkey="+HttpUtility.UrlEncode(startkey);
}
if(endkey!=null)
{
if(startkey==null) url+="?"; else url+="&";
url+="endkey="+HttpUtility.UrlEncode(endkey);
}
return DoRequest(url,"GET");
}


/// <summary>
/// Delete a document.
/// </summary>
/// <param name="server">The server URL</param>
/// <param name="db">The database name</param>
/// <param name="docid">The document ID.</param>
public void DeleteDocument(string server,string db,string docid)
{
DoRequest(server+"/"+db+"/"+docid,"DELETE");
}

/// <summary>
/// Internal helper to make an HTTP request and return the
/// response. Throws an exception in the event of any kind
/// of failure. Overloaded - use the other version if you
/// need to post data with the request.
/// </summary>
/// <param name="url">The URL</param>
/// <param name="method">The method, e.g. "GET"</param>
/// <returns>The server's response</returns>
private string DoRequest(string url,string method)
{
return DoRequest(url,method,null,null);
}

/// <summary>
/// Internal helper to make an HTTP request and return the
/// response. Throws an exception in the event of any kind
/// of failure. Overloaded - use the other version if no
/// post data is required.
/// </summary>
/// <param name="url">The URL</param>
/// <param name="method">The method, e.g. "GET"</param>
/// <param name="postdata">Data to be posted with the request,
/// or null if not required.</param>
/// <param name="contenttype">The content type to send, or null
/// if not required.</param>
/// <returns>The server's response</returns>
private string DoRequest(string url,string method,string postdata,string contenttype)
{
HttpWebRequest req=WebRequest.Create(url) as HttpWebRequest;
req.Method=method;
// Yuk - set an infinite timeout on this for now, because
// executing a temporary view (for example) can take a very
// long time...
req.Timeout=System.Threading.Timeout.Infinite;
if(contenttype!=null)
req.ContentType=contenttype;

if(postdata!=null)
{
byte[] bytes=UTF8Encoding.UTF8.GetBytes(postdata.ToString());
req.ContentLength=bytes.Length;
using(Stream ps = req.GetRequestStream())
{
ps.Write(bytes, 0, bytes.Length);
}
}

HttpWebResponse resp=req.GetResponse() as HttpWebResponse;
string result;
using(StreamReader reader=new StreamReader(resp.GetResponseStream()))
{
result=reader.ReadToEnd();
}
return result;
}

}
}

Change log

r9 by ciaran.gultnieks on Aug 5, 2008   Diff
Changes to the GUI project to work with
CouchDB 0.8/0.9 and the earlier library
updates
Go to: 
Project members, sign in to write a code review

Older revisions

r8 by ciaran.gultnieks on Aug 4, 2008   Diff
Changes for compatibility with CouchDB
0.9 from trunk
r7 by ciaran.gultnieks on Sep 30, 2007   Diff
Added additional GetDocument overload
allowing startkey and endkey to be
specified, for use when getting
indexed views, and added proper
URLEncoding of startkey/endkey usage
...
r6 by ciaran.gultnieks on Sep 16, 2007   Diff
Found a more efficient way of getting
the document count for a database
All revisions of this file

File info

Size: 9408 bytes, 285 lines
Powered by Google Project Hosting