c7aa4038997c8c2a58cf2b6782deba7672a9915c
[platform/core/context/context-provider.git] / common / include / DatabaseManager.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_DATABASE_MANAGER_H_
18 #define _CONTEXT_DATABASE_MANAGER_H_
19
20 #include <IDatabaseListener.h>
21
22 namespace ctx {
23
24         class DatabaseManager {
25         public:
26                 DatabaseManager();
27                 ~DatabaseManager();
28
29                 /**
30                  * @brief               Creates a table if not exists. Async.
31                  * @details             The column @c rowId is created by default, thus do not use the same column name.
32                  *                              It is the primary of the auto-increment integer type.
33                  * @param[in]   queryId         This number will be returned through IDatabaseListener::onTableCreated().
34                  * @param[in]   tableName       A table name to be created.
35                  * @param[in]   columns         Columns. In SQL format. INTEGER, REAL, and TEXT types are allowed.
36                  * @param[in]   option          Additional options. Allows NULL.
37                  * @param[in]   listener        A listener object to receive the result. Allows NULL.
38                  */
39                 bool createTable(unsigned int queryId, const char *tableName, const char *columns, const char *option = NULL, IDatabaseListener *listener = NULL);
40
41                 /**
42                  * @brief               Inserts a record to a table. Async.
43                  * @param[in]   queryId         This number will be returned through IDatabaseListener::onInserted().
44                  * @param[in]   tableName       A table name in which the record is inserted.
45                  * @param[in]   record          A json object containing key, value pairs.
46                  * @param[in]   listener        A listener object to receive the result. Allows NULL.
47                  */
48                 bool insert(unsigned int queryId, const char *tableName, CtxJson1 record, IDatabaseListener *listener = NULL);
49
50                 /**
51                  * @brief               Executes a SQL query. Async.
52                  * @param[in]   queryId         This number will be returned through IDatabaseListener::onExecuted().
53                  * @param[in]   query           A query to be executed.
54                  * @param[in]   listener        A listener object to receive the result.
55                  */
56                 bool execute(unsigned int queryId, const char *query, IDatabaseListener *listener = NULL);
57
58                 /**
59                  * @brief               creates a table if not exists. Async.
60                  * @param[in]   tableName       A table name to be created.
61                  * @param[in]   columns         Columns. In SQL format. INTEGER, REAL, and TEXT types are allowed.
62                  * @param[in]   option          Additional options. Allows NULL.
63                  */
64                 bool createTableSync(const char *tableName, const char *columns, const char *option = NULL);
65
66                 /**
67                  * @brief               Inserts a record to a table. Sync.
68                  * @attention   This blocks the current thread.
69                  * @param[in]   tableName       A table name in which the record is inserted.
70                  * @param[in]   record          A json object containing key, value pairs.
71                  * @param[out]  rowId           The row id of the inserted record. If fails, a negative integer.
72                  */
73                 bool insertSync(const char *tableName, CtxJson1 record, int64_t *rowId);
74
75                 /**
76                  * @brief               Executes a SQL query. Sync.
77                  * @attention   This blocks the current thread.
78                  * @param[in]   query           A query to be executed.
79                  * @param[out]  records         Query result.
80                  */
81                 bool executeSync(const char *query, std::vector<CtxJson1> *records);
82
83         private:
84                 static bool __init();
85                 static void __release();
86
87                 friend class Server;
88         };
89
90 }       /* namespace ctx */
91
92 #endif  /* _CONTEXT_DATABASE_MANAGER_H_ */