Remove C++ template declarance from C linkage
[platform/core/security/drm-service-core-tizen.git] / tappsd / src / db / DTapps2SqliteDB.cpp
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17 /**
18  * @file        DTapps2SqliteDB.cpp
19  * @brief       This file includes functions relating to DataBase.
20  */
21
22 #include "DTapps2SqliteDB.h"
23 #include <map>
24
25 /* Define EXPORT_API temporary */
26 #ifndef EXPORT_API
27 #define EXPORT_API __attribute__((visibility("default")))
28 #endif
29
30 static dtapps_mutex_t dtapps_sqlite_db_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 typedef long int dtappsThreadID;
33
34  class DtappsDBConnectionInfo
35  {
36  public:
37          DtappsDBConnectionInfo();
38          int     countOpenConnection;
39          sqlite3 *pDBConnection;
40  };
41
42  using namespace std;
43  static std::map<dtappsThreadID, DtappsDBConnectionInfo *> g_dtapps_sqlite_connection_table;
44
45  DtappsDBConnectionInfo::DtappsDBConnectionInfo()
46  {
47          countOpenConnection=0;
48          pDBConnection=NULL;
49  }
50
51  class TAPPSDbApiLock
52  {
53  public:
54         TAPPSDbApiLock();
55         ~TAPPSDbApiLock();
56  };
57
58  TAPPSDbApiLock::TAPPSDbApiLock()
59  {
60          DRM_TAPPS_FRQ_LOG("LOCK by TID = %ld",drmgettid());
61          if (0 != dtapps_mutex_lock (&dtapps_sqlite_db_mutex))
62          {
63                  DRM_TAPPS_EXCEPTION("Error while mutex locking.");
64          }
65  }
66
67  TAPPSDbApiLock::~TAPPSDbApiLock()
68  {
69          DRM_TAPPS_FRQ_LOG("UNLOCK by TID = %ld",drmgettid());
70          if (0 != dtapps_mutex_unlock(&dtapps_sqlite_db_mutex))
71          {
72                  DRM_TAPPS_EXCEPTION("Error while mutex unlocking");
73          }
74  }
75
76 #define __DTAPPS_DB_SQLITE_RETRY__  (10)
77
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"
81
82 /* DataBase Related API Wrappers */
83 BOOL DTappsDBOpen(void *&pDb, const char* CallingFun)
84 {
85         DRM_TAPPS_LOG("[%s]: started.Calling function = %s",__func__,CallingFun);
86
87         TAPPSDbApiLock Dblock;
88         dtappsThreadID id_curr_thread = drmgettid();
89
90         bool IsMemAllocated = false;
91         int     result = SQLITE_ERROR;
92         unsigned int pairCount = 0;
93         sqlite3*        h_db = NULL;
94         DtappsDBConnectionInfo *pDBConnectionInfo = NULL;
95
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);
97
98         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
99         DRM_TAPPS_FRQ_LOG("pairCount=[%u]", pairCount);
100
101         if (0 != pairCount)
102         {
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);
106
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);
110
111                 ++(pDBConnectionInfo->countOpenConnection);
112                 pDb = pDBConnectionInfo->pDBConnection;
113         }
114         else
115         {
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
120
121                 DRM_TAPPS_FRQ_LOG("Opening DB connection.");
122
123                 result = db_util_open(DTAPPS_DB_NAME, &h_db, 0);
124                 if (result != SQLITE_OK)
125                 {
126                         DRM_TAPPS_EXCEPTION("sqlite msg :[%d]%s",result, sqlite3_errmsg(h_db));
127                         DRM_TAPPS_SECURE_LOG("db name :%s", DTAPPS_DB_NAME);
128                         MTHROW_BL
129                 }
130
131                 DRM_TAPPS_FRQ_LOG("sqlite3_open() is success. h_db:%x", h_db);
132
133                 pDBConnectionInfo->countOpenConnection = 1;
134                 pDBConnectionInfo->pDBConnection = h_db; h_db = NULL;
135
136                 pDb = pDBConnectionInfo->pDBConnection;
137
138                 // Insert the node
139                 DRM_TAPPS_FRQ_LOG("pDBConnectionInfo->countOpenConnection=[%d], pDBConnectionInfo->pDBConnection=[0x%x]", pDBConnectionInfo->countOpenConnection, pDBConnectionInfo->pDBConnection);
140
141                 g_dtapps_sqlite_connection_table.insert(pair<dtappsThreadID, DtappsDBConnectionInfo *>(id_curr_thread, pDBConnectionInfo));pDBConnectionInfo = NULL;
142         }
143
144         DRM_TAPPS_LOG("This fn finishes successfully.");
145
146         return TRUE;
147
148 MCATCH_B
149
150         if (IsMemAllocated && pDBConnectionInfo)
151                 delete pDBConnectionInfo;
152
153         DRM_TAPPS_EXCEPTION("This fn fails");
154
155         return FALSE;
156 }
157
158 BOOL DTappsDBGet(void *& pDBConnection)
159 {
160         DRM_TAPPS_LOG("Inside %s", __func__);
161
162         TAPPSDbApiLock Dblock;
163         unsigned int pairCount;
164         dtappsThreadID id_curr_thread = drmgettid();
165         pDBConnection = NULL;
166
167         DRM_TAPPS_LOG("Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",getppid(),getpid(),drmgettid(), id_curr_thread);
168
169         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
170         MTRY_BL(0 != pairCount);
171
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);
173
174         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
175
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);
179
180         return TRUE;
181
182 MCATCH_B
183         DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
184
185         return FALSE;
186 }
187
188 BOOL DTappsDBClose(const char* CallingFun)
189 {
190         DRM_TAPPS_LOG("Inside %s Calling function = %s", __func__, CallingFun);
191
192         TAPPSDbApiLock Dblock;
193         unsigned int pairCount;
194         sqlite3_stmt*   pstmt   = NULL;
195         dtappsThreadID id_curr_thread = drmgettid();
196         int countConnection;
197         sqlite3 *pDBConnection = NULL;
198         int result = SQLITE_ERROR;
199
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);
201
202         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
203         MTRY_BL(0 != pairCount);
204
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);
206
207         --(g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection);
208         countConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection;
209
210         DRM_TAPPS_LOG(" countConnection=[%d] ", countConnection);
211
212         if (0 == countConnection)
213         {
214                 DRM_TAPPS_LOG("closing DB connection info ");
215
216                 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
217
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);
220
221                 g_dtapps_sqlite_connection_table.erase(id_curr_thread);
222
223                 DRM_TAPPS_LOG("finalizing all statements..pDBConnection=[0x%x]", pDBConnection);
224
225                 while ((pstmt = sqlite3_next_stmt(pDBConnection, pstmt)) != NULL) // prevent fix
226                 {
227                         DRM_TAPPS_LOG("finalizing DB statement..pstmt=[0x%x]", pstmt);
228                         sqlite3_finalize(pstmt);
229                 }
230
231                 DRM_TAPPS_LOG(" Closing DB connection..pDBConnection=[0x%x]", pDBConnection);
232
233                 result = db_util_close(pDBConnection);
234                 if(result != SQLITE_OK)
235                 {
236                         DRM_TAPPS_EXCEPTION("SQLite Close fails. errmsg:%s", sqlite3_errmsg(pDBConnection));
237                         MTHROW_BL
238                 }
239         }
240
241         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
242
243         return TRUE;
244
245 MCATCH_B
246
247         DRM_TAPPS_EXCEPTION(" This fn fails.. pDBConnection=[0x%x]", pDBConnection);
248
249         return FALSE;
250 }
251
252 BOOL DTappsDBBeginImmedTrans (const char* CallingFun)
253 {
254         DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__, CallingFun);
255
256         unsigned int pairCount;
257         dtappsThreadID id_curr_thread = drmgettid();
258         sqlite3* pDBConnection = NULL;
259
260         int count_try_db=0,rc = -1;
261
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);
263
264         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
265         MTRY_BL(0 != pairCount);
266
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);
268
269         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
270
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);
273
274         DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
275
276         do
277         {
278                 DRM_TAPPS_LOG("START BEGIN");
279                 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_BEGIN_IMMEDIATE, NULL, NULL, NULL);
280
281                 DRM_TAPPS_FRQ_LOG("START BEGIN rc=%d", rc);
282                 if (rc != SQLITE_OK)
283                 {
284                         DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
285                         if (rc == SQLITE_BUSY)
286                         {
287                                 dtapps_sleep(2, 0);
288                                 DRM_TAPPS_FRQ_LOG("Tried [%d] times to begin", count_try_db);
289                                 count_try_db++;
290
291                                 if (count_try_db >= (int)__DTAPPS_DB_SQLITE_RETRY__)
292                                 {
293                                         DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
294                                         MTHROW_BL
295                                 }
296                         }
297                         else
298                         {
299                                 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
300                                 MTHROW_BL
301                         }
302                 }
303                 else
304                 {
305                         DRM_TAPPS_LOG("begin SUCCESS count_try_db=%d", count_try_db);
306                         break;
307                 }
308         } while(1);
309
310         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
311
312         return TRUE;
313
314 MCATCH_B
315     DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
316     return FALSE;
317 }
318
319 BOOL DTappsDBCommit(const char* CallingFun)
320 {
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;
325
326         int count_try_db_commit=0,rc = -1;
327
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);
329
330         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
331         MTRY_BL(1 == pairCount);
332
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);
334
335         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
336
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);
340
341         do
342         {
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);
346
347                 if (rc != SQLITE_OK)
348                 {
349                         DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
350
351                         if (rc == SQLITE_BUSY)
352                         {
353                                 dtapps_sleep(2, 0);
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__)
357                                 {
358                                         DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
359                                         MTHROW_BL
360                                 }
361                         }
362                         else
363                         {
364                                 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
365                                 MTHROW_BL
366                         }
367                 }
368                 else
369                 {
370                         DRM_TAPPS_FRQ_LOG("Commit SUCCESS count_try_db_commit=%d", count_try_db_commit);
371                         break;
372                 }
373         } while(1);
374
375         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
376
377         return TRUE;
378
379 MCATCH_B
380     DRM_TAPPS_EXCEPTION("[%s] This fn fails.. pDBConnection=[0x%x]", pDBConnection);
381
382     return FALSE;
383 }
384
385 BOOL DTappsDBRollback (const char* CallingFun)
386 {
387         DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__,CallingFun);
388
389         unsigned int pairCount;
390         dtappsThreadID id_curr_thread = drmgettid();
391         sqlite3* pDBConnection = NULL;
392
393         int count_try_db_rollback = 0, rc = -1;
394
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);
396
397         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
398         MTRY_BL(1 == pairCount);
399
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);
401
402         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
403
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);
406
407         DRM_TAPPS_FRQ_LOG("[%s] pDBConnection=[0x%x]",pDBConnection);
408
409         do
410         {
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);
414
415                 if (rc != SQLITE_OK)
416                 {
417                         DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
418
419                         if(rc == SQLITE_BUSY)
420                         {
421                                 dtapps_sleep(2,0);
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__)
425                                 {
426                                         DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
427                                         MTHROW_BL
428                                 }
429                         }
430                         else
431                         {
432                                 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
433                                 MTHROW_BL
434                         }
435                 }
436                 else
437                 {
438                         DRM_TAPPS_FRQ_LOG("Rollback SUCCESS count_try_db_rollback=%d",count_try_db_rollback);
439                         break;
440                 }
441         } while(1);
442
443         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
444
445         return TRUE;
446
447 MCATCH_B
448     DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
449
450     return FALSE;
451 }
452
453
454 BOOL DTappsExecuteSQL(void* pDB, const char* query)
455 {
456         int count_try_db_exe = 0, rc = 0;
457         sqlite3* h_db = (sqlite3*)pDB;
458
459         TAPPSDbApiLock Dblock;
460
461         do
462         {
463                 rc = sqlite3_exec (h_db, query, NULL, NULL, NULL);
464                 DRM_TAPPS_FRQ_LOG("EXECUTE rc=%d", rc);
465
466                 if (rc != SQLITE_OK)
467                 {
468                         if (rc == SQLITE_BUSY)
469                         {
470                                 dtapps_sleep(2, 0);
471                                 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute",count_try_db_exe);
472                                 count_try_db_exe++;
473
474                                 if (count_try_db_exe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
475                                 {
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);
478                                         return FALSE;
479                                 }
480                         }
481                         else
482                         {
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);
485                                 return FALSE;
486                         }
487                 }
488                 else
489                 {
490                         DRM_TAPPS_FRQ_LOG("EXECUTE SUCCESS count_try_db_exe=%d", count_try_db_exe);
491                         break;
492                 }
493         } while(1);
494
495         return TRUE;
496 }
497
498
499 BOOL DTappsSQLGetTable(void* pDB, const char* query, TAPPSSqliteSelectTable* select_table)
500 {
501         int rc = 0;
502         sqlite3* h_db = (sqlite3*)pDB;
503
504         DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s", h_db,query);
505         TAPPSDbApiLock Dblock;
506         select_table->handle = h_db;
507
508         DRM_TAPPS_FRQ_LOG("select_table->handle=%x", select_table->handle);
509
510         int count_try_db_select = 0;
511
512         do
513         {
514                 rc = sqlite3_get_table(h_db, query, &select_table->result, &select_table->n_rows, &select_table->n_cols, NULL);
515
516                 DRM_TAPPS_FRQ_LOG("SELECT rc=%d", rc);
517
518                 if (rc != SQLITE_OK)
519                 {
520                         if (rc == SQLITE_BUSY)
521                         {
522                                 dtapps_sleep(2,0);
523                                 DRM_TAPPS_FRQ_LOG("SELECT  Tried [%d] times to select \n",count_try_db_select);
524
525                                 count_try_db_select++;
526
527                                 if(count_try_db_select >= (int)__DTAPPS_DB_SQLITE_RETRY__)
528                                 {
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);
531
532                                         return FALSE;
533                                 }
534                         }
535                         else
536                         {
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);
539
540                                 return FALSE;
541                         }
542                 }
543                 else
544                 {
545                         DRM_TAPPS_FRQ_LOG("SELECT SUCCESS count_try_db_select=%d \n",count_try_db_select);
546
547                         break;
548                 }
549         } while(1);
550
551         DRM_TAPPS_FRQ_LOG("n_rows=%d n_cols=%d \n",select_table->n_rows, select_table->n_cols);
552
553         return TRUE;
554 }
555
556 extern const char* dtappsCreateTableSQLData[];
557
558 char* DTappsGetSQLCreateTable(const char* tableName)
559 {
560         int index=0;
561
562         if(!tableName)
563         {
564                 return NULL;
565         }
566
567         DRM_TAPPS_FRQ_LOG("tableName = %s",tableName);
568
569         while(dtappsCreateTableSQLData[index])
570         {
571                 if(TAPPS_strnicmp( dtappsCreateTableSQLData[index],tableName,TAPPS_STRLEN(tableName))==0)
572                 {
573                         char* sql = (char*)dtappsCreateTableSQLData[index];
574                         sql = sql + TAPPS_STRLEN(dtappsCreateTableSQLData[index])+1;
575
576                         DRM_TAPPS_FRQ_LOG("sql query = %s",sql);
577
578                         return sql;
579                 }
580                 else
581                 {
582                         index++;
583                 }
584         }
585
586         DRM_TAPPS_EXCEPTION("Specified Table Name is not Valid!!!");
587
588         return NULL;
589 }
590
591 void* DTappsStmtPrepare(void* pDB, const char* query)
592 {
593         int rc = 0;
594         sqlite3*        h_db = (sqlite3*)pDB;
595         sqlite3_stmt*   stmt = 0;
596
597         DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s \n",h_db,query);
598
599         TAPPSDbApiLock Dblock;
600
601         if(0 == TAPPS_strnicmp(query, "SELECT", 6))
602         {
603                 DRM_TAPPS_EXCEPTION("Doesn't support 'Select' query h_db=0x%x query=%s \n",h_db,query);
604
605                 return NULL;
606         }
607
608         rc = sqlite3_prepare( h_db, query, -1, &stmt, NULL);
609
610         DRM_TAPPS_FRQ_LOG("%s: rc=%d query=%s stmt=0x%x \n",__func__,rc,query,stmt);
611
612         if (rc != SQLITE_OK)
613         {
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);
616
617                 return NULL;
618         }
619
620         return stmt;
621 }
622
623
624 int DTappsStmtBindParam (
625         void* pStmt,
626         unsigned int dIdx,
627         unsigned int Type,
628         void* pParam,
629         unsigned int dParamSize )
630 {
631         int rc = 0;
632         sqlite3_stmt*   stmt = (sqlite3_stmt*)pStmt;
633
634         DRM_TAPPS_FRQ_LOG("Enter \n");
635
636         TAPPSDbApiLock Dblock;
637
638         if(stmt == NULL)
639         {
640                 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: stmt is NULL \n");
641
642                 return FALSE;
643         }
644
645         /*
646          * Notice:
647          *  The parameter index in SQL starts from 1 in sqlite3.
648          */
649         switch(Type)
650         {
651         case TAPPSDB_TYPE_NONE:
652                 rc = sqlite3_bind_null( stmt, dIdx+1);
653
654                 break;
655
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);
659
660                 break;
661
662         case TAPPSDB_TYPE_DATETIME:
663
664         case TAPPSDB_TYPE_CHAR:
665
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);
669
670                 break;
671
672         case TAPPSDB_TYPE_BINARY:
673
674         case TAPPSDB_TYPE_BLOB:
675                 #if 0
676                 {
677                         char* packet64=NULL;
678                         unsigned int packet64_size=0;
679                         if(CMStringUtil::GetBase64Encode((unsigned char*)pParam,dParamSize, &packet64) == true)
680                         {
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);
685                         }
686                 }
687                 #endif
688                 break;
689
690         case TAPPSDB_TYPE_UNKNOWN:
691
692         default:
693                 break;
694         }//switch
695
696         if(rc != SQLITE_OK)
697         {
698                 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: errno: %d\n", rc);
699
700                 return FALSE;
701         }
702
703         return TRUE;
704 }
705
706
707 int DTappsStmtExecute (void* pStmt)
708 {
709         int CntTryStmtExe=0, rc =-1;
710         sqlite3_stmt*   stmt = (sqlite3_stmt*)pStmt;
711
712         DRM_TAPPS_FRQ_LOG("%s:stmt=0x%x \n",__func__,stmt);
713
714         TAPPSDbApiLock Dblock;
715
716         if(stmt == NULL)
717         {
718                 DRM_TAPPS_EXCEPTION("stmt is NULL \n");
719
720                 return FALSE;
721         }
722
723         do
724         {
725                 rc = sqlite3_step(stmt);
726
727                 DRM_TAPPS_FRQ_LOG(" rc=%d \n",rc);
728
729                 if ( rc != SQLITE_DONE )
730                 {
731                         if(rc == SQLITE_BUSY)
732                         {
733                                 dtapps_sleep(2,0);
734                                 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute stmt \n",CntTryStmtExe);
735
736                                 CntTryStmtExe++;
737
738                                 if(CntTryStmtExe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
739                                 {
740                                         DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d CntTryStmtExe=%d \n",stmt,rc,CntTryStmtExe);
741
742                                         return FALSE;
743                                 }
744                         }
745                         else
746                         {
747                                 DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d \n",stmt,rc);
748
749                                 return FALSE;
750                         }
751                 }
752                 else
753                 {
754                         DRM_TAPPS_FRQ_LOG("STMT EXECUTE SUCCESS CntTryStmtExe=%d \n",CntTryStmtExe);
755
756                         return TRUE;
757                 }
758         } while(1);
759 }
760
761
762 int DTappsStmtRelease (void* pStmt)
763 {
764         int     rc = 0;
765         sqlite3_stmt*   stmt = (sqlite3_stmt*)pStmt;
766         DRM_TAPPS_FRQ_LOG("%s:Enter \n",__func__);
767         TAPPSDbApiLock Dblock;
768         if (!stmt)
769         {
770                 return FALSE;
771         }
772
773         rc = sqlite3_finalize (stmt);
774
775         DRM_TAPPS_FRQ_LOG("rc=%d \n",rc);
776
777         if ( rc != SQLITE_OK )
778         {
779                 DRM_TAPPS_EXCEPTION("DTappsStmtRelease() Errmsg ; %d \n", rc);
780
781                 return FALSE;
782         }
783
784         return TRUE;
785 }
786
787
788 BOOL DTapps_DB_Install(const char* sql_query)
789 {
790         void *pDb = NULL;
791         BOOL ret_value = FALSE;
792
793         if(sql_query == NULL)
794         {
795                 DRM_TAPPS_EXCEPTION("Parameter NULL!!!, sql_query = %p",sql_query);
796
797                 return FALSE;
798         }
799
800         DRM_TAPPS_LOG("Open DB......");
801
802         ret_value = DTappsDBOpen(pDb,__func__);
803
804         if(ret_value != TRUE)
805         {
806                 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
807
808                 return FALSE;
809         }
810
811         DRM_TAPPS_LOG("Begin Transaction........");
812
813         ret_value = DTappsDBBeginImmedTrans(__func__);
814
815         if(ret_value != TRUE)
816         {
817                 DRM_TAPPS_EXCEPTION("DB Begin Transaction ret_value = %d",ret_value);
818
819                 goto Error_Exit;
820         }
821
822         DRM_TAPPS_LOG("Execute SQL to Insert Contents into Table........");
823
824         ret_value = DTappsExecuteSQL(pDb,sql_query);
825
826         if(ret_value != TRUE)
827         {
828                 DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d",ret_value);
829
830                 goto Error_Exit;
831         }
832
833         DRM_TAPPS_LOG("Commit DB........");
834
835         ret_value = DTappsDBCommit(__func__);
836
837         if(ret_value != TRUE)
838         {
839                 DRM_TAPPS_EXCEPTION("Commit DB Failed!! ret_value = %d",ret_value);
840
841                 goto Error_Exit;
842         }
843
844         DRM_TAPPS_LOG("Close DB........");
845
846         ret_value = DTappsDBClose(__func__);
847
848         if(ret_value != TRUE)
849         {
850                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
851
852                 goto Error_Exit;
853         }
854
855         DRM_TAPPS_LOG("Install DB Operartion Successful!!!");
856
857         return TRUE;
858
859 Error_Exit:
860
861         ret_value = DTappsDBRollback (__func__);
862
863         if(ret_value != TRUE)
864         {
865                 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
866         }
867
868         ret_value = DTappsDBClose(__func__);
869
870         if(ret_value != TRUE)
871         {
872                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
873         }
874
875         return FALSE;
876 }
877
878 BOOL DTapps_Read_DB(const char* sql_query, TAPPSSqliteSelectTable* select_table)
879 {
880         void *pDb = NULL;
881         BOOL ret_value = FALSE;
882
883         if(sql_query == NULL || select_table == NULL)
884         {
885                 DRM_TAPPS_EXCEPTION("Parameters NULL, sql_query = %p, select_table = %p",sql_query,select_table);
886
887                 return FALSE;
888         }
889
890         DRM_TAPPS_LOG("Open DB......");
891
892         ret_value = DTappsDBOpen(pDb,__func__);
893
894         if(ret_value != TRUE)
895         {
896                 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
897
898                 return FALSE;
899         }
900
901         DRM_TAPPS_LOG("Begin Transaction........");
902
903         ret_value = DTappsDBBeginImmedTrans(__func__);
904
905         if(ret_value != TRUE)
906         {
907                 DRM_TAPPS_EXCEPTION("DB Begin Transaction Failed!! ret_value = %d",ret_value);
908
909                 goto Error_Exit;
910         }
911
912         DRM_TAPPS_LOG("Get the Result Table........");
913
914         ret_value = DTappsSQLGetTable(pDb,sql_query,select_table);
915
916         if(ret_value != TRUE)
917         {
918                 DRM_TAPPS_EXCEPTION("DB Get Table failed!! ret_value = %d",ret_value);
919
920                 goto Error_Exit;
921         }
922
923         DRM_TAPPS_LOG("Close DB........");
924
925         ret_value = DTappsDBClose(__func__);
926
927         if(ret_value != TRUE)
928         {
929                 DRM_TAPPS_EXCEPTION("DB Close failed!! ret_value = %d",ret_value);
930
931                 goto Error_Exit;
932         }
933
934         DRM_TAPPS_LOG("Reading from DB Successful!!!");
935
936         return TRUE;
937
938 Error_Exit:
939
940         ret_value = DTappsDBRollback (__func__);
941
942         if(ret_value != TRUE)
943         {
944                 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
945         }
946
947         ret_value = DTappsDBClose(__func__);
948
949         if(ret_value != TRUE)
950         {
951                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
952         }
953
954         DRM_TAPPS_EXCEPTION("Reading DB function failed!!!");
955
956         return FALSE;
957 }