[couchdb-glib] Add CouchdbDatabase object
- From: Rodrigo Moya <rodrigo src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [couchdb-glib] Add CouchdbDatabase object
- Date: Mon, 17 May 2010 10:24:09 +0000 (UTC)
commit 61013c8cf4a8c448d4a82f2d049a5a5b67b62a65
Author: Rodrigo Moya <rodrigo gnome-db org>
Date: Mon May 17 12:23:50 2010 +0200
Add CouchdbDatabase object
couchdb-glib/Makefile.am | 3 +
couchdb-glib/couchdb-database.c | 150 +++++++++++++++++++++++++++++++++++++++
couchdb-glib/couchdb-database.h | 58 +++++++++++++++
couchdb-glib/couchdb-session.c | 49 ++++++++++---
couchdb-glib/couchdb-session.h | 8 ++-
5 files changed, 258 insertions(+), 10 deletions(-)
---
diff --git a/couchdb-glib/Makefile.am b/couchdb-glib/Makefile.am
index 553d980..f540b57 100644
--- a/couchdb-glib/Makefile.am
+++ b/couchdb-glib/Makefile.am
@@ -28,6 +28,7 @@ libcouchdb_glib_1_0_la_headers = \
couchdb-array-field.h \
couchdb-credentials.h \
couchdb-database-info.h \
+ couchdb-database.h \
couchdb-document.h \
couchdb-document-info.h \
couchdb-glib.h \
@@ -41,6 +42,7 @@ libcouchdb_glib_1_0_la_sources = \
couchdb-array-field.c \
couchdb-credentials.c \
couchdb-database-info.c \
+ couchdb-database.c \
couchdb-document.c \
couchdb-document-info.c \
couchdb-session.c \
@@ -69,6 +71,7 @@ h_DATA = \
couchdb-array-field.h \
couchdb-credentials.h \
couchdb-database-info.h \
+ couchdb-database.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
new file mode 100644
index 0000000..5de07ce
--- /dev/null
+++ b/couchdb-glib/couchdb-database.c
@@ -0,0 +1,150 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 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-database.h"
+
+struct _CouchdbDatabasePrivate {
+ CouchdbSession *session;
+ char *dbname;
+};
+
+enum {
+ PROP_0,
+ PROP_SESSION,
+ PROP_DATABASE_NAME
+};
+
+G_DEFINE_TYPE(CouchdbDatabase, couchdb_database, G_TYPE_OBJECT)
+
+static void
+couchdb_database_finalize (GObject *object)
+{
+ CouchdbDatabase *database = COUCHDB_DATABASE (object);
+
+ if (database->priv->session != NULL) {
+ g_object_unref (G_OBJECT (database->priv->session));
+ database->priv->session = NULL;
+ }
+
+ if (database->priv->dbname != NULL) {
+ g_free (database->priv->dbname);
+ database->priv->dbname = NULL;
+ }
+
+ g_free (database->priv);
+
+ G_OBJECT_CLASS (couchdb_database_parent_class)->finalize (object);
+}
+
+static void
+couchdb_database_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ CouchdbDatabase *database = COUCHDB_DATABASE (object);
+
+ switch (prop_id) {
+ case PROP_SESSION:
+ g_object_unref (G_OBJECT (database->priv->session));
+ database->priv->session = g_object_ref (G_OBJECT (g_value_get_object (value)));
+ break;
+ case PROP_DATABASE_NAME:
+ g_free (database->priv->dbname);
+ database->priv->dbname = g_value_dup_string (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+couchdb_database_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ CouchdbDatabase *database = COUCHDB_DATABASE (object);
+
+ switch (prop_id) {
+ case PROP_SESSION:
+ g_value_set_object (value, database->priv->session);
+ break;
+ case PROP_DATABASE_NAME:
+ g_value_set_string (value, database->priv->dbname);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+couchdb_database_class_init (CouchdbDatabaseClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = couchdb_database_finalize;
+ object_class->set_property = couchdb_database_set_property;
+ object_class->get_property = couchdb_database_get_property;
+
+ g_object_class_install_property (object_class,
+ PROP_SESSION,
+ g_param_spec_object ("session",
+ "Session",
+ "CouchDB session associated with this database",
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+ g_object_class_install_property (object_class,
+ PROP_DATABASE_NAME,
+ g_param_spec_string ("database_name",
+ "Database name",
+ "Name of the database",
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+}
+
+static void
+couchdb_database_init (CouchdbDatabase *database)
+{
+ database->priv = g_new0 (CouchdbDatabasePrivate, 1);
+}
+
+/**
+ * couchdb_database_new:
+ * @session: A #CouchdbSession object
+ * @dbname: Name of the database
+ *
+ * Create a new #CouchdbDatabase object, which is to be used for operations on specific
+ * databases on the underlying CouchDB instance.
+ *
+ * Return value: A new #CouchdbDatabase object.
+ */
+CouchdbDatabase *
+couchdb_database_new (CouchdbSession *session, const char *dbname)
+{
+ return g_object_new (COUCHDB_TYPE_DATABASE,
+ "session", session,
+ "database_name", dbname,
+ NULL);
+}
+
diff --git a/couchdb-glib/couchdb-database.h b/couchdb-glib/couchdb-database.h
new file mode 100644
index 0000000..7073c35
--- /dev/null
+++ b/couchdb-glib/couchdb-database.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)
+ * 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
+ * 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_DATABASE_H__
+#define __COUCHDB_DATABASE_H__
+
+#include <glib-object.h>
+#include "couchdb-session.h"
+
+G_BEGIN_DECLS
+
+#define COUCHDB_TYPE_DATABASE (couchdb_database_get_type ())
+#define COUCHDB_DATABASE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COUCHDB_TYPE_DATABASE, CouchdbDatabase))
+#define COUCHDB_IS_DATABASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COUCHDB_TYPE_DATABASE))
+#define COUCHDB_DATABASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), COUCHDB_TYPE_DATABASE, CouchdbDatabaseClass))
+#define COUCHDB_IS_DATABASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), COUCHDB_TYPE_DATABASE))
+#define COUCHDB_DATABASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), COUCHDB_TYPE_DATABASE, CouchdbDatabaseClass))
+
+typedef struct _CouchdbDatabasePrivate CouchdbDatabasePrivate;
+
+struct _CouchdbDatabase {
+ GObject parent;
+ CouchdbDatabasePrivate *priv;
+};
+
+typedef struct {
+ GObjectClass parent_class;
+} CouchdbDatabaseClass;
+
+GType couchdb_database_get_type (void);
+CouchdbDatabase *couchdb_database_new (CouchdbSession *session, const char *dbname);
+
+CouchdbSession *couchdb_database_get_session (CouchdbDatabase *database);
+void couchdb_database_set_session (CouchdbDatabase *database, CouchdbSession *session);
+
+G_END_DECLS
+
+#endif
diff --git a/couchdb-glib/couchdb-session.c b/couchdb-glib/couchdb-session.c
index 62e2b05..d0a580a 100644
--- a/couchdb-glib/couchdb-session.c
+++ b/couchdb-glib/couchdb-session.c
@@ -100,17 +100,17 @@ couchdb_session_finalize (GObject *object)
}
static void
-couchdb_session_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *pspec)
+couchdb_session_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
{
CouchdbSession *session = COUCHDB_SESSION (object);
switch (prop_id) {
case PROP_URI:
- g_free(session->priv->uri);
+ g_free (session->priv->uri);
session->priv->uri = g_value_dup_string (value);
break;
default:
@@ -120,10 +120,10 @@ couchdb_session_set_property (GObject *object,
}
static void
-couchdb_session_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *pspec)
+couchdb_session_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
{
CouchdbSession *session = COUCHDB_SESSION (object);
@@ -145,6 +145,7 @@ couchdb_session_class_init (CouchdbSessionClass *klass)
object_class->finalize = couchdb_session_finalize;
object_class->set_property = couchdb_session_set_property;
object_class->get_property = couchdb_session_get_property;
+ klass->get_database = NULL;
g_object_class_install_property (object_class,
PROP_URI,
@@ -360,6 +361,36 @@ couchdb_session_get_database_info (CouchdbSession *session, const char *dbname,
}
/**
+ * couchdb_session_get_database:
+ * @session: A #CouchdbSession object
+ * @dbname: Name of the database to retrieve
+ * @error: Placeholder for error information
+ *
+ * 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.
+ */
+CouchdbDatabase *
+couchdb_session_get_database (CouchdbSession *session, const char *dbname, GError **error)
+{
+ CouchdbDatabaseInfo *dbinfo;
+
+ g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
+ g_return_val_if_fail (dbname != NULL, NULL);
+
+ dbinfo = couchdb_session_get_database_info (session, dbname, error);
+ if (dbinfo != NULL) {
+ if (COUCHDB_SESSION_GET_CLASS (session)->get_database != NULL)
+ return COUCHDB_SESSION_GET_CLASS (session)->get_database (session, dbname);
+ else
+ return couchdb_database_new (session, dbname);
+ }
+
+ return NULL;
+}
+
+/**
* couchdb_session_create_database
* @session: A #CouchdbSession object
* @dbname: Name of the database to be created
diff --git a/couchdb-glib/couchdb-session.h b/couchdb-glib/couchdb-session.h
index 8abb283..4ce00b6 100644
--- a/couchdb-glib/couchdb-session.h
+++ b/couchdb-glib/couchdb-session.h
@@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
- * Copyright (C) 2009 Canonical Services Ltd (www.canonical.com)
+ * Copyright (C) 2009-2010 Canonical Services Ltd (www.canonical.com)
* 2009 Mikkel Kamstrup Erlandsen
*
* Authors: Rodrigo Moya <rodrigo moya canonical com>
@@ -40,6 +40,7 @@ G_BEGIN_DECLS
#define COUCHDB_SESSION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), COUCHDB_TYPE_SESSION, CouchdbSessionClass))
typedef struct _CouchdbSessionPrivate CouchdbSessionPrivate;
+typedef struct _CouchdbDatabase CouchdbDatabase;
typedef struct {
GObject parent;
@@ -50,6 +51,7 @@ typedef struct {
typedef struct {
GObjectClass parent_class;
+ /* Signals */
void (* authentication_failed) (CouchdbSession *session);
void (* database_created) (CouchdbSession *session, const char *dbname);
@@ -58,6 +60,9 @@ typedef struct {
void (* document_created) (CouchdbSession *session, const char *dbname, CouchdbDocument *document);
void (* document_updated) (CouchdbSession *session, const char *dbname, CouchdbDocument *document);
void (* document_deleted) (CouchdbSession *session, const char *dbname, const char *docid);
+
+ /* Virtual methods */
+ CouchdbDatabase * (* get_database) (CouchdbSession *session, const char *dbname);
} CouchdbSessionClass;
GType couchdb_session_get_type (void);
@@ -70,6 +75,7 @@ void couchdb_session_free_database_list (GSList *dblist);
CouchdbDatabaseInfo *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);
gboolean couchdb_session_delete_database (CouchdbSession *session, const char *dbname, GError **error);
gboolean couchdb_session_compact_database (CouchdbSession *session, const char *dbname, GError **error);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]