[couchdb-glib/wip/query-response] port all CouchdbSession methods to the new execute_query API



commit a0509128c0f5669387e7b8da2580beaf7db54de2
Author: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
Date:   Wed Jan 19 13:35:01 2011 +0100

    port all CouchdbSession methods to the new execute_query API

 couchdb-glib/couchdb-session.c |  320 ++++++++++++++++++++--------------------
 couchdb-glib/couchdb-session.h |   27 +++-
 2 files changed, 177 insertions(+), 170 deletions(-)
---
diff --git a/couchdb-glib/couchdb-session.c b/couchdb-glib/couchdb-session.c
index f0706f8..720d5e3 100644
--- a/couchdb-glib/couchdb-session.c
+++ b/couchdb-glib/couchdb-session.c
@@ -26,8 +26,6 @@
 #include <libsoup/soup-message.h>
 #include "couchdb-session.h"
 #include "couchdb-database.h"
-#include "couchdb-document.h"
-#include "couchdb-document-info.h"
 #include "couchdb-marshal.h"
 #include "utils.h"
 #include <string.h>
@@ -437,39 +435,41 @@ cleanup:
  * that exist in the CouchDB instance connected to. Once no longer needed, this
  * list can be freed by calling #couchdb_session_free_database_list.
  */
-GSList *
-couchdb_session_list_databases (CouchdbSession *session, GError **error)
+GList *
+couchdb_session_list_databases (CouchdbSession * self, GError ** error)
 {
-	char *url;
-	GSList *dblist = NULL;
-	JsonParser *parser;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	JsonObject *object;
+	JsonArray *array;
+	GList *names, *elements, *iter;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (self), NULL);
 
-	/* Prepare request */
-	url = g_strdup_printf ("%s/_all_dbs", session->priv->uri);
-	parser = json_parser_new ();
-	if (couchdb_session_send_message (session, SOUP_METHOD_GET, url, NULL, parser, error)) {
-		JsonNode *root_node;
-
-		root_node = json_parser_get_root (parser);
-		if (json_node_get_node_type (root_node) == JSON_NODE_ARRAY) {
-			GList *json_elements, *sl;
-
-			json_elements = json_array_get_elements (json_node_get_array (root_node));
-			for (sl = json_elements; sl != NULL; sl = sl->next) {
-				dblist = g_slist_append (
-					dblist,
-					g_strdup (json_node_get_string ((JsonNode *) sl->data)));
-			}
-		}		
+	names = NULL;
+
+	query = couchdb_query_new_for_path ("/_all_dbs");
+
+	response = couchdb_session_execute_query (self, query, error);
+	if (response == NULL) {
+		g_object_unref (query);
+
+		return NULL;
 	}
 
-	/* Free memory */
-	g_object_unref (G_OBJECT (parser));
-	g_free (url);
+	object = couchdb_response_get_json_object (response);
+	array = json_object_get_array_member (object, "array");
+
+	elements = json_array_get_elements (array);
+	for (iter = elements; iter != NULL; iter = g_list_next (iter)) {
+		names = g_list_prepend (names, json_node_dup_string (iter->data));
+	}
+	names = g_list_reverse (names);
 
-	return dblist;
+	g_object_unref (response);
+	g_object_unref (query);
+
+	return names;
 }
 
 /**
@@ -480,43 +480,40 @@ couchdb_session_list_databases (CouchdbSession *session, GError **error)
  *
  * Retrieve information about a given database.
  *
- * Return value: A #CouchdbDatabaseInfo object, whose API can be used to retrieve
- * all the information returned by CouchDB about this database.
+ * Return value: A #JsonObject containing informations about database.
  */
-CouchdbDatabaseInfo *
-couchdb_session_get_database_info (CouchdbSession *session, const char *dbname, GError **error)
+JsonObject *
+couchdb_session_get_database_info (CouchdbSession * self,
+				   const char * dbname,
+				   GError ** error)
 {
-	char *url;
-	JsonParser *parser;
-	CouchdbDatabaseInfo *result = NULL;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	JsonObject *object;
+	gchar *encoded_dbname;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (self), NULL);
 	g_return_val_if_fail (dbname != NULL, NULL);
 
-	url = g_strdup_printf ("%s/%s/", session->priv->uri, dbname);
-	parser = json_parser_new ();
-	if (couchdb_session_send_message (session, SOUP_METHOD_GET, url, NULL, parser, error)) {
-		JsonNode *root_node;
-
-		root_node = json_parser_get_root (parser);
-		if (json_node_get_node_type (root_node) == JSON_NODE_OBJECT) {
-			JsonObject *object = json_node_get_object (root_node);
-
-			result = couchdb_database_info_new (json_object_get_string_member (object, "db_name"),
-							    json_object_get_int_member (object, "doc_count"),
-							    json_object_get_int_member (object, "doc_del_count"),
-							    json_object_get_int_member (object, "update_seq"),
-							    json_object_get_int_member (object, "purge_seq"),
-							    json_object_get_boolean_member (object, "compact_running"),
-							    json_object_get_int_member (object, "disk_size"),
-							    json_object_get_int_member (object, "disk_format_version"),
-							    json_object_get_int_member (object, "instance_start_time"));
-		}
+	response = NULL;
+
+	encoded_dbname = couchdb_uri_encode (dbname);
+	query = couchdb_query_new_for_path (encoded_dbname);
+	g_free (encoded_dbname);
+
+	response = couchdb_session_execute_query (self, query, error);
+	if (!response) {
+		g_object_unref (query);
+
+		return NULL;
 	}
-	g_object_unref (G_OBJECT (parser));
-	g_free (url);
 
-	return result;
+	object = json_object_ref (couchdb_response_get_json_object (response));
+
+	g_object_unref (response);
+	g_object_unref (query);
+
+	return object;
 }
 
 /**
@@ -527,13 +524,13 @@ couchdb_session_get_database_info (CouchdbSession *session, const char *dbname,
  *
  * Get a #CouchdbDatabase object reference for an existing database.
  *
- * Return value: A #CouchdbDatabase object if the database exists on the specified
- * #CouchdbSession object, or NULL if the database does not exist.
+ * Return value: (transfer full): A #CouchdbDatabase object
+ * if the database exists, or %NULL if the database does not exist.
  */
 CouchdbDatabase *
 couchdb_session_get_database (CouchdbSession *session, const char *dbname, GError **error)
 {
-	CouchdbDatabaseInfo *dbinfo;
+	JsonObject *dbinfo;
 
 	g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
 	g_return_val_if_fail (dbname != NULL, NULL);
@@ -543,7 +540,7 @@ couchdb_session_get_database (CouchdbSession *session, const char *dbname, GErro
 		CouchdbDatabase *db;
 
 		db = couchdb_database_new (session, dbname);
-		couchdb_database_info_unref (dbinfo);
+		json_object_unref (dbinfo);
 
 		return db;
 	}
@@ -552,43 +549,50 @@ couchdb_session_get_database (CouchdbSession *session, const char *dbname, GErro
 }
 
 /**
- * couchdb_session_create_database
+ * couchdb_session_create_database:
  * @session: A #CouchdbSession object
- * @dbname: Name of the database to be created
+ * @dbname: Name of the database to retrieve
  * @error: Placeholder for error information
  *
- * Create a new database on a CouchDB instance.
+ * Create a new database on CouchDB instance.
  *
- * Return value: TRUE if successful, FALSE otherwise.
+ * Return value: (transfer full): A #CouchdbDatabase object if database were
+ * created successfully, or %NULL instead.
  */
-gboolean
-couchdb_session_create_database (CouchdbSession *session, const char *dbname, GError **error)
+CouchdbDatabase *
+couchdb_session_create_database (CouchdbSession * self,
+				 const char * dbname, GError ** error)
 {
-	char *url;
-	JsonParser *parser;
-	gboolean result = FALSE;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	CouchdbDatabase *database;
+	gchar *encoded_dbname;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (self), FALSE);
 	g_return_val_if_fail (dbname != NULL, FALSE);
 
-	url = g_strdup_printf ("%s/%s/", session->priv->uri, dbname);
-	parser = json_parser_new ();
-	if (couchdb_session_send_message (session, SOUP_METHOD_PUT, url, NULL, parser, error)) {
-		JsonNode *root_node;
+	response = NULL;
+
+	encoded_dbname = couchdb_uri_encode (dbname);
+	query = couchdb_query_new_for_path (encoded_dbname);
+	g_free (encoded_dbname);
 
-		root_node = json_parser_get_root (parser);
-		if (json_node_get_node_type (root_node) == JSON_NODE_OBJECT)
-			result = json_object_get_boolean_member (
-				json_node_get_object (root_node), "ok");		
+	couchdb_query_set_method (query, "PUT");
+
+	response = couchdb_session_execute_query (self, query, error);
+	if (!response) {
+		g_object_unref (query);
+
+		return NULL;
 	}
-	
-	g_object_unref (G_OBJECT (parser));
-	g_free (url);
 
-	if (result)
-		g_signal_emit_by_name (session, "database_created", dbname);
+	database = couchdb_session_get_database (self, dbname, NULL);
+	g_signal_emit_by_name (self, "database_created", dbname);
+
+	g_object_unref (response);
+	g_object_unref (query);
 
-	return result;
+	return database;
 }
 
 /**
@@ -602,34 +606,37 @@ couchdb_session_create_database (CouchdbSession *session, const char *dbname, GE
  * Return value: TRUE if successful, FALSE otherwise.
  */
 gboolean
-couchdb_session_delete_database (CouchdbSession *session, const char *dbname, GError **error)
+couchdb_session_delete_database (CouchdbSession * self,
+				 const char * dbname, GError ** error)
 {
-	char *url;
-	JsonParser *parser;
-	gboolean result = FALSE;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	gchar *encoded_dbname;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (self), FALSE);
 	g_return_val_if_fail (dbname != NULL, FALSE);
 
-	url = g_strdup_printf ("%s/%s/", session->priv->uri, dbname);
-	parser = json_parser_new ();
-	if (couchdb_session_send_message (session, SOUP_METHOD_DELETE, url, NULL, parser, error)) {
-		JsonNode *root_node;
+	response = NULL;
 
-		root_node = json_parser_get_root (parser);
-		if (json_node_get_node_type (root_node) == JSON_NODE_OBJECT)
-			result = json_object_get_boolean_member (
-				json_node_get_object (root_node), "ok");		
-	}
+	encoded_dbname = couchdb_uri_encode (dbname);
+	query = couchdb_query_new_for_path (encoded_dbname);
+	g_free (encoded_dbname);
 
-	g_object_unref (G_OBJECT (parser));
-	g_free (url);
+	couchdb_query_set_method (query, "DELETE");
+
+	response = couchdb_session_execute_query (self, query, error);
+	if (!response) {
+		g_object_unref (query);
 
-	if (result) {
-		g_signal_emit_by_name (session, "database_deleted", dbname);
+		return FALSE;
 	}
 
-	return result;
+	g_signal_emit_by_name (self, "database_deleted", dbname);
+
+	g_object_unref (response);
+	g_object_unref (query);
+
+	return TRUE;
 }
 
 /**
@@ -644,46 +651,34 @@ couchdb_session_delete_database (CouchdbSession *session, const char *dbname, GE
  * Return value: TRUE if successful, FALSE otherwise.
  */
 gboolean
-couchdb_session_compact_database (CouchdbSession *session, const char *dbname, GError **error)
+couchdb_session_compact_database (CouchdbSession * self,
+				  const char * dbname, GError ** error)
 {
-	char *url;
-	JsonParser *output;
-	gboolean result = FALSE;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	gchar *path, *encoded_dbname;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (self), FALSE);
 	g_return_val_if_fail (dbname != NULL, FALSE);
 
-	url = g_strdup_printf ("%s/%s/_compact", couchdb_session_get_uri (session), dbname);
-	output = json_parser_new ();
+	encoded_dbname = couchdb_uri_encode (dbname);
+	path = g_strconcat (encoded_dbname, "/", "_compact", NULL);
+	query = couchdb_query_new_for_path (path);
 
-	if (couchdb_session_send_message (session, SOUP_METHOD_POST, url, "", output, error)) {
-		JsonNode *root_node;
+	g_free (path);
+	g_free (encoded_dbname);
 
-		root_node = json_parser_get_root (output);
-		if (json_node_get_node_type (root_node) == JSON_NODE_OBJECT)
-			result = json_object_get_boolean_member (
-				json_node_get_object (root_node), "ok");
-	}
-
-	g_object_unref (G_OBJECT (output));
-	g_free (url);
+	response = couchdb_session_execute_query (self, query, error);
+	if (!response) {
+		g_object_unref (query);
 
-	return result;
-}
+		return FALSE;
+	}
 
-/**
- * couchdb_session_free_database_list:
- * @dblist: A list of databases, as returned by #couchdb_session_list_databases
- *
- * Free the list of databases returned by #couchdb_session_list_databases.
- */
-void
-couchdb_session_free_database_list (GSList *dblist)
-{
-	g_return_if_fail (dblist != NULL);
+	g_object_unref (response);
+	g_object_unref (query);
 
-	g_slist_foreach (dblist, (GFunc) couchdb_database_info_unref, NULL);
-	g_slist_free (dblist);
+	return TRUE;
 }
 
 /**
@@ -938,42 +933,45 @@ debug_message (const gchar *log_domain, GLogLevelFlags log_level,
  * Return value: TRUE if successful, FALSE otherwise, in which case the @error
  * parameter will be set to contain information about the error.
  */
-gboolean
-couchdb_session_replicate (CouchdbSession *session,
+JsonObject *
+couchdb_session_replicate (CouchdbSession *self,
 			   const gchar *source,
 			   const gchar *target,
 			   gboolean continous,
 			   GError **error)
 {
-	char *url, *body;
-	CouchdbDocument *input;
-	JsonParser *output;
-	gboolean send_ok;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	JsonObject *body, *object;
+	gchar *uri;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (self), FALSE);
 	g_return_val_if_fail (source != NULL, FALSE);
 	g_return_val_if_fail (target != NULL, FALSE);
 
-	/* Build the input document */
-	input = couchdb_document_new ();
-	couchdb_document_set_string_field (input, "source", source);
-	couchdb_document_set_string_field (input, "target", target);
+	query = couchdb_query_new_for_path ("/_replicate");
+
+	body = json_object_new ();
+	json_object_set_string_member (body, "source", source);
+	json_object_set_string_member (body, "target", target);
 	if (continous)
-		couchdb_document_set_boolean_field (input, "continous", TRUE);
+		json_object_set_boolean_member (body, "continous", TRUE);
 
-	/* Send message */
-	url = g_strdup_printf ("%s/_replicate", couchdb_session_get_uri (session));
-	body = couchdb_document_to_string (input);
-	output = json_parser_new ();
+	couchdb_query_set_json_object (query, body);
 
-	send_ok = couchdb_session_send_message (session, SOUP_METHOD_POST, url, body, output, error);
-	/* FIXME: what to do with the information returned? -> http://books.couchdb.org/relax/reference/replication */
+	response = couchdb_session_execute_query (self, query, error);
+	if (response == NULL) {
+		g_object_unref (query);
+		json_object_unref (body);
 
-	/* Free memory */
-	g_object_unref (G_OBJECT (output));
-	g_object_unref (G_OBJECT (input));
-	g_free (body);
-	g_free (url);
+		return NULL;
+	}
+
+	object = json_object_ref (couchdb_response_get_json_object (response));
+
+	json_object_unref (body);
+	g_object_unref (query);
+	g_object_unref (response);
 
-	return send_ok;
+	return object;
 }
diff --git a/couchdb-glib/couchdb-session.h b/couchdb-glib/couchdb-session.h
index 116b901..4a933bb 100644
--- a/couchdb-glib/couchdb-session.h
+++ b/couchdb-glib/couchdb-session.h
@@ -62,22 +62,31 @@ typedef struct {
 	void (* database_deleted) (CouchdbSession *session, const char *dbname);
 } CouchdbSessionClass;
 
+enum {
+	COUCHDB_SESSION_ERROR_CONFLICT,
+	COUCHDB_SESSION_ERROR_EXISTS,
+	COUCHDB_SESSION_ERROR_INVALID_JSON,
+	COUCHDB_SESSION_ERROR_UNKNOWN
+};
+
+GQuark couchdb_session_error_quark (void);
+
 GType                couchdb_session_get_type (void);
 CouchdbSession      *couchdb_session_new (const char *uri);
 
 const char          *couchdb_session_get_uri (CouchdbSession *session);
 
-GSList              *couchdb_session_list_databases (CouchdbSession *session, GError **error);
-void                 couchdb_session_free_database_list (GSList *dblist);
 
-CouchdbDatabaseInfo *couchdb_session_get_database_info (CouchdbSession *session, const char *dbname, GError **error);
+GList *couchdb_session_list_databases (CouchdbSession *session, GError **error);
 CouchdbResponse *couchdb_session_execute_query (CouchdbSession * self,
 						CouchdbQuery * query,
 						GError ** error);
 
+JsonObject          *couchdb_session_get_database_info (CouchdbSession *session, const char *dbname, GError **error);
 
 CouchdbDatabase     *couchdb_session_get_database (CouchdbSession *session, const char *dbname, GError **error);
-gboolean             couchdb_session_create_database (CouchdbSession *session, const char *dbname, GError **error);
+CouchdbDatabase     *couchdb_session_create_database (CouchdbSession *session, const char *dbname, GError **error);
+gboolean             couchdb_session_has_database (CouchdbSession *session, const char *dbname);
 gboolean             couchdb_session_delete_database (CouchdbSession *session, const char *dbname, GError **error);
 gboolean             couchdb_session_compact_database (CouchdbSession *session, const char *dbname, GError **error);
 
@@ -85,11 +94,11 @@ void                 couchdb_session_enable_authentication (CouchdbSession *sess
 void                 couchdb_session_disable_authentication (CouchdbSession *session);
 gboolean             couchdb_session_is_authentication_enabled (CouchdbSession *session);
 
-gboolean             couchdb_session_replicate (CouchdbSession *session,
-						const gchar *source,
-						const gchar *target,
-						gboolean continous,
-						GError **error);
+JsonObject *		couchdb_session_replicate			(CouchdbSession *session,
+									 const gchar *source,
+									 const gchar *target,
+									 gboolean continous,
+									 GError **error);
 
 G_END_DECLS
 



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