Rename the class Json to avoid symbol conflicts with Jsoncpp
[platform/core/context/context-provider.git] / common / server / DatabaseManager.cpp
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 #include <tzplatform_config.h>
18 #include <ScopeMutex.h>
19 #include <DatabaseManager.h>
20 #include "DatabaseThread.h"
21
22 #define CONTEXT_DB_PATH tzplatform_mkpath(TZ_USER_DB, ".context-service.db")
23
24 using namespace ctx;
25
26 static unsigned int __refCount = 0;
27 static GMutex __mutex;
28 static DatabaseThread *__databaseThread = NULL;
29
30 SO_EXPORT DatabaseManager::DatabaseManager()
31 {
32         ScopeMutex sm(&__mutex);
33
34         if (++__refCount > 1)
35                 return;
36
37         if (!__databaseThread->start())
38                 _E("Failed to start Database thread");
39 }
40
41 SO_EXPORT DatabaseManager::~DatabaseManager()
42 {
43         ScopeMutex sm(&__mutex);
44
45         if (--__refCount > 0)
46                 return;
47
48         __databaseThread->stop();
49 }
50
51 SO_EXPORT bool DatabaseManager::__init()
52 {
53         __databaseThread = new(std::nothrow) DatabaseThread();
54         IF_FAIL_RETURN_TAG(__databaseThread, false, _E, "Memory allocation failed");
55
56         if (__databaseThread->open(CONTEXT_DB_PATH))
57                 return true;
58
59         _E("Database initialization failed");
60         delete __databaseThread;
61         __databaseThread = NULL;
62         return false;
63 }
64
65 SO_EXPORT void DatabaseManager::__release()
66 {
67         if (!__databaseThread)
68                 return;
69
70         __databaseThread->stop();
71         __databaseThread->close();
72
73         delete __databaseThread;
74         __databaseThread = NULL;
75 }
76
77 SO_EXPORT bool DatabaseManager::createTable(unsigned int queryId, const char *tableName, const char *columns, const char *option, IDatabaseListener *listener)
78 {
79         return __databaseThread->createTable(queryId, tableName, columns, option, listener);
80 }
81
82 SO_EXPORT bool DatabaseManager::insert(unsigned int queryId, const char *tableName, CtxJson1 record, IDatabaseListener *listener)
83 {
84         return __databaseThread->insert(queryId, tableName, record, listener);
85 }
86
87 SO_EXPORT bool DatabaseManager::execute(unsigned int queryId, const char *query, IDatabaseListener *listener)
88 {
89         return __databaseThread->execute(queryId, query, listener);
90 }
91
92 SO_EXPORT bool DatabaseManager::createTableSync(const char *tableName, const char *columns, const char *option)
93 {
94         return __databaseThread->createTableSync(tableName, columns, option);
95 }
96
97 SO_EXPORT bool DatabaseManager::insertSync(const char *tableName, CtxJson1 record, int64_t *rowId)
98 {
99         return __databaseThread->insertSync(tableName, record, rowId);
100 }
101
102 SO_EXPORT bool DatabaseManager::executeSync(const char *query, std::vector<CtxJson1> *records)
103 {
104         return __databaseThread->executeSync(query, records);
105 }