Avoid conflicts with standard integer typedefs.
[profile/ivi/persistence-common-object.git] / src / pers_local_shared_db_access.c
1 /**********************************************************************************************************************
2 *
3 * Copyright (C) 2012 Continental Automotive Systems, Inc.
4 *
5 * Author: Ionut.Ieremie@continental-corporation.com
6 *
7 * Implementation of persComDbAccess.h
8 *
9 * This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was not distributed with this
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 *
13 * Date             Author              Reason
14 * 2013.02.05       uidl9757            CSP_WZ#2220:  Adaptation for open source
15 * 2013.01.03       uidl9757            CSP_WZ#2060:  Remove "cursor" interface
16 * 2012.12.17       uidl9757            CSP_WZ#2060:  Changes to allow optimized access to DB
17 * 2012.12.10       uidl9757            CSP_WZ#2060:  Created
18 *
19 **********************************************************************************************************************/
20
21 #include "persComTypes.h"
22 #include <stdio.h>
23 #include "string.h"
24
25 #include "persComDataOrg.h"
26 #include "pers_low_level_db_access_if.h"
27 #include "persComDbAccess.h"
28 #include "persComErrors.h"
29
30 /**
31  * \brief Obtain a handler to DB indicated by dbPathname
32  * \note : DB is created if it does not exist and (bForceCreationIfNotPresent != 0)
33  *
34  * \param dbPathname                    [in] absolute path to database (length limited to \ref PERS_ORG_MAX_LENGTH_PATH_FILENAME)
35  * \param bForceCreationIfNotPresent    [in] if !=0x0, the database is created if it does not exist
36  *
37  * \return >= 0 for valid handler, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES)
38  */
39 signed int persComDbOpen(char const * dbPathname, unsigned char bForceCreationIfNotPresent)
40 {
41     sint_t iErrCode = PERS_COM_SUCCESS ;
42
43     if(NIL != dbPathname)
44     {
45         if(strlen(dbPathname) >= PERS_ORG_MAX_LENGTH_PATH_FILENAME)
46         {
47             iErrCode = PERS_COM_ERR_INVALID_PARAM ;
48         }
49     }
50     else
51     {
52         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
53     }
54
55     if(PERS_COM_SUCCESS == iErrCode)
56     {
57         iErrCode = pers_lldb_open(dbPathname, PersLldbPurpose_DB, bForceCreationIfNotPresent) ;
58     }
59
60     return iErrCode ;
61 }
62
63 /**
64  * \brief Close handler to DB
65  *
66  * \param handlerDB     [in] handler obtained with persComDbOpen
67  *
68  * \return 0 for success, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES)
69  */
70 signed int persComDbClose(signed int handlerDB)
71 {
72     sint_t iErrCode = PERS_COM_SUCCESS ;
73
74     if(handlerDB < 0)
75     {
76         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
77     }
78
79     if(PERS_COM_SUCCESS == iErrCode)
80     {
81         iErrCode = pers_lldb_close(handlerDB) ;
82     }
83
84     return iErrCode ;
85 }
86
87 /**
88  * \brief write a key-value pair into local/shared database
89  *
90  * \param handlerDB     [in] handler obtained with persComDbOpen
91  * \param key           [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)
92  * \param data          [in] buffer with key's data
93  * \param dataSize      [in] size of key's data (max allowed \ref PERS_DB_MAX_SIZE_KEY_DATA)
94  *
95  * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES)
96  */
97 signed int persComDbWriteKey(signed int handlerDB, char const * key, char const * data, signed int dataSize)
98 {
99     sint_t iErrCode = PERS_COM_SUCCESS ;
100
101     if(     (handlerDB < 0)
102         ||  (NIL == key)
103         ||  (NIL == data)
104         ||  (dataSize <= 0)
105         ||  (dataSize > PERS_DB_MAX_SIZE_KEY_DATA)
106     )
107     {
108         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
109     }
110     else
111     {
112         if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
113         {
114             iErrCode = PERS_COM_ERR_INVALID_PARAM ;
115         }
116     }
117
118     if(PERS_COM_SUCCESS == iErrCode)
119     {
120         iErrCode = pers_lldb_write_key(handlerDB, PersLldbPurpose_DB, key, data, dataSize) ;
121     }
122
123     return iErrCode ;
124 }
125
126
127 /**
128  * \brief read a key's value from local/shared database
129  *
130  * \param handlerDB         [in] handler obtained with persComDbOpen
131  * \param key               [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)
132  * \param dataBuffer_out    [out]buffer where to return the read data
133  * \param dataBufferSize    [in] size of dataBuffer_out
134  *
135  * \return read size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
136  */
137 signed int persComDbReadKey(signed int handlerDB, char const * key, char* dataBuffer_out, signed int dataBufferSize)
138 {
139     sint_t iErrCode = PERS_COM_SUCCESS ;
140
141     if(     (handlerDB < 0)
142         ||  (NIL == key)
143         ||  (NIL == dataBuffer_out)
144         ||  (dataBufferSize <= 0)
145     )
146     {
147         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
148     }
149     else
150     {
151         if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
152         {
153             iErrCode = PERS_COM_ERR_INVALID_PARAM ;
154         }
155     }
156
157     if(PERS_COM_SUCCESS == iErrCode)
158     {
159         iErrCode = pers_lldb_read_key(handlerDB, PersLldbPurpose_DB, key, dataBuffer_out, dataBufferSize) ;
160     }
161
162     return iErrCode ;
163 }
164
165 /**
166  * \brief read a key's value from local/shared database
167  *
168  * \param handlerDB         [in] handler obtained with persComDbOpen
169  * \param key               [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)
170  *
171  * \return key's size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
172  */
173 signed int persComDbGetKeySize(signed int handlerDB, char const * key)
174 {
175     sint_t iErrCode = PERS_COM_SUCCESS ;
176
177     if(     (handlerDB < 0)
178         ||  (NIL == key)
179     )
180     {
181         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
182     }
183     else
184     {
185         if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
186         {
187             iErrCode = PERS_COM_ERR_INVALID_PARAM ;
188         }
189     }
190
191     if(PERS_COM_SUCCESS == iErrCode)
192     {
193         iErrCode = pers_lldb_get_key_size(handlerDB, PersLldbPurpose_DB, key) ;
194     }
195
196     return iErrCode ;
197 }
198
199 /**
200  * \brief delete key from local/shared database
201  *
202  * \param handlerDB     [in] handler obtained with persComDbOpen
203  * \param key           [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)
204  *
205  * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES)
206  */
207 signed int persComDbDeleteKey(signed int handlerDB, char const * key)
208 {
209     sint_t iErrCode = PERS_COM_SUCCESS ;
210
211     if(     (handlerDB < 0)
212         ||  (NIL == key)
213     )
214     {
215         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
216     }
217     else
218     {
219         if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
220         {
221             iErrCode = PERS_COM_ERR_INVALID_PARAM ;
222         }
223     }
224
225     if(PERS_COM_SUCCESS == iErrCode)
226     {
227         iErrCode = pers_lldb_delete_key(handlerDB, PersLldbPurpose_DB, key) ;
228     }
229
230     return iErrCode ;
231 }
232
233
234 /**
235  * \brief Find the buffer's size needed to accomodate the list of keys' names in local/shared database
236  *
237  * \param handlerDB     [in] handler obtained with persComDbOpen
238  *
239  * \return needed size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
240  */
241 signed int persComDbGetSizeKeysList(signed int handlerDB)
242 {
243     sint_t iErrCode = PERS_COM_SUCCESS ;
244
245     if(handlerDB < 0)
246     {
247         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
248     }
249
250     if(PERS_COM_SUCCESS == iErrCode)
251     {
252         iErrCode = pers_lldb_get_size_keys_list(handlerDB, PersLldbPurpose_DB) ;
253     }
254
255     return iErrCode ;
256 }
257
258
259 /**
260  * \brief Obtain the list of the keys' names in local/shared database
261  * \note : keys in the list are separated by '\0'
262  *
263  * \param handlerDB         [in] handler obtained with persComDbOpen
264  * \param listBuffer_out    [out]buffer where to return the list of keys
265  * \param listBufferSize    [in] size of listingBuffer_out
266  * \return >=0 for size of the list, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
267  */
268 signed int persComDbGetKeysList(signed int handlerDB, char* listBuffer_out, signed int listBufferSize)
269  {
270     sint_t iErrCode = PERS_COM_SUCCESS ;
271
272     if(     (handlerDB < 0)
273         ||  (NIL == listBuffer_out)
274         ||  (listBufferSize <= 0)
275     )
276     {
277         iErrCode = PERS_COM_ERR_INVALID_PARAM ;
278     }
279
280     if(PERS_COM_SUCCESS == iErrCode)
281     {
282         iErrCode = pers_lldb_get_keys_list(handlerDB, PersLldbPurpose_DB, listBuffer_out, listBufferSize) ;
283     }
284
285     return iErrCode ;
286 }
287