Migrate from 2.4 code repo
[platform/core/context/context-service.git] / src / db_mgr_impl.h
1 /*
2  * Copyright (c) 2014 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_IMPL_H__
18 #define __CONTEXT_DB_MANAGER_IMPL_H__
19
20 #include <vector>
21 #include <json.h>
22 #include <sqlite3.h>
23
24 #include <event_driven.h>
25 #include <db_listener_iface.h>
26 #include <db_mgr_iface.h>
27
28 namespace ctx {
29
30         class db_manager_impl : public event_driven_thread, public db_listener_iface, public db_manager_iface {
31                 private:
32                         enum query_type_e {
33                                 QTYPE_CREATE_TABLE = 1,
34                                 QTYPE_INSERT,
35                                 QTYPE_EXECUTE,
36                         };
37
38                         struct query_info_s {
39                                 unsigned int id;
40                                 db_listener_iface* listener;
41                                 std::string query;
42                         };
43
44                         struct query_result_s {
45                                 int type;
46                                 unsigned int id;
47                                 int error;
48                                 db_listener_iface* listener;
49                                 std::vector<json>* result;
50                         };
51
52                         sqlite3 *db_handle;
53
54                         static bool is_main_thread();
55
56                         void on_thread_event_popped(int type, void* data);
57                         void delete_thread_event(int type, void* data);
58
59                         bool open();
60                         void close();
61
62                         void _execute(int query_type, unsigned int query_id, const char* query, db_listener_iface* listener);
63                         void send_result(int query_type, unsigned int query_id, db_listener_iface* listener, int error, std::vector<json>* result);
64
65                         static int execution_result_cb(void *user_data, int dim, char **value, char **column);
66                         static gboolean _send_result(gpointer data);
67
68                         void on_creation_result_received(unsigned int query_id, int error);
69                         void on_insertion_result_received(unsigned int query_id, int error, int64_t row_id);
70                         void on_query_result_received(unsigned int query_id, int error, std::vector<json>& records);
71
72                 public:
73                         db_manager_impl();
74                         ~db_manager_impl();
75
76                         bool init();
77                         void release();
78
79                         bool create_table(unsigned int query_id, const char* table_name, const char* columns, const char* option = NULL, db_listener_iface* listener = NULL);
80                         bool insert(unsigned int query_id, const char* table_name, json& record, db_listener_iface* listener = NULL);
81                         bool execute(unsigned int query_id, const char* query, db_listener_iface* listener);
82                         bool insert_sync(const char* table_name, json& record, int64_t* row_id);
83                         bool execute_sync(const char* query, std::vector<json>* records);
84
85         };      /* class db_manager */
86 }
87
88 #endif  /* __CONTEXT_DB_MANAGER_IMPL_H__ */