[couchdb-glib] Added the rest of the views API



commit 0500d37ebc41d299bd45279f37aecc83a40cdd3f
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Mon May 24 18:07:53 2010 +0200

    Added the rest of the views API

 couchdb-glib/couchdb-database.c        |   91 +++++++++++++++++++++++-
 couchdb-glib/couchdb-database.h        |    4 +
 couchdb-glib/couchdb-design-document.c |  121 ++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-design-document.h |    9 +++
 4 files changed, 223 insertions(+), 2 deletions(-)
---
diff --git a/couchdb-glib/couchdb-database.c b/couchdb-glib/couchdb-database.c
index 5cbc2e7..3f0d5b5 100644
--- a/couchdb-glib/couchdb-database.c
+++ b/couchdb-glib/couchdb-database.c
@@ -380,10 +380,11 @@ couchdb_database_get_all_documents (CouchdbDatabase *database, GError **error)
 			if (!obj)
 				continue;
 
-			if (g_str_has_prefix (json_object_get_string_member (obj, "_id"),
+			if (g_str_has_prefix (json_object_get_string_member (obj, "id"),
 					      "_design/")) {
 				document = COUCHDB_DOCUMENT (couchdb_design_document_new ());
-				couchdb_document_set_from_json_object (document, obj);
+				couchdb_document_set_from_json_object (document,
+								       json_object_get_object_member (obj, "doc"));
 			} else {
 				if (COUCHDB_DATABASE_GET_CLASS (database)->create_document_from_json != NULL) {
 					document = COUCHDB_DATABASE_GET_CLASS (database)->create_document_from_json (
@@ -406,6 +407,92 @@ couchdb_database_get_all_documents (CouchdbDatabase *database, GError **error)
 }
 
 /**
+ * couchdb_database_execute_view:
+ * @database: A #CouchdbDatabase object
+ * @design_doc: Name of the design document where the view to execute is
+ * @view_name: Name of the view to execute
+ * @error: Placeholder for error information
+ *
+ * Run a view on the database to retrieve documents. For
+ * each document found in the database, a #CouchdbDocument object is returned
+ * on the list, which represents the document's contents as found on the
+ * underlying database.
+ *
+ * Return value: a list of #CouchdbDocument objects, or NULL if there are none
+ * or there was an error (in which case the error argument will contain information
+ * about the error). Once no longer needed, the list should be freed by calling
+ * #couchdb_database_free_document_list.
+ */
+GSList *
+couchdb_database_execute_view (CouchdbDatabase *database,
+			       const char *design_doc,
+			       const char *view_name,
+			       GError **error)
+{
+	char *url;
+	JsonParser *parser;
+	JsonArray *rows;
+	GSList *doclist = NULL;
+
+	g_return_val_if_fail (COUCHDB_IS_DATABASE (database), NULL);
+
+	if (g_str_has_prefix (design_doc, "_design/")) {
+		url = g_strdup_printf ("%s/%s/%s/_view/%s",
+				       couchdb_session_get_uri (database->priv->session),
+				       database->priv->dbname,
+				       design_doc,
+				       view_name);
+	} else {
+		url = g_strdup_printf ("%s/%s/_design/%s/_view/%s",
+				       couchdb_session_get_uri (database->priv->session),
+				       database->priv->dbname,
+				       design_doc,
+				       view_name);
+	}
+
+	parser = json_parser_new ();
+
+	rows = call_all_docs_uri (database->priv->session, url, parser, error);
+	if (rows != NULL) {
+		gint i;
+		for (i = 0; i < json_array_get_length (rows); i++) {
+			JsonObject *obj;
+			CouchdbDocument *document = NULL;
+
+			obj = json_array_get_object_element (rows, i);
+			if (!obj)
+				continue;
+
+			if (g_str_has_prefix (json_object_get_string_member (obj, "id"),
+					      "_design/")) {
+				document = COUCHDB_DOCUMENT (couchdb_design_document_new ());
+				couchdb_document_set_from_json_object (document,
+								       json_object_get_object_member (obj, "value"));
+			} else {
+				if (COUCHDB_DATABASE_GET_CLASS (database)->create_document_from_json != NULL) {
+					document = COUCHDB_DATABASE_GET_CLASS (database)->create_document_from_json (
+						database,
+						json_object_get_object_member (obj, "value"));
+				} else {
+					document = couchdb_document_new_from_json_object (
+						json_object_get_object_member (obj, "value"));
+				}
+			}
+
+			couchdb_document_set_id (document, json_object_get_string_member (obj, "id"));
+
+			if (document != NULL)
+				doclist = g_slist_append (doclist, document);
+		}
+	}
+
+	g_object_unref (G_OBJECT (parser));
+	g_free (url);
+
+	return doclist;
+}
+
+/**
  * couchdb_session_free_document_list:
  * @doclist: A list of #CouchdbDocumentInfo or #CouchdbDocument objects, as returned by
  * #couchdb_database_list_documents or #couchdb_database_get_all_documents
diff --git a/couchdb-glib/couchdb-database.h b/couchdb-glib/couchdb-database.h
index 042cc2f..47eeb17 100644
--- a/couchdb-glib/couchdb-database.h
+++ b/couchdb-glib/couchdb-database.h
@@ -61,6 +61,10 @@ CouchdbDatabase *couchdb_database_new (CouchdbSession *session, const char *dbna
 GSList          *couchdb_database_list_documents (CouchdbDatabase *database, GError **error);
 GSList          *couchdb_database_get_design_documents (CouchdbDatabase *database, GError **error);
 GSList          *couchdb_database_get_all_documents (CouchdbDatabase *database, GError **error);
+GSList          *couchdb_database_execute_view (CouchdbDatabase *database,
+						const char *design_doc,
+						const char *view_name,
+						GError **error);
 void             couchdb_database_free_document_list (GSList *doclist);
 
 CouchdbDocument *couchdb_database_get_document (CouchdbDatabase *database,
diff --git a/couchdb-glib/couchdb-design-document.c b/couchdb-glib/couchdb-design-document.c
index 4c46a40..f5e6966 100644
--- a/couchdb-glib/couchdb-design-document.c
+++ b/couchdb-glib/couchdb-design-document.c
@@ -48,6 +48,127 @@ couchdb_design_document_new (void)
 }
 
 /**
+ * couchdb_design_document_list_views:
+ * @document: A #CouchdbDesignDocument object
+ *
+ * Return a list of the names of the views contained in the given
+ * #CouchdbDesignDocument object.
+ *
+ * Return value: A list of names of views. When no longer needed, this list
+ * should be freed by calling #couchdb_design_document_free_views_list.
+ */
+GSList *
+couchdb_design_document_list_views (CouchdbDesignDocument *document)
+{
+	JsonObject *json_object;
+	GSList *list = NULL;
+
+	g_return_val_if_fail (COUCHDB_IS_DESIGN_DOCUMENT (document), NULL);
+
+	json_object = couchdb_document_get_json_object (COUCHDB_DOCUMENT (document));
+	if (json_object != NULL) {
+		if (json_object_has_member (json_object, "views")) {
+			GList *members_list, *l;
+
+			members_list = json_object_get_members (
+				json_object_get_object_member (json_object, "views"));
+			for (l = members_list; l != NULL; l = l->next)
+				list = g_slist_append (list, g_strdup (l->data));
+
+			g_list_free (members_list);
+		}
+	}
+
+	return list;
+}
+
+/**
+ * couchdb_design_document_free_views_list:
+ * @list: A list of views' names, as returned by #couchdb_design_document_list_views
+ *
+ * Free the list of views' names returned by #couchdb_design_document_list_views.
+ */
+void
+couchdb_design_document_free_views_list (GSList *list)
+{
+	while (list != NULL) {
+		char *name = list->data;
+
+		list = g_slist_remove (list, name);
+		g_free (name);
+	}
+}
+
+/**
+ * couchdb_design_document_add_view:
+ * @document: A #CouchdbDesignDocument object
+ * @name: Name of the view
+ * @map_function: Source code for the map function
+ * @reduce_function: Source code for the reduce function
+ *
+ * Add a new view to the given #CouchdbDesignDocument object. If the view already exists,
+ * it will get replaced by the new one. To make the change permanent, the document
+ * must be saved to the database by calling #couchdb_database_put_document.
+ */
+void
+couchdb_design_document_add_view (CouchdbDesignDocument *document,
+				  const char *name,
+				  const char *map_function,
+				  const char *reduce_function)
+{
+	JsonObject *json_object;
+
+	g_return_if_fail (COUCHDB_IS_DESIGN_DOCUMENT (document));
+	g_return_if_fail (name != NULL);
+
+	json_object = couchdb_document_get_json_object (COUCHDB_DOCUMENT (document));
+	if (json_object != NULL) {
+		JsonObject *json_views, *this_view;
+
+		json_views = json_object_get_object_member (json_object, "views");
+		if (json_views == NULL) {
+			json_views = json_object_new ();
+			json_object_set_object_member (json_object, "views", json_views);
+		}
+
+		this_view = json_object_new ();
+		if (map_function != NULL)
+			json_object_set_string_member (this_view, "map", map_function);
+		if (reduce_function != NULL)
+			json_object_set_string_member (this_view, "reduce", reduce_function);
+
+		json_object_set_object_member (json_views, name, this_view);
+	}
+}
+
+/**
+ * couchdb_design_document_delete_view:
+ * @document: A #CouchdbDesignDocument object
+ * @name: Name of the view to remove
+ *
+ * Delete a view from the given #CouchdbDesignDocument object. To make the change permanent,
+ * the document must be saved to the database by calling #couchdb_database_put_document.
+ */
+void
+couchdb_design_document_delete_view (CouchdbDesignDocument *document,
+				     const char *name)
+{
+	JsonObject *json_object;
+
+	g_return_if_fail (COUCHDB_IS_DESIGN_DOCUMENT (document));
+	g_return_if_fail (name != NULL);
+
+	json_object = couchdb_document_get_json_object (COUCHDB_DOCUMENT (document));
+	if (json_object != NULL) {
+		JsonObject *json_views, *this_view;
+
+		json_views = json_object_get_object_member (json_object, "views");
+		if (json_views != NULL && json_object_has_member (json_views, name))
+			json_object_remove_member (json_views, name);
+	}
+}
+
+/**
  * couchdb_design_document_get_language:
  * @document: A #CouchdbDesignDocument object
  *
diff --git a/couchdb-glib/couchdb-design-document.h b/couchdb-glib/couchdb-design-document.h
index 642a0c1..a993b5f 100644
--- a/couchdb-glib/couchdb-design-document.h
+++ b/couchdb-glib/couchdb-design-document.h
@@ -45,6 +45,15 @@ GType                  couchdb_design_document_get_type (void);
 
 CouchdbDesignDocument *couchdb_design_document_new (void);
 
+GSList                *couchdb_design_document_list_views (CouchdbDesignDocument *document);
+void                   couchdb_design_document_free_views_list (GSList *list);
+void                   couchdb_design_document_add_view (CouchdbDesignDocument *document,
+							 const char *name,
+							 const char *map_function,
+							 const char *reduce_function);
+void                   couchdb_design_document_delete_view (CouchdbDesignDocument *document,
+							    const char *name);
+
 typedef enum {
 	COUCHDB_DESIGN_DOCUMENT_LANGUAGE_UNKNOWN,
 	COUCHDB_DESIGN_DOCUMENT_LANGUAGE_JAVASCRIPT,



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