11c99a4e6a7ca0293450f9b59eaac62bd311ec45
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-counters.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 /*
18  * This file implements counters entity handler methods.
19  *
20  * @file        table-counter.c
21  */
22
23 #include "stc-db.h"
24 #include "db-internal.h"
25 #include "table-counters.h"
26
27 #define DELETE_COUNTER "DELETE FROM counters WHERE restriction_id=?"
28
29 #define SELECT_COUNTER "SELECT data_counter FROM counters WHERE restriction_id=?"
30
31 #define INSERT_COUNTER "INSERT INTO counters (restriction_id, data_counter) " \
32         " VALUES (?, ?)"
33
34 #define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, data_counter) " \
35         " VALUES (?, ?)"
36
37 static void __finalize_delete(void);
38
39 #define PREPARE_DELETE(stm, query) do { \
40         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
41         if (rc != SQLITE_OK) { \
42                 stm = NULL; \
43                 __finalize_delete(); \
44                 STC_LOGE("Failed to prepare \"%s\"query" \
45                          , query); \
46                 return rc; \
47         } \
48 } while (0)
49
50 static void __finalize_select(void);
51
52 #define PREPARE_SELECT(stm, query) do { \
53         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
54         if (rc != SQLITE_OK) { \
55                 stm = NULL; \
56                 __finalize_select(); \
57                 STC_LOGE("Failed to prepare \"%s\"query" \
58                          , query); \
59                 return rc; \
60         } \
61 } while (0)
62
63 static void __finalize_update(void);
64
65 #define PREPARE_UPDATE(stm, query) do { \
66         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
67         if (rc != SQLITE_OK) { \
68                 stm = NULL; \
69                 __finalize_update(); \
70                 STC_LOGE("Failed to prepare \"%s\"query" \
71                          , query); \
72                 return rc; \
73         } \
74 } while (0)
75
76 #define FINALIZE(stm) do { \
77         if (stm) { \
78                 sqlite3_finalize(stm); \
79                 stm = NULL; \
80         } \
81 } while (0)
82
83 static sqlite3_stmt *delete_counter;
84 static sqlite3_stmt *select_counter;
85 static sqlite3_stmt *update_counter;
86
87 static int __prepare_delete(sqlite3 *db)
88 {
89         __STC_LOG_FUNC_ENTER__;
90         int rc;
91         static int initialized;
92
93         if (initialized) {
94                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
95                 return SQLITE_OK; //LCOV_EXCL_LINE
96         }
97
98         PREPARE_DELETE(delete_counter, DELETE_COUNTER);
99
100         initialized = 1;
101         __STC_LOG_FUNC_EXIT__;
102         return rc;
103 }
104
105 static void __finalize_delete(void)
106 {
107         __STC_LOG_FUNC_ENTER__;
108
109         FINALIZE(delete_counter);
110
111         __STC_LOG_FUNC_EXIT__;
112 }
113
114 static int __prepare_select(sqlite3 *db)
115 {
116         __STC_LOG_FUNC_ENTER__;
117         int rc;
118         static int initialized;
119
120         if (initialized) {
121                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
122                 return SQLITE_OK; //LCOV_EXCL_LINE
123         }
124
125         PREPARE_SELECT(select_counter, SELECT_COUNTER);
126
127         initialized = 1;
128         __STC_LOG_FUNC_EXIT__;
129         return rc;
130 }
131
132 static void __finalize_select(void)
133 {
134         __STC_LOG_FUNC_ENTER__;
135
136         FINALIZE(select_counter);
137
138         __STC_LOG_FUNC_EXIT__;
139 }
140
141 static int __prepare_update(sqlite3 *db)
142 {
143         __STC_LOG_FUNC_ENTER__;
144         int rc;
145         static int initialized;
146
147         if (initialized) {
148                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
149                 return SQLITE_OK; //LCOV_EXCL_LINE
150         }
151
152         PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
153
154         initialized = 1;
155         __STC_LOG_FUNC_EXIT__;
156         return rc;
157 }
158
159 static void __finalize_update(void)
160 {
161         __STC_LOG_FUNC_ENTER__;
162
163         FINALIZE(update_counter);
164
165         __STC_LOG_FUNC_EXIT__;
166 }
167
168 stc_error_e table_counters_get(uint64_t restriction_id,
169                                table_counters_info *info)
170 {
171         stc_error_e error_code = STC_ERROR_NONE;
172         sqlite3_stmt *stmt = select_counter;
173         int rc;
174
175         if (info == NULL)
176                 goto handle_error;
177
178         info->restriction_id = restriction_id;
179
180         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
181
182         do {
183                 rc = sqlite3_step(stmt);
184
185                  //LCOV_EXCL_START
186                 switch (rc) {
187                 case SQLITE_DONE:
188                         break;
189                 case SQLITE_ROW:
190                         info->data_counter = sqlite3_column_int64(stmt, 0);
191
192                         STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes",
193                                  restriction_id, info->data_counter);
194                         break;
195                 case SQLITE_ERROR:
196                 default:
197                         STC_LOGE("Failed to enumerate counters: %s\n",
198                                  sqlite3_errmsg(stc_db_get_database()));
199
200                         error_code = STC_ERROR_DB_FAILED;
201                 }
202                  //LCOV_EXCL_STOP
203         } while (rc == SQLITE_ROW);
204
205 handle_error:
206         sqlite3_reset(stmt);
207         return error_code;
208 }
209
210 //LCOV_EXCL_START
211 stc_error_e table_counters_update_counters(const table_counters_info *info)
212 {
213         stc_error_e error_code = STC_ERROR_NONE;
214         sqlite3_stmt *stmt = update_counter;
215
216         if (!info->data_counter) {
217                 error_code = STC_ERROR_INVALID_PARAMETER;
218                 goto handle_error;
219         }
220
221         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
222         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
223
224         if (sqlite3_step(stmt) != SQLITE_DONE) {
225                 STC_LOGE("Failed to update counter: %s\n",
226                          sqlite3_errmsg(stc_db_get_database()));
227                 error_code = STC_ERROR_DB_FAILED;
228                 __STC_LOG_FUNC_EXIT__;
229                 goto handle_error;
230         }
231
232         STC_LOGD("Counter updated for restriction_id [%llu]",
233                  info->restriction_id);
234
235 handle_error:
236         sqlite3_reset(stmt);
237         return error_code;
238 }
239 //LCOV_EXCL_STOP
240
241 stc_error_e table_counters_delete(uint64_t restriction_id)
242 {
243         stc_error_e error_code = STC_ERROR_NONE;
244         sqlite3_stmt *stmt = delete_counter;
245
246         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
247
248         if (sqlite3_step(stmt) != SQLITE_DONE) {
249                 STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
250                          sqlite3_errmsg(stc_db_get_database()));
251                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
252                 goto handle_error; //LCOV_EXCL_LINE
253         }
254
255         STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
256
257 handle_error:
258         sqlite3_reset(stmt);
259         return error_code;
260 }
261
262 stc_error_e table_counters_prepare(sqlite3 *db)
263 {
264         __STC_LOG_FUNC_ENTER__;
265
266         stc_error_e error_code = STC_ERROR_NONE;
267
268         if (db == NULL) {
269                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
270                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
271         }
272
273         DB_ACTION(__prepare_delete(db));
274         DB_ACTION(__prepare_select(db));
275         DB_ACTION(__prepare_update(db));
276
277 handle_error:
278         __STC_LOG_FUNC_EXIT__;
279         return error_code;
280 }
281
282 void table_counters_finalize(void)
283 {
284         __STC_LOG_FUNC_ENTER__;
285         __finalize_delete();
286         __finalize_select();
287         __finalize_update();
288         __STC_LOG_FUNC_EXIT__;
289 }