Synchronize with tizen 2.4
[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 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 (true == IsMemAllocated)
151         {
152                 MDELETE(pDBConnectionInfo);
153         }
154         DRM_TAPPS_EXCEPTION("This fn fails");
155
156         return FALSE;
157 }
158
159 BOOL DTappsDBGet(void *& pDBConnection)
160 {
161         DRM_TAPPS_LOG("Inside %s", __func__);
162
163         TAPPSDbApiLock Dblock;
164         unsigned int pairCount;
165         dtappsThreadID id_curr_thread = drmgettid();
166         pDBConnection = NULL;
167
168         DRM_TAPPS_LOG("Parent Process ID=[%ld]:Process-ID=[%ld]:Thread-ID=[%ld], id_curr_thread=[%ld]",getppid(),getpid(),drmgettid(), id_curr_thread);
169
170         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
171         MTRY_BL(0 != pairCount);
172
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);
174
175         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
176
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);
180
181         return TRUE;
182
183 MCATCH_B
184         DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
185
186         return FALSE;
187 }
188
189 BOOL DTappsDBClose(const char* CallingFun)
190 {
191         DRM_TAPPS_LOG("Inside %s Calling function = %s", __func__, CallingFun);
192
193         TAPPSDbApiLock Dblock;
194         unsigned int pairCount;
195         sqlite3_stmt*   pstmt   = NULL;
196         dtappsThreadID id_curr_thread = drmgettid();
197         int countConnection;
198         sqlite3 *pDBConnection = NULL;
199         int result = SQLITE_ERROR;
200
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);
202
203         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
204         MTRY_BL(0 != pairCount);
205
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);
207
208         --(g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection);
209         countConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->countOpenConnection;
210
211         DRM_TAPPS_LOG(" countConnection=[%d] ", countConnection);
212
213         if (0 == countConnection)
214         {
215                 DRM_TAPPS_LOG("closing DB connection info ");
216
217                 pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
218
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);
221
222                 g_dtapps_sqlite_connection_table.erase(id_curr_thread);
223
224                 DRM_TAPPS_LOG("finalizing all statements..pDBConnection=[0x%x]", pDBConnection);
225
226                 while ((pstmt = sqlite3_next_stmt(pDBConnection, pstmt)) != NULL) // prevent fix
227                 {
228                         DRM_TAPPS_LOG("finalizing DB statement..pstmt=[0x%x]", pstmt);
229                         sqlite3_finalize(pstmt);
230                 }
231
232                 DRM_TAPPS_LOG(" Closing DB connection..pDBConnection=[0x%x]", pDBConnection);
233
234                 result = db_util_close(pDBConnection);
235                 if(result != SQLITE_OK)
236                 {
237                         DRM_TAPPS_EXCEPTION("SQLite Close fails. errmsg:%s", sqlite3_errmsg(pDBConnection));
238                         MTHROW_BL
239                 }
240         }
241
242         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
243
244         return TRUE;
245
246 MCATCH_B
247
248         DRM_TAPPS_EXCEPTION(" This fn fails.. pDBConnection=[0x%x]", pDBConnection);
249
250         return FALSE;
251 }
252
253 BOOL DTappsDBBeginImmedTrans (const char* CallingFun)
254 {
255         DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__, CallingFun);
256
257         unsigned int pairCount;
258         dtappsThreadID id_curr_thread = drmgettid();
259         sqlite3* pDBConnection = NULL;
260
261         int count_try_db=0,rc = -1;
262
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);
264
265         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
266         MTRY_BL(0 != pairCount);
267
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);
269
270         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
271
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);
274
275         DRM_TAPPS_FRQ_LOG("pDBConnection=[0x%x]",pDBConnection);
276
277         do
278         {
279                 DRM_TAPPS_LOG("START BEGIN");
280                 rc = sqlite3_exec (pDBConnection, DTAPPS_SQLITE3_SQL_BEGIN_IMMEDIATE, NULL, NULL, NULL);
281
282                 DRM_TAPPS_FRQ_LOG("START BEGIN rc=%d", rc);
283                 if (rc != SQLITE_OK)
284                 {
285                         DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
286                         if (rc == SQLITE_BUSY)
287                         {
288                                 dtapps_sleep(2, 0);
289                                 DRM_TAPPS_FRQ_LOG("Tried [%d] times to begin", count_try_db);
290                                 count_try_db++;
291
292                                 if (count_try_db >= (int)__DTAPPS_DB_SQLITE_RETRY__)
293                                 {
294                                         DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
295                                         MTHROW_BL
296                                 }
297                         }
298                         else
299                         {
300                                 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
301                                 MTHROW_BL
302                         }
303                 }
304                 else
305                 {
306                         DRM_TAPPS_LOG("begin SUCCESS count_try_db=%d", count_try_db);
307                         break;
308                 }
309         } while(1);
310
311         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
312
313         return TRUE;
314
315 MCATCH_B
316     DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
317     return FALSE;
318 }
319
320 BOOL DTappsDBCommit(const char* CallingFun)
321 {
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;
326
327         int count_try_db_commit=0,rc = -1;
328
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);
330
331         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
332         MTRY_BL(1 == pairCount);
333
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);
335
336         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
337
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);
341
342         do
343         {
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);
347
348                 if (rc != SQLITE_OK)
349                 {
350                         DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
351
352                         if (rc == SQLITE_BUSY)
353                         {
354                                 dtapps_sleep(2, 0);
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__)
358                                 {
359                                         DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
360                                         MTHROW_BL
361                                 }
362                         }
363                         else
364                         {
365                                 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s", pDBConnection, rc, sqlite3_errmsg(pDBConnection));
366                                 MTHROW_BL
367                         }
368                 }
369                 else
370                 {
371                         DRM_TAPPS_FRQ_LOG("Commit SUCCESS count_try_db_commit=%d", count_try_db_commit);
372                         break;
373                 }
374         } while(1);
375
376         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
377
378         return TRUE;
379
380 MCATCH_B
381     DRM_TAPPS_EXCEPTION("[%s] This fn fails.. pDBConnection=[0x%x]", pDBConnection);
382
383     return FALSE;
384 }
385
386 BOOL DTappsDBRollback (const char* CallingFun)
387 {
388         DRM_TAPPS_LOG("Inside %s, Calling function = %s", __func__,CallingFun);
389
390         unsigned int pairCount;
391         dtappsThreadID id_curr_thread = drmgettid();
392         sqlite3* pDBConnection = NULL;
393
394         int count_try_db_rollback = 0, rc = -1;
395
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);
397
398         pairCount = g_dtapps_sqlite_connection_table.count(id_curr_thread);
399         MTRY_BL(1 == pairCount);
400
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);
402
403         pDBConnection = g_dtapps_sqlite_connection_table[id_curr_thread]->pDBConnection;
404
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);
407
408         DRM_TAPPS_FRQ_LOG("[%s] pDBConnection=[0x%x]",pDBConnection);
409
410         do
411         {
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);
415
416                 if (rc != SQLITE_OK)
417                 {
418                         DRM_TAPPS_FRQ_LOG("pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
419
420                         if(rc == SQLITE_BUSY)
421                         {
422                                 dtapps_sleep(2,0);
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__)
426                                 {
427                                         DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
428                                         MTHROW_BL
429                                 }
430                         }
431                         else
432                         {
433                                 DRM_TAPPS_EXCEPTION("Error pDBConnection=0x%x rc=%d ErrMsg:%s",pDBConnection,rc,sqlite3_errmsg(pDBConnection));
434                                 MTHROW_BL
435                         }
436                 }
437                 else
438                 {
439                         DRM_TAPPS_FRQ_LOG("Rollback SUCCESS count_try_db_rollback=%d",count_try_db_rollback);
440                         break;
441                 }
442         } while(1);
443
444         DRM_TAPPS_LOG("This fn finishes successfully..pDBConnection=[0x%x]", pDBConnection);
445
446         return TRUE;
447
448 MCATCH_B
449     DRM_TAPPS_EXCEPTION("This fn fails.. pDBConnection=[0x%x]", pDBConnection);
450
451     return FALSE;
452 }
453
454
455 BOOL DTappsExecuteSQL(void* pDB, const char* query)
456 {
457         int count_try_db_exe = 0, rc = 0;
458         sqlite3* h_db = (sqlite3*)pDB;
459
460         TAPPSDbApiLock Dblock;
461
462         do
463         {
464                 rc = sqlite3_exec (h_db, query, NULL, NULL, NULL);
465                 DRM_TAPPS_FRQ_LOG("EXECUTE rc=%d", rc);
466
467                 if (rc != SQLITE_OK)
468                 {
469                         if (rc == SQLITE_BUSY)
470                         {
471                                 dtapps_sleep(2, 0);
472                                 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute",count_try_db_exe);
473                                 count_try_db_exe++;
474
475                                 if (count_try_db_exe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
476                                 {
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);
479                                         return FALSE;
480                                 }
481                         }
482                         else
483                         {
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);
486                                 return FALSE;
487                         }
488                 }
489                 else
490                 {
491                         DRM_TAPPS_FRQ_LOG("EXECUTE SUCCESS count_try_db_exe=%d", count_try_db_exe);
492                         break;
493                 }
494         } while(1);
495
496         return TRUE;
497 }
498
499
500 BOOL DTappsSQLGetTable(void* pDB, const char* query, TAPPSSqliteSelectTable* select_table)
501 {
502         int rc = 0;
503         sqlite3* h_db = (sqlite3*)pDB;
504
505         DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s", h_db,query);
506         TAPPSDbApiLock Dblock;
507         select_table->handle = h_db;
508
509         DRM_TAPPS_FRQ_LOG("select_table->handle=%x", select_table->handle);
510
511         int count_try_db_select = 0;
512
513         do
514         {
515                 rc = sqlite3_get_table(h_db, query, &select_table->result, &select_table->n_rows, &select_table->n_cols, NULL);
516
517                 DRM_TAPPS_FRQ_LOG("SELECT rc=%d", rc);
518
519                 if (rc != SQLITE_OK)
520                 {
521                         if (rc == SQLITE_BUSY)
522                         {
523                                 dtapps_sleep(2,0);
524                                 DRM_TAPPS_FRQ_LOG("SELECT  Tried [%d] times to select \n",count_try_db_select);
525
526                                 count_try_db_select++;
527
528                                 if(count_try_db_select >= (int)__DTAPPS_DB_SQLITE_RETRY__)
529                                 {
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);
532
533                                         return FALSE;
534                                 }
535                         }
536                         else
537                         {
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);
540
541                                 return FALSE;
542                         }
543                 }
544                 else
545                 {
546                         DRM_TAPPS_FRQ_LOG("SELECT SUCCESS count_try_db_select=%d \n",count_try_db_select);
547
548                         break;
549                 }
550         } while(1);
551
552         DRM_TAPPS_FRQ_LOG("n_rows=%d n_cols=%d \n",select_table->n_rows, select_table->n_cols);
553
554         return TRUE;
555 }
556
557 extern const char* dtappsCreateTableSQLData[];
558
559 char* DTappsGetSQLCreateTable(const char* tableName)
560 {
561         int index=0;
562
563         if(!tableName)
564         {
565                 return NULL;
566         }
567
568         DRM_TAPPS_FRQ_LOG("tableName = %s",tableName);
569
570         while(dtappsCreateTableSQLData[index])
571         {
572                 if(TAPPS_strnicmp( dtappsCreateTableSQLData[index],tableName,TAPPS_STRLEN(tableName))==0)
573                 {
574                         char* sql = (char*)dtappsCreateTableSQLData[index];
575                         sql = sql + TAPPS_STRLEN(dtappsCreateTableSQLData[index])+1;
576
577                         DRM_TAPPS_FRQ_LOG("sql query = %s",sql);
578
579                         return sql;
580                 }
581                 else
582                 {
583                         index++;
584                 }
585         }
586
587         DRM_TAPPS_EXCEPTION("Specified Table Name is not Valid!!!");
588
589         return NULL;
590 }
591
592 void* DTappsStmtPrepare(void* pDB, const char* query)
593 {
594         int rc = 0;
595         sqlite3*        h_db = (sqlite3*)pDB;
596         sqlite3_stmt*   stmt = 0;
597
598         DRM_TAPPS_FRQ_LOG("h_db=0x%x query=%s \n",h_db,query);
599
600         TAPPSDbApiLock Dblock;
601
602         if(0 == TAPPS_strnicmp(query, "SELECT", 6))
603         {
604                 DRM_TAPPS_EXCEPTION("Doesn't support 'Select' query h_db=0x%x query=%s \n",h_db,query);
605
606                 return NULL;
607         }
608
609         rc = sqlite3_prepare( h_db, query, -1, &stmt, NULL);
610
611         DRM_TAPPS_FRQ_LOG("%s: rc=%d query=%s stmt=0x%x \n",__func__,rc,query,stmt);
612
613         if (rc != SQLITE_OK)
614         {
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);
617
618                 return NULL;
619         }
620
621         return stmt;
622 }
623
624
625 int DTappsStmtBindParam (
626         void* pStmt,
627         unsigned int dIdx,
628         unsigned int Type,
629         void* pParam,
630         unsigned int dParamSize )
631 {
632         int rc = 0;
633         sqlite3_stmt*   stmt = (sqlite3_stmt*)pStmt;
634
635         DRM_TAPPS_FRQ_LOG("Enter \n");
636
637         TAPPSDbApiLock Dblock;
638
639         if(stmt == NULL)
640         {
641                 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: stmt is NULL \n");
642
643                 return FALSE;
644         }
645
646         /*
647          * Notice:
648          *  The parameter index in SQL starts from 1 in sqlite3.
649          */
650         switch(Type)
651         {
652         case TAPPSDB_TYPE_NONE:
653                 rc = sqlite3_bind_null( stmt, dIdx+1);
654
655                 break;
656
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);
660
661                 break;
662
663         case TAPPSDB_TYPE_DATETIME:
664
665         case TAPPSDB_TYPE_CHAR:
666
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);
670
671                 break;
672
673         case TAPPSDB_TYPE_BINARY:
674
675         case TAPPSDB_TYPE_BLOB:
676                 #if 0
677                 {
678                         char* packet64=NULL;
679                         unsigned int packet64_size=0;
680                         if(CMStringUtil::GetBase64Encode((unsigned char*)pParam,dParamSize, &packet64) == true)
681                         {
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);
686                         }
687                 }
688                 #endif
689                 break;
690
691         case TAPPSDB_TYPE_UNKNOWN:
692
693         default:
694                 break;
695         }//switch
696
697         if(rc != SQLITE_OK)
698         {
699                 DRM_TAPPS_EXCEPTION("DTappsStmtBindParam: errno: %d\n", rc);
700
701                 return FALSE;
702         }
703
704         return TRUE;
705 }
706
707
708 int DTappsStmtExecute (void* pStmt)
709 {
710         int CntTryStmtExe=0, rc =-1;
711         sqlite3_stmt*   stmt = (sqlite3_stmt*)pStmt;
712
713         DRM_TAPPS_FRQ_LOG("%s:stmt=0x%x \n",__func__,stmt);
714
715         TAPPSDbApiLock Dblock;
716
717         if(stmt == NULL)
718         {
719                 DRM_TAPPS_EXCEPTION("stmt is NULL \n");
720
721                 return FALSE;
722         }
723
724         do
725         {
726                 rc = sqlite3_step(stmt);
727
728                 DRM_TAPPS_FRQ_LOG(" rc=%d \n",rc);
729
730                 if ( rc != SQLITE_DONE )
731                 {
732                         if(rc == SQLITE_BUSY)
733                         {
734                                 dtapps_sleep(2,0);
735                                 DRM_TAPPS_FRQ_LOG("Tried [%d] times to execute stmt \n",CntTryStmtExe);
736
737                                 CntTryStmtExe++;
738
739                                 if(CntTryStmtExe >= (int)__DTAPPS_DB_SQLITE_RETRY__)
740                                 {
741                                         DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d CntTryStmtExe=%d \n",stmt,rc,CntTryStmtExe);
742
743                                         return FALSE;
744                                 }
745                         }
746                         else
747                         {
748                                 DRM_TAPPS_EXCEPTION("stmt=0x%x rc=%d \n",stmt,rc);
749
750                                 return FALSE;
751                         }
752                 }
753                 else
754                 {
755                         DRM_TAPPS_FRQ_LOG("STMT EXECUTE SUCCESS CntTryStmtExe=%d \n",CntTryStmtExe);
756
757                         return TRUE;
758                 }
759         } while(1);
760 }
761
762
763 int DTappsStmtRelease (void* pStmt)
764 {
765         int     rc = 0;
766         sqlite3_stmt*   stmt = (sqlite3_stmt*)pStmt;
767         DRM_TAPPS_FRQ_LOG("%s:Enter \n",__func__);
768         TAPPSDbApiLock Dblock;
769         if (!stmt)
770         {
771                 return FALSE;
772         }
773
774         rc = sqlite3_finalize (stmt);
775
776         DRM_TAPPS_FRQ_LOG("rc=%d \n",rc);
777
778         if ( rc != SQLITE_OK )
779         {
780                 DRM_TAPPS_EXCEPTION("DTappsStmtRelease() Errmsg ; %d \n", rc);
781
782                 return FALSE;
783         }
784
785         return TRUE;
786 }
787
788
789 BOOL DTapps_DB_Install(const char* sql_query)
790 {
791         void *pDb = NULL;
792         BOOL ret_value = FALSE;
793
794         if(sql_query == NULL)
795         {
796                 DRM_TAPPS_EXCEPTION("Parameter NULL!!!, sql_query = %p",sql_query);
797
798                 return FALSE;
799         }
800
801         DRM_TAPPS_LOG("Open DB......");
802
803         ret_value = DTappsDBOpen(pDb,__func__);
804
805         if(ret_value != TRUE)
806         {
807                 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
808
809                 return FALSE;
810         }
811
812         DRM_TAPPS_LOG("Begin Transaction........");
813
814         ret_value = DTappsDBBeginImmedTrans(__func__);
815
816         if(ret_value != TRUE)
817         {
818                 DRM_TAPPS_EXCEPTION("DB Begin Transaction ret_value = %d",ret_value);
819
820                 goto Error_Exit;
821         }
822
823         DRM_TAPPS_LOG("Execute SQL to Insert Contents into Table........");
824
825         ret_value = DTappsExecuteSQL(pDb,sql_query);
826
827         if(ret_value != TRUE)
828         {
829                 DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d",ret_value);
830
831                 goto Error_Exit;
832         }
833
834         DRM_TAPPS_LOG("Commit DB........");
835
836         ret_value = DTappsDBCommit(__func__);
837
838         if(ret_value != TRUE)
839         {
840                 DRM_TAPPS_EXCEPTION("Commit DB Failed!! ret_value = %d",ret_value);
841
842                 goto Error_Exit;
843         }
844
845         DRM_TAPPS_LOG("Close DB........");
846
847         ret_value = DTappsDBClose(__func__);
848
849         if(ret_value != TRUE)
850         {
851                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
852
853                 goto Error_Exit;
854         }
855
856         DRM_TAPPS_LOG("Install DB Operartion Successful!!!");
857
858         return TRUE;
859
860 Error_Exit:
861
862         ret_value = DTappsDBRollback (__func__);
863
864         if(ret_value != TRUE)
865         {
866                 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
867         }
868
869         ret_value = DTappsDBClose(__func__);
870
871         if(ret_value != TRUE)
872         {
873                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
874         }
875
876         return FALSE;
877 }
878
879 BOOL DTapps_Read_DB(const char* sql_query, TAPPSSqliteSelectTable* select_table)
880 {
881         void *pDb = NULL;
882         BOOL ret_value = FALSE;
883
884         if(sql_query == NULL || select_table == NULL)
885         {
886                 DRM_TAPPS_EXCEPTION("Parameters NULL, sql_query = %p, select_table = %p",sql_query,select_table);
887
888                 return FALSE;
889         }
890
891         DRM_TAPPS_LOG("Open DB......");
892
893         ret_value = DTappsDBOpen(pDb,__func__);
894
895         if(ret_value != TRUE)
896         {
897                 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
898
899                 return FALSE;
900         }
901
902         DRM_TAPPS_LOG("Begin Transaction........");
903
904         ret_value = DTappsDBBeginImmedTrans(__func__);
905
906         if(ret_value != TRUE)
907         {
908                 DRM_TAPPS_EXCEPTION("DB Begin Transaction Failed!! ret_value = %d",ret_value);
909
910                 goto Error_Exit;
911         }
912
913         DRM_TAPPS_LOG("Get the Result Table........");
914
915         ret_value = DTappsSQLGetTable(pDb,sql_query,select_table);
916
917         if(ret_value != TRUE)
918         {
919                 DRM_TAPPS_EXCEPTION("DB Get Table failed!! ret_value = %d",ret_value);
920
921                 goto Error_Exit;
922         }
923
924         DRM_TAPPS_LOG("Close DB........");
925
926         ret_value = DTappsDBClose(__func__);
927
928         if(ret_value != TRUE)
929         {
930                 DRM_TAPPS_EXCEPTION("DB Close failed!! ret_value = %d",ret_value);
931
932                 goto Error_Exit;
933         }
934
935         DRM_TAPPS_LOG("Reading from DB Successful!!!");
936
937         return TRUE;
938
939 Error_Exit:
940
941         ret_value = DTappsDBRollback (__func__);
942
943         if(ret_value != TRUE)
944         {
945                 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
946         }
947
948         ret_value = DTappsDBClose(__func__);
949
950         if(ret_value != TRUE)
951         {
952                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
953         }
954
955         DRM_TAPPS_EXCEPTION("Reading DB function failed!!!");
956
957         return FALSE;
958 }