2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "MsgSqliteWrapper.h"
32 /*==================================================================================================
34 ==================================================================================================*/
35 __thread sqlite3 *handle = NULL;
36 __thread sqlite3_stmt *stmt = NULL;
37 //__thread char **result = NULL;
40 /*==================================================================================================
41 IMPLEMENTATION OF MsgDbHandler - Member Functions
42 ==================================================================================================*/
43 MsgDbHandler::MsgDbHandler()
51 MsgDbHandler::~MsgDbHandler()
54 if (disconnect() != MSG_SUCCESS)
55 MSG_DEBUG("disconnect is failed!!");
66 msg_error_t MsgDbHandler::connect()
74 memset(strDBName, 0x00, sizeof(strDBName));
75 snprintf(strDBName, 64, "%s", MSGFW_DB_NAME);
77 ret = db_util_open(strDBName, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
81 MSG_DEBUG("DB Connect Success : [%p]", handle);
86 MSG_DEBUG("DB Connect Fail [%d]", ret);
87 return MSG_ERR_DB_CONNECT;
92 MSG_DEBUG("DB Connect exist : [%p]", handle);
99 msg_error_t MsgDbHandler::disconnect()
105 ret = db_util_close(handle);
107 if (ret == SQLITE_OK)
110 MSG_DEBUG("DB Disconnect Success");
115 MSG_DEBUG("DB Disconnect Fail [%d]", ret);
116 return MSG_ERR_DB_DISCONNECT;
124 bool MsgDbHandler::checkTableExist(const char *pTableName)
127 int nRowCnt = 0, nResult = 0;
129 /* Formulate the Query */
130 memset(strQuery, 0x00, sizeof(strQuery));
131 snprintf(strQuery, sizeof(strQuery), "select count(name) from sqlite_master where name='%s'", pTableName);
133 if (getTable(strQuery, &nRowCnt) != MSG_SUCCESS)
139 nResult = getColumnToInt(1);
140 MSG_DEBUG("Result [%d]", nResult);
151 msg_error_t MsgDbHandler::execQuery(const char *pQuery)
156 return MSG_ERR_INVALID_PARAMETER;
158 if(connect() != MSG_SUCCESS)
159 return MSG_ERR_DB_DISCONNECT;
161 ret = sqlite3_exec(handle, pQuery, 0, 0, NULL);
163 if (ret == SQLITE_OK)
165 MSG_DEBUG("Execute Query Success");
170 MSG_DEBUG("Execute Query Fail [%d]", ret);
171 return MSG_ERR_DB_EXEC;
178 msg_error_t MsgDbHandler::getTable(const char *pQuery, int *pRowCnt)
184 if(connect() != MSG_SUCCESS)
185 return MSG_ERR_DB_DISCONNECT;
188 ret = sqlite3_get_table(handle, pQuery, &result, pRowCnt, 0, NULL);
190 if (ret == SQLITE_OK)
192 if (*pRowCnt == 0) // when the no record return 'MSG_ERR_DB_NORECORD'
194 MSG_DEBUG("No Query Result");
195 return MSG_ERR_DB_NORECORD;
198 MSG_DEBUG("Get Table Success");
203 MSG_DEBUG("Get Table Fail [%d]", ret);
204 return MSG_ERR_DB_GETTABLE;
211 void MsgDbHandler::freeTable()
215 sqlite3_free_table(result);
221 msg_error_t MsgDbHandler::bindText(const char *pBindStr, int index)
225 if (pBindStr != NULL)
226 ret = sqlite3_bind_text(stmt, index, pBindStr, (strlen(pBindStr) + sizeof(unsigned char)), SQLITE_STATIC);
232 msg_error_t MsgDbHandler::bindBlob(const void * pBindBlob, int size, int index)
236 ret = sqlite3_bind_blob(stmt, index, pBindBlob, size, SQLITE_STATIC);
242 msg_error_t MsgDbHandler::prepareQuery(const char *pQuery)
248 if(connect() != MSG_SUCCESS)
249 return MSG_ERR_DB_DISCONNECT;
252 if ((ret = sqlite3_prepare_v2(handle, pQuery, strlen(pQuery), &stmt, NULL)) == SQLITE_OK)
254 MSG_DEBUG("Prepare Query Success");
259 MSG_DEBUG("Prepare Query Fail [%d]", ret);
260 return MSG_ERR_DB_PREPARE;
267 msg_error_t MsgDbHandler::stepQuery()
271 ret = sqlite3_step(stmt);
273 if (ret == SQLITE_ROW)
275 MSG_DEBUG("MsgStepQuery() SQLITE_ROW");
276 return MSG_ERR_DB_ROW;
278 else if (ret == SQLITE_DONE)
280 MSG_DEBUG("MsgStepQuery() SQLITE_DONE");
281 return MSG_ERR_DB_DONE;
285 MSG_DEBUG("MsgStepQuery() Fail [%d]", ret);
286 return MSG_ERR_DB_STEP;
293 void MsgDbHandler::finalizeQuery()
296 sqlite3_finalize(stmt);
301 int MsgDbHandler::columnInt(int ColumnIndex)
303 return sqlite3_column_int(stmt, ColumnIndex);
307 const unsigned char* MsgDbHandler::columnText(int ColumnIndex)
309 return sqlite3_column_text(stmt, ColumnIndex);
313 const void* MsgDbHandler::columnBlob(int ColumnIndex)
315 return sqlite3_column_blob(stmt, ColumnIndex);
319 msg_error_t MsgDbHandler::beginTrans()
323 if(connect() != MSG_SUCCESS)
324 return MSG_ERR_DB_DISCONNECT;
327 ret = sqlite3_exec(handle, "BEGIN deferred;", 0, 0, NULL);
329 if (ret == SQLITE_OK)
331 MSG_DEBUG("Begin Transaction Success");
336 MSG_DEBUG("Begin Transaction Fail [%d]", ret);
337 return MSG_ERR_DB_EXEC;
344 msg_error_t MsgDbHandler::endTrans(bool Success)
348 if(connect() != MSG_SUCCESS)
349 return MSG_ERR_DB_DISCONNECT;
354 ret = sqlite3_exec(handle, "END;", 0, 0, NULL);
358 ret = sqlite3_exec(handle, "rollback", 0, 0, NULL);
359 ret = sqlite3_exec(handle, "END;", 0, 0, NULL);
362 if (ret == SQLITE_OK)
364 MSG_DEBUG("End Transaction Success");
369 MSG_DEBUG("End Transaction Fail [%d]", ret);
370 return MSG_ERR_DB_EXEC;
377 int MsgDbHandler::getColumnToInt(int RowIndex)
379 char* pTemp = result[RowIndex];
389 nTemp = (int)strtol(pTemp, (char**)NULL, 10);
395 char MsgDbHandler::getColumnToChar(int RowIndex)
397 char* pTemp = result[RowIndex];
409 void MsgDbHandler::getColumnToString(int RowIndex, int Length, char *pString)
411 char* pTemp = result[RowIndex];
419 strncpy(pString, pTemp, Length);
423 msg_error_t MsgDbHandler::getRowId(const char *pTableName, unsigned int *pRowId)
425 int ret = 0, nRowId = 0, nRowCnt = 0;
428 if (pTableName == NULL || pRowId == NULL)
429 return MSG_ERR_INVALID_PARAMETER;
431 MSG_DEBUG("Table Name [%s]", pTableName);
433 memset(strQuery, 0x00, sizeof(strQuery));
434 snprintf(strQuery, sizeof(strQuery), "select max(rowid) from %s", pTableName);
436 ret = getTable(strQuery, &nRowCnt);
438 if (ret == SQLITE_OK)
440 nRowId = getColumnToInt(1);
442 if ((nRowCnt <= 1) && (nRowId == 0))
445 *pRowId = nRowId + 1;
449 MSG_DEBUG("MsgGetRowId failed");
452 return MSG_ERR_DB_GETTABLE;
457 MSG_DEBUG("Row ID [%d]", *pRowId);
463 /*==================================================================================================
464 FUNCTION IMPLEMENTATION
465 ==================================================================================================*/
466 void MsgReleaseMemoryDB()
470 freeSize = sqlite3_release_memory(-1);
472 MSG_DEBUG("freed memory size (bytes) : [%d]", freeSize);