Check validity of database
[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         int rc;
127         static int initialized;
128
129         if (initialized)
130                 return SQLITE_OK;
131
132         PREPARE_DELETE(delete_counter, DELETE_COUNTER);
133
134         initialized = 1;
135         return rc;
136 }
137
138 static void __finalize_delete(void)
139 {
140         FINALIZE(delete_counter);
141 }
142
143 static int __prepare_select(sqlite3 *db)
144 {
145         int rc;
146         static int initialized;
147
148         if (initialized)
149                 return SQLITE_OK;
150
151         PREPARE_SELECT(select_counter, SELECT_COUNTER);
152         PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP);
153         PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
154
155         initialized = 1;
156         return rc;
157 }
158
159 static void __finalize_select(void)
160 {
161         FINALIZE(select_counter);
162         FINALIZE(select_timestamp);
163         FINALIZE(select_restriction_id);
164 }
165
166 static int __prepare_update(sqlite3 *db)
167 {
168         int rc;
169         static int initialized;
170
171         if (initialized)
172                 return SQLITE_OK;
173
174         PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
175         PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP);
176
177         initialized = 1;
178         return rc;
179 }
180
181 static void __finalize_update(void)
182 {
183         FINALIZE(update_counter);
184         FINALIZE(update_timestamp);
185 }
186
187 static int __prepare_insert(sqlite3 *db)
188 {
189         int rc;
190         static int initialized;
191
192         if (initialized)
193                 return SQLITE_OK;
194
195         PREPARE_INSERT(insert_counter, INSERT_COUNTER);
196         PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP);
197
198         initialized = 1;
199         return rc;
200 }
201
202 static void __finalize_insert(void)
203 {
204         FINALIZE(insert_counter);
205         FINALIZE(insert_timestamp);
206 }
207
208 static bool __table_counters_is_entry_present(long long int restriction_id)
209 {
210         bool ret = FALSE;
211         int rc;
212         long long int l_restriction_id = -1;
213         sqlite3_stmt *stmt = select_restriction_id;
214
215         if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) {
216                 ret = FALSE;
217                 goto handle_error;
218         }
219
220         rc = sqlite3_step(stmt);
221
222         switch (rc) {
223         case SQLITE_DONE:
224                 break;
225         case SQLITE_ROW:
226                 l_restriction_id = sqlite3_column_int64(stmt, 0);
227                 STC_LOGD("restriction id [%llu]", l_restriction_id);
228                 ret = TRUE;
229                 break;
230         case SQLITE_ERROR:
231         default:
232                 STC_LOGE("Failed to get restriction id : %s",
233                         sqlite3_errmsg(stc_db_get_database()));
234         }
235
236 handle_error:
237         rc = sqlite3_reset(stmt);
238         if (rc != SQLITE_OK)
239                 ret = FALSE;
240
241         return ret;
242 }
243
244 API stc_error_e table_counters_get(long long int restriction_id,
245                                table_counters_info *info)
246 {
247         stc_error_e error_code = STC_ERROR_NONE;
248         sqlite3_stmt *stmt = select_counter;
249         int rc;
250
251         if (info == NULL)
252                 goto handle_error;
253
254         info->restriction_id = restriction_id;
255
256         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
257
258         do {
259                 rc = sqlite3_step(stmt);
260
261                  //LCOV_EXCL_START
262                 switch (rc) {
263                 case SQLITE_DONE:
264                         break;
265                 case SQLITE_ROW:
266                         info->data_counter = sqlite3_column_int64(stmt, 0);
267                         info->warn_counter = sqlite3_column_int64(stmt, 1);
268                         info->monthly_counter = sqlite3_column_int64(stmt, 2);
269                         info->weekly_counter = sqlite3_column_int64(stmt, 3);
270                         info->daily_counter = sqlite3_column_int64(stmt, 4);
271
272                         STC_LOGD("rstn_id[%llu] data[%lld] warn[%lld] "
273                                 "monthly[%lld] weekly[%lld] daily[%lld]",
274                                  restriction_id, info->data_counter,
275                                  info->warn_counter, info->monthly_counter,
276                                  info->weekly_counter, info->daily_counter);
277                         break;
278                 case SQLITE_ERROR:
279                 default:
280                         STC_LOGE("Failed to enumerate counters: %s\n",
281                                  sqlite3_errmsg(stc_db_get_database()));
282
283                         error_code = STC_ERROR_DB_FAILED;
284                         __STC_LOG_FUNC_EXIT__;
285                 }
286                  //LCOV_EXCL_STOP
287         } while (rc == SQLITE_ROW);
288
289 handle_error:
290         rc = sqlite3_reset(stmt);
291         if (rc != SQLITE_OK)
292                 error_code = STC_ERROR_DB_FAILED;
293
294         return error_code;
295 }
296
297 //LCOV_EXCL_START
298 API stc_error_e table_counters_update_counters(const table_counters_info *info)
299 {
300         stc_error_e error_code = STC_ERROR_NONE;
301         sqlite3_stmt *stmt = update_counter;
302
303         if (!info->data_counter) {
304                 error_code = STC_ERROR_INVALID_PARAMETER;
305                 goto handle_error;
306         }
307
308         if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
309                 stmt = insert_counter;
310
311         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
312         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
313         DB_ACTION(sqlite3_bind_int64(stmt, 3, info->warn_counter));
314         DB_ACTION(sqlite3_bind_int64(stmt, 4, info->monthly_counter));
315         DB_ACTION(sqlite3_bind_int64(stmt, 5, info->weekly_counter));
316         DB_ACTION(sqlite3_bind_int64(stmt, 6, info->daily_counter));
317
318         if (sqlite3_step(stmt) != SQLITE_DONE) {
319                 STC_LOGE("Failed to update counter: %s\n",
320                          sqlite3_errmsg(stc_db_get_database()));
321                 error_code = STC_ERROR_DB_FAILED;
322                 __STC_LOG_FUNC_EXIT__;
323                 goto handle_error;
324         }
325
326         STC_LOGD("Counter updated for restriction_id [%llu]",
327                  info->restriction_id);
328
329 handle_error:
330         if (sqlite3_reset(stmt) != SQLITE_OK)
331                 error_code = STC_ERROR_DB_FAILED;
332
333         return error_code;
334 }
335 //LCOV_EXCL_STOP
336
337 API stc_error_e table_counters_get_timestamps(long long int restriction_id,
338                                                                                 table_counters_info *info)
339 {
340         stc_error_e error_code = STC_ERROR_NONE;
341         sqlite3_stmt *stmt = select_timestamp;
342         int rc;
343
344         if (info == NULL) {
345                 __STC_LOG_FUNC_EXIT__;
346                 goto handle_error;
347         }
348
349         info->restriction_id = restriction_id;
350
351         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
352
353         do {
354                 rc = sqlite3_step(stmt);
355
356                  //LCOV_EXCL_START
357                 switch (rc) {
358                 case SQLITE_DONE:
359                         break;
360                 case SQLITE_ROW:
361                         info->month_start_date = sqlite3_column_int(stmt, 0);
362                         info->month_start_ts = sqlite3_column_int64(stmt, 1);
363                         info->week_start_ts = sqlite3_column_int64(stmt, 2);
364                         info->day_start_ts = sqlite3_column_int64(stmt, 3);
365
366                         STC_LOGD("rstn_id [%llu] month_start_date [%d], "
367                                 "month_start_ts [%lld], week_start_ts [%lld], "
368                                 "day_start_ts [%lld]", restriction_id,
369                                 info->month_start_date, info->month_start_ts,
370                                 info->week_start_ts, info->day_start_ts);
371                         break;
372                 case SQLITE_ERROR:
373                 default:
374                         STC_LOGE("Failed to enumerate counters: %s\n",
375                                  sqlite3_errmsg(stc_db_get_database()));
376
377                         error_code = STC_ERROR_DB_FAILED;
378                         __STC_LOG_FUNC_EXIT__;
379                 }
380                  //LCOV_EXCL_STOP
381         } while (rc == SQLITE_ROW);
382
383 handle_error:
384         rc = sqlite3_reset(stmt);
385         if (rc != SQLITE_OK)
386                 error_code = STC_ERROR_DB_FAILED;
387
388         return error_code;
389 }
390
391 //LCOV_EXCL_START
392 API stc_error_e table_counters_update_timestamps(const table_counters_info *info)
393 {
394         stc_error_e error_code = STC_ERROR_NONE;
395         sqlite3_stmt *stmt = update_timestamp;
396
397         if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
398                 stmt = insert_timestamp;
399
400         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
401         DB_ACTION(sqlite3_bind_int(stmt, 2, info->month_start_date));
402         DB_ACTION(sqlite3_bind_int64(stmt, 3, info->month_start_ts));
403         DB_ACTION(sqlite3_bind_int64(stmt, 4, info->week_start_ts));
404         DB_ACTION(sqlite3_bind_int64(stmt, 5, info->day_start_ts));
405
406         if (sqlite3_step(stmt) != SQLITE_DONE) {
407                 STC_LOGE("Failed to update timestamps: %s\n",
408                          sqlite3_errmsg(stc_db_get_database()));
409                 error_code = STC_ERROR_DB_FAILED;
410                 __STC_LOG_FUNC_EXIT__;
411                 goto handle_error;
412         }
413
414         STC_LOGD("Timestamps updated for restriction_id [%llu]",
415                  info->restriction_id);
416
417 handle_error:
418         if (sqlite3_reset(stmt) != SQLITE_OK)
419                 error_code = STC_ERROR_DB_FAILED;
420
421         return error_code;
422 }
423 //LCOV_EXCL_STOP
424
425
426 API stc_error_e table_counters_delete(long long int restriction_id)
427 {
428         stc_error_e error_code = STC_ERROR_NONE;
429         sqlite3_stmt *stmt = delete_counter;
430
431         DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
432
433         if (sqlite3_step(stmt) != SQLITE_DONE) {
434                 STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
435                          sqlite3_errmsg(stc_db_get_database()));
436                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
437                 goto handle_error; //LCOV_EXCL_LINE
438         }
439
440         STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
441
442 handle_error:
443         if (sqlite3_reset(stmt) != SQLITE_OK)
444                 error_code = STC_ERROR_DB_FAILED;
445
446         return error_code;
447 }
448
449 stc_error_e table_counters_prepare(sqlite3 *db)
450 {
451         __STC_LOG_FUNC_ENTER__;
452
453         stc_error_e error_code = STC_ERROR_NONE;
454
455         if (db == NULL) {
456                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
457                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
458         }
459
460         DB_ACTION(__prepare_delete(db));
461         DB_ACTION(__prepare_select(db));
462         DB_ACTION(__prepare_update(db));
463         DB_ACTION(__prepare_insert(db));
464
465 handle_error:
466         __STC_LOG_FUNC_EXIT__;
467         return error_code;
468 }
469
470 void table_counters_finalize(void)
471 {
472         __STC_LOG_FUNC_ENTER__;
473         __finalize_delete();
474         __finalize_select();
475         __finalize_update();
476         __finalize_insert();
477         __STC_LOG_FUNC_EXIT__;
478 }