d2ad1ea1198c245ef06a31b79e6858ce6ccd5f0f
[platform/core/connectivity/stc-manager.git] / src / database / db-common.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include "stc-db.h"
18 #include "db-internal.h"
19 #include "table-statistics.h"
20 #include "table-restrictions.h"
21 #include "table-counters.h"
22
23 #ifndef DATABASE_FULL_PATH
24 #define DATABASE_FULL_PATH "/opt/usr/dbspace/.stc-manager-datausage.db"
25 #endif
26
27 #define SQLITE_BUSY_TIMEOUT 500000
28
29 static sqlite3 *database;
30
31 static int __stc_db_busy(void *user, int attempts)
32 {
33         __STC_LOG_FUNC_ENTER__;
34         STC_LOGE("DB locked by another process, attempts number %d",
35                  attempts);
36
37         usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
38         __STC_LOG_FUNC_EXIT__;
39         return 1;
40 }
41
42 stc_error_e stc_db_initialize_once(void)
43 {
44         __STC_LOG_FUNC_ENTER__;
45         int res = 0;
46         int retry_count = 0;
47         if (database != NULL) {
48                 __STC_LOG_FUNC_EXIT__;
49                 return STC_ERROR_NONE;
50         }
51
52         do {
53                 res = sqlite3_open(DATABASE_FULL_PATH, &database);
54                 if (res != SQLITE_OK) {
55                         STC_LOGD("Retry[%d] opening database %s: %s\n",
56                                  ++retry_count, DATABASE_FULL_PATH,
57                                  sqlite3_errmsg(database));
58                 } else {
59                         break;
60                 }
61                 usleep(MAX_USLEEP_TIMEOUT);
62         } while (retry_count <= MAX_DB_RETRY_COUNT);
63
64         if (res != SQLITE_OK) {
65                 STC_LOGE("Can't open database %s: %s\n", DATABASE_FULL_PATH,
66                          sqlite3_errmsg(database));
67                 __STC_LOG_FUNC_EXIT__;
68                 return STC_ERROR_DB_FAILED;
69         }
70
71         STC_LOGD("Successfully opened database");
72
73         res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
74         if (res != SQLITE_OK) {
75                 STC_LOGE("Can't set locking mode %s, skip set busy handler.",
76                          sqlite3_errmsg(database));
77                 sqlite3_close(database);
78                 __STC_LOG_FUNC_EXIT__;
79                 return STC_ERROR_DB_FAILED;
80         }
81
82         /* Set how many times we'll repeat our attempts for sqlite_step */
83         if (sqlite3_busy_handler(database, __stc_db_busy, NULL)
84             != SQLITE_OK)
85                 STC_LOGE("Couldn't set busy handler!");
86
87         __STC_LOG_FUNC_EXIT__;
88         return STC_ERROR_NONE;
89 }
90
91 sqlite3 *stc_db_get_database(void)
92 {
93         if (database == NULL)
94                 stc_db_initialize_once();
95
96         return database;
97 }
98
99 stc_error_e stc_db_initialize(void)
100 {
101         __STC_LOG_FUNC_ENTER__;
102         database = NULL;
103
104         stc_db_initialize_once();
105
106         EXEC(STC_ERROR_NONE, table_statistics_prepare(database));
107         EXEC(STC_ERROR_NONE, table_restrictions_prepare(database));
108         EXEC(STC_ERROR_NONE, table_counters_prepare(database));
109         EXEC(STC_ERROR_NONE, stc_init_db_guard());
110
111         __STC_LOG_FUNC_EXIT__;
112         return STC_ERROR_NONE;
113
114 handle_error:
115         stc_db_deinitialize();
116         __STC_LOG_FUNC_EXIT__;
117         return STC_ERROR_DB_FAILED;
118 }
119
120 gboolean stc_db_deinitialize(void)
121 {
122         __STC_LOG_FUNC_ENTER__;
123         if (database == NULL) {
124                 __STC_LOG_FUNC_EXIT__;
125                 return TRUE;
126         }
127
128         table_statistics_finalize();
129         table_restrictions_finalize();
130         table_counters_finalize();
131         sqlite3_close(database);
132
133         __STC_LOG_FUNC_EXIT__;
134         return TRUE;
135 }