- Add a very preliminary and light libmapistore implementation.
[jelmer/openchange-proposed.git/.git] / mapiproxy / libmapistore / backends / mapistore_sqlite3.c
1 /*
2    OpenChange Storage Abstraction Layer library
3    MAPIStore SQLite backend
4
5    OpenChange Project
6
7    Copyright (C) Julien Kerihuel 2009
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "mapistore_sqlite3.h"
24
25
26 /**
27    \details Initialize sqlite3 mapistore backend
28
29    \return MAPISTORE_SUCCESS on success
30  */
31 static int sqlite3_init(void)
32 {
33         DEBUG(0, ("sqlite3 backend initialized\n"));
34
35         return MAPISTORE_SUCCESS;
36 }
37
38
39 /**
40    \details Create a connection context to the sqlite3 backend
41
42    \param mem_ctx pointer to the memory context
43    \param uri pointer to the database path
44    \param context_id pointer to the context identifier the function
45    returns
46
47    \return MAPISTORE_SUCCESS on success
48  */
49 static int sqlite3_create_context(TALLOC_CTX *mem_ctx, const char *uri, void **private_data)
50 {
51         struct sqlite3_context          *sqlite_ctx;
52         sqlite3                         *db;
53         int                             ret;
54
55         DEBUG(0, ("[%s:%d]\n", __FUNCTION__, __LINE__));
56
57         ret = sqlite3_open(uri, &db);
58         if (ret) {
59                 DEBUG(3, ("[%s:%d]: %s\n", __FUNCTION__, __LINE__,
60                           sqlite3_errmsg(db)));
61                 sqlite3_close(db);
62                 return -1;
63         }
64
65         sqlite_ctx = talloc_zero(mem_ctx, struct sqlite3_context);
66         sqlite_ctx->db = db;
67         sqlite_ctx->private_data = NULL;
68
69         *private_data = (void *)sqlite_ctx;
70
71         return MAPISTORE_SUCCESS;
72 }
73
74
75 /**
76    \details Delete a connection context from the sqlite3 backend
77
78    \return MAPISTORE_SUCCESS on success
79  */
80 static int sqlite3_delete_context(void *private_data)
81 {
82         struct sqlite3_context  *sqlite_ctx = (struct sqlite3_context *)private_data;
83         int                     ret;
84
85         DEBUG(5, ("[%s:%d]\n", __FUNCTION__, __LINE__));
86
87         if (!private_data) {
88                 return MAPISTORE_SUCCESS;
89         }
90
91         ret = sqlite3_close(sqlite_ctx->db);
92         if (ret) return MAPISTORE_ERROR;
93
94         return MAPISTORE_SUCCESS;
95 }
96
97
98 /**
99    \details Entry point for mapistore SQLite backend
100
101    \return MAPISTORE_SUCCESS on success, otherwise -1
102  */
103 int mapistore_init_backend(void)
104 {
105         struct mapistore_backend        backend;
106         int                             ret;
107
108         /* Fill in our name */
109         backend.name = "sqlite3";
110         backend.description = "mapistore sqlite3 backend";
111         backend.namespace = "sqlite://";
112
113         /* Fill in all the operations */
114         backend.init = sqlite3_init;
115         backend.create_context = sqlite3_create_context;
116         backend.delete_context = sqlite3_delete_context;
117
118         /* Register ourselves with the MAPIPROXY subsystem */
119         ret = mapistore_backend_register(&backend);
120         if (ret != MAPISTORE_SUCCESS) {
121                 DEBUG(0, ("Failed to register the '%s' mapistore backend!\n", backend.name));
122                 return ret;
123         }
124
125         return MAPISTORE_SUCCESS;
126 }