summaryrefslogtreecommitdiff
path: root/src/pers_local_shared_db_access.c
blob: a7a46784052769c50f03ad7ba4dd8d8534df33bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**********************************************************************************************************************

*

* Copyright (C) 2012 Continental Automotive Systems, Inc.

*

* Author: Ionut.Ieremie@continental-corporation.com

*

* Implementation of persComDbAccess.h

*

* This Source Code Form is subject to the terms of the Mozilla Public

* License, v. 2.0. If a copy of the MPL was not distributed with this

* file, You can obtain one at http://mozilla.org/MPL/2.0/.

*

* Date             Author              Reason

* 2013.02.05       uidl9757            CSP_WZ#2220:  Adaptation for open source

* 2013.01.03       uidl9757            CSP_WZ#2060:  Remove "cursor" interface

* 2012.12.17       uidl9757            CSP_WZ#2060:  Changes to allow optimized access to DB

* 2012.12.10       uidl9757            CSP_WZ#2060:  Created

*

**********************************************************************************************************************/



#include "persComTypes.h"

#include <stdio.h>

#include "string.h"



#include "persComDataOrg.h"

#include "pers_low_level_db_access_if.h"

#include "persComDbAccess.h"

#include "persComErrors.h"



/**

 * \brief Obtain a handler to DB indicated by dbPathname

 * \note : DB is created if it does not exist and (bForceCreationIfNotPresent != 0)

 *

 * \param dbPathname                    [in] absolute path to database (length limited to \ref PERS_ORG_MAX_LENGTH_PATH_FILENAME)

 * \param bForceCreationIfNotPresent    [in] if !=0x0, the database is created if it does not exist

 *

 * \return >= 0 for valid handler, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbOpen(char const * dbPathname, unsigned char bForceCreationIfNotPresent)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(NIL != dbPathname)

    {

        if(strlen(dbPathname) >= PERS_ORG_MAX_LENGTH_PATH_FILENAME)

        {

            iErrCode = PERS_COM_ERR_INVALID_PARAM ;

        }

    }

    else

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_open(dbPathname, PersLldbPurpose_DB, bForceCreationIfNotPresent) ;

    }



    return iErrCode ;

}



/**

 * \brief Close handler to DB

 *

 * \param handlerDB     [in] handler obtained with persComDbOpen

 *

 * \return 0 for success, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbClose(signed int handlerDB)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(handlerDB < 0)

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_close(handlerDB) ;

    }



    return iErrCode ;

}



/**

 * \brief write a key-value pair into local/shared database

 *

 * \param handlerDB     [in] handler obtained with persComDbOpen

 * \param key           [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)

 * \param data          [in] buffer with key's data

 * \param dataSize      [in] size of key's data (max allowed \ref PERS_DB_MAX_SIZE_KEY_DATA)

 *

 * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbWriteKey(signed int handlerDB, char const * key, char const * data, signed int dataSize)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(     (handlerDB < 0)

        ||  (NIL == key)

        ||  (NIL == data)

        ||  (dataSize <= 0)

        ||  (dataSize > PERS_DB_MAX_SIZE_KEY_DATA)

    )

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }

    else

    {

        if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)

        {

            iErrCode = PERS_COM_ERR_INVALID_PARAM ;

        }

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_write_key(handlerDB, PersLldbPurpose_DB, key, data, dataSize) ;

    }



    return iErrCode ;

}





/**

 * \brief read a key's value from local/shared database

 *

 * \param handlerDB         [in] handler obtained with persComDbOpen

 * \param key               [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)

 * \param dataBuffer_out    [out]buffer where to return the read data

 * \param dataBufferSize    [in] size of dataBuffer_out

 *

 * \return read size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbReadKey(signed int handlerDB, char const * key, char* dataBuffer_out, signed int dataBufferSize)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(     (handlerDB < 0)

        ||  (NIL == key)

        ||  (NIL == dataBuffer_out)

        ||  (dataBufferSize <= 0)

    )

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }

    else

    {

        if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)

        {

            iErrCode = PERS_COM_ERR_INVALID_PARAM ;

        }

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_read_key(handlerDB, PersLldbPurpose_DB, key, dataBuffer_out, dataBufferSize) ;

    }



    return iErrCode ;

}



/**

 * \brief read a key's value from local/shared database

 *

 * \param handlerDB         [in] handler obtained with persComDbOpen

 * \param key               [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)

 *

 * \return key's size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbGetKeySize(signed int handlerDB, char const * key)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(     (handlerDB < 0)

        ||  (NIL == key)

    )

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }

    else

    {

        if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)

        {

            iErrCode = PERS_COM_ERR_INVALID_PARAM ;

        }

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_get_key_size(handlerDB, PersLldbPurpose_DB, key) ;

    }



    return iErrCode ;

}



/**

 * \brief delete key from local/shared database

 *

 * \param handlerDB     [in] handler obtained with persComDbOpen

 * \param key           [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME)

 *

 * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbDeleteKey(signed int handlerDB, char const * key)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(     (handlerDB < 0)

        ||  (NIL == key)

    )

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }

    else

    {

        if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)

        {

            iErrCode = PERS_COM_ERR_INVALID_PARAM ;

        }

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_delete_key(handlerDB, PersLldbPurpose_DB, key) ;

    }



    return iErrCode ;

}





/**

 * \brief Find the buffer's size needed to accomodate the list of keys' names in local/shared database

 *

 * \param handlerDB     [in] handler obtained with persComDbOpen

 *

 * \return needed size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbGetSizeKeysList(signed int handlerDB)

{

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(handlerDB < 0)

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_get_size_keys_list(handlerDB, PersLldbPurpose_DB) ;

    }



    return iErrCode ;

}





/**

 * \brief Obtain the list of the keys' names in local/shared database

 * \note : keys in the list are separated by '\0'

 *

 * \param handlerDB         [in] handler obtained with persComDbOpen

 * \param listBuffer_out    [out]buffer where to return the list of keys

 * \param listBufferSize    [in] size of listingBuffer_out

 * \return >=0 for size of the list, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)

 */

signed int persComDbGetKeysList(signed int handlerDB, char* listBuffer_out, signed int listBufferSize)

 {

    sint_t iErrCode = PERS_COM_SUCCESS ;



    if(     (handlerDB < 0)

        ||  (NIL == listBuffer_out)

        ||  (listBufferSize <= 0)

    )

    {

        iErrCode = PERS_COM_ERR_INVALID_PARAM ;

    }



    if(PERS_COM_SUCCESS == iErrCode)

    {

        iErrCode = pers_lldb_get_keys_list(handlerDB, PersLldbPurpose_DB, listBuffer_out, listBufferSize) ;

    }



    return iErrCode ;

}