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_DATABASE_MANAGER_H_
18 #define _CONTEXT_DATABASE_MANAGER_H_
20 #include <IDatabaseListener.h>
24 class DatabaseManager {
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.
39 bool createTable(unsigned int queryId, const char *tableName, const char *columns, const char *option = NULL, IDatabaseListener *listener = NULL);
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.
48 bool insert(unsigned int queryId, const char *tableName, CtxJson1 record, IDatabaseListener *listener = NULL);
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.
56 bool execute(unsigned int queryId, const char *query, IDatabaseListener *listener = NULL);
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.
64 bool createTableSync(const char *tableName, const char *columns, const char *option = NULL);
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.
73 bool insertSync(const char *tableName, CtxJson1 record, int64_t *rowId);
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.
81 bool executeSync(const char *query, std::vector<CtxJson1> *records);
85 static void __release();
92 #endif /* _CONTEXT_DATABASE_MANAGER_H_ */