a540bc8d06f3158045a3216bff7b7fbe9fd6be51
[jelmer/openchange.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 private_data pointer to the private backend context
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    \param private_data pointer to the current sqlite3 context
79
80    \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE_ERROR
81  */
82 static int sqlite3_delete_context(void *private_data)
83 {
84         struct sqlite3_context  *sqlite_ctx = (struct sqlite3_context *)private_data;
85         int                     ret;
86
87         DEBUG(5, ("[%s:%d]\n", __FUNCTION__, __LINE__));
88
89         if (!private_data) {
90                 return MAPISTORE_SUCCESS;
91         }
92
93         ret = sqlite3_close(sqlite_ctx->db);
94         if (ret) return MAPISTORE_ERROR;
95
96         return MAPISTORE_SUCCESS;
97 }
98
99
100 /**
101    \details Atomic operation: Create directory (mkdir)
102
103    \param private_data generic pointer to the sqlite3 context
104
105    \return MAPI_E_SUCCESS on success
106  */
107 static int sqlite3_op_mkdir(void *private_data)
108 {
109         struct sqlite3_context          *sqlite_ctx = (struct sqlite3_context *)private_data;
110
111         if (!sqlite_ctx) {
112                 return MAPISTORE_ERROR;
113         }
114
115         return MAPISTORE_SUCCESS;
116 }
117
118
119 /**
120    \details Atomic operation: Delete directory (rmdir)
121
122    \param private_data generic pointer to the sqlite3 context
123
124    \return MAPI_E_SUCCESS on success
125  */
126 static int sqlite3_op_rmdir(void *private_data)
127 {
128         struct sqlite3_context          *sqlite_ctx = (struct sqlite3_context *)private_data;
129
130         if (!sqlite_ctx) {
131                 return MAPISTORE_ERROR;
132         }
133
134         return MAPISTORE_SUCCESS;
135 }
136
137
138 /**
139    \details Atomic operation: Open directory (opendir)
140
141    \param private_data generic pointer to the sqlite3 context
142    \param parent_fid the parent folder identifier
143    \param fid the identifier of the colder to open  
144
145    \return MAPI_E_SUCCESS on success
146  */
147 static int sqlite3_op_opendir(void *private_data, uint64_t parent_fid, uint64_t fid)
148 {
149         struct sqlite3_context          *sqlite_ctx = (struct sqlite3_context *)private_data;
150
151         if (!sqlite_ctx) {
152                 return MAPISTORE_ERROR;
153         }
154
155         return MAPISTORE_SUCCESS;
156 }
157
158
159 /**
160    \details Atomic operation: Close directory (closedir)
161
162    \param private_data generic pointer to the sqlite3 context
163
164    \return MAPI_E_SUCCESS on success
165  */
166 static int sqlite3_op_closedir(void *private_data)
167 {
168         struct sqlite3_context          *sqlite_ctx = (struct sqlite3_context *)private_data;
169
170         if (!sqlite_ctx) {
171                 return MAPISTORE_ERROR;
172         }
173
174         return MAPISTORE_SUCCESS;
175 }
176
177
178 /**
179    \details Entry point for mapistore SQLite backend
180
181    \return MAPISTORE_SUCCESS on success, otherwise -1
182  */
183 int mapistore_init_backend(void)
184 {
185         struct mapistore_backend        backend;
186         int                             ret;
187
188         /* Fill in our name */
189         backend.name = "sqlite3";
190         backend.description = "mapistore sqlite3 backend";
191         backend.namespace = "sqlite://";
192
193         /* Fill in all the operations */
194         backend.init = sqlite3_init;
195         backend.create_context = sqlite3_create_context;
196         backend.delete_context = sqlite3_delete_context;
197         backend.op_mkdir = sqlite3_op_mkdir;
198         backend.op_rmdir = sqlite3_op_rmdir;
199         backend.op_opendir = sqlite3_op_opendir;
200         backend.op_closedir = sqlite3_op_closedir;
201         backend.op_readdir_count = NULL;
202
203         /* Register ourselves with the MAPISTORE subsystem */
204         ret = mapistore_backend_register(&backend);
205         if (ret != MAPISTORE_SUCCESS) {
206                 DEBUG(0, ("Failed to register the '%s' mapistore backend!\n", backend.name));
207                 return ret;
208         }
209
210         return MAPISTORE_SUCCESS;
211 }