2 * BongoDB, an embedded document-based engine
3 * Copyright (C) 2015 by Javier Sancho Fernandez <jsf at jsancho dot org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 bongo_client_new (void)
30 bongo_client_t *client;
32 client = malloc (sizeof (bongo_client_t));
33 client->n_databases = 0;
34 client->databases = NULL;
39 bongo_database_node_t *
40 bongo_database_node_new (bongo_client_t client,
43 bongo_database_node_t *database_node;
48 database_node = malloc (sizeof (bongo_database_node_t));
49 database_node->db_name = db_name;
50 database_node->n_collections = 0;
51 database_node->collections = NULL;
52 database_node->next = (struct bongo_database_node_t *) client->databases;
54 client->databases = (bongo_database_node_t *) database_node;
55 client->n_databases++;
60 bongo_collection_node_t *
61 bongo_collection_node_new (bongo_database_node_t *database_node,
64 bongo_collection_node_t *collection_node;
66 if (database_node == NULL)
69 collection_node = malloc (sizeof (bongo_collection_node_t));
70 collection_node->col_name = col_name;
71 collection_node->next = (struct bongo_collection_node_t *) database_node->collections;
73 database_node->collections = (bongo_collection_node_t *) collection_node;
74 database_node->n_collections++;
76 return collection_node;
79 bongo_database_node_t *
80 bongo_database_node_search (bongo_client_t *client,
83 bongo_database_node_t *database_node;
88 database_node = client->databases;
89 while (database_node != NULL && strcmp (database_node->db_name, db_name) != 0)
90 database_node = (bongo_database_node_t *) database_node->next;
95 bongo_collection_node_t *
96 bongo_collection_node_search (bongo_database_node_t *database_node,
99 bongo_collection_node_t *collection_node;
101 if (database_node == NULL)
104 collection_node = database_node->collections;
105 while (collection_node != NULL && strcmp (collection_node->col_name, col_name) != 0)
106 collection_node = (bongo_collection_node_t *) collection_node->next;
108 return collection_node;
112 bongo_collection_node_destroy (bongo_collection_node_t *collection_node)
114 free (collection_node);
118 bongo_database_node_destroy (bongo_database_node_t *database_node)
120 bongo_collection_node_t *collection_node;
122 collection_node = database_node->collections;
123 while (collection_node != NULL)
125 database_node->collections = (bongo_collection_node_t *) collection_node->next;
126 collection_node_destroy (collection_node);
127 collection_node = database_node->collections;
130 free (database_node);
134 bongo_client_destroy (bongo_client_t *client)
136 bongo_database_node_t *database_node;
138 database_node = client->databases;
139 while (database_node != NULL)
141 client->databases = (bongo_database_node_t *) database_node->next;
142 database_node_destroy (database_node);
143 database_node = client->databases;
150 /* BongoDB Database */
153 bongo_client_get_database (bongo_client_t *client,
156 bongo_database_node_t *database_node;
157 bongo_database_t *database;
162 database_node = bongo_database_node_search(client, db_name);
163 if (database_node == NULL)
164 database_node = bongo_database_node_new(client, db_name);
166 database = malloc(sizeof(bongo_database_t));
167 database->database_node = database_node;
173 bongo_database_destroy(bongo_database_t *database)
179 /* BongoDB Collection */
182 bongo_client_get_collection(bongo_client_t *client,
184 const char *col_name)
186 bongo_database_t *database;
187 bongo_collection_node_t *collection_node;
188 bongo_collection_t *collection;
193 database = bongo_client_get_database(client, db_name);
195 collection_node = bongo_collection_node_search(database->database_node, col_name);
196 if (collection_node == NULL)
197 collection_node = bongo_collection_node_new(database->database_node, col_name);
199 bongo_database_destroy(database);
201 collection = malloc(sizeof(bongo_collection_t));
202 collection->collection_node = collection_node;
208 bongo_collection_insert (bongo_collection_t *collection,
209 bongo_insert_flags_t flags,
210 const char *document,
211 const bongo_write_concern_t *write_concern,
212 bongo_error_t *error)
217 bongo_collection_destroy(bongo_collection_t *collection)