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_counters [%lld] bytes",
303 restriction_id, info->data_counter);
307 STC_LOGE("Failed to enumerate counters: %s\n",
308 sqlite3_errmsg(stc_db_get_database()));
310 error_code = STC_ERROR_DB_FAILED;
311 __STC_LOG_FUNC_EXIT__;
314 } while (rc == SQLITE_ROW);
322 stc_error_e table_counters_update_counters(const table_counters_info *info)
324 stc_error_e error_code = STC_ERROR_NONE;
325 sqlite3_stmt *stmt = update_counter;
327 if (!info->data_counter) {
328 error_code = STC_ERROR_INVALID_PARAMETER;
332 if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
333 stmt = insert_counter;
335 DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
336 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
337 DB_ACTION(sqlite3_bind_int64(stmt, 3, info->warn_counter));
338 DB_ACTION(sqlite3_bind_int64(stmt, 4, info->monthly_counter));
339 DB_ACTION(sqlite3_bind_int64(stmt, 5, info->weekly_counter));
340 DB_ACTION(sqlite3_bind_int64(stmt, 6, info->daily_counter));
342 if (sqlite3_step(stmt) != SQLITE_DONE) {
343 STC_LOGE("Failed to update counter: %s\n",
344 sqlite3_errmsg(stc_db_get_database()));
345 error_code = STC_ERROR_DB_FAILED;
346 __STC_LOG_FUNC_EXIT__;
350 STC_LOGD("Counter updated for restriction_id [%llu]",
351 info->restriction_id);
359 stc_error_e table_counters_get_timestamps(uint64_t restriction_id,
360 table_counters_info *info)
362 stc_error_e error_code = STC_ERROR_NONE;
363 sqlite3_stmt *stmt = select_timestamp;
367 __STC_LOG_FUNC_EXIT__;
371 info->restriction_id = restriction_id;
373 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
376 rc = sqlite3_step(stmt);
383 info->month_start_date = sqlite3_column_int(stmt, 0);
384 info->month_start_ts = sqlite3_column_int64(stmt, 1);
385 info->week_start_ts = sqlite3_column_int64(stmt, 2);
386 info->day_start_ts = sqlite3_column_int64(stmt, 3);
388 STC_LOGD("rstn_id [%llu] month_start_date [%d], "
389 "month_start_ts [%lld], week_start_ts [%lld], "
390 "day_start_ts [%lld]", restriction_id,
391 info->month_start_date, info->month_start_ts,
392 info->week_start_ts, info->day_start_ts);
396 STC_LOGE("Failed to enumerate counters: %s\n",
397 sqlite3_errmsg(stc_db_get_database()));
399 error_code = STC_ERROR_DB_FAILED;
400 __STC_LOG_FUNC_EXIT__;
403 } while (rc == SQLITE_ROW);
411 stc_error_e table_counters_update_timestamps(const table_counters_info *info)
413 stc_error_e error_code = STC_ERROR_NONE;
414 sqlite3_stmt *stmt = update_timestamp;
416 if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
417 stmt = insert_timestamp;
419 DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
420 DB_ACTION(sqlite3_bind_int(stmt, 2, info->month_start_date));
421 DB_ACTION(sqlite3_bind_int64(stmt, 3, info->month_start_ts));
422 DB_ACTION(sqlite3_bind_int64(stmt, 4, info->week_start_ts));
423 DB_ACTION(sqlite3_bind_int64(stmt, 5, info->day_start_ts));
425 if (sqlite3_step(stmt) != SQLITE_DONE) {
426 STC_LOGE("Failed to update timestamps: %s\n",
427 sqlite3_errmsg(stc_db_get_database()));
428 error_code = STC_ERROR_DB_FAILED;
429 __STC_LOG_FUNC_EXIT__;
433 STC_LOGD("Timestamps updated for restriction_id [%llu]",
434 info->restriction_id);
443 stc_error_e table_counters_delete(uint64_t restriction_id)
445 stc_error_e error_code = STC_ERROR_NONE;
446 sqlite3_stmt *stmt = delete_counter;
448 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
450 if (sqlite3_step(stmt) != SQLITE_DONE) {
451 STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
452 sqlite3_errmsg(stc_db_get_database()));
453 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
454 goto handle_error; //LCOV_EXCL_LINE
457 STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
464 stc_error_e table_counters_prepare(sqlite3 *db)
466 __STC_LOG_FUNC_ENTER__;
468 stc_error_e error_code = STC_ERROR_NONE;
471 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
472 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
475 DB_ACTION(__prepare_delete(db));
476 DB_ACTION(__prepare_select(db));
477 DB_ACTION(__prepare_update(db));
478 DB_ACTION(__prepare_insert(db));
481 __STC_LOG_FUNC_EXIT__;
485 void table_counters_finalize(void)
487 __STC_LOG_FUNC_ENTER__;
492 __STC_LOG_FUNC_EXIT__;