2 * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.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.
18 * @file DTapps2SqliteDB.cpp
19 * @brief This file includes functions relating to DataBase.
22 #include "DTapps2SqliteDB.h"
25 /* Define EXPORT_API temporary */
27 #define EXPORT_API __attribute__((visibility("default")))
30 static dtapps_mutex_t dtapps_sqlite_db_mutex = PTHREAD_MUTEX_INITIALIZER;
32 typedef long int dtappsThreadID;
34 class DtappsDBConnectionInfo
37 DtappsDBConnectionInfo();
38 int countOpenConnection;
39 sqlite3 *pDBConnection;
43 static std::map<dtappsThreadID, DtappsDBConnectionInfo *> g_dtapps_sqlite_connection_table;
45 DtappsDBConnectionInfo::DtappsDBConnectionInfo()
47 countOpenConnection=0;
58 TAPPSDbApiLock::TAPPSDbApiLock()
60 DRM_TAPPS_FRQ_LOG("LOCK by TID = %ld",drmgettid());
61 if (0 != dtapps_mutex_lock (&dtapps_sqlite_db_mutex))
63 DRM_TAPPS_EXCEPTION("Error while mutex locking.");
67 TAPPSDbApiLock::~TAPPSDbApiLock()
69 DRM_TAPPS_FRQ_LOG("UNLOCK by TID = %ld",drmgettid());
70 if (0 != dtapps_mutex_unlock(&dtapps_sqlite_db_mutex))
72 DRM_TAPPS_EXCEPTION("Error while mutex unlocking");
76 #define __DTAPPS_DB_SQLITE_RETRY__ (10)
78 #define DTAPPS_SQLITE3_SQL_BEGIN_IMMEDIATE "BEGIN IMMEDIATE TRANSACTION"
79 #define DTAPPS_SQLITE3_SQL_COMMIT "COMMIT TRANSACTION"
80 #define DTAPPS_SQLITE3_SQL_ROLLBACK "ROLLBACK TRANSACTION"
82 /* DataBase Related API Wrappers */
83 BOOL DTappsDBOpen(void *&pDb, const char* CallingFun)
85 DRM_TAPPS_LOG("[%s]: started.Calling function = %s",__func__,CallingFun);
87 TAPPSDbApiLock Dblock;
88 dtappsThreadID id_curr_thread = drmgettid();
90 bool IsMemAllocated = false;
91 int result = SQLITE_ERROR;
92 unsigned int pairCount = 0;
94 DtappsDBConnectionInfo *pDBConnectionInfo = NULL;
96 DRM_TAPPS_SECURE_LOG("DB-OPEN-CLOSE [%s]Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",__func__,getppid(),getpid(),drmgettid(), id_curr_thread);
98 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
99 DRM_TAPPS_FRQ_LOG("pairCount=[%u]", pairCount);
103 DRM_TAPPS_FRQ_LOG("Connection already exists.. pairCount=[%ld]", pairCount);
104 pDBConnectionInfo = g_dtapps_sqlite_connection_table[id_curr_thread];
105 DRM_TAPPS_FRQ_LOG("pDBConnectionInfo=[0x%x]", pDBConnectionInfo);
107 MTRY_BL(NULL != pDBConnectionInfo);
108 MTRY_BL(NULL != pDBConnectionInfo->pDBConnection);
109 DRM_TAPPS_FRQ_LOG("pDBConnectionInfo->countOpenConnection=[%d], pDBConnectionInfo->pDBConnection=[0x%x]", pDBConnectionInfo->countOpenConnection, pDBConnectionInfo->pDBConnection);
111 ++(pDBConnectionInfo->countOpenConnection);
112 pDb = pDBConnectionInfo->pDBConnection;
116 DRM_TAPPS_LOG("no connection exists.. pairCount=[%ld]", pairCount);
117 pDBConnectionInfo = NEW DtappsDBConnectionInfo;
118 MTRY_BL(NULL != pDBConnectionInfo);
119 IsMemAllocated = true; // prevent fix
121 DRM_TAPPS_FRQ_LOG("Opening DB connection.");
123 result = db_util_open(DTAPPS_DB_NAME, &h_db, 0);
124 if (result != SQLITE_OK)
126 DRM_TAPPS_EXCEPTION("sqlite msg :[%d]%s",result, sqlite3_errmsg(h_db));
127 DRM_TAPPS_SECURE_LOG("db name :%s", DTAPPS_DB_NAME);
131 DRM_TAPPS_FRQ_LOG("sqlite3_open() is success. h_db:%x", h_db);
133 pDBConnectionInfo->countOpenConnection = 1;
134 pDBConnectionInfo->pDBConnection = h_db; h_db = NULL;
136 pDb = pDBConnectionInfo->pDBConnection;
139 DRM_TAPPS_FRQ_LOG("pDBConnectionInfo->countOpenConnection=[%d], pDBConnectionInfo->pDBConnection=[0x%x]", pDBConnectionInfo->countOpenConnection, pDBConnectionInfo->pDBConnection);
141 g_dtapps_sqlite_connection_table.insert(pair<dtappsThreadID, DtappsDBConnectionInfo *>(id_curr_thread, pDBConnectionInfo));pDBConnectionInfo = NULL;
144 DRM_TAPPS_LOG("This fn finishes successfully.");
150 if (true == IsMemAllocated)
152 MDELETE(pDBConnectionInfo);
154 DRM_TAPPS_EXCEPTION("This fn fails");
159 BOOL DTappsDBGet(void *& pDBConnection)
161 DRM_TAPPS_LOG("Inside %s", __func__);
163 TAPPSDbApiLock Dblock;
164 unsigned int pairCount;
165 dtappsThreadID id_curr_thread = drmgettid();
166 pDBConnection = NULL;
168 DRM_TAPPS_LOG("Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",getppid(),getpid(),drmgettid(), id_curr_thread);
170 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
171 MTRY_BL(0 != pairCount);
173 DRM_TAPPS_FRQ_LOG("g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection=[%d], g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection=[0x%x]", g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection, g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection);
175 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
177 DRM_TAPPS_LOG("pairCount=[%u], g_dtapps_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
178 DRM_TAPPS_LOG(" This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
179 DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
184 DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
189 BOOL DTappsDBClose(const char* CallingFun)
191 DRM_TAPPS_LOG("Inside %s Calling function = %s", __func__, CallingFun);
193 TAPPSDbApiLock Dblock;
194 unsigned int pairCount;
195 sqlite3_stmt* pstmt = NULL;
196 dtappsThreadID id_curr_thread = drmgettid();
198 sqlite3 *pDBConnection = NULL;
199 int result = SQLITE_ERROR;
201 DRM_TAPPS_SECURE_LOG("DB-OPEN-CLOSE [%s]Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",__func__,getppid(),getpid(),drmgettid(), id_curr_thread);
203 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
204 MTRY_BL(0 != pairCount);
206 DRM_TAPPS_FRQ_LOG("g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection=[%d], g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection=[0x%x] \n", g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection, g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection);
208 --(g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection);
209 countConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection;
211 DRM_TAPPS_LOG(" countConnection=[%d] ", countConnection);
213 if (0 == countConnection)
215 DRM_TAPPS_LOG("closing DB connection info ");
217 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
219 DRM_TAPPS_LOG("pairCount=[%u], g_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
220 DRM_TAPPS_LOG("erasing map entry..pDBConnection=[0x%x]", pDBConnection);
222 g_dtapps_sqlite_connection_table.erase(id_curr_thread);
224 DRM_TAPPS_LOG("finalizing all statements..pDBConnection=[0x%x]", pDBConnection);
226 while ((pstmt = sqlite3_next_stmt(pDBConnection, pstmt)) != NULL) // prevent fix
228 DRM_TAPPS_LOG("finalizing DB statement..pstmt=[0x%x]", pstmt);
229 sqlite3_finalize(pstmt);
232 DRM_TAPPS_LOG(" Closing DB connection..pDBConnection=[0x%x]", pDBConnection);
234 result = db_util_close(pDBConnection);
235 if(result != SQLITE_OK)
237 DRM_TAPPS_EXCEPTION("SQLite Close fails. errmsg:%s", sqlite3_errmsg(pDBConnection));
242 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
248 DRM_TAPPS_EXCEPTION(" This fn fails.. pDBConnection=[0x%x]", pDBConnection);
253 BOOL DTappsDBBeginImmedTrans (const char* CallingFun)
255 DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__, CallingFun);
257 unsigned int pairCount;
258 dtappsThreadID id_curr_thread = drmgettid();
259 sqlite3* pDBConnection = NULL;
261 int count_try_db=0,rc = -1;
263 DRM_TAPPS_SECURE_LOG("DB-OPEN-CLOSE-BEG-COM-RB [%s]Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",__func__,getppid(),getpid(),drmgettid(), id_curr_thread);
265 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
266 MTRY_BL(0 != pairCount);
268 DRM_TAPPS_FRQ_LOG("g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection=[%d], g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection=[0x%x]", __func__, g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection, g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection);
270 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
272 DRM_TAPPS_LOG("pairCount=[%u], g_dtapps_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
273 DRM_TAPPS_LOG("Beginning DB operations..pDBConnection=[0x%x]", pDBConnection);
275 DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
279 DRM_TAPPS_LOG("START BEGIN");
280 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_BEGIN_IMMEDIATE, NULL, NULL, NULL);
282 DRM_TAPPS_FRQ_LOG("START BEGIN rc=%d", rc);
285 DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
286 if (rc == SQLITE_BUSY)
289 DRM_TAPPS_FRQ_LOG("Tried [%d] times to begin", count_try_db);
292 if (count_try_db >= (int)__DTAPPS_DB_SQLITE_RETRY__)
294 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
300 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
306 DRM_TAPPS_LOG("begin SUCCESS count_try_db=%d", count_try_db);
311 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
316 DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
320 BOOL DTappsDBCommit(const char* CallingFun)
322 DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__,CallingFun);
323 unsigned int pairCount;
324 dtappsThreadID id_curr_thread = drmgettid();
325 sqlite3* pDBConnection = NULL;
327 int count_try_db_commit=0,rc = -1;
329 DRM_TAPPS_SECURE_LOG("DB-OPEN-CLOSE-BEG-COM-RB [%s]Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",__func__,getppid(),getpid(),drmgettid(), id_curr_thread);
331 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
332 MTRY_BL(1 == pairCount);
334 DRM_TAPPS_FRQ_LOG("g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection=[%d], g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection=[0x%x] \n", g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection, g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection);
336 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
338 DRM_TAPPS_LOG("pairCount=[%u], g_dtapps_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
339 DRM_TAPPS_LOG("Commiting DB operations..pDBConnection=[0x%x]", pDBConnection);
340 DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
344 DRM_TAPPS_FRQ_LOG("START Commit");
345 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_COMMIT, NULL, NULL, NULL);
346 DRM_TAPPS_FRQ_LOG("START Commit rc=%d", rc);
350 DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
352 if (rc == SQLITE_BUSY)
355 DRM_TAPPS_FRQ_LOG("Tried [%d] times to Commit", count_try_db_commit);
356 count_try_db_commit++;
357 if (count_try_db_commit >= (int)__DTAPPS_DB_SQLITE_RETRY__)
359 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
365 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
371 DRM_TAPPS_FRQ_LOG("Commit SUCCESS count_try_db_commit=%d", count_try_db_commit);
376 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
381 DRM_TAPPS_EXCEPTION("[%s] This fn fails.. pDBConnection=[0x%x]", pDBConnection);
386 BOOL DTappsDBRollback (const char* CallingFun)
388 DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__,CallingFun);
390 unsigned int pairCount;
391 dtappsThreadID id_curr_thread = drmgettid();
392 sqlite3* pDBConnection = NULL;
394 int count_try_db_rollback = 0, rc = -1;
396 DRM_TAPPS_SECURE_LOG("DB-OPEN-CLOSE-BEG-COM-RB [%s]Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",__func__,getppid(),getpid(),drmgettid(), id_curr_thread);
398 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
399 MTRY_BL(1 == pairCount);
401 DRM_TAPPS_SECURE_LOG("[%s] g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection=[%d], g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection=[0x%x]", __func__, g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection, g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection);
403 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
405 DRM_TAPPS_LOG("pairCount=[%u], g_sqlite_connection_table[connectionIterator]=[0x%x]", __func__, pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
406 DRM_TAPPS_LOG("Rollback DB operations..pDBConnection=[0x%x]", pDBConnection);
408 DRM_TAPPS_FRQ_LOG("[%s] pDBConnection=[0x%x]",pDBConnection);
412 DRM_TAPPS_FRQ_LOG("START Rollback");
413 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_ROLLBACK, NULL, NULL, NULL);
414 DRM_TAPPS_FRQ_LOG("START Rollback rc=%d",rc);
418 DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
420 if(rc == SQLITE_BUSY)
423 DRM_TAPPS_FRQ_LOG("Tried [%d] times to Rollback",count_try_db_rollback);
424 count_try_db_rollback++;
425 if(count_try_db_rollback >= (int)__DTAPPS_DB_SQLITE_RETRY__)
427 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
433 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
439 DRM_TAPPS_FRQ_LOG("Rollback SUCCESS count_try_db_rollback=%d",count_try_db_rollback);
444 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
449 DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
455 BOOL DTappsExecuteSQL(void* pDB, const char* query)
457 int count_try_db_exe = 0, rc = 0;
458 sqlite3* h_db = (sqlite3*)pDB;
460 TAPPSDbApiLock Dblock;
464 rc = sqlite3_exec (h_db, query, NULL, NULL, NULL);
465 DRM_TAPPS_FRQ_LOG("EXECUTE rc=%d", rc);
469 if (rc == SQLITE_BUSY)
472 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute",count_try_db_exe);
475 if (count_try_db_exe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
477 DRM_TAPPS_EXCEPTION("h_db=0x%x rc=%d ErrMsg:%s count_try_db_exe=%d",h_db,rc,sqlite3_errmsg(h_db),count_try_db_exe);
478 DRM_TAPPS_EXCEPTION("query=[%s]",query);
484 DRM_TAPPS_EXCEPTION("h_db=0x%x rc=%d ErrMsg:%s", h_db, rc, sqlite3_errmsg(h_db));
485 DRM_TAPPS_EXCEPTION("query=[%s]", query);
491 DRM_TAPPS_FRQ_LOG("EXECUTE SUCCESS count_try_db_exe=%d", count_try_db_exe);
500 BOOL DTappsSQLGetTable(void* pDB, const char* query, TAPPSSqliteSelectTable* select_table)
503 sqlite3* h_db = (sqlite3*)pDB;
505 DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s", h_db,query);
506 TAPPSDbApiLock Dblock;
507 select_table->handle = h_db;
509 DRM_TAPPS_FRQ_LOG("select_table->handle=%x", select_table->handle);
511 int count_try_db_select = 0;
515 rc = sqlite3_get_table(h_db, query, &select_table->result, &select_table->n_rows, &select_table->n_cols, NULL);
517 DRM_TAPPS_FRQ_LOG("SELECT rc=%d", rc);
521 if (rc == SQLITE_BUSY)
524 DRM_TAPPS_FRQ_LOG("SELECT Tried [%d] times to select \n",count_try_db_select);
526 count_try_db_select++;
528 if(count_try_db_select >= (int)__DTAPPS_DB_SQLITE_RETRY__)
530 DRM_TAPPS_EXCEPTION("SELECT h_db=0x%x rc=%d ErrMsg:%s count_try_db_select=%d \n",h_db,rc,sqlite3_errmsg(h_db),count_try_db_select);
531 DRM_TAPPS_EXCEPTION("query=[%s] \n",query);
538 DRM_TAPPS_EXCEPTION("SELECT h_db=0x%x rc=%d ErrMsg:%s \n",h_db,rc,sqlite3_errmsg(h_db));
539 DRM_TAPPS_EXCEPTION("%s query=[%s] \n",query);
546 DRM_TAPPS_FRQ_LOG("SELECT SUCCESS count_try_db_select=%d \n",count_try_db_select);
552 DRM_TAPPS_FRQ_LOG("n_rows=%d n_cols=%d \n",select_table->n_rows, select_table->n_cols);
557 extern const char* dtappsCreateTableSQLData[];
559 char* DTappsGetSQLCreateTable(const char* tableName)
568 DRM_TAPPS_FRQ_LOG("tableName = %s",tableName);
570 while(dtappsCreateTableSQLData[index])
572 if(TAPPS_strnicmp( dtappsCreateTableSQLData[index],tableName,TAPPS_STRLEN(tableName))==0)
574 char* sql = (char*)dtappsCreateTableSQLData[index];
575 sql = sql + TAPPS_STRLEN(dtappsCreateTableSQLData[index])+1;
577 DRM_TAPPS_FRQ_LOG("sql query = %s",sql);
587 DRM_TAPPS_EXCEPTION("Specified Table Name is not Valid!!!");
592 void* DTappsStmtPrepare(void* pDB, const char* query)
595 sqlite3* h_db = (sqlite3*)pDB;
596 sqlite3_stmt* stmt = 0;
598 DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s \n",h_db,query);
600 TAPPSDbApiLock Dblock;
602 if(0 == TAPPS_strnicmp(query, "SELECT", 6))
604 DRM_TAPPS_EXCEPTION("Doesn't support 'Select' query h_db=0x%x query=%s \n",h_db,query);
609 rc = sqlite3_prepare( h_db, query, -1, &stmt, NULL);
611 DRM_TAPPS_FRQ_LOG("%s: rc=%d query=%s stmt=0x%x \n",__func__,rc,query,stmt);
615 DRM_TAPPS_EXCEPTION("DTappsStmtPrepare: h_db=0x%x err: %s \n",h_db,sqlite3_errmsg(h_db));
616 DRM_TAPPS_EXCEPTION("DTappsStmtPrepare: query: %s\n", query);
625 int DTappsStmtBindParam (
630 unsigned int dParamSize )
633 sqlite3_stmt* stmt = (sqlite3_stmt*)pStmt;
635 DRM_TAPPS_FRQ_LOG("Enter \n");
637 TAPPSDbApiLock Dblock;
641 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: stmt is NULL \n");
648 * The parameter index in SQL starts from 1 in sqlite3.
652 case TAPPSDB_TYPE_NONE:
653 rc = sqlite3_bind_null( stmt, dIdx+1);
657 case TAPPSDB_TYPE_INT:
658 rc = sqlite3_bind_int( stmt, dIdx+1, *(int*)pParam);
659 DRM_TAPPS_FRQ_LOG(" rc=%d type=%d pParam=%d \n",rc,Type,*(int*)pParam);
663 case TAPPSDB_TYPE_DATETIME:
665 case TAPPSDB_TYPE_CHAR:
667 case TAPPSDB_TYPE_VARCHAR:
668 rc = sqlite3_bind_text( stmt, dIdx+1, (char*)pParam, dParamSize, SQLITE_TRANSIENT);
669 DRM_TAPPS_FRQ_LOG("rc=%d type=%d dParamSize=%d pParam=%s \n",rc,Type,dParamSize,pParam);
673 case TAPPSDB_TYPE_BINARY:
675 case TAPPSDB_TYPE_BLOB:
679 unsigned int packet64_size=0;
680 if(CMStringUtil::GetBase64Encode((unsigned char*)pParam,dParamSize, &packet64) == true)
682 packet64_size = MSTRLEN(packet64);
683 rc = sqlite3_bind_blob( stmt, dIdx+1, packet64, packet64_size, SQLITE_TRANSIENT);
684 DRM_OMA_FRQ_LOG("%s: rc=%d type=%d packet64_size=%d packet64=%s \n",__func__,rc,Type,packet64_size,packet64);
685 if(packet64) MDELETES0(packet64);
691 case TAPPSDB_TYPE_UNKNOWN:
699 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: errno: %d\n", rc);
708 int DTappsStmtExecute (void* pStmt)
710 int CntTryStmtExe=0, rc =-1;
711 sqlite3_stmt* stmt = (sqlite3_stmt*)pStmt;
713 DRM_TAPPS_FRQ_LOG("%s:stmt=0x%x \n",__func__,stmt);
715 TAPPSDbApiLock Dblock;
719 DRM_TAPPS_EXCEPTION("stmt is NULL \n");
726 rc = sqlite3_step(stmt);
728 DRM_TAPPS_FRQ_LOG(" rc=%d \n",rc);
730 if ( rc != SQLITE_DONE )
732 if(rc == SQLITE_BUSY)
735 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute stmt \n",CntTryStmtExe);
739 if(CntTryStmtExe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
741 DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d CntTryStmtExe=%d \n",stmt,rc,CntTryStmtExe);
748 DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d \n",stmt,rc);
755 DRM_TAPPS_FRQ_LOG("STMT EXECUTE SUCCESS CntTryStmtExe=%d \n",CntTryStmtExe);
763 int DTappsStmtRelease (void* pStmt)
766 sqlite3_stmt* stmt = (sqlite3_stmt*)pStmt;
767 DRM_TAPPS_FRQ_LOG("%s:Enter \n",__func__);
768 TAPPSDbApiLock Dblock;
774 rc = sqlite3_finalize (stmt);
776 DRM_TAPPS_FRQ_LOG("rc=%d \n",rc);
778 if ( rc != SQLITE_OK )
780 DRM_TAPPS_EXCEPTION("DTappsStmtRelease() Errmsg ; %d \n", rc);
789 BOOL DTapps_DB_Install(const char* sql_query)
792 BOOL ret_value = FALSE;
794 if(sql_query == NULL)
796 DRM_TAPPS_EXCEPTION("Parameter NULL!!!, sql_query = %p",sql_query);
801 DRM_TAPPS_LOG("Open DB......");
803 ret_value = DTappsDBOpen(pDb,__func__);
805 if(ret_value != TRUE)
807 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
812 DRM_TAPPS_LOG("Begin Transaction........");
814 ret_value = DTappsDBBeginImmedTrans(__func__);
816 if(ret_value != TRUE)
818 DRM_TAPPS_EXCEPTION("DB Begin Transaction ret_value = %d",ret_value);
823 DRM_TAPPS_LOG("Execute SQL to Insert Contents into Table........");
825 ret_value = DTappsExecuteSQL(pDb,sql_query);
827 if(ret_value != TRUE)
829 DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d",ret_value);
834 DRM_TAPPS_LOG("Commit DB........");
836 ret_value = DTappsDBCommit(__func__);
838 if(ret_value != TRUE)
840 DRM_TAPPS_EXCEPTION("Commit DB Failed!! ret_value = %d",ret_value);
845 DRM_TAPPS_LOG("Close DB........");
847 ret_value = DTappsDBClose(__func__);
849 if(ret_value != TRUE)
851 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
856 DRM_TAPPS_LOG("Install DB Operartion Successful!!!");
862 ret_value = DTappsDBRollback (__func__);
864 if(ret_value != TRUE)
866 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
869 ret_value = DTappsDBClose(__func__);
871 if(ret_value != TRUE)
873 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
879 BOOL DTapps_Read_DB(const char* sql_query, TAPPSSqliteSelectTable* select_table)
882 BOOL ret_value = FALSE;
884 if(sql_query == NULL || select_table == NULL)
886 DRM_TAPPS_EXCEPTION("Parameters NULL, sql_query = %p, select_table = %p",sql_query,select_table);
891 DRM_TAPPS_LOG("Open DB......");
893 ret_value = DTappsDBOpen(pDb,__func__);
895 if(ret_value != TRUE)
897 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
902 DRM_TAPPS_LOG("Begin Transaction........");
904 ret_value = DTappsDBBeginImmedTrans(__func__);
906 if(ret_value != TRUE)
908 DRM_TAPPS_EXCEPTION("DB Begin Transaction Failed!! ret_value = %d",ret_value);
913 DRM_TAPPS_LOG("Get the Result Table........");
915 ret_value = DTappsSQLGetTable(pDb,sql_query,select_table);
917 if(ret_value != TRUE)
919 DRM_TAPPS_EXCEPTION("DB Get Table failed!! ret_value = %d",ret_value);
924 DRM_TAPPS_LOG("Close DB........");
926 ret_value = DTappsDBClose(__func__);
928 if(ret_value != TRUE)
930 DRM_TAPPS_EXCEPTION("DB Close failed!! ret_value = %d",ret_value);
935 DRM_TAPPS_LOG("Reading from DB Successful!!!");
941 ret_value = DTappsDBRollback (__func__);
943 if(ret_value != TRUE)
945 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
948 ret_value = DTappsDBClose(__func__);
950 if(ret_value != TRUE)
952 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
955 DRM_TAPPS_EXCEPTION("Reading DB function failed!!!");