2 * Copyright (c) 2015 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.
17 #ifndef __CONTEXT_DB_MANAGER_H__
18 #define __CONTEXT_DB_MANAGER_H__
21 #include <sys/types.h>
25 /* Forward Declaration */
27 class db_listener_iface;
29 namespace db_manager {
31 * @brief Create a table if not exists. Async.
32 * @details The column @c row_id is created by default, thus do not use the same column name.
33 * It is the primary of the auto-increment integer type.
34 * @param[in] query_id This number will be returned through db_listener_iface::on_creation_result_received().
35 * @param[in] table_name A table name to be created.
36 * @param[in] columns Columns. In SQL format. INTEGER, REAL, and TEXT types are allowed.
37 * @param[in] option Additional options. Allows NULL.
38 * @param[in] listener A listner object to receive the result. Allows NULL.
40 bool create_table(unsigned int query_id, const char* table_name, const char* columns, const char* option = NULL, db_listener_iface* listener = NULL);
43 * @brief Insert a record to a table. Async.
44 * @param[in] query_id This number will be returned through db_listener_iface::on_insertion_result_received().
45 * @param[in] table_name A table name in which the record is inserted.
46 * @param[in] record A json object containing key, value pairs.
47 * @param[in] listener A listner object to receive the result. Allows NULL.
49 bool insert(unsigned int query_id, const char* table_name, json& record, db_listener_iface* listener = NULL);
52 * @brief Execute a SQL query. Async.
53 * @param[in] query_id This number will be returned through db_listener_iface::on_query_result_received().
54 * @param[in] query A query to be executed.
55 * @param[in] listener A listner object to receive the result.
57 bool execute(unsigned int query_id, const char* query, db_listener_iface* listener);
60 * @brief Insert a record to a table. Sync.
61 * @attention This cannot be used in the main thread.
62 * @param[in] table_name A table name in which the record is inserted.
63 * @param[in] record A json object containing key, value pairs.
64 * @param[out] row_id The row id of the inserted record. If fails, a negative integer.
66 bool insert_sync(const char* table_name, json& record, int64_t* row_id);
69 * @brief Execute a SQL query. Sync.
70 * @attention This cannot be used in the main thread.
71 * @param[in] query A query to be executed.
72 * @param[out] records Query result.
74 bool execute_sync(const char* query, std::vector<json>* records);
76 } /* namespace ctx::db_manager */
79 #endif /* __CONTEXT_DB_MANAGER_H__ */