[couchdb-glib] Added couchdb_document_delete and couchdb_document_set_revision functions



commit be92cd1eade2a25069cf31f7afded5315e12c884
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Wed Jun 24 17:14:27 2009 +0200

    Added couchdb_document_delete and couchdb_document_set_revision functions

 couchdb-glib/couchdb-document.c |   43 +++++++++++++++++++++++++++++++++++++-
 couchdb-glib/couchdb-glib.h     |    3 ++
 2 files changed, 44 insertions(+), 2 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index b375461..a5a6a7f 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -128,8 +128,8 @@ couchdb_document_put (CouchDBDocument *document,
 		JsonObject *object;
 
 		object = json_node_get_object (json_parser_get_root (parser));
-		couchdb_document_set_string_field (document, "_id", json_object_get_string_member (object, "id"));
-		couchdb_document_set_string_field (document, "_rev", json_object_get_string_member (object, "rev"));
+		couchdb_document_set_id (document, json_object_get_string_member (object, "id"));
+		couchdb_document_set_revision (document, json_object_get_string_member (object, "rev"));
 
 		g_object_unref (G_OBJECT (parser));
 
@@ -148,6 +148,34 @@ couchdb_document_put (CouchDBDocument *document,
 	return result;
 }
 
+gboolean
+couchdb_document_delete (CouchDBDocument *document, GError **error)
+{
+	const char *id, *revision;
+	char *url;
+	JsonParser *parser;
+	gboolean result = FALSE;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), FALSE);
+
+	id = couchdb_document_get_id (document);
+	revision = couchdb_document_get_revision (document);
+	if (!id || !revision) /* we can't remove a document without an ID and/or a REVISION */
+		return FALSE;
+
+	url = g_strdup_printf ("http://%s/%s/%s?rev=%s";, document->couchdb->hostname, document->dbname, id, revision);
+	parser = send_message_and_parse (document->couchdb, SOUP_METHOD_DELETE, url, NULL, error);
+	if (parser) {
+		g_object_unref (G_OBJECT (parser));
+
+		result = TRUE;
+	}
+
+	g_free (url);
+
+	return result;
+}
+
 const char *
 couchdb_document_get_id (CouchDBDocument *document)
 {
@@ -189,6 +217,17 @@ couchdb_document_get_revision (CouchDBDocument *document)
 	return NULL;
 }
 
+void
+couchdb_document_set_revision (CouchDBDocument *document, const char *revision)
+{
+	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+	g_return_if_fail (revision != NULL);
+
+	json_object_set_string_member (json_node_get_object (document->root_node),
+				       "_rev",
+				       revision);
+}
+
 gboolean
 couchdb_document_is_contact (CouchDBDocument *document)
 {
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index 20882d0..d8d270d 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -81,10 +81,13 @@ CouchDBDocument *couchdb_document_get (CouchDB *couchdb,
 gboolean         couchdb_document_put (CouchDBDocument *document,
 				       const char *dbname,
 				       GError **error);
+gboolean         couchdb_document_delete (CouchDBDocument *document, GError **error);
+
 const char      *couchdb_document_get_id (CouchDBDocument *document);
 void             couchdb_document_set_id (CouchDBDocument *document, const char *id);
 
 const char      *couchdb_document_get_revision (CouchDBDocument *document);
+void             couchdb_document_set_revision (CouchDBDocument *document, const char *revision);
 
 gboolean         couchdb_document_is_contact (CouchDBDocument *document);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]