[couchdb-glib] Added _put method



commit 600d978d3e698f3208b1aafd9e9d681c04b02dcc
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Mon Jun 22 13:30:31 2009 +0200

    Added _put method

 couchdb-glib/couchdb-document.c |   38 +++++++++++++++++++++++++++++++++++++-
 couchdb-glib/couchdb-glib.h     |    2 +-
 couchdb-glib/couchdb-types.c    |    6 +++---
 3 files changed, 41 insertions(+), 5 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 1389f71..bab28d2 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -99,9 +99,45 @@ couchdb_document_get (CouchDB *couchdb,
 }
 
 gboolean
-couchdb_document_put (CouchDBDocument *document)
+couchdb_document_put (CouchDBDocument *document, GError **error)
 {
+	char *url, *body;
+	const char *id;
+	JsonParser *parser;
+	gboolean result = FALSE;
+
 	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), FALSE);
+
+	if (!document->dbname) {
+		g_warning ("Document does not have a database associated to it");
+		return FALSE;
+	}
+
+	id = couchdb_document_get_string_field (document, "_id");
+	body = couchdb_document_to_string (document);
+	if (id) {
+		url = g_strdup_printf ("http://%s/%s/%s";, document->couchdb->hostname, document->dbname, id);
+		parser = send_message_and_parse (document->couchdb, SOUP_METHOD_PUT, url, body, error);
+	} else {
+		url = g_strdup_printf ("http://%s/%s/";, document->couchdb->hostname, document->dbname);
+		parser = send_message_and_parse (document->couchdb, SOUP_METHOD_POST, url, body, error);
+	}
+
+	if (parser) {
+		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"));
+
+		g_object_unref (G_OBJECT (parser));
+	}
+
+	/* free memory */
+	g_free (url);
+	g_free (body);
+
+	return result;
 }
 
 const char *
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index c87e083..cfd142b 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -76,7 +76,7 @@ CouchDBDocument *couchdb_document_get (CouchDB *couchdb,
 				       const char *dbname,
 				       const char *docid,
 				       GError **error);
-gboolean         couchdb_document_put (CouchDBDocument *document);
+gboolean         couchdb_document_put (CouchDBDocument *document, GError **error);
 const char      *couchdb_document_get_id (CouchDBDocument *document);
 void             couchdb_document_set_id (CouchDBDocument *document, const char *id);
 
diff --git a/couchdb-glib/couchdb-types.c b/couchdb-glib/couchdb-types.c
index 0d09ea9..859cf52 100644
--- a/couchdb-glib/couchdb-types.c
+++ b/couchdb-glib/couchdb-types.c
@@ -284,7 +284,7 @@ gboolean
 couchdb_struct_field_has_field (CouchDBStructField *sf, const char *field)
 {
 	g_return_val_if_fail (sf != NULL, FALSE);
-	g_return_val_if_fail (field != NULL, field);
+	g_return_val_if_fail (field != NULL, FALSE);
 
 	return json_object_has_member (sf->json_object, field);
 }
@@ -292,8 +292,8 @@ couchdb_struct_field_has_field (CouchDBStructField *sf, const char *field)
 void
 couchdb_struct_field_remove_field (CouchDBStructField *sf, const char *field)
 {
-	g_return_val_if_fail (sf != NULL, 0);
-	g_return_val_if_fail (field != NULL, 0);
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (field != NULL);
 
 	json_object_remove_member (sf->json_object, field);
 }



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