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 sent_bytes, rcv_bytes FROM counters WHERE restriction_id=?"
31 #define INSERT_COUNTER "INSERT INTO counters (restriction_id, sent_bytes, rcv_bytes) " \
34 #define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, sent_bytes, rcv_bytes) " \
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__;
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__;
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__;
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 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
181 rc = sqlite3_step(stmt);
187 info->sent_bytes = sqlite3_column_int64(stmt, 0);
188 info->rcv_bytes = sqlite3_column_int64(stmt, 1);
190 STC_LOGD("rstn_id [%llu] counters [sent = %llu rcv = %llu]",
191 restriction_id, info->sent_bytes,
196 STC_LOGE("Failed to enumerate counters: %s\n",
197 sqlite3_errmsg(stc_db_get_database()));
199 error_code = STC_ERROR_DB_FAILED;
201 } while (rc == SQLITE_ROW);
208 stc_error_e table_counters_update_counters(const table_counters_info *info)
210 stc_error_e error_code = STC_ERROR_NONE;
211 sqlite3_stmt *stmt = update_counter;
213 if (!info->rcv_bytes && !info->sent_bytes) {
214 error_code = STC_ERROR_INVALID_PARAMETER;
218 DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
219 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->sent_bytes));
220 DB_ACTION(sqlite3_bind_int64(stmt, 3, info->rcv_bytes));
222 if (sqlite3_step(stmt) != SQLITE_DONE) {
223 STC_LOGE("Failed to update counter: %s\n",
224 sqlite3_errmsg(stc_db_get_database()));
225 error_code = STC_ERROR_DB_FAILED;
226 __STC_LOG_FUNC_EXIT__;
230 STC_LOGD("Counter updated for restriction_id [%llu]",
231 info->restriction_id);
238 stc_error_e table_counters_delete(uint64_t restriction_id)
240 stc_error_e error_code = STC_ERROR_NONE;
241 sqlite3_stmt *stmt = delete_counter;
243 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
245 if (sqlite3_step(stmt) != SQLITE_DONE) {
246 STC_LOGE("Failed to delete counter: %s\n",
247 sqlite3_errmsg(stc_db_get_database()));
248 error_code = STC_ERROR_DB_FAILED;
252 STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
259 stc_error_e table_counters_prepare(sqlite3 *db)
261 __STC_LOG_FUNC_ENTER__;
263 stc_error_e error_code = STC_ERROR_NONE;
266 __STC_LOG_FUNC_EXIT__;
267 return STC_ERROR_FAIL;
270 DB_ACTION(__prepare_delete(db));
271 DB_ACTION(__prepare_select(db));
272 DB_ACTION(__prepare_update(db));
275 __STC_LOG_FUNC_EXIT__;
279 void table_counters_finalize(void)
281 __STC_LOG_FUNC_ENTER__;
285 __STC_LOG_FUNC_EXIT__;