summaryrefslogtreecommitdiffstats
path: root/tests/test-list-databases.c
blob: 4c7908e353283dd65dbab02b6b0939cbd0b77927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 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-glib.h>

int
main (int argc, char *argv[])
{
	CouchdbSession *couchdb;
	GSList *dblist, *sl;
	GError *error = NULL;


	g_type_init ();
	g_thread_init (NULL);

	/* Initialize Couchdb */
	couchdb =  argc > 1 ? couchdb_session_new (argv[1]) : couchdb_session_new (NULL);
	g_print ("Connecting to Couchdb at %s\n", couchdb_session_get_uri (couchdb));
	
	if (!couchdb) {
		g_print ("Could not create Couchdb object\n");
		return -1;
	}

	dblist = couchdb_session_list_databases (couchdb, &error);
	if (error != NULL) {
		g_critical ("Error listing databases: %s", error->message);
		g_error_free (error);
	}
	
	for (sl = dblist; sl != NULL; sl = sl->next) {
		CouchdbDatabaseInfo *dbinfo;
		CouchdbDatabase *database;
		GSList *doclist;

		error = NULL;
		g_print ("Found database %s\n", (const char *) sl->data);
		dbinfo = couchdb_session_get_database_info (couchdb, (const char *) sl->data, &error);
		if (dbinfo) {
			g_print ("\tDatabase name: %s\n",
				 couchdb_database_info_get_dbname (dbinfo));
			g_print ("\t# of documents: %d\n",
				 couchdb_database_info_get_documents_count (dbinfo));
			g_print ("\t# of deleted documents: %d\n",
				 couchdb_database_info_get_deleted_documents_count (dbinfo));
			g_print ("\tUpdate sequence: %d\n",
				 couchdb_database_info_get_update_sequence (dbinfo));
			g_print ("\tCompact running?: %s\n",
				 couchdb_database_info_is_compact_running (dbinfo) ? "True" : "False");
			g_print ("\tDisk size: %d\n",
				 couchdb_database_info_get_disk_size (dbinfo));

			couchdb_database_info_unref (dbinfo);
		} else {
			g_print ("Could not retrieve info for database %s\n", (const char *) sl->data);
		}

		/* now, get list of documents */
		error = NULL;
		database = couchdb_session_get_database (couchdb, (const char *) sl->data, &error);
		g_assert (database != NULL);

		error = NULL;
		doclist = couchdb_database_get_all_documents (database, &error);
		if (doclist) {
			GSList *sl_doc;

			g_print ("\tDocuments:\n");
			for (sl_doc = doclist; sl_doc != NULL; sl_doc = sl_doc->next) {
				CouchdbDocument *document = COUCHDB_DOCUMENT (sl_doc->data);

				if (document) {
					char *json;

					g_print ("\t\t%s\t(rev: %s)\n",
						 couchdb_document_get_id (document),
						 couchdb_document_get_revision (document));

					json = couchdb_document_to_string (document);
					g_print ("\t\t\t%s\n", json);
					g_free (json);

					g_object_unref (G_OBJECT (document));
				}
			}

			couchdb_database_free_document_list (doclist);
		} else {
			g_print ("\tNo documents\n");
		}
	}

	couchdb_session_free_database_list (dblist);
	g_object_unref (G_OBJECT (couchdb));

	return 0;
}