9bd49a23b26268d443b46d12b107d6a8d69b983d
[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_RESTRICTION_ID "SELECT restriction_id FROM counters " \
30         " WHERE restriction_id = ?"
31
32 #define SELECT_COUNTER "SELECT data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter " \
33         " FROM counters WHERE restriction_id = ?"
34
35 #define UPDATE_COUNTER "UPDATE counters " \
36         " SET data_counter = ?, warn_counter = ?, monthly_counter = ?, weekly_counter = ?, daily_counter = ? " \
37         " WHERE restriction_id = ?"
38
39 #define INSERT_COUNTER "INSERT INTO counters " \
40         " (restriction_id, data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter) " \
41         " VALUES (?, ?, ?, ?, ?, ?)"
42
43 #define SELECT_TIMESTAMP "SELECT month_start_date, month_start_ts, week_start_ts, day_start_ts " \
44         " FROM counters WHERE restriction_id = ?"
45
46 #define UPDATE_TIMESTAMP "UPDATE counters " \
47         " SET month_start_date = ?, month_start_ts = ?, week_start_ts = ?, day_start_ts = ? " \
48         " WHERE restriction_id = ?"
49
50 #define INSERT_TIMESTAMP "INSERT INTO counters " \
51         " (restriction_id, month_start_date, month_start_ts, week_start_ts, day_start_ts) " \
52         " VALUES (?, ?, ?, ?, ?)"
53
54 static void __finalize_delete(void);
55
56 #define PREPARE_DELETE(stm, query) do { \
57         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
58         if (rc != SQLITE_OK) { \
59                 stm = NULL; \
60                 __finalize_delete(); \
61                 STC_LOGE("Failed to prepare \"%s\"query" \
62                          , query); \
63                 return rc; \
64         } \
65 } while (0)
66
67 static void __finalize_select(void);
68
69 #define PREPARE_SELECT(stm, query) do { \
70         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
71         if (rc != SQLITE_OK) { \
72                 stm = NULL; \
73                 __finalize_select(); \
74                 STC_LOGE("Failed to prepare \"%s\"query" \
75                          , query); \
76                 return rc; \
77         } \
78 } while (0)
79
80 static void __finalize_update(void);
81
82 #define PREPARE_UPDATE(stm, query) do { \
83         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
84         if (rc != SQLITE_OK) { \
85                 stm = NULL; \
86                 __finalize_update(); \
87                 STC_LOGE("Failed to prepare \"%s\"query" \
88                          , query); \
89                 return rc; \
90         } \
91 } while (0)
92
93 static void __finalize_insert(void);
94
95 #define PREPARE_INSERT(stm, query) do { \
96                 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
97                 if (rc != SQLITE_OK) { \
98                         stm = NULL; \
99                         __finalize_insert(); \
100                         STC_LOGE("Failed to prepare \"%s\"query" \
101                                  , query); \
102                         return rc; \
103                 } \
104         } while (0)
105
106
107
108 #define FINALIZE(stm) do { \
109         if (stm) { \
110                 sqlite3_finalize(stm); \
111                 stm = NULL; \
112         } \
113 } while (0)
114
115 static sqlite3_stmt *delete_counter;
116 static sqlite3_stmt *select_restriction_id;
117 static sqlite3_stmt *select_counter;
118 static sqlite3_stmt *update_counter;
119 static sqlite3_stmt *insert_counter;
120 static sqlite3_stmt *select_timestamp;
121 static sqlite3_stmt *update_timestamp;
122 static sqlite3_stmt *insert_timestamp;
123
124 static int __prepare_delete(sqlite3 *db)
125 {
126         __STC_LOG_FUNC_ENTER__;
127         int rc;
128         static int initialized;
129
130         if (initialized) {
131                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
132                 return SQLITE_OK; //LCOV_EXCL_LINE
133         }
134
135         PREPARE_DELETE(delete_counter, DELETE_COUNTER);
136
137         initialized = 1;
138         __STC_LOG_FUNC_EXIT__;
139         return rc;
140 }
141
142 static void __finalize_delete(void)
143 {
144         __STC_LOG_FUNC_ENTER__;
145
146         FINALIZE(delete_counter);
147
148         __STC_LOG_FUNC_EXIT__;
149 }
150
151 static int __prepare_select(sqlite3 *db)
152 {
153         __STC_LOG_FUNC_ENTER__;
154         int rc;
155         static int initialized;
156
157         if (initialized) {
158                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
159                 return SQLITE_OK; //LCOV_EXCL_LINE
160         }
161
162         PREPARE_SELECT(select_counter, SELECT_COUNTER);
163         PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP);
164         PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
165
166         initialized = 1;
167         __STC_LOG_FUNC_EXIT__;
168         return rc;
169 }
170
171 static void __finalize_select(void)
172 {
173         __STC_LOG_FUNC_ENTER__;
174
175         FINALIZE(select_counter);
176         FINALIZE(select_timestamp);
177         FINALIZE(select_restriction_id);
178
179         __STC_LOG_FUNC_EXIT__;
180 }
181
182 static int __prepare_update(sqlite3 *db)
183 {
184         __STC_LOG_FUNC_ENTER__;
185         int rc;
186         static int initialized;
187
188         if (initialized) {
189                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
190                 return SQLITE_OK; //LCOV_EXCL_LINE
191         }
192
193         PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
194         PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP);
195
196         initialized = 1;
197         __STC_LOG_FUNC_EXIT__;
198         return rc;
199 }
200
201 static void __finalize_update(void)
202 {
203         __STC_LOG_FUNC_ENTER__;
204
205         FINALIZE(update_counter);
206         FINALIZE(update_timestamp);
207
208         __STC_LOG_FUNC_EXIT__;
209 }
210
211 static int __prepare_insert(sqlite3 *db)
212 {
213         __STC_LOG_FUNC_ENTER__;
214         int rc;
215         static int initialized;
216
217         if (initialized) {
218                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
219                 return SQLITE_OK; //LCOV_EXCL_LINE
220         }
221
222         PREPARE_INSERT(insert_counter, INSERT_COUNTER);
223         PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP);
224
225         initialized = 1;
226         __STC_LOG_FUNC_EXIT__;
227         return rc;
228 }
229
230 static void __finalize_insert(void)
231 {
232         __STC_LOG_FUNC_ENTER__;
233
234         FINALIZE(insert_counter);
235         FINALIZE(insert_timestamp);
236
237         __STC_LOG_FUNC_EXIT__;
238 }
239
240 static bool __table_counters_is_entry_present(uint64_t restriction_id)
241 {
242         bool ret = FALSE;
243         int rc;
244         uint64_t l_restriction_id = -1;
245         sqlite3_stmt *stmt = select_restriction_id;
246
247         if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) {
248                 ret = FALSE;
249                 __STC_LOG_FUNC_EXIT__;
250                 goto handle_error;
251         }
252
253         rc = sqlite3_step(stmt);
254
255         switch (rc) {
256         case SQLITE_DONE:
257                 break;
258         case SQLITE_ROW:
259                 l_restriction_id = sqlite3_column_int64(stmt, 0);
260                 STC_LOGD("restriction id [%llu]", l_restriction_id);
261                 ret = TRUE;
262                 break;
263         case SQLITE_ERROR:
264         default:
265                 STC_LOGE("Failed to get restriction id : %s",
266                         sqlite3_errmsg(stc_db_get_database()));
267         }
268
269 handle_error:
270         sqlite3_reset(stmt);
271         return ret;
272 }
273
274 stc_error_e table_counters_get(uint64_t restriction_id,
275                                table_counters_info *info)
276 {
277         stc_error_e error_code = STC_ERROR_NONE;
278         sqlite3_stmt *stmt = select_counter;
279         int rc;
280
281         if (info == NULL)
282                 goto handle_error;
283
284         info->restriction_id = restriction_id;
285
286         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
287
288         do {
289                 rc = sqlite3_step(stmt);
290
291                  //LCOV_EXCL_START
292                 switch (rc) {
293                 case SQLITE_DONE:
294                         break;
295                 case SQLITE_ROW:
296                         info->data_counter = sqlite3_column_int64(stmt, 0);
297                         info->warn_counter = sqlite3_column_int64(stmt, 1);
298                         info->monthly_counter = sqlite3_column_int64(stmt, 2);
299                         info->weekly_counter = sqlite3_column_int64(stmt, 3);
300                         info->daily_counter = sqlite3_column_int64(stmt, 4);
301
302                         STC_LOGD("rstn_id[%llu] data[%lld] warn[%lld] "
303                                 "monthly[%lld] weekly[%lld] daily[%lld]",
304                                  restriction_id, info->data_counter,
305                                  info->warn_counter, info->monthly_counter,
306                                  info->weekly_counter, info->daily_counter);
307                         break;
308                 case SQLITE_ERROR:
309                 default:
310                         STC_LOGE("Failed to enumerate counters: %s\n",
311                                  sqlite3_errmsg(stc_db_get_database()));
312
313                         error_code = STC_ERROR_DB_FAILED;
314                         __STC_LOG_FUNC_EXIT__;
315                 }
316                  //LCOV_EXCL_STOP
317         } while (rc == SQLITE_ROW);
318
319 handle_error:
320         sqlite3_reset(stmt);
321         return error_code;
322 }
323
324 //LCOV_EXCL_START
325 stc_error_e table_counters_update_counters(const table_counters_info *info)
326 {
327         stc_error_e error_code = STC_ERROR_NONE;
328         sqlite3_stmt *stmt = update_counter;
329
330         if (!info->data_counter) {
331                 error_code = STC_ERROR_INVALID_PARAMETER;
332                 goto handle_error;
333         }
334
335         if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
336                 stmt = insert_counter;
337
338         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
339         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
340         DB_ACTION(sqlite3_bind_int64(stmt, 3, info->warn_counter));
341         DB_ACTION(sqlite3_bind_int64(stmt, 4, info->monthly_counter));
342         DB_ACTION(sqlite3_bind_int64(stmt, 5, info->weekly_counter));
343         DB_ACTION(sqlite3_bind_int64(stmt, 6, info->daily_counter));
344
345         if (sqlite3_step(stmt) != SQLITE_DONE) {
346                 STC_LOGE("Failed to update counter: %s\n",
347                          sqlite3_errmsg(stc_db_get_database()));
348                 error_code = STC_ERROR_DB_FAILED;
349                 __STC_LOG_FUNC_EXIT__;
350                 goto handle_error;
351         }
352
353         STC_LOGD("Counter updated for restriction_id [%llu]",
354                  info->restriction_id);
355
356 handle_error:
357         sqlite3_reset(stmt);
358         return error_code;
359 }
360 //LCOV_EXCL_STOP
361
362 stc_error_e table_counters_get_timestamps(uint64_t restriction_id,
363                                                                                 table_counters_info *info)
364 {
365         stc_error_e error_code = STC_ERROR_NONE;
366         sqlite3_stmt *stmt = select_timestamp;
367         int rc;
368
369         if (info == NULL) {
370                 __STC_LOG_FUNC_EXIT__;
371                 goto handle_error;
372         }
373
374         info->restriction_id = restriction_id;
375
376         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
377
378         do {
379                 rc = sqlite3_step(stmt);
380
381                  //LCOV_EXCL_START
382                 switch (rc) {
383                 case SQLITE_DONE:
384                         break;
385                 case SQLITE_ROW:
386                         info->month_start_date = sqlite3_column_int(stmt, 0);
387                         info->month_start_ts = sqlite3_column_int64(stmt, 1);
388                         info->week_start_ts = sqlite3_column_int64(stmt, 2);
389                         info->day_start_ts = sqlite3_column_int64(stmt, 3);
390
391                         STC_LOGD("rstn_id [%llu] month_start_date [%d], "
392                                 "month_start_ts [%lld], week_start_ts [%lld], "
393                                 "day_start_ts [%lld]", restriction_id,
394                                 info->month_start_date, info->month_start_ts,
395                                 info->week_start_ts, info->day_start_ts);
396                         break;
397                 case SQLITE_ERROR:
398                 default:
399                         STC_LOGE("Failed to enumerate counters: %s\n",
400                                  sqlite3_errmsg(stc_db_get_database()));
401
402                         error_code = STC_ERROR_DB_FAILED;
403                         __STC_LOG_FUNC_EXIT__;
404                 }
405                  //LCOV_EXCL_STOP
406         } while (rc == SQLITE_ROW);
407
408 handle_error:
409         sqlite3_reset(stmt);
410         return error_code;
411 }
412
413 //LCOV_EXCL_START
414 stc_error_e table_counters_update_timestamps(const table_counters_info *info)
415 {
416         stc_error_e error_code = STC_ERROR_NONE;
417         sqlite3_stmt *stmt = update_timestamp;
418
419         if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
420                 stmt = insert_timestamp;
421
422         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
423         DB_ACTION(sqlite3_bind_int(stmt, 2, info->month_start_date));
424         DB_ACTION(sqlite3_bind_int64(stmt, 3, info->month_start_ts));
425         DB_ACTION(sqlite3_bind_int64(stmt, 4, info->week_start_ts));
426         DB_ACTION(sqlite3_bind_int64(stmt, 5, info->day_start_ts));
427
428         if (sqlite3_step(stmt) != SQLITE_DONE) {
429                 STC_LOGE("Failed to update timestamps: %s\n",
430                          sqlite3_errmsg(stc_db_get_database()));
431                 error_code = STC_ERROR_DB_FAILED;
432                 __STC_LOG_FUNC_EXIT__;
433                 goto handle_error;
434         }
435
436         STC_LOGD("Timestamps updated for restriction_id [%llu]",
437                  info->restriction_id);
438
439 handle_error:
440         sqlite3_reset(stmt);
441         return error_code;
442 }
443 //LCOV_EXCL_STOP
444
445
446 stc_error_e table_counters_delete(uint64_t restriction_id)
447 {
448         stc_error_e error_code = STC_ERROR_NONE;
449         sqlite3_stmt *stmt = delete_counter;
450
451         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
452
453         if (sqlite3_step(stmt) != SQLITE_DONE) {
454                 STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
455                          sqlite3_errmsg(stc_db_get_database()));
456                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
457                 goto handle_error; //LCOV_EXCL_LINE
458         }
459
460         STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
461
462 handle_error:
463         sqlite3_reset(stmt);
464         return error_code;
465 }
466
467 stc_error_e table_counters_prepare(sqlite3 *db)
468 {
469         __STC_LOG_FUNC_ENTER__;
470
471         stc_error_e error_code = STC_ERROR_NONE;
472
473         if (db == NULL) {
474                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
475                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
476         }
477
478         DB_ACTION(__prepare_delete(db));
479         DB_ACTION(__prepare_select(db));
480         DB_ACTION(__prepare_update(db));
481         DB_ACTION(__prepare_insert(db));
482
483 handle_error:
484         __STC_LOG_FUNC_EXIT__;
485         return error_code;
486 }
487
488 void table_counters_finalize(void)
489 {
490         __STC_LOG_FUNC_ENTER__;
491         __finalize_delete();
492         __finalize_select();
493         __finalize_update();
494         __finalize_insert();
495         __STC_LOG_FUNC_EXIT__;
496 }