[couchdb-glib] Add CouchdbDesignDocument class to deal with CouchDB design documents



commit 994a24e4cbdb254762a8bddf41420dd11ceb5475
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Mon May 24 17:19:09 2010 +0200

    Add CouchdbDesignDocument class to deal with CouchDB design documents

 couchdb-glib/Makefile.am               |    3 +
 couchdb-glib/couchdb-database.c        |   63 +++++++++++++++++++
 couchdb-glib/couchdb-database.h        |    3 +-
 couchdb-glib/couchdb-design-document.c |  106 ++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-design-document.h |   58 +++++++++++++++++
 couchdb-glib/couchdb-glib.h            |    1 +
 6 files changed, 232 insertions(+), 2 deletions(-)
---
diff --git a/couchdb-glib/Makefile.am b/couchdb-glib/Makefile.am
index f540b57..d8208ab 100644
--- a/couchdb-glib/Makefile.am
+++ b/couchdb-glib/Makefile.am
@@ -29,6 +29,7 @@ libcouchdb_glib_1_0_la_headers =	\
 	couchdb-credentials.h		\
 	couchdb-database-info.h		\
 	couchdb-database.h		\
+	couchdb-design-document.h	\
 	couchdb-document.h		\
 	couchdb-document-info.h		\
 	couchdb-glib.h			\
@@ -43,6 +44,7 @@ libcouchdb_glib_1_0_la_sources =	\
 	couchdb-credentials.c		\
 	couchdb-database-info.c		\
 	couchdb-database.c		\
+	couchdb-design-document.c	\
 	couchdb-document.c		\
 	couchdb-document-info.c		\
 	couchdb-session.c		\
@@ -72,6 +74,7 @@ h_DATA = 				\
 	couchdb-credentials.h		\
 	couchdb-database-info.h		\
 	couchdb-database.h		\
+	couchdb-design-document.h	\
 	couchdb-document.h		\
 	couchdb-document-info.h		\
 	couchdb-glib.h			\
diff --git a/couchdb-glib/couchdb-database.c b/couchdb-glib/couchdb-database.c
index e8cbc0d..d7d6f75 100644
--- a/couchdb-glib/couchdb-database.c
+++ b/couchdb-glib/couchdb-database.c
@@ -22,6 +22,7 @@
 #include <libsoup/soup-message.h>
 #include <libsoup/soup-uri.h>
 #include "couchdb-database.h"
+#include "couchdb-design-document.h"
 #include "couchdb-document-info.h"
 #include "couchdb-document.h"
 #include "couchdb-marshal.h"
@@ -277,6 +278,68 @@ couchdb_database_list_documents (CouchdbDatabase *database, GError **error)
 }
 
 /**
+ * couchdb_database_get_design_documents:
+ * @database: A #CouchdbDatabase object
+ * @error: Placeholder for error information
+ *
+ * Retrieve all design documents from the given database.
+ *
+ * Design documents are special documents (well, they are really normal documents in
+ * the CouchDB database, just with a special ID) that contain views' code, which are used
+ * to create queries on the database that are cached and so make access to the database
+ * much quicker.
+ *
+ * Return value: A list of #CouchdbDesignDocument 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_get_design_documents (CouchdbDatabase *database, GError **error)
+{
+	char *url;
+	JsonParser *parser;
+	JsonArray *rows;
+	GSList *doclist = NULL;
+
+	g_return_val_if_fail (COUCHDB_IS_DATABASE (database), NULL);
+
+	url = g_strdup_printf ("%s/%s/_all_docs?include_docs=true",
+			       couchdb_session_get_uri (database->priv->session),
+			       database->priv->dbname);
+	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;
+			const gchar *id;
+			CouchdbDesignDocument *document;
+
+			obj = json_array_get_object_element (rows, i);
+			if (!obj)
+				continue;
+
+			id = json_object_get_string_member (obj, "_id");
+			if (!g_str_has_prefix (id, "_design/"))
+				continue;
+
+			document = couchdb_design_document_new ();
+			if (document != NULL) {
+				couchdb_document_set_from_json_object (document, obj);
+				doclist = g_slist_append (doclist, document);
+			}
+		}
+	}
+
+	g_object_unref (G_OBJECT (parser));
+	g_free (url);
+
+	return doclist;
+}
+
+/**
  * couchdb_database_get_all_documents:
  * @database: A #CouchdbDatabase object
  * @error: Placeholder for error information
diff --git a/couchdb-glib/couchdb-database.h b/couchdb-glib/couchdb-database.h
index bb6a3bf..84c7dd8 100644
--- a/couchdb-glib/couchdb-database.h
+++ b/couchdb-glib/couchdb-database.h
@@ -1,10 +1,8 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2009-2010 Canonical Services Ltd (www.canonical.com)
- *               2009 Mikkel Kamstrup Erlandsen
  *
  * Authors: Rodrigo Moya <rodrigo moya canonical com>
- *          Mikkel Kamstrup Erlandsen <mikkel kamstrup gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU Lesser General Public
@@ -60,6 +58,7 @@ GType            couchdb_database_get_type (void);
 CouchdbDatabase *couchdb_database_new (CouchdbSession *session, const char *dbname);
 
 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);
 void             couchdb_database_free_document_list (GSList *doclist);
 
diff --git a/couchdb-glib/couchdb-design-document.c b/couchdb-glib/couchdb-design-document.c
new file mode 100644
index 0000000..a08fac4
--- /dev/null
+++ b/couchdb-glib/couchdb-design-document.c
@@ -0,0 +1,106 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009-2010 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "couchdb-design-document.h"
+
+G_DEFINE_TYPE(CouchdbDesignDocument, couchdb_design_document, COUCHDB_TYPE_DOCUMENT)
+
+static void
+couchdb_design_document_class_init (CouchdbDesignDocumentClass *klass)
+{
+}
+
+static void
+couchdb_design_document_init (CouchdbDesignDocument *document)
+{
+}
+
+/**
+ * couchdb_design_document_new:
+ *
+ * Create a new design document, to contain views' code.
+ *
+ * Return value: A new #CouchdbDesignDocument object.
+ */
+CouchdbDesignDocument *
+couchdb_design_document_new (void)
+{
+	return g_object_new (COUCHDB_TYPE_DESIGN_DOCUMENT, NULL);
+}
+
+/**
+ * couchdb_design_document_get_language:
+ * @document: A #CouchdbDesignDocument object
+ *
+ * Return the programming language in which the views on the given design
+ * document are written.
+ *
+ * Return value: A value representing the language the views are written in.
+ */
+CouchdbDesignDocumentLanguage
+couchdb_design_document_get_language (CouchdbDesignDocument *document)
+{
+	JsonObject *json_object;
+
+	g_return_val_if_fail (COUCHDB_IS_DESIGN_DOCUMENT (document), COUCHDB_DESIGN_DOCUMENT_LANGUAGE_UNKNOWN);
+
+	json_object = couchdb_document_get_json_object (COUCHDB_DOCUMENT (document));
+	if (json_object != NULL) {
+		const gchar *language;
+
+		language = json_object_get_string_member (json_object, "language");
+		if (g_strcmp0 (language, "javascript") == 0)
+			return COUCHDB_DESIGN_DOCUMENT_LANGUAGE_JAVASCRIPT;
+		else if (g_strcmp0 (language, "python") == 0)
+			return COUCHDB_DESIGN_DOCUMENT_LANGUAGE_PYTHON;
+	}
+
+	return COUCHDB_DESIGN_DOCUMENT_LANGUAGE_UNKNOWN;
+}
+
+/**
+ * couchdb_design_document_set_language:
+ * @document: A #CouchdbDesignDocument object
+ * @language: Language for the views in this design document
+ *
+ * Set the language used in the views contained in the given #CouchdbDesignDocument
+ * object.
+ */
+void
+couchdb_design_document_set_language (CouchdbDesignDocument *document,
+				      CouchdbDesignDocumentLanguage language)
+{
+	JsonObject *json_object;
+
+	g_return_if_fail (COUCHDB_IS_DESIGN_DOCUMENT (document));
+
+	json_object = couchdb_document_get_json_object (COUCHDB_DOCUMENT (document));
+	if (json_object != NULL) {
+		switch (language) {
+		case COUCHDB_DESIGN_DOCUMENT_LANGUAGE_JAVASCRIPT:
+			json_object_set_string_member (json_object, "language", "javascript");
+			break;
+		case COUCHDB_DESIGN_DOCUMENT_LANGUAGE_PYTHON:
+			json_object_set_string_member (json_object, "language", "python");
+			break;
+		}
+	}
+}
diff --git a/couchdb-glib/couchdb-design-document.h b/couchdb-glib/couchdb-design-document.h
new file mode 100644
index 0000000..29eca41
--- /dev/null
+++ b/couchdb-glib/couchdb-design-document.h
@@ -0,0 +1,58 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009-2010 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __COUCHDB_DESIGN_DOCUMENT_H__
+#define __COUCHDB_DESIGN_DOCUMENT_H__
+
+#include "couchdb-document.h"
+
+G_BEGIN_DECLS
+
+#define COUCHDB_TYPE_DESIGN_DOCUMENT                (couchdb_design_document_get_type ())
+#define COUCHDB_DESIGN_DOCUMENT(obj)                (G_TYPE_CHECK_INSTANCE_CAST ((obj), COUCHDB_TYPE_DESIGN_DOCUMENT, CouchdbDesignDocument))
+#define COUCHDB_IS_DESIGN_DOCUMENT(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COUCHDB_TYPE_DESIGN_DOCUMENT))
+#define COUCHDB_DESIGN_DOCUMENT_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST ((klass), COUCHDB_TYPE_DESIGN_DOCUMENT, CouchdbDesignDocumentClass))
+#define COUCHDB_IS_DESIGN_DOCUMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE ((klass), COUCHDB_TYPE_DESIGN_DOCUMENT))
+#define COUCHDB_DESIGN_DOCUMENT_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), COUCHDB_TYPE_DESIGN_DOCUMENT, CouchdbDesignDocumentClass))
+
+struct CouchdbDesignDocument {
+	CouchdbDocument parent;
+};
+
+struct CouchdbDesignDocumentClass {
+	CouchdbDocumentClass parent_class;
+};
+
+GType                         couchdb_design_document_get_type (void);
+
+typedef enum {
+	COUCHDB_DESIGN_DOCUMENT_LANGUAGE_UNKNOWN,
+	COUCHDB_DESIGN_DOCUMENT_LANGUAGE_JAVASCRIPT,
+	COUCHDB_DESIGN_DOCUMENT_LANGUAGE_PYTHON
+} CouchdbDesignDocumentLanguage;
+
+CouchdbDesignDocumentLanguage couchdb_design_document_get_language (CouchdbDesignDocument *document);
+void                          couchdb_design_document_set_language (CouchdbDesignDocument *document,
+								    CouchdbDesignDocumentLanguage language);
+
+G_END_DECLS
+
+#endif
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index fd214cc..ea8ae7c 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -29,6 +29,7 @@
 #include <couchdb-credentials.h>
 #include <couchdb-database-info.h>
 #include <couchdb-database.h>
+#include <couchdb-design-document.h>
 #include <couchdb-document.h>
 #include <couchdb-document-info.h>
 #include <couchdb-session.h>



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