Fix some svace issues for unchecking return value
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-restrictions.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 restrictions entity handler methods.
19  *
20  * @file        table-restrictions.c
21  */
22
23 #include "stc-db.h"
24 #include "db-internal.h"
25 #include "table-restrictions.h"
26
27 /* DELETE statements */
28 #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \
29         " WHERE binpath = ? AND iftype = ? AND ifname = ? " \
30         " AND subscriber_id = ? AND roaming = ?"
31
32 /* SELECT statements */
33 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
34         " iftype, rstn_type, roaming, ifname, subscriber_id, " \
35         " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \
36         " restriction_id FROM restrictions"
37
38 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \
39         " iftype, rstn_type, roaming, ifname, subscriber_id, " \
40         " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \
41         " restriction_id " \
42         " FROM restrictions INDEXED BY restrictions_index " \
43         " WHERE binpath = ?"
44
45 #define SELECT_RESTRICTION_TYPE "SELECT rstn_type " \
46         " FROM restrictions INDEXED BY restrictions_index " \
47         " WHERE binpath = ? AND iftype = ?"
48
49 #define SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID "SELECT rstn_type " \
50         " FROM restrictions INDEXED BY restrictions_index " \
51         " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
52
53 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
54         " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \
55         " roaming = ? AND ifname = ?"
56
57 /* UPDATE statement */
58 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
59         " SET binpath = ?, data_limit = ?, iftype = ?, rstn_type = ?, " \
60         " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ?, " \
61         " monthly_limit = ?, weekly_limit = ?, daily_limit = ? " \
62         " WHERE restriction_id = ?"
63
64 /* INSERT statement */
65 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
66         " (binpath, data_limit, iftype, rstn_type, " \
67         " roaming, ifname, subscriber_id, data_warn_limit, " \
68         " monthly_limit, weekly_limit, daily_limit, month_start_date) " \
69         " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
70
71 static void __finalize_delete(void);
72
73 #define PREPARE_DELETE(stm, query) do { \
74         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
75         if (rc != SQLITE_OK) { \
76                 stm = NULL; \
77                 __finalize_delete(); \
78                 STC_LOGE("Failed to prepare \"%s\"query" \
79                          , query); \
80                 return rc; \
81         } \
82 } while (0)
83
84 static void __finalize_select(void);
85
86 #define PREPARE_SELECT(stm, query) do { \
87         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
88         if (rc != SQLITE_OK) { \
89                 stm = NULL; \
90                 __finalize_select(); \
91                 STC_LOGE("Failed to prepare \"%s\"query" \
92                          , query); \
93                 return rc; \
94         } \
95 } while (0)
96
97 static void __finalize_update(void);
98
99 #define PREPARE_UPDATE(stm, query) do { \
100         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
101         if (rc != SQLITE_OK) { \
102                 stm = NULL; \
103                 __finalize_update(); \
104                 STC_LOGE("Failed to prepare \"%s\"query" \
105                          , query); \
106                 return rc; \
107         } \
108 } while (0)
109
110 static void __finalize_insert(void);
111
112 #define PREPARE_INSERT(stm, query) do { \
113         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
114         if (rc != SQLITE_OK) { \
115                 stm = NULL; \
116                 __finalize_insert(); \
117                 STC_LOGE("Failed to prepare \"%s\"query" \
118                          , query); \
119                 return rc; \
120         } \
121 } while (0)
122
123 #define FINALIZE(stm) do { \
124         if (stm) { \
125                 sqlite3_finalize(stm); \
126                 stm = NULL; \
127         } \
128 } while (0)
129
130 /* DELETE statements */
131 static sqlite3_stmt *delete_restrictions;
132
133 /* SELECT statements */
134 static sqlite3_stmt *select_restriction;
135 static sqlite3_stmt *select_restriction_per_app;
136 static sqlite3_stmt *select_restriction_type;
137 static sqlite3_stmt *select_restriction_type_subscriber_id;
138 static sqlite3_stmt *select_restriction_id;
139
140 /* REPLACE statements */
141 static sqlite3_stmt *update_net_restrictions;
142
143 /* INSERT statements */
144 static sqlite3_stmt *insert_net_restrictions;
145
146 static int __prepare_delete(sqlite3 *db)
147 {
148         __STC_LOG_FUNC_ENTER__;
149         int rc;
150         static int initialized;
151
152         if (initialized) {
153                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
154                 return SQLITE_OK; //LCOV_EXCL_LINE
155         }
156
157         PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS);
158
159         initialized = 1;
160         __STC_LOG_FUNC_EXIT__;
161         return rc;
162 }
163
164 static void __finalize_delete(void)
165 {
166         __STC_LOG_FUNC_ENTER__;
167
168         FINALIZE(delete_restrictions);
169
170         __STC_LOG_FUNC_EXIT__;
171 }
172
173 static int __prepare_select(sqlite3 *db)
174 {
175         __STC_LOG_FUNC_ENTER__;
176         int rc;
177         static int initialized;
178
179         if (initialized) {
180                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
181                 return SQLITE_OK; //LCOV_EXCL_LINE
182         }
183
184         PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
185         PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
186         PREPARE_SELECT(select_restriction_type, SELECT_RESTRICTION_TYPE);
187         PREPARE_SELECT(select_restriction_type_subscriber_id, SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID);
188         PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
189
190         initialized = 1;
191         __STC_LOG_FUNC_EXIT__;
192         return rc;
193 }
194
195 static void __finalize_select(void)
196 {
197         __STC_LOG_FUNC_ENTER__;
198
199         FINALIZE(select_restriction);
200         FINALIZE(select_restriction_per_app);
201         FINALIZE(select_restriction_type);
202         FINALIZE(select_restriction_type_subscriber_id);
203         FINALIZE(select_restriction_id);
204
205         __STC_LOG_FUNC_EXIT__;
206 }
207
208 static int __prepare_replace(sqlite3 *db)
209 {
210         __STC_LOG_FUNC_ENTER__;
211         int rc;
212         static int initialized;
213
214         if (initialized) {
215                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
216                 return SQLITE_OK; //LCOV_EXCL_LINE
217         }
218
219         PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS);
220
221         initialized = 1;
222         __STC_LOG_FUNC_EXIT__;
223         return rc;
224 }
225
226 static void __finalize_update(void)
227 {
228         __STC_LOG_FUNC_ENTER__;
229
230         FINALIZE(update_net_restrictions);
231
232         __STC_LOG_FUNC_EXIT__;
233 }
234
235 static int __prepare_insert(sqlite3 *db)
236 {
237         __STC_LOG_FUNC_ENTER__;
238         int rc;
239         static int initialized;
240
241         if (initialized) {
242                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
243                 return SQLITE_OK; //LCOV_EXCL_LINE
244         }
245
246         PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS);
247
248         initialized = 1;
249         __STC_LOG_FUNC_EXIT__;
250         return rc;
251 }
252
253 static void __finalize_insert(void)
254 {
255         __STC_LOG_FUNC_ENTER__;
256
257         FINALIZE(insert_net_restrictions);
258
259         __STC_LOG_FUNC_EXIT__;
260 }
261
262 stc_error_e table_restrictions_per_app(const gchar* app_id,
263                                        const table_restrictions_info_cb restriction_cb,
264                                        void *user_data)
265 {
266         __STC_LOG_FUNC_ENTER__;
267         table_restrictions_info data;
268         int rc;
269         stc_error_e error_code = STC_ERROR_NONE;
270         sqlite3_stmt *stmt = select_restriction_per_app;
271
272         if (!app_id) {
273                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
274                 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
275         }
276
277         DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1,
278                                     SQLITE_TRANSIENT));
279         data.app_id = (char *)app_id;
280
281         do {
282                 rc = sqlite3_step(stmt);
283
284                 memset(&data, 0, sizeof(data));
285
286                 switch (rc) {
287                 case SQLITE_DONE:
288                         break;
289                 case SQLITE_ROW:
290                         data.app_id = (char *)sqlite3_column_text(stmt, 0);
291                         data.data_limit = sqlite3_column_int64(stmt, 1);
292                         data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
293                         data.rstn_type =
294                                 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
295                         data.roaming = sqlite3_column_int(stmt, 4);
296                         data.ifname = (char *)sqlite3_column_text(stmt, 5);
297                         data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
298                         data.data_warn_limit = sqlite3_column_int64(stmt, 7);
299                         data.monthly_limit = sqlite3_column_int64(stmt, 8);
300                         data.weekly_limit = sqlite3_column_int64(stmt, 9);
301                         data.daily_limit = sqlite3_column_int64(stmt, 10);
302                         data.restriction_id = sqlite3_column_int64(stmt, 11);
303
304                         if (restriction_cb(&data, user_data) == STC_CANCEL)
305                                 rc = SQLITE_DONE; //LCOV_EXCL_LINE
306                         break;
307                 case SQLITE_ERROR:
308                 default:
309                         STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
310                                  sqlite3_errmsg(stc_db_get_database()));
311
312                         __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
313                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
314                 }
315         } while (rc == SQLITE_ROW);
316
317 handle_error:
318         rc = sqlite3_reset(stmt);
319         if (rc != SQLITE_OK)
320                 error_code = STC_ERROR_DB_FAILED;
321
322         __STC_LOG_FUNC_EXIT__;
323         return error_code;
324 }
325
326 API stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
327                                        void *user_data)
328 {
329         __STC_LOG_FUNC_ENTER__;
330         table_restrictions_info data;
331         int rc;
332         stc_error_e error_code = STC_ERROR_NONE;
333         sqlite3_stmt *stmt = select_restriction;
334
335         do {
336                 rc = sqlite3_step(stmt);
337
338                 memset(&data, 0, sizeof(data));
339
340                 switch (rc) {
341                 case SQLITE_DONE:
342                         break;
343                 case SQLITE_ROW:
344                         data.app_id = (char *)sqlite3_column_text(stmt, 0);
345                         data.data_limit = sqlite3_column_int64(stmt, 1);
346                         data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
347                         data.rstn_type =
348                                 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
349                         data.roaming = sqlite3_column_int(stmt, 4);
350                         data.ifname = (char *)sqlite3_column_text(stmt, 5);
351                         data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
352                         data.data_warn_limit = sqlite3_column_int64(stmt, 7);
353                         data.monthly_limit = sqlite3_column_int64(stmt, 8);
354                         data.weekly_limit = sqlite3_column_int64(stmt, 9);
355                         data.daily_limit = sqlite3_column_int64(stmt, 10);
356                         data.month_start_date = sqlite3_column_int(stmt, 11);
357                         data.restriction_id = sqlite3_column_int64(stmt, 12);
358
359                         if (restriction_cb(&data, user_data) == STC_CANCEL)
360                                 rc = SQLITE_DONE; //LCOV_EXCL_LINE
361                         break;
362                 case SQLITE_ERROR:
363                 default:
364                         STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
365                                  sqlite3_errmsg(stc_db_get_database()));
366
367                         __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
368                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
369                 }
370         } while (rc == SQLITE_ROW);
371
372         rc = sqlite3_reset(stmt);
373         if (rc != SQLITE_OK)
374                 error_code = STC_ERROR_DB_FAILED;
375
376         __STC_LOG_FUNC_EXIT__;
377         return error_code;
378 }
379
380 stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *app_id,
381                                                           stc_iface_type_e iftype,
382                                                           const char *subscriber_id,
383                                                           stc_rstn_type_e *type)
384 {
385         __STC_LOG_FUNC_ENTER__;
386         int error_code = STC_ERROR_NONE;
387         int ret;
388         bool state_subscriber_id = 0;
389
390         if (type == NULL) {
391                 STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE
392                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
393                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
394         }
395
396         *type = STC_RSTN_TYPE_UNKNOWN;
397         ret = sqlite3_reset(select_restriction_type_subscriber_id);
398         if (ret != SQLITE_OK) {
399                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
400                 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
401         }
402
403         ret = sqlite3_reset(select_restriction_type);
404         if (ret != SQLITE_OK) {
405                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
406                 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
407         }
408
409         if (subscriber_id == NULL) {
410                 state_subscriber_id = 0;
411                 DB_ACTION(sqlite3_bind_text(select_restriction_type, 1,
412                                             app_id ? app_id : "", -1,
413                                             SQLITE_STATIC));
414                 DB_ACTION(sqlite3_bind_int(select_restriction_type, 2,
415                                            iftype));
416                 ret = sqlite3_step(select_restriction_type);
417         } else {
418                 state_subscriber_id = 1;
419                 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 1,
420                                             app_id ? app_id : "", -1,
421                                             SQLITE_STATIC));
422                 DB_ACTION(sqlite3_bind_int(select_restriction_type_subscriber_id, 2,
423                                            iftype));
424                 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 3,
425                                             subscriber_id, -1, SQLITE_STATIC));
426                 ret = sqlite3_step(select_restriction_type_subscriber_id);
427         }
428
429         switch (ret) {
430         case SQLITE_DONE:
431                 break;
432         case SQLITE_ROW:
433                 if (state_subscriber_id)
434                         *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type_subscriber_id, 0);
435                 else
436                         *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type, 0);
437                 break;
438         case SQLITE_ERROR:
439         default:
440                 STC_LOGE("Can't perform sql query: %s\n", //LCOV_EXCL_LINE
441                          sqlite3_errmsg(stc_db_get_database()));
442                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
443         }
444
445 handle_error:
446         ret = sqlite3_reset(select_restriction_type);
447         if (ret != SQLITE_OK)
448                 error_code = STC_ERROR_DB_FAILED;
449
450         ret = sqlite3_reset(select_restriction_type_subscriber_id);
451         if (ret != SQLITE_OK)
452                 error_code = STC_ERROR_DB_FAILED;
453
454         return error_code;
455 }
456
457 stc_error_e table_restrictions_get_restriction_type(const char *app_id,
458                                                      stc_iface_type_e iftype,
459                                                      stc_rstn_type_e *type)
460 {
461         __STC_LOG_FUNC_ENTER__;
462         __STC_LOG_FUNC_EXIT__;
463         return table_restrictions_get_restriction_type_subscriber_id(app_id, iftype,
464                                                              NULL, type);
465 }
466
467 stc_error_e table_restrictions_delete(const char *app_id,
468                                       const stc_iface_type_e iftype,
469                                       const char *ifname,
470                                       const char *subscriber_id,
471                                       const stc_roaming_type_e roaming)
472 {
473         stc_error_e error_code = STC_ERROR_NONE;
474         sqlite3_stmt *stmt = delete_restrictions;
475
476         STC_LOGD("app_id[%s] iftype[%d] ifname[%s] subscriber_id[%s] roaming[%d]",
477                  app_id, iftype, ifname, subscriber_id, roaming);
478
479         DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
480                                     -1, SQLITE_TRANSIENT));
481         DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
482         DB_ACTION(sqlite3_bind_text(stmt, 3, ifname ? ifname : "",
483                                     -1, SQLITE_TRANSIENT));
484         DB_ACTION(sqlite3_bind_text(stmt, 4, subscriber_id ? subscriber_id : "",
485                                         -1, SQLITE_TRANSIENT));
486         DB_ACTION(sqlite3_bind_int(stmt, 5, roaming));
487
488         if (sqlite3_step(stmt) != SQLITE_DONE) {
489                 STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
490                          sqlite3_errmsg(stc_db_get_database()));
491                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
492                 goto handle_error; //LCOV_EXCL_LINE
493         }
494
495         STC_LOGD("Restriction deleted for app_id [%s]", app_id);
496
497 handle_error:
498
499         if (sqlite3_reset(stmt) != SQLITE_OK)
500                 error_code = STC_ERROR_DB_FAILED;
501
502         return error_code;
503 }
504
505 stc_error_e __get_restriction_id(table_restrictions_info *info)
506 {
507         __STC_LOG_FUNC_ENTER__;
508         int rc;
509         stc_error_e error_code = STC_ERROR_NONE;
510         sqlite3_stmt *stmt = select_restriction_id;
511
512         DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
513                                     -1, SQLITE_TRANSIENT));
514         DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
515         DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "",
516                                     -1, SQLITE_TRANSIENT));
517         DB_ACTION(sqlite3_bind_int(stmt, 4, info->roaming));
518         DB_ACTION(sqlite3_bind_text(stmt, 5, info->ifname ? info->ifname : "",
519                                     -1, SQLITE_TRANSIENT));
520
521         rc = sqlite3_step(stmt);
522
523         switch (rc) {
524         case SQLITE_DONE:
525                 break;
526         case SQLITE_ROW:
527                 info->restriction_id = sqlite3_column_int64(stmt, 0);
528                 STC_LOGD("restriction id [%llu]", info->restriction_id);
529                 break;
530         case SQLITE_ERROR:
531         default:
532                 STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE
533                          sqlite3_errmsg(stc_db_get_database()));
534                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
535         }
536
537 handle_error:
538         rc = sqlite3_reset(stmt);
539         if (rc != SQLITE_OK)
540                 error_code = STC_ERROR_DB_FAILED;
541
542         __STC_LOG_FUNC_EXIT__;
543         return error_code;
544 }
545
546 stc_error_e table_restrictions_update(table_restrictions_info *info)
547 {
548         stc_error_e error_code = STC_ERROR_NONE;
549         sqlite3_stmt *stmt = insert_net_restrictions;
550
551         if (!info) {
552                 error_code = STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
553                 goto handle_error; //LCOV_EXCL_LINE
554         }
555
556         if (__get_restriction_id(info) != STC_ERROR_NONE) {
557                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
558                 goto handle_error; //LCOV_EXCL_LINE
559         }
560
561         if (info->restriction_id)
562                 stmt = update_net_restrictions;
563
564         DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
565                                     -1, SQLITE_TRANSIENT));
566         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
567         DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
568         DB_ACTION(sqlite3_bind_int(stmt, 4, info->rstn_type));
569         DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
570         DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
571                                     -1, SQLITE_TRANSIENT));
572         DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "",
573                                     -1, SQLITE_TRANSIENT));
574         DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
575         DB_ACTION(sqlite3_bind_int64(stmt, 9, info->monthly_limit));
576         DB_ACTION(sqlite3_bind_int64(stmt, 10, info->weekly_limit));
577         DB_ACTION(sqlite3_bind_int64(stmt, 11, info->daily_limit));
578
579         if (info->restriction_id)
580                 DB_ACTION(sqlite3_bind_int64(stmt, 12, info->restriction_id));
581         else
582                 DB_ACTION(sqlite3_bind_int64(stmt, 12, info->month_start_date));
583
584         if (sqlite3_step(stmt) != SQLITE_DONE) {
585                 STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE
586                          sqlite3_errmsg(stc_db_get_database()));
587                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
588                 goto handle_error; //LCOV_EXCL_LINE
589         }
590
591         if (info->restriction_id) {
592                 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
593         } else {
594                 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
595                 if (__get_restriction_id(info) != STC_ERROR_NONE)
596                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
597         }
598
599 handle_error:
600         if (sqlite3_reset(stmt) != SQLITE_OK)
601                 error_code = STC_ERROR_DB_FAILED;
602
603         return error_code;
604 }
605
606 stc_error_e table_restrictions_prepare(sqlite3 *db)
607 {
608         __STC_LOG_FUNC_ENTER__;
609
610         stc_error_e error_code = STC_ERROR_NONE;
611
612         if (db == NULL) {
613                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
614                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
615         }
616
617         DB_ACTION(__prepare_delete(db));
618         DB_ACTION(__prepare_select(db));
619         DB_ACTION(__prepare_replace(db));
620         DB_ACTION(__prepare_insert(db));
621
622 handle_error:
623
624         __STC_LOG_FUNC_EXIT__;
625         return error_code;
626 }
627
628 void table_restrictions_finalize(void)
629 {
630         __STC_LOG_FUNC_ENTER__;
631         __finalize_delete();
632         __finalize_select();
633         __finalize_update();
634         __finalize_insert();
635         __STC_LOG_FUNC_EXIT__;
636 }