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/>.
27 bongo_client_t *bongo_client_new(void)
29 bongo_client_t *bongo_client;
31 bongo_client = malloc(sizeof(bongo_client_t));
32 bongo_client->n_databases = 0;
33 bongo_client->databases = NULL;
38 bongo_database_node_t *bongo_database_node_new(bongo_client_t *bongo_client, const char *db_name)
40 bongo_database_node_t *bongo_database_node;
42 if (bongo_client == NULL)
45 bongo_database_node = malloc(sizeof(bongo_database_node_t));
46 bongo_database_node->db_name = db_name;
47 bongo_database_node->n_collections = 0;
48 bongo_database_node->collections = NULL;
49 bongo_database_node->next = (struct bongo_database_node_t *)bongo_client->databases;
51 bongo_client->databases = (bongo_database_node_t *)bongo_database_node;
52 bongo_client->n_databases++;
54 return bongo_database_node;
57 bongo_collection_node_t *bongo_collection_node_new(bongo_database_node_t *bongo_database_node, const char *col_name)
59 bongo_collection_node_t *bongo_collection_node;
61 if (bongo_database_node == NULL)
64 bongo_collection_node = malloc(sizeof(bongo_collection_node_t));
65 bongo_collection_node->col_name = col_name;
66 bongo_collection_node->next = (struct bongo_collection_node_t *)bongo_database_node->collections;
68 bongo_database_node->collections = (bongo_collection_node_t *)bongo_collection_node;
69 bongo_database_node->n_collections++;
71 return bongo_collection_node;
74 bongo_database_node_t *bongo_database_node_search(bongo_client_t *bongo_client, const char *db_name)
76 bongo_database_node_t *bongo_database_node;
78 if (bongo_client == NULL)
81 bongo_database_node = bongo_client->databases;
82 while (bongo_database_node != NULL && strcmp(bongo_database_node->db_name, db_name) != 0)
83 bongo_database_node = (bongo_database_node_t *)bongo_database_node->next;
85 return bongo_database_node;
88 bongo_collection_node_t *bongo_collection_node_search(bongo_database_node_t *bongo_database_node, const char *col_name)
90 bongo_collection_node_t *bongo_collection_node;
92 if (bongo_database_node == NULL)
95 bongo_collection_node = bongo_database_node->collections;
96 while (bongo_collection_node != NULL && strcmp(bongo_collection_node->col_name, col_name) != 0)
97 bongo_collection_node = (bongo_collection_node_t *)bongo_collection_node->next;
99 return bongo_collection_node;
102 void bongo_collection_node_destroy(bongo_collection_node_t *bongo_collection_node)
104 free(bongo_collection_node);
107 void bongo_database_node_destroy(bongo_database_node_t *bongo_database_node)
109 bongo_collection_node_t *bongo_collection_node;
111 bongo_collection_node = bongo_database_node->collections;
112 while (bongo_collection_node != NULL)
114 bongo_database_node->collections = (bongo_collection_node_t *)bongo_collection_node->next;
115 bongo_collection_node_destroy(bongo_collection_node);
116 bongo_collection_node = bongo_database_node->collections;
119 free(bongo_database_node);
122 void bongo_client_destroy(bongo_client_t *bongo_client)
124 bongo_database_node_t *bongo_database_node;
126 bongo_database_node = bongo_client->databases;
127 while (bongo_database_node != NULL)
129 bongo_client->databases = (bongo_database_node_t *)bongo_database_node->next;
130 bongo_database_node_destroy(bongo_database_node);
131 bongo_database_node = bongo_client->databases;
138 /* BongoDB Database */
140 bongo_database_t *bongo_client_get_database(bongo_client_t *bongo_client, const char *db_name)
142 bongo_database_node_t *bongo_database_node;
143 bongo_database_t *bongo_database;
145 if (bongo_client == NULL)
148 bongo_database_node = bongo_database_node_search(bongo_client, db_name);
149 if (bongo_database_node == NULL)
150 bongo_database_node = bongo_database_node_new(bongo_client, db_name);
152 bongo_database = malloc(sizeof(bongo_database_t));
153 bongo_database->database_node = bongo_database_node;
155 return bongo_database;
158 void bongo_database_destroy(bongo_database_t *bongo_database)
160 free(bongo_database);
164 /* BongoDB Collection */
166 bongo_collection_t *bongo_client_get_collection(bongo_client_t *bongo_client, const char *db_name, const char *col_name)
168 bongo_database_t *bongo_database;
169 bongo_collection_node_t *bongo_collection_node;
170 bongo_collection_t *bongo_collection;
172 if (bongo_client == NULL)
175 bongo_database = bongo_client_get_database(bongo_client, db_name);
177 bongo_collection_node = bongo_collection_node_search(bongo_database->database_node, col_name);
178 if (bongo_collection_node == NULL)
179 bongo_collection_node = bongo_collection_node_new(bongo_database->database_node, col_name);
181 bongo_database_destroy(bongo_database);
183 bongo_collection = malloc(sizeof(bongo_collection_t));
184 bongo_collection->collection_node = bongo_collection_node;
186 return bongo_collection;
189 void bongo_collection_destroy(bongo_collection_t *bongo_collection)
191 free(bongo_collection);