Fix some svace issues for unchecking return value
[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(long long int restriction_id)
241 {
242         bool ret = FALSE;
243         int rc;
244         long long int 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                 goto handle_error;
250         }
251
252         rc = sqlite3_step(stmt);
253
254         switch (rc) {
255         case SQLITE_DONE:
256                 break;
257         case SQLITE_ROW:
258                 l_restriction_id = sqlite3_column_int64(stmt, 0);
259                 STC_LOGD("restriction id [%llu]", l_restriction_id);
260                 ret = TRUE;
261                 break;
262         case SQLITE_ERROR:
263         default:
264                 STC_LOGE("Failed to get restriction id : %s",
265                         sqlite3_errmsg(stc_db_get_database()));
266         }
267
268 handle_error:
269         rc = sqlite3_reset(stmt);
270         if (rc != SQLITE_OK)
271                 ret = FALSE;
272
273         return ret;
274 }
275
276 API stc_error_e table_counters_get(long long int restriction_id,
277                                table_counters_info *info)
278 {
279         stc_error_e error_code = STC_ERROR_NONE;
280         sqlite3_stmt *stmt = select_counter;
281         int rc;
282
283         if (info == NULL)
284                 goto handle_error;
285
286         info->restriction_id = restriction_id;
287
288         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
289
290         do {
291                 rc = sqlite3_step(stmt);
292
293                  //LCOV_EXCL_START
294                 switch (rc) {
295                 case SQLITE_DONE:
296                         break;
297                 case SQLITE_ROW:
298                         info->data_counter = sqlite3_column_int64(stmt, 0);
299                         info->warn_counter = sqlite3_column_int64(stmt, 1);
300                         info->monthly_counter = sqlite3_column_int64(stmt, 2);
301                         info->weekly_counter = sqlite3_column_int64(stmt, 3);
302                         info->daily_counter = sqlite3_column_int64(stmt, 4);
303
304                         STC_LOGD("rstn_id[%llu] data[%lld] warn[%lld] "
305                                 "monthly[%lld] weekly[%lld] daily[%lld]",
306                                  restriction_id, info->data_counter,
307                                  info->warn_counter, info->monthly_counter,
308                                  info->weekly_counter, info->daily_counter);
309                         break;
310                 case SQLITE_ERROR:
311                 default:
312                         STC_LOGE("Failed to enumerate counters: %s\n",
313                                  sqlite3_errmsg(stc_db_get_database()));
314
315                         error_code = STC_ERROR_DB_FAILED;
316                         __STC_LOG_FUNC_EXIT__;
317                 }
318                  //LCOV_EXCL_STOP
319         } while (rc == SQLITE_ROW);
320
321 handle_error:
322         rc = sqlite3_reset(stmt);
323         if (rc != SQLITE_OK)
324                 error_code = STC_ERROR_DB_FAILED;
325
326         return error_code;
327 }
328
329 //LCOV_EXCL_START
330 API stc_error_e table_counters_update_counters(const table_counters_info *info)
331 {
332         stc_error_e error_code = STC_ERROR_NONE;
333         sqlite3_stmt *stmt = update_counter;
334
335         if (!info->data_counter) {
336                 error_code = STC_ERROR_INVALID_PARAMETER;
337                 goto handle_error;
338         }
339
340         if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
341                 stmt = insert_counter;
342
343         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
344         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
345         DB_ACTION(sqlite3_bind_int64(stmt, 3, info->warn_counter));
346         DB_ACTION(sqlite3_bind_int64(stmt, 4, info->monthly_counter));
347         DB_ACTION(sqlite3_bind_int64(stmt, 5, info->weekly_counter));
348         DB_ACTION(sqlite3_bind_int64(stmt, 6, info->daily_counter));
349
350         if (sqlite3_step(stmt) != SQLITE_DONE) {
351                 STC_LOGE("Failed to update counter: %s\n",
352                          sqlite3_errmsg(stc_db_get_database()));
353                 error_code = STC_ERROR_DB_FAILED;
354                 __STC_LOG_FUNC_EXIT__;
355                 goto handle_error;
356         }
357
358         STC_LOGD("Counter updated for restriction_id [%llu]",
359                  info->restriction_id);
360
361 handle_error:
362         if (sqlite3_reset(stmt) != SQLITE_OK)
363                 error_code = STC_ERROR_DB_FAILED;
364
365         return error_code;
366 }
367 //LCOV_EXCL_STOP
368
369 API stc_error_e table_counters_get_timestamps(long long int restriction_id,
370                                                                                 table_counters_info *info)
371 {
372         stc_error_e error_code = STC_ERROR_NONE;
373         sqlite3_stmt *stmt = select_timestamp;
374         int rc;
375
376         if (info == NULL) {
377                 __STC_LOG_FUNC_EXIT__;
378                 goto handle_error;
379         }
380
381         info->restriction_id = restriction_id;
382
383         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
384
385         do {
386                 rc = sqlite3_step(stmt);
387
388                  //LCOV_EXCL_START
389                 switch (rc) {
390                 case SQLITE_DONE:
391                         break;
392                 case SQLITE_ROW:
393                         info->month_start_date = sqlite3_column_int(stmt, 0);
394                         info->month_start_ts = sqlite3_column_int64(stmt, 1);
395                         info->week_start_ts = sqlite3_column_int64(stmt, 2);
396                         info->day_start_ts = sqlite3_column_int64(stmt, 3);
397
398                         STC_LOGD("rstn_id [%llu] month_start_date [%d], "
399                                 "month_start_ts [%lld], week_start_ts [%lld], "
400                                 "day_start_ts [%lld]", restriction_id,
401                                 info->month_start_date, info->month_start_ts,
402                                 info->week_start_ts, info->day_start_ts);
403                         break;
404                 case SQLITE_ERROR:
405                 default:
406                         STC_LOGE("Failed to enumerate counters: %s\n",
407                                  sqlite3_errmsg(stc_db_get_database()));
408
409                         error_code = STC_ERROR_DB_FAILED;
410                         __STC_LOG_FUNC_EXIT__;
411                 }
412                  //LCOV_EXCL_STOP
413         } while (rc == SQLITE_ROW);
414
415 handle_error:
416         rc = sqlite3_reset(stmt);
417         if (rc != SQLITE_OK)
418                 error_code = STC_ERROR_DB_FAILED;
419
420         return error_code;
421 }
422
423 //LCOV_EXCL_START
424 API stc_error_e table_counters_update_timestamps(const table_counters_info *info)
425 {
426         stc_error_e error_code = STC_ERROR_NONE;
427         sqlite3_stmt *stmt = update_timestamp;
428
429         if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
430                 stmt = insert_timestamp;
431
432         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
433         DB_ACTION(sqlite3_bind_int(stmt, 2, info->month_start_date));
434         DB_ACTION(sqlite3_bind_int64(stmt, 3, info->month_start_ts));
435         DB_ACTION(sqlite3_bind_int64(stmt, 4, info->week_start_ts));
436         DB_ACTION(sqlite3_bind_int64(stmt, 5, info->day_start_ts));
437
438         if (sqlite3_step(stmt) != SQLITE_DONE) {
439                 STC_LOGE("Failed to update timestamps: %s\n",
440                          sqlite3_errmsg(stc_db_get_database()));
441                 error_code = STC_ERROR_DB_FAILED;
442                 __STC_LOG_FUNC_EXIT__;
443                 goto handle_error;
444         }
445
446         STC_LOGD("Timestamps updated for restriction_id [%llu]",
447                  info->restriction_id);
448
449 handle_error:
450         if (sqlite3_reset(stmt) != SQLITE_OK)
451                 error_code = STC_ERROR_DB_FAILED;
452
453         return error_code;
454 }
455 //LCOV_EXCL_STOP
456
457
458 API stc_error_e table_counters_delete(long long int restriction_id)
459 {
460         stc_error_e error_code = STC_ERROR_NONE;
461         sqlite3_stmt *stmt = delete_counter;
462
463         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
464
465         if (sqlite3_step(stmt) != SQLITE_DONE) {
466                 STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
467                          sqlite3_errmsg(stc_db_get_database()));
468                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
469                 goto handle_error; //LCOV_EXCL_LINE
470         }
471
472         STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
473
474 handle_error:
475         if (sqlite3_reset(stmt) != SQLITE_OK)
476                 error_code = STC_ERROR_DB_FAILED;
477
478         return error_code;
479 }
480
481 stc_error_e table_counters_prepare(sqlite3 *db)
482 {
483         __STC_LOG_FUNC_ENTER__;
484
485         stc_error_e error_code = STC_ERROR_NONE;
486
487         if (db == NULL) {
488                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
489                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
490         }
491
492         DB_ACTION(__prepare_delete(db));
493         DB_ACTION(__prepare_select(db));
494         DB_ACTION(__prepare_update(db));
495         DB_ACTION(__prepare_insert(db));
496
497 handle_error:
498         __STC_LOG_FUNC_EXIT__;
499         return error_code;
500 }
501
502 void table_counters_finalize(void)
503 {
504         __STC_LOG_FUNC_ENTER__;
505         __finalize_delete();
506         __finalize_select();
507         __finalize_update();
508         __finalize_insert();
509         __STC_LOG_FUNC_EXIT__;
510 }