tizen 2.4 release
[framework/context/context-common.git] / include / db_mgr.h
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __CONTEXT_DB_MANAGER_H__
18 #define __CONTEXT_DB_MANAGER_H__
19
20 #include <cstddef>
21 #include <sys/types.h>
22 #include <vector>
23
24 namespace ctx {
25         /* Forward Declaration */
26         class json;
27         class db_listener_iface;
28
29         namespace db_manager {
30                 /**
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.
39                  */
40                 bool create_table(unsigned int query_id, const char* table_name, const char* columns, const char* option = NULL, db_listener_iface* listener = NULL);
41
42                 /**
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.
48                  */
49                 bool insert(unsigned int query_id, const char* table_name, json& record, db_listener_iface* listener = NULL);
50
51                 /**
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.
56                  */
57                 bool execute(unsigned int query_id, const char* query, db_listener_iface* listener);
58
59                 /**
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.
65                  */
66                 bool insert_sync(const char* table_name, json& record, int64_t* row_id);
67
68                 /**
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.
73                  */
74                 bool execute_sync(const char* query, std::vector<json>* records);
75
76         }       /* namespace ctx::db_manager */
77 }       /* namespace ctx */
78
79 #endif  /* __CONTEXT_DB_MANAGER_H__ */