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)
126 __STC_LOG_FUNC_ENTER__;
128 static int initialized;
131 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
132 return SQLITE_OK; //LCOV_EXCL_LINE
135 PREPARE_DELETE(delete_counter, DELETE_COUNTER);
138 __STC_LOG_FUNC_EXIT__;
142 static void __finalize_delete(void)
144 __STC_LOG_FUNC_ENTER__;
146 FINALIZE(delete_counter);
148 __STC_LOG_FUNC_EXIT__;
151 static int __prepare_select(sqlite3 *db)
153 __STC_LOG_FUNC_ENTER__;
155 static int initialized;
158 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
159 return SQLITE_OK; //LCOV_EXCL_LINE
162 PREPARE_SELECT(select_counter, SELECT_COUNTER);
163 PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP);
164 PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
167 __STC_LOG_FUNC_EXIT__;
171 static void __finalize_select(void)
173 __STC_LOG_FUNC_ENTER__;
175 FINALIZE(select_counter);
176 FINALIZE(select_timestamp);
177 FINALIZE(select_restriction_id);
179 __STC_LOG_FUNC_EXIT__;
182 static int __prepare_update(sqlite3 *db)
184 __STC_LOG_FUNC_ENTER__;
186 static int initialized;
189 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
190 return SQLITE_OK; //LCOV_EXCL_LINE
193 PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
194 PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP);
197 __STC_LOG_FUNC_EXIT__;
201 static void __finalize_update(void)
203 __STC_LOG_FUNC_ENTER__;
205 FINALIZE(update_counter);
206 FINALIZE(update_timestamp);
208 __STC_LOG_FUNC_EXIT__;
211 static int __prepare_insert(sqlite3 *db)
213 __STC_LOG_FUNC_ENTER__;
215 static int initialized;
218 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
219 return SQLITE_OK; //LCOV_EXCL_LINE
222 PREPARE_INSERT(insert_counter, INSERT_COUNTER);
223 PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP);
226 __STC_LOG_FUNC_EXIT__;
230 static void __finalize_insert(void)
232 __STC_LOG_FUNC_ENTER__;
234 FINALIZE(insert_counter);
235 FINALIZE(insert_timestamp);
237 __STC_LOG_FUNC_EXIT__;
240 static bool __table_counters_is_entry_present(uint64_t restriction_id)
244 uint64_t l_restriction_id = -1;
245 sqlite3_stmt *stmt = select_restriction_id;
247 if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) {
249 __STC_LOG_FUNC_EXIT__;
253 rc = sqlite3_step(stmt);
259 l_restriction_id = sqlite3_column_int64(stmt, 0);
260 STC_LOGD("restriction id [%llu]", l_restriction_id);
265 STC_LOGE("Failed to get restriction id : %s",
266 sqlite3_errmsg(stc_db_get_database()));
274 stc_error_e table_counters_get(uint64_t restriction_id,
275 table_counters_info *info)
277 stc_error_e error_code = STC_ERROR_NONE;
278 sqlite3_stmt *stmt = select_counter;
284 info->restriction_id = restriction_id;
286 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
289 rc = sqlite3_step(stmt);
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);
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);
310 STC_LOGE("Failed to enumerate counters: %s\n",
311 sqlite3_errmsg(stc_db_get_database()));
313 error_code = STC_ERROR_DB_FAILED;
314 __STC_LOG_FUNC_EXIT__;
317 } while (rc == SQLITE_ROW);
325 stc_error_e table_counters_update_counters(const table_counters_info *info)
327 stc_error_e error_code = STC_ERROR_NONE;
328 sqlite3_stmt *stmt = update_counter;
330 if (!info->data_counter) {
331 error_code = STC_ERROR_INVALID_PARAMETER;
335 if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
336 stmt = insert_counter;
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));
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__;
353 STC_LOGD("Counter updated for restriction_id [%llu]",
354 info->restriction_id);
362 stc_error_e table_counters_get_timestamps(uint64_t restriction_id,
363 table_counters_info *info)
365 stc_error_e error_code = STC_ERROR_NONE;
366 sqlite3_stmt *stmt = select_timestamp;
370 __STC_LOG_FUNC_EXIT__;
374 info->restriction_id = restriction_id;
376 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
379 rc = sqlite3_step(stmt);
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);
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);
399 STC_LOGE("Failed to enumerate counters: %s\n",
400 sqlite3_errmsg(stc_db_get_database()));
402 error_code = STC_ERROR_DB_FAILED;
403 __STC_LOG_FUNC_EXIT__;
406 } while (rc == SQLITE_ROW);
414 stc_error_e table_counters_update_timestamps(const table_counters_info *info)
416 stc_error_e error_code = STC_ERROR_NONE;
417 sqlite3_stmt *stmt = update_timestamp;
419 if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
420 stmt = insert_timestamp;
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));
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__;
436 STC_LOGD("Timestamps updated for restriction_id [%llu]",
437 info->restriction_id);
446 stc_error_e table_counters_delete(uint64_t restriction_id)
448 stc_error_e error_code = STC_ERROR_NONE;
449 sqlite3_stmt *stmt = delete_counter;
451 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
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
460 STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
467 stc_error_e table_counters_prepare(sqlite3 *db)
469 __STC_LOG_FUNC_ENTER__;
471 stc_error_e error_code = STC_ERROR_NONE;
474 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
475 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
478 DB_ACTION(__prepare_delete(db));
479 DB_ACTION(__prepare_select(db));
480 DB_ACTION(__prepare_update(db));
481 DB_ACTION(__prepare_insert(db));
484 __STC_LOG_FUNC_EXIT__;
488 void table_counters_finalize(void)
490 __STC_LOG_FUNC_ENTER__;
495 __STC_LOG_FUNC_EXIT__;