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__;
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 info->restriction_id = restriction_id;
180 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
183 rc = sqlite3_step(stmt);
189 info->data_counter = sqlite3_column_int64(stmt, 0);
191 STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes",
192 restriction_id, info->data_counter);
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->data_counter) {
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->data_counter));
221 if (sqlite3_step(stmt) != SQLITE_DONE) {
222 STC_LOGE("Failed to update counter: %s\n",
223 sqlite3_errmsg(stc_db_get_database()));
224 error_code = STC_ERROR_DB_FAILED;
225 __STC_LOG_FUNC_EXIT__;
229 STC_LOGD("Counter updated for restriction_id [%llu]",
230 info->restriction_id);
237 stc_error_e table_counters_delete(uint64_t restriction_id)
239 stc_error_e error_code = STC_ERROR_NONE;
240 sqlite3_stmt *stmt = delete_counter;
242 DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
244 if (sqlite3_step(stmt) != SQLITE_DONE) {
245 STC_LOGE("Failed to delete counter: %s\n",
246 sqlite3_errmsg(stc_db_get_database()));
247 error_code = STC_ERROR_DB_FAILED;
251 STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
258 stc_error_e table_counters_prepare(sqlite3 *db)
260 __STC_LOG_FUNC_ENTER__;
262 stc_error_e error_code = STC_ERROR_NONE;
265 __STC_LOG_FUNC_EXIT__;
266 return STC_ERROR_FAIL;
269 DB_ACTION(__prepare_delete(db));
270 DB_ACTION(__prepare_select(db));
271 DB_ACTION(__prepare_update(db));
274 __STC_LOG_FUNC_EXIT__;
278 void table_counters_finalize(void)
280 __STC_LOG_FUNC_ENTER__;
284 __STC_LOG_FUNC_EXIT__;