2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This file implements counters entity handler methods.
20 * @file table-counter.c
24 #include "db-internal.h"
25 #include "table-counters.h"
27 #define DELETE_COUNTER "DELETE FROM counters WHERE restriction_id=?"
29 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM counters " \
30 " WHERE restriction_id = ?"
32 #define SELECT_COUNTER "SELECT data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter " \
33 " FROM counters WHERE restriction_id = ?"
35 #define UPDATE_COUNTER "UPDATE counters " \
36 " SET data_counter = ?, warn_counter = ?, monthly_counter = ?, weekly_counter = ?, daily_counter = ? " \
37 " WHERE restriction_id = ?"
39 #define INSERT_COUNTER "INSERT INTO counters " \
40 " (restriction_id, data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter) " \
41 " VALUES (?, ?, ?, ?, ?, ?)"
43 #define SELECT_TIMESTAMP "SELECT month_start_date, month_start_ts, week_start_ts, day_start_ts " \
44 " FROM counters WHERE restriction_id = ?"
46 #define UPDATE_TIMESTAMP "UPDATE counters " \
47 " SET month_start_date = ?, month_start_ts = ?, week_start_ts = ?, day_start_ts = ? " \
48 " WHERE restriction_id = ?"
50 #define INSERT_TIMESTAMP "INSERT INTO counters " \
51 " (restriction_id, month_start_date, month_start_ts, week_start_ts, day_start_ts) " \
52 " VALUES (?, ?, ?, ?, ?)"
54 static void __finalize_delete(void);
56 #define PREPARE_DELETE(stm, query) do { \
57 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
58 if (rc != SQLITE_OK) { \
60 __finalize_delete(); \
61 STC_LOGE("Failed to prepare \"%s\"query" \
67 static void __finalize_select(void);
69 #define PREPARE_SELECT(stm, query) do { \
70 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
71 if (rc != SQLITE_OK) { \
73 __finalize_select(); \
74 STC_LOGE("Failed to prepare \"%s\"query" \
80 static void __finalize_update(void);
82 #define PREPARE_UPDATE(stm, query) do { \
83 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
84 if (rc != SQLITE_OK) { \
86 __finalize_update(); \
87 STC_LOGE("Failed to prepare \"%s\"query" \
93 static void __finalize_insert(void);
95 #define PREPARE_INSERT(stm, query) do { \
96 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
97 if (rc != SQLITE_OK) { \
99 __finalize_insert(); \
100 STC_LOGE("Failed to prepare \"%s\"query" \
108 #define FINALIZE(stm) do { \
110 sqlite3_finalize(stm); \
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;
124 static int __prepare_delete(sqlite3 *db)
127 static int initialized;
132 PREPARE_DELETE(delete_counter, DELETE_COUNTER);
138 static void __finalize_delete(void)
140 FINALIZE(delete_counter);
143 static int __prepare_select(sqlite3 *db)
146 static int initialized;
151 PREPARE_SELECT(select_counter, SELECT_COUNTER);
152 PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP);
153 PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
159 static void __finalize_select(void)
161 FINALIZE(select_counter);
162 FINALIZE(select_timestamp);
163 FINALIZE(select_restriction_id);
166 static int __prepare_update(sqlite3 *db)
169 static int initialized;
174 PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
175 PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP);
181 static void __finalize_update(void)
183 FINALIZE(update_counter);
184 FINALIZE(update_timestamp);
187 static int __prepare_insert(sqlite3 *db)
190 static int initialized;
195 PREPARE_INSERT(insert_counter, INSERT_COUNTER);
196 PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP);
202 static void __finalize_insert(void)
204 FINALIZE(insert_counter);
205 FINALIZE(insert_timestamp);
208 static bool __table_counters_is_entry_present(long long int restriction_id)
212 long long int l_restriction_id = -1;
213 sqlite3_stmt *stmt = select_restriction_id;
215 if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) {
220 rc = sqlite3_step(stmt);
226 l_restriction_id = sqlite3_column_int64(stmt, 0);
227 STC_LOGD("restriction id [%llu]", l_restriction_id);
232 STC_LOGE("Failed to get restriction id : %s",
233 sqlite3_errmsg(stc_db_get_database()));
237 rc = sqlite3_reset(stmt);
244 API stc_error_e table_counters_get(long long int restriction_id,
245 table_counters_info *info)
247 stc_error_e error_code = STC_ERROR_NONE;
248 sqlite3_stmt *stmt = select_counter;
254 info->restriction_id = restriction_id;
256 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
259 rc = sqlite3_step(stmt);
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);
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);
280 STC_LOGE("Failed to enumerate counters: %s\n",
281 sqlite3_errmsg(stc_db_get_database()));
283 error_code = STC_ERROR_DB_FAILED;
284 __STC_LOG_FUNC_EXIT__;
287 } while (rc == SQLITE_ROW);
290 rc = sqlite3_reset(stmt);
292 error_code = STC_ERROR_DB_FAILED;
298 API stc_error_e table_counters_update_counters(const table_counters_info *info)
300 stc_error_e error_code = STC_ERROR_NONE;
301 sqlite3_stmt *stmt = update_counter;
303 if (!info->data_counter) {
304 error_code = STC_ERROR_INVALID_PARAMETER;
308 if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
309 stmt = insert_counter;
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));
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__;
326 STC_LOGD("Counter updated for restriction_id [%llu]",
327 info->restriction_id);
330 if (sqlite3_reset(stmt) != SQLITE_OK)
331 error_code = STC_ERROR_DB_FAILED;
337 API stc_error_e table_counters_get_timestamps(long long int restriction_id,
338 table_counters_info *info)
340 stc_error_e error_code = STC_ERROR_NONE;
341 sqlite3_stmt *stmt = select_timestamp;
345 __STC_LOG_FUNC_EXIT__;
349 info->restriction_id = restriction_id;
351 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
354 rc = sqlite3_step(stmt);
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);
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);
374 STC_LOGE("Failed to enumerate counters: %s\n",
375 sqlite3_errmsg(stc_db_get_database()));
377 error_code = STC_ERROR_DB_FAILED;
378 __STC_LOG_FUNC_EXIT__;
381 } while (rc == SQLITE_ROW);
384 rc = sqlite3_reset(stmt);
386 error_code = STC_ERROR_DB_FAILED;
392 API stc_error_e table_counters_update_timestamps(const table_counters_info *info)
394 stc_error_e error_code = STC_ERROR_NONE;
395 sqlite3_stmt *stmt = update_timestamp;
397 if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
398 stmt = insert_timestamp;
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));
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__;
414 STC_LOGD("Timestamps updated for restriction_id [%llu]",
415 info->restriction_id);
418 if (sqlite3_reset(stmt) != SQLITE_OK)
419 error_code = STC_ERROR_DB_FAILED;
426 API stc_error_e table_counters_delete(long long int restriction_id)
428 stc_error_e error_code = STC_ERROR_NONE;
429 sqlite3_stmt *stmt = delete_counter;
431 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
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
440 STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
443 if (sqlite3_reset(stmt) != SQLITE_OK)
444 error_code = STC_ERROR_DB_FAILED;
449 stc_error_e table_counters_prepare(sqlite3 *db)
451 __STC_LOG_FUNC_ENTER__;
453 stc_error_e error_code = STC_ERROR_NONE;
456 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
457 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
460 DB_ACTION(__prepare_delete(db));
461 DB_ACTION(__prepare_select(db));
462 DB_ACTION(__prepare_update(db));
463 DB_ACTION(__prepare_insert(db));
466 __STC_LOG_FUNC_EXIT__;
470 void table_counters_finalize(void)
472 __STC_LOG_FUNC_ENTER__;
477 __STC_LOG_FUNC_EXIT__;