1 /**********************************************************************************************************************
3 * Copyright (C) 2012 Continental Automotive Systems, Inc.
5 * Author: Ionut.Ieremie@continental-corporation.com
7 * Implementation of persComDbAccess.h
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/.
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
19 **********************************************************************************************************************/
21 #include "persComTypes.h"
25 #include "persComDataOrg.h"
26 #include "pers_low_level_db_access_if.h"
27 #include "persComDbAccess.h"
28 #include "persComErrors.h"
31 * \brief Obtain a handler to DB indicated by dbPathname
32 * \note : DB is created if it does not exist and (bForceCreationIfNotPresent != 0)
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
37 * \return >= 0 for valid handler, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES)
39 signed int persComDbOpen(char const * dbPathname, unsigned char bForceCreationIfNotPresent)
41 sint_t iErrCode = PERS_COM_SUCCESS ;
45 if(strlen(dbPathname) >= PERS_ORG_MAX_LENGTH_PATH_FILENAME)
47 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
52 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
55 if(PERS_COM_SUCCESS == iErrCode)
57 iErrCode = pers_lldb_open(dbPathname, PersLldbPurpose_DB, bForceCreationIfNotPresent) ;
64 * \brief Close handler to DB
66 * \param handlerDB [in] handler obtained with persComDbOpen
68 * \return 0 for success, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES)
70 signed int persComDbClose(signed int handlerDB)
72 sint_t iErrCode = PERS_COM_SUCCESS ;
76 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
79 if(PERS_COM_SUCCESS == iErrCode)
81 iErrCode = pers_lldb_close(handlerDB) ;
88 * \brief write a key-value pair into local/shared database
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)
95 * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES)
97 signed int persComDbWriteKey(signed int handlerDB, char const * key, char const * data, signed int dataSize)
99 sint_t iErrCode = PERS_COM_SUCCESS ;
105 || (dataSize > PERS_DB_MAX_SIZE_KEY_DATA)
108 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
112 if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
114 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
118 if(PERS_COM_SUCCESS == iErrCode)
120 iErrCode = pers_lldb_write_key(handlerDB, PersLldbPurpose_DB, key, data, dataSize) ;
128 * \brief read a key's value from local/shared database
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
135 * \return read size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
137 signed int persComDbReadKey(signed int handlerDB, char const * key, char* dataBuffer_out, signed int dataBufferSize)
139 sint_t iErrCode = PERS_COM_SUCCESS ;
143 || (NIL == dataBuffer_out)
144 || (dataBufferSize <= 0)
147 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
151 if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
153 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
157 if(PERS_COM_SUCCESS == iErrCode)
159 iErrCode = pers_lldb_read_key(handlerDB, PersLldbPurpose_DB, key, dataBuffer_out, dataBufferSize) ;
166 * \brief read a key's value from local/shared database
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)
171 * \return key's size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
173 signed int persComDbGetKeySize(signed int handlerDB, char const * key)
175 sint_t iErrCode = PERS_COM_SUCCESS ;
181 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
185 if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
187 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
191 if(PERS_COM_SUCCESS == iErrCode)
193 iErrCode = pers_lldb_get_key_size(handlerDB, PersLldbPurpose_DB, key) ;
200 * \brief delete key from local/shared database
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)
205 * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES)
207 signed int persComDbDeleteKey(signed int handlerDB, char const * key)
209 sint_t iErrCode = PERS_COM_SUCCESS ;
215 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
219 if(strlen(key) >= PERS_DB_MAX_LENGTH_KEY_NAME)
221 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
225 if(PERS_COM_SUCCESS == iErrCode)
227 iErrCode = pers_lldb_delete_key(handlerDB, PersLldbPurpose_DB, key) ;
235 * \brief Find the buffer's size needed to accomodate the list of keys' names in local/shared database
237 * \param handlerDB [in] handler obtained with persComDbOpen
239 * \return needed size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES)
241 signed int persComDbGetSizeKeysList(signed int handlerDB)
243 sint_t iErrCode = PERS_COM_SUCCESS ;
247 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
250 if(PERS_COM_SUCCESS == iErrCode)
252 iErrCode = pers_lldb_get_size_keys_list(handlerDB, PersLldbPurpose_DB) ;
260 * \brief Obtain the list of the keys' names in local/shared database
261 * \note : keys in the list are separated by '\0'
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)
268 signed int persComDbGetKeysList(signed int handlerDB, char* listBuffer_out, signed int listBufferSize)
270 sint_t iErrCode = PERS_COM_SUCCESS ;
273 || (NIL == listBuffer_out)
274 || (listBufferSize <= 0)
277 iErrCode = PERS_COM_ERR_INVALID_PARAM ;
280 if(PERS_COM_SUCCESS == iErrCode)
282 iErrCode = pers_lldb_get_keys_list(handlerDB, PersLldbPurpose_DB, listBuffer_out, listBufferSize) ;