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(std::nothrow) 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 (IsMemAllocated && pDBConnectionInfo)
151 delete pDBConnectionInfo;
153 DRM_TAPPS_EXCEPTION("This fn fails");
158 BOOL DTappsDBGet(void *& pDBConnection)
160 DRM_TAPPS_LOG("Inside %s", __func__);
162 TAPPSDbApiLock Dblock;
163 unsigned int pairCount;
164 dtappsThreadID id_curr_thread = drmgettid();
165 pDBConnection = NULL;
167 DRM_TAPPS_LOG("Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",getppid(),getpid(),drmgettid(), id_curr_thread);
169 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
170 MTRY_BL(0 != pairCount);
172 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);
174 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
176 DRM_TAPPS_LOG("pairCount=[%u], g_dtapps_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
177 DRM_TAPPS_LOG(" This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
178 DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
183 DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
188 BOOL DTappsDBClose(const char* CallingFun)
190 DRM_TAPPS_LOG("Inside %s Calling function = %s", __func__, CallingFun);
192 TAPPSDbApiLock Dblock;
193 unsigned int pairCount;
194 sqlite3_stmt* pstmt = NULL;
195 dtappsThreadID id_curr_thread = drmgettid();
197 sqlite3 *pDBConnection = NULL;
198 int result = SQLITE_ERROR;
200 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);
202 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
203 MTRY_BL(0 != pairCount);
205 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);
207 --(g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection);
208 countConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection;
210 DRM_TAPPS_LOG(" countConnection=[%d] ", countConnection);
212 if (0 == countConnection)
214 DRM_TAPPS_LOG("closing DB connection info ");
216 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
218 DRM_TAPPS_LOG("pairCount=[%u], g_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
219 DRM_TAPPS_LOG("erasing map entry..pDBConnection=[0x%x]", pDBConnection);
221 g_dtapps_sqlite_connection_table.erase(id_curr_thread);
223 DRM_TAPPS_LOG("finalizing all statements..pDBConnection=[0x%x]", pDBConnection);
225 while ((pstmt = sqlite3_next_stmt(pDBConnection, pstmt)) != NULL) // prevent fix
227 DRM_TAPPS_LOG("finalizing DB statement..pstmt=[0x%x]", pstmt);
228 sqlite3_finalize(pstmt);
231 DRM_TAPPS_LOG(" Closing DB connection..pDBConnection=[0x%x]", pDBConnection);
233 result = db_util_close(pDBConnection);
234 if(result != SQLITE_OK)
236 DRM_TAPPS_EXCEPTION("SQLite Close fails. errmsg:%s", sqlite3_errmsg(pDBConnection));
241 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
247 DRM_TAPPS_EXCEPTION(" This fn fails.. pDBConnection=[0x%x]", pDBConnection);
252 BOOL DTappsDBBeginImmedTrans (const char* CallingFun)
254 DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__, CallingFun);
256 unsigned int pairCount;
257 dtappsThreadID id_curr_thread = drmgettid();
258 sqlite3* pDBConnection = NULL;
260 int count_try_db=0,rc = -1;
262 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);
264 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
265 MTRY_BL(0 != pairCount);
267 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);
269 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
271 DRM_TAPPS_LOG("pairCount=[%u], g_dtapps_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
272 DRM_TAPPS_LOG("Beginning DB operations..pDBConnection=[0x%x]", pDBConnection);
274 DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
278 DRM_TAPPS_LOG("START BEGIN");
279 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_BEGIN_IMMEDIATE, NULL, NULL, NULL);
281 DRM_TAPPS_FRQ_LOG("START BEGIN rc=%d", rc);
284 DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
285 if (rc == SQLITE_BUSY)
288 DRM_TAPPS_FRQ_LOG("Tried [%d] times to begin", count_try_db);
291 if (count_try_db >= (int)__DTAPPS_DB_SQLITE_RETRY__)
293 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
299 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
305 DRM_TAPPS_LOG("begin SUCCESS count_try_db=%d", count_try_db);
310 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
315 DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
319 BOOL DTappsDBCommit(const char* CallingFun)
321 DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__,CallingFun);
322 unsigned int pairCount;
323 dtappsThreadID id_curr_thread = drmgettid();
324 sqlite3* pDBConnection = NULL;
326 int count_try_db_commit=0,rc = -1;
328 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);
330 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
331 MTRY_BL(1 == pairCount);
333 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);
335 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
337 DRM_TAPPS_LOG("pairCount=[%u], g_dtapps_sqlite_connection_table[connectionIterator]=[0x%x]", pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
338 DRM_TAPPS_LOG("Commiting DB operations..pDBConnection=[0x%x]", pDBConnection);
339 DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
343 DRM_TAPPS_FRQ_LOG("START Commit");
344 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_COMMIT, NULL, NULL, NULL);
345 DRM_TAPPS_FRQ_LOG("START Commit rc=%d", rc);
349 DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
351 if (rc == SQLITE_BUSY)
354 DRM_TAPPS_FRQ_LOG("Tried [%d] times to Commit", count_try_db_commit);
355 count_try_db_commit++;
356 if (count_try_db_commit >= (int)__DTAPPS_DB_SQLITE_RETRY__)
358 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
364 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
370 DRM_TAPPS_FRQ_LOG("Commit SUCCESS count_try_db_commit=%d", count_try_db_commit);
375 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
380 DRM_TAPPS_EXCEPTION("[%s] This fn fails.. pDBConnection=[0x%x]", pDBConnection);
385 BOOL DTappsDBRollback (const char* CallingFun)
387 DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__,CallingFun);
389 unsigned int pairCount;
390 dtappsThreadID id_curr_thread = drmgettid();
391 sqlite3* pDBConnection = NULL;
393 int count_try_db_rollback = 0, rc = -1;
395 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);
397 pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
398 MTRY_BL(1 == pairCount);
400 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);
402 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
404 DRM_TAPPS_LOG("pairCount=[%u], g_sqlite_connection_table[connectionIterator]=[0x%x]", __func__, pairCount, g_dtapps_sqlite_connection_table[id_curr_thread]);
405 DRM_TAPPS_LOG("Rollback DB operations..pDBConnection=[0x%x]", pDBConnection);
407 DRM_TAPPS_FRQ_LOG("[%s] pDBConnection=[0x%x]",pDBConnection);
411 DRM_TAPPS_FRQ_LOG("START Rollback");
412 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_ROLLBACK, NULL, NULL, NULL);
413 DRM_TAPPS_FRQ_LOG("START Rollback rc=%d",rc);
417 DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
419 if(rc == SQLITE_BUSY)
422 DRM_TAPPS_FRQ_LOG("Tried [%d] times to Rollback",count_try_db_rollback);
423 count_try_db_rollback++;
424 if(count_try_db_rollback >= (int)__DTAPPS_DB_SQLITE_RETRY__)
426 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
432 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
438 DRM_TAPPS_FRQ_LOG("Rollback SUCCESS count_try_db_rollback=%d",count_try_db_rollback);
443 DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
448 DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
454 BOOL DTappsExecuteSQL(void* pDB, const char* query)
456 int count_try_db_exe = 0, rc = 0;
457 sqlite3* h_db = (sqlite3*)pDB;
459 TAPPSDbApiLock Dblock;
463 rc = sqlite3_exec (h_db, query, NULL, NULL, NULL);
464 DRM_TAPPS_FRQ_LOG("EXECUTE rc=%d", rc);
468 if (rc == SQLITE_BUSY)
471 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute",count_try_db_exe);
474 if (count_try_db_exe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
476 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);
477 DRM_TAPPS_EXCEPTION("query=[%s]",query);
483 DRM_TAPPS_EXCEPTION("h_db=0x%x rc=%d ErrMsg:%s", h_db, rc, sqlite3_errmsg(h_db));
484 DRM_TAPPS_EXCEPTION("query=[%s]", query);
490 DRM_TAPPS_FRQ_LOG("EXECUTE SUCCESS count_try_db_exe=%d", count_try_db_exe);
499 BOOL DTappsSQLGetTable(void* pDB, const char* query, TAPPSSqliteSelectTable* select_table)
502 sqlite3* h_db = (sqlite3*)pDB;
504 DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s", h_db,query);
505 TAPPSDbApiLock Dblock;
506 select_table->handle = h_db;
508 DRM_TAPPS_FRQ_LOG("select_table->handle=%x", select_table->handle);
510 int count_try_db_select = 0;
514 rc = sqlite3_get_table(h_db, query, &select_table->result, &select_table->n_rows, &select_table->n_cols, NULL);
516 DRM_TAPPS_FRQ_LOG("SELECT rc=%d", rc);
520 if (rc == SQLITE_BUSY)
523 DRM_TAPPS_FRQ_LOG("SELECT Tried [%d] times to select \n",count_try_db_select);
525 count_try_db_select++;
527 if(count_try_db_select >= (int)__DTAPPS_DB_SQLITE_RETRY__)
529 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);
530 DRM_TAPPS_EXCEPTION("query=[%s] \n",query);
537 DRM_TAPPS_EXCEPTION("SELECT h_db=0x%x rc=%d ErrMsg:%s \n",h_db,rc,sqlite3_errmsg(h_db));
538 DRM_TAPPS_EXCEPTION("%s query=[%s] \n",query);
545 DRM_TAPPS_FRQ_LOG("SELECT SUCCESS count_try_db_select=%d \n",count_try_db_select);
551 DRM_TAPPS_FRQ_LOG("n_rows=%d n_cols=%d \n",select_table->n_rows, select_table->n_cols);
556 extern const char* dtappsCreateTableSQLData[];
558 char* DTappsGetSQLCreateTable(const char* tableName)
567 DRM_TAPPS_FRQ_LOG("tableName = %s",tableName);
569 while(dtappsCreateTableSQLData[index])
571 if(TAPPS_strnicmp( dtappsCreateTableSQLData[index],tableName,TAPPS_STRLEN(tableName))==0)
573 char* sql = (char*)dtappsCreateTableSQLData[index];
574 sql = sql + TAPPS_STRLEN(dtappsCreateTableSQLData[index])+1;
576 DRM_TAPPS_FRQ_LOG("sql query = %s",sql);
586 DRM_TAPPS_EXCEPTION("Specified Table Name is not Valid!!!");
591 void* DTappsStmtPrepare(void* pDB, const char* query)
594 sqlite3* h_db = (sqlite3*)pDB;
595 sqlite3_stmt* stmt = 0;
597 DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s \n",h_db,query);
599 TAPPSDbApiLock Dblock;
601 if(0 == TAPPS_strnicmp(query, "SELECT", 6))
603 DRM_TAPPS_EXCEPTION("Doesn't support 'Select' query h_db=0x%x query=%s \n",h_db,query);
608 rc = sqlite3_prepare( h_db, query, -1, &stmt, NULL);
610 DRM_TAPPS_FRQ_LOG("%s: rc=%d query=%s stmt=0x%x \n",__func__,rc,query,stmt);
614 DRM_TAPPS_EXCEPTION("DTappsStmtPrepare: h_db=0x%x err: %s \n",h_db,sqlite3_errmsg(h_db));
615 DRM_TAPPS_EXCEPTION("DTappsStmtPrepare: query: %s\n", query);
624 int DTappsStmtBindParam (
629 unsigned int dParamSize )
632 sqlite3_stmt* stmt = (sqlite3_stmt*)pStmt;
634 DRM_TAPPS_FRQ_LOG("Enter \n");
636 TAPPSDbApiLock Dblock;
640 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: stmt is NULL \n");
647 * The parameter index in SQL starts from 1 in sqlite3.
651 case TAPPSDB_TYPE_NONE:
652 rc = sqlite3_bind_null( stmt, dIdx+1);
656 case TAPPSDB_TYPE_INT:
657 rc = sqlite3_bind_int( stmt, dIdx+1, *(int*)pParam);
658 DRM_TAPPS_FRQ_LOG(" rc=%d type=%d pParam=%d \n",rc,Type,*(int*)pParam);
662 case TAPPSDB_TYPE_DATETIME:
664 case TAPPSDB_TYPE_CHAR:
666 case TAPPSDB_TYPE_VARCHAR:
667 rc = sqlite3_bind_text( stmt, dIdx+1, (char*)pParam, dParamSize, SQLITE_TRANSIENT);
668 DRM_TAPPS_FRQ_LOG("rc=%d type=%d dParamSize=%d pParam=%s \n",rc,Type,dParamSize,pParam);
672 case TAPPSDB_TYPE_BINARY:
674 case TAPPSDB_TYPE_BLOB:
678 unsigned int packet64_size=0;
679 if(CMStringUtil::GetBase64Encode((unsigned char*)pParam,dParamSize, &packet64) == true)
681 packet64_size = MSTRLEN(packet64);
682 rc = sqlite3_bind_blob( stmt, dIdx+1, packet64, packet64_size, SQLITE_TRANSIENT);
683 DRM_OMA_FRQ_LOG("%s: rc=%d type=%d packet64_size=%d packet64=%s \n",__func__,rc,Type,packet64_size,packet64);
684 if(packet64) MDELETES0(packet64);
690 case TAPPSDB_TYPE_UNKNOWN:
698 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: errno: %d\n", rc);
707 int DTappsStmtExecute (void* pStmt)
709 int CntTryStmtExe=0, rc =-1;
710 sqlite3_stmt* stmt = (sqlite3_stmt*)pStmt;
712 DRM_TAPPS_FRQ_LOG("%s:stmt=0x%x \n",__func__,stmt);
714 TAPPSDbApiLock Dblock;
718 DRM_TAPPS_EXCEPTION("stmt is NULL \n");
725 rc = sqlite3_step(stmt);
727 DRM_TAPPS_FRQ_LOG(" rc=%d \n",rc);
729 if ( rc != SQLITE_DONE )
731 if(rc == SQLITE_BUSY)
734 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute stmt \n",CntTryStmtExe);
738 if(CntTryStmtExe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
740 DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d CntTryStmtExe=%d \n",stmt,rc,CntTryStmtExe);
747 DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d \n",stmt,rc);
754 DRM_TAPPS_FRQ_LOG("STMT EXECUTE SUCCESS CntTryStmtExe=%d \n",CntTryStmtExe);
762 int DTappsStmtRelease (void* pStmt)
765 sqlite3_stmt* stmt = (sqlite3_stmt*)pStmt;
766 DRM_TAPPS_FRQ_LOG("%s:Enter \n",__func__);
767 TAPPSDbApiLock Dblock;
773 rc = sqlite3_finalize (stmt);
775 DRM_TAPPS_FRQ_LOG("rc=%d \n",rc);
777 if ( rc != SQLITE_OK )
779 DRM_TAPPS_EXCEPTION("DTappsStmtRelease() Errmsg ; %d \n", rc);
788 BOOL DTapps_DB_Install(const char* sql_query)
791 BOOL ret_value = FALSE;
793 if(sql_query == NULL)
795 DRM_TAPPS_EXCEPTION("Parameter NULL!!!, sql_query = %p",sql_query);
800 DRM_TAPPS_LOG("Open DB......");
802 ret_value = DTappsDBOpen(pDb,__func__);
804 if(ret_value != TRUE)
806 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
811 DRM_TAPPS_LOG("Begin Transaction........");
813 ret_value = DTappsDBBeginImmedTrans(__func__);
815 if(ret_value != TRUE)
817 DRM_TAPPS_EXCEPTION("DB Begin Transaction ret_value = %d",ret_value);
822 DRM_TAPPS_LOG("Execute SQL to Insert Contents into Table........");
824 ret_value = DTappsExecuteSQL(pDb,sql_query);
826 if(ret_value != TRUE)
828 DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d",ret_value);
833 DRM_TAPPS_LOG("Commit DB........");
835 ret_value = DTappsDBCommit(__func__);
837 if(ret_value != TRUE)
839 DRM_TAPPS_EXCEPTION("Commit DB Failed!! ret_value = %d",ret_value);
844 DRM_TAPPS_LOG("Close DB........");
846 ret_value = DTappsDBClose(__func__);
848 if(ret_value != TRUE)
850 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
855 DRM_TAPPS_LOG("Install DB Operartion Successful!!!");
861 ret_value = DTappsDBRollback (__func__);
863 if(ret_value != TRUE)
865 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
868 ret_value = DTappsDBClose(__func__);
870 if(ret_value != TRUE)
872 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
878 BOOL DTapps_Read_DB(const char* sql_query, TAPPSSqliteSelectTable* select_table)
881 BOOL ret_value = FALSE;
883 if(sql_query == NULL || select_table == NULL)
885 DRM_TAPPS_EXCEPTION("Parameters NULL, sql_query = %p, select_table = %p",sql_query,select_table);
890 DRM_TAPPS_LOG("Open DB......");
892 ret_value = DTappsDBOpen(pDb,__func__);
894 if(ret_value != TRUE)
896 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
901 DRM_TAPPS_LOG("Begin Transaction........");
903 ret_value = DTappsDBBeginImmedTrans(__func__);
905 if(ret_value != TRUE)
907 DRM_TAPPS_EXCEPTION("DB Begin Transaction Failed!! ret_value = %d",ret_value);
912 DRM_TAPPS_LOG("Get the Result Table........");
914 ret_value = DTappsSQLGetTable(pDb,sql_query,select_table);
916 if(ret_value != TRUE)
918 DRM_TAPPS_EXCEPTION("DB Get Table failed!! ret_value = %d",ret_value);
923 DRM_TAPPS_LOG("Close DB........");
925 ret_value = DTappsDBClose(__func__);
927 if(ret_value != TRUE)
929 DRM_TAPPS_EXCEPTION("DB Close failed!! ret_value = %d",ret_value);
934 DRM_TAPPS_LOG("Reading from DB Successful!!!");
940 ret_value = DTappsDBRollback (__func__);
942 if(ret_value != TRUE)
944 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
947 ret_value = DTappsDBClose(__func__);
949 if(ret_value != TRUE)
951 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
954 DRM_TAPPS_EXCEPTION("Reading DB function failed!!!");