Beispiel #1
0
        /// <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 url = _server + "/" + _database + "/_temp_view";

            if (startkey != null)
            {
                url += "?startkey=" + Uri.EscapeDataString(startkey);
            }
            if (endkey != null)
            {
                if (startkey == null)
                {
                    url += "?";
                }
                else
                {
                    url += "&";
                }
                url += "endkey=" + Uri.EscapeDataString(endkey);
            }

            return(CouchDbRequest.Execute(url, "POST", viewdef, "application/json"));
        }
Beispiel #2
0
        /// <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, "DELETE");
        }
        /// <summary>
        /// Delete a database
        /// </summary>
        public static void DeleteDatabase(string server, string db)
        {
            var result = CouchDbRequest.Execute(server + "/" + db, "DELETE");

            if (result.Trim() != "{\"ok\":true}")
            {
                throw new ApplicationException("Failed to delete database: " + result);
            }
        }
        /// <summary>
        /// Delete a database
        /// </summary>
        public static void DeleteDatabase(string server, string db)
        {
            var result = CouchDbRequest.Execute(server, db, HttpMethod.Delete);

            if (result.Trim() != "{\"ok\":true}")
            {
                throw new Exception("Failed to delete database: " + result); // was a ApplicationException, will be added back netstandard2.0
            }
        }
Beispiel #5
0
        /// <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, "GET");

            var dictionary = JsonConvert.DeserializeObject <IDictionary <string, string> >(result);

            // The document count is a field within...
            return(Int32.Parse(dictionary["doc_count"]));
        }
Beispiel #6
0
        public string ExecTempView(string url, string map, string reduce)
        {
            var viewdef = "{ \"map\":\"" + map + "\"";

            if (reduce != null)
            {
                viewdef += ",\"reduce\":\"" + reduce + "\"";
            }
            viewdef += "}";

            return(CouchDbRequest.Execute(url, "POST", viewdef, "application/json"));
        }
Beispiel #7
0
        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"));
        }
Beispiel #8
0
        /// <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, "PUT", item, "application/json");
        }
Beispiel #9
0
        /// <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, "POST", item, "application/json");
        }
Beispiel #10
0
        private string Get(string id)
        {
            var url = _server + "/" + _database + "/" + id;

            return(CouchDbRequest.Execute(url, "GET"));
        }
 /// <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 static IList <string> GetDatabases(string server)
 {
     return(JsonConvert.DeserializeObject <IList <string> >(
                CouchDbRequest.Execute(server + "/_all_dbs", "GET")
                ));
 }
 /// <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 static IList <string> GetDatabases(string server)
 {
     return(JsonConvert.DeserializeObject <IList <string> >(
                CouchDbRequest.Execute(server, "_all_dbs", HttpMethod.Get)
                ));
 }
Beispiel #13
0
        private string Get(string id)
        {
            var uri = _database + "/" + id;

            return(CouchDbRequest.Execute(_server, uri, HttpMethod.Get));
        }