[couchdb-glib] Associate CouchdbDocument's with the container CouchdbDatabase



commit 69e8fafa1df60642a675b547380c281747188697
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Thu Jun 3 09:41:02 2010 +0200

    Associate CouchdbDocument's with the container CouchdbDatabase

 couchdb-glib/couchdb-database.c |   14 +++++++-
 couchdb-glib/couchdb-document.c |   63 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 4 deletions(-)
---
diff --git a/couchdb-glib/couchdb-database.c b/couchdb-glib/couchdb-database.c
index e228a7e..2436aa7 100644
--- a/couchdb-glib/couchdb-database.c
+++ b/couchdb-glib/couchdb-database.c
@@ -396,8 +396,10 @@ couchdb_database_get_all_documents (CouchdbDatabase *database, GError **error)
 						json_object_get_object_member (obj, "doc"));
 				}
 			}
-			if (document != NULL)
+			if (document != NULL) {
+				g_object_set (G_OBJECT (document), "database", database, NULL);
 				doclist = g_slist_append (doclist, document);
+			}
 		}
 	}
 
@@ -482,8 +484,10 @@ couchdb_database_execute_view (CouchdbDatabase *database,
 
 			couchdb_document_set_id (document, json_object_get_string_member (obj, "id"));
 
-			if (document != NULL)
+			if (document != NULL) {
+				g_object_set (G_OBJECT (document), "database", database, NULL);
 				doclist = g_slist_append (doclist, document);
+			}
 		}
 	}
 
@@ -552,6 +556,8 @@ couchdb_database_get_document (CouchdbDatabase *database,
 		} else {
 			document = couchdb_document_new_from_json_object (json_node_get_object (json_parser_get_root (parser)));
 		}
+
+		g_object_set (G_OBJECT (document), "database", database, NULL);
 	}
 
 	g_object_unref (G_OBJECT (parser));
@@ -595,6 +601,8 @@ couchdb_database_get_design_document (CouchdbDatabase *database, const char *doc
 		document = couchdb_design_document_new ();
 		couchdb_document_set_from_json_object (COUCHDB_DOCUMENT (document),
 						       json_node_get_object (json_parser_get_root (parser)));
+
+		g_object_set (G_OBJECT (document), "database", database, NULL);
 	}
 
 	g_object_unref (G_OBJECT (parser));
@@ -663,6 +671,8 @@ couchdb_database_put_document (CouchdbDatabase *database,
 		else
 			g_signal_emit_by_name (database, "document_created", document);
 
+		g_object_set (G_OBJECT (document), "database", database, NULL);
+
 		result = TRUE;
 	}
 
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 2bdadf2..2227cf6 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -30,27 +30,86 @@
 
 struct _CouchdbDocumentPrivate {
 	JsonObject *root_object;
+	CouchdbDatabase *database;
 };
 
 G_DEFINE_TYPE(CouchdbDocument, couchdb_document, G_TYPE_OBJECT);
 
+enum {
+	PROP_0,
+	PROP_DATABASE
+};
+
 static void
 couchdb_document_finalize (GObject *object)
 {
 	CouchdbDocument *document = COUCHDB_DOCUMENT (object);
 
-	json_object_unref (document->priv->root_object);
-	g_free (document->priv);
+	if (document->priv != NULL) {
+		if (document->priv->database != NULL)
+			g_object_unref (G_OBJECT (document->priv->database));
+
+		json_object_unref (document->priv->root_object);
+		g_free (document->priv);
+	}
 
 	G_OBJECT_CLASS (couchdb_document_parent_class)->finalize (object);
 }
 
 static void
+couchdb_document_set_property (GObject *object,
+			       guint prop_id,
+			       const GValue *value,
+			       GParamSpec *pspec)
+{
+	CouchdbDocument *document = COUCHDB_DOCUMENT (object);
+
+	switch (prop_id) {
+	case PROP_DATABASE:
+		if (document->priv->database != NULL)
+			g_object_unref (G_OBJECT (document->priv->database));
+		document->priv->database = g_object_ref (G_OBJECT (g_value_get_object (value)));
+		break;
+	default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                break;
+	}
+}
+
+static void
+couchdb_document_get_property (GObject *object,
+                               guint prop_id,
+                               GValue *value,
+                               GParamSpec *pspec)
+{
+	CouchdbDocument *document = COUCHDB_DOCUMENT (object);
+
+	switch (prop_id) {
+	case PROP_DATABASE:
+		g_value_set_object (value, document->priv->database);
+		break;
+	default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                break;
+	}
+}
+
+static void
 couchdb_document_class_init (CouchdbDocumentClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
 	object_class->finalize = couchdb_document_finalize;
+	object_class->set_property = couchdb_document_set_property;
+	object_class->get_property = couchdb_document_get_property;
+
+	g_object_class_install_property (object_class,
+					 PROP_DATABASE,
+					 g_param_spec_object ("database",
+							      "Database object",
+							      "Database from which this document was retrieved/saved",
+							      NULL,
+							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 }
 
 static void



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