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_COUNTER "SELECT data_counter FROM counters WHERE restriction_id=?"
31 #define INSERT_COUNTER "INSERT INTO counters (restriction_id, data_counter) " \
34 #define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, data_counter) " \
37 static void __finalize_delete(void);
39 #define PREPARE_DELETE(stm, query) do { \
40 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
41 if (rc != SQLITE_OK) { \
43 __finalize_delete(); \
44 STC_LOGE("Failed to prepare \"%s\"query" \
50 static void __finalize_select(void);
52 #define PREPARE_SELECT(stm, query) do { \
53 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
54 if (rc != SQLITE_OK) { \
56 __finalize_select(); \
57 STC_LOGE("Failed to prepare \"%s\"query" \
63 static void __finalize_update(void);
65 #define PREPARE_UPDATE(stm, query) do { \
66 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
67 if (rc != SQLITE_OK) { \
69 __finalize_update(); \
70 STC_LOGE("Failed to prepare \"%s\"query" \
76 #define FINALIZE(stm) do { \
78 sqlite3_finalize(stm); \
83 static sqlite3_stmt *delete_counter;
84 static sqlite3_stmt *select_counter;
85 static sqlite3_stmt *update_counter;
87 static int __prepare_delete(sqlite3 *db)
89 __STC_LOG_FUNC_ENTER__;
91 static int initialized;
94 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
95 return SQLITE_OK; //LCOV_EXCL_LINE
98 PREPARE_DELETE(delete_counter, DELETE_COUNTER);
101 __STC_LOG_FUNC_EXIT__;
105 static void __finalize_delete(void)
107 __STC_LOG_FUNC_ENTER__;
109 FINALIZE(delete_counter);
111 __STC_LOG_FUNC_EXIT__;
114 static int __prepare_select(sqlite3 *db)
116 __STC_LOG_FUNC_ENTER__;
118 static int initialized;
121 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
122 return SQLITE_OK; //LCOV_EXCL_LINE
125 PREPARE_SELECT(select_counter, SELECT_COUNTER);
128 __STC_LOG_FUNC_EXIT__;
132 static void __finalize_select(void)
134 __STC_LOG_FUNC_ENTER__;
136 FINALIZE(select_counter);
138 __STC_LOG_FUNC_EXIT__;
141 static int __prepare_update(sqlite3 *db)
143 __STC_LOG_FUNC_ENTER__;
145 static int initialized;
148 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
149 return SQLITE_OK; //LCOV_EXCL_LINE
152 PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
155 __STC_LOG_FUNC_EXIT__;
159 static void __finalize_update(void)
161 __STC_LOG_FUNC_ENTER__;
163 FINALIZE(update_counter);
165 __STC_LOG_FUNC_EXIT__;
168 stc_error_e table_counters_get(uint64_t restriction_id,
169 table_counters_info *info)
171 stc_error_e error_code = STC_ERROR_NONE;
172 sqlite3_stmt *stmt = select_counter;
178 info->restriction_id = restriction_id;
180 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
183 rc = sqlite3_step(stmt);
190 info->data_counter = sqlite3_column_int64(stmt, 0);
192 STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes",
193 restriction_id, info->data_counter);
197 STC_LOGE("Failed to enumerate counters: %s\n",
198 sqlite3_errmsg(stc_db_get_database()));
200 error_code = STC_ERROR_DB_FAILED;
203 } while (rc == SQLITE_ROW);
211 stc_error_e table_counters_update_counters(const table_counters_info *info)
213 stc_error_e error_code = STC_ERROR_NONE;
214 sqlite3_stmt *stmt = update_counter;
216 if (!info->data_counter) {
217 error_code = STC_ERROR_INVALID_PARAMETER;
221 DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
222 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
224 if (sqlite3_step(stmt) != SQLITE_DONE) {
225 STC_LOGE("Failed to update counter: %s\n",
226 sqlite3_errmsg(stc_db_get_database()));
227 error_code = STC_ERROR_DB_FAILED;
228 __STC_LOG_FUNC_EXIT__;
232 STC_LOGD("Counter updated for restriction_id [%llu]",
233 info->restriction_id);
241 stc_error_e table_counters_delete(uint64_t restriction_id)
243 stc_error_e error_code = STC_ERROR_NONE;
244 sqlite3_stmt *stmt = delete_counter;
246 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
248 if (sqlite3_step(stmt) != SQLITE_DONE) {
249 STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
250 sqlite3_errmsg(stc_db_get_database()));
251 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
252 goto handle_error; //LCOV_EXCL_LINE
255 STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
262 stc_error_e table_counters_prepare(sqlite3 *db)
264 __STC_LOG_FUNC_ENTER__;
266 stc_error_e error_code = STC_ERROR_NONE;
269 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
270 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
273 DB_ACTION(__prepare_delete(db));
274 DB_ACTION(__prepare_select(db));
275 DB_ACTION(__prepare_update(db));
278 __STC_LOG_FUNC_EXIT__;
282 void table_counters_finalize(void)
284 __STC_LOG_FUNC_ENTER__;
288 __STC_LOG_FUNC_EXIT__;