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