[STC Manager] Support set/get for total data usage(Tx + Rx) in restriction.
[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__;
95                 return SQLITE_OK;
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__;
122                 return SQLITE_OK;
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__;
149                 return SQLITE_OK;
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                 switch (rc) {
186                 case SQLITE_DONE:
187                         break;
188                 case SQLITE_ROW:
189                         info->data_counter = sqlite3_column_int64(stmt, 0);
190
191                         STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes",
192                                  restriction_id, info->data_counter);
193                         break;
194                 case SQLITE_ERROR:
195                 default:
196                         STC_LOGE("Failed to enumerate counters: %s\n",
197                                  sqlite3_errmsg(stc_db_get_database()));
198
199                         error_code = STC_ERROR_DB_FAILED;
200                 }
201         } while (rc == SQLITE_ROW);
202
203 handle_error:
204         sqlite3_reset(stmt);
205         return error_code;
206 }
207
208 stc_error_e table_counters_update_counters(const table_counters_info *info)
209 {
210         stc_error_e error_code = STC_ERROR_NONE;
211         sqlite3_stmt *stmt = update_counter;
212
213         if (!info->data_counter) {
214                 error_code = STC_ERROR_INVALID_PARAMETER;
215                 goto handle_error;
216         }
217
218         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
219         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
220
221         if (sqlite3_step(stmt) != SQLITE_DONE) {
222                 STC_LOGE("Failed to update counter: %s\n",
223                          sqlite3_errmsg(stc_db_get_database()));
224                 error_code = STC_ERROR_DB_FAILED;
225                 __STC_LOG_FUNC_EXIT__;
226                 goto handle_error;
227         }
228
229         STC_LOGD("Counter updated for restriction_id [%llu]",
230                  info->restriction_id);
231
232 handle_error:
233         sqlite3_reset(stmt);
234         return error_code;
235 }
236
237 stc_error_e table_counters_delete(uint64_t restriction_id)
238 {
239         stc_error_e error_code = STC_ERROR_NONE;
240         sqlite3_stmt *stmt = delete_counter;
241
242         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
243
244         if (sqlite3_step(stmt) != SQLITE_DONE) {
245                 STC_LOGE("Failed to delete counter: %s\n",
246                          sqlite3_errmsg(stc_db_get_database()));
247                 error_code = STC_ERROR_DB_FAILED;
248                 goto handle_error;
249         }
250
251         STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
252
253 handle_error:
254         sqlite3_reset(stmt);
255         return error_code;
256 }
257
258 stc_error_e table_counters_prepare(sqlite3 *db)
259 {
260         __STC_LOG_FUNC_ENTER__;
261
262         stc_error_e error_code = STC_ERROR_NONE;
263
264         if (db == NULL) {
265                 __STC_LOG_FUNC_EXIT__;
266                 return STC_ERROR_FAIL;
267         }
268
269         DB_ACTION(__prepare_delete(db));
270         DB_ACTION(__prepare_select(db));
271         DB_ACTION(__prepare_update(db));
272
273 handle_error:
274         __STC_LOG_FUNC_EXIT__;
275         return error_code;
276 }
277
278 void table_counters_finalize(void)
279 {
280         __STC_LOG_FUNC_ENTER__;
281         __finalize_delete();
282         __finalize_select();
283         __finalize_update();
284         __STC_LOG_FUNC_EXIT__;
285 }