2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "db-internal.h"
19 #include "table-statistics.h"
20 #include "table-restrictions.h"
21 #include "table-counters.h"
23 #ifndef DATABASE_FULL_PATH
24 #define DATABASE_FULL_PATH "/opt/usr/dbspace/.stc-manager-datausage.db"
27 #define SQLITE_BUSY_TIMEOUT 500000
29 static sqlite3 *database;
31 static int __stc_db_busy(void *user, int attempts)
33 __STC_LOG_FUNC_ENTER__;
34 STC_LOGE("DB locked by another process, attempts number %d",
37 usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
38 __STC_LOG_FUNC_EXIT__;
42 stc_error_e stc_db_initialize_once(void)
44 __STC_LOG_FUNC_ENTER__;
47 if (database != NULL) {
48 __STC_LOG_FUNC_EXIT__;
49 return STC_ERROR_NONE;
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));
61 usleep(MAX_USLEEP_TIMEOUT);
62 } while (retry_count <= MAX_DB_RETRY_COUNT);
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;
71 STC_LOGD("Successfully opened database");
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;
82 /* Set how many times we'll repeat our attempts for sqlite_step */
83 if (sqlite3_busy_handler(database, __stc_db_busy, NULL)
85 STC_LOGE("Couldn't set busy handler!");
87 __STC_LOG_FUNC_EXIT__;
88 return STC_ERROR_NONE;
91 sqlite3 *stc_db_get_database(void)
94 stc_db_initialize_once();
99 stc_error_e stc_db_initialize(void)
101 __STC_LOG_FUNC_ENTER__;
104 stc_db_initialize_once();
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());
111 __STC_LOG_FUNC_EXIT__;
112 return STC_ERROR_NONE;
115 stc_db_deinitialize();
116 __STC_LOG_FUNC_EXIT__;
117 return STC_ERROR_DB_FAILED;
120 gboolean stc_db_deinitialize(void)
122 __STC_LOG_FUNC_ENTER__;
123 if (database == NULL) {
124 __STC_LOG_FUNC_EXIT__;
128 table_statistics_finalize();
129 table_restrictions_finalize();
130 table_counters_finalize();
131 sqlite3_close(database);
133 __STC_LOG_FUNC_EXIT__;