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 #include <tzplatform_config.h>
18 #include <ScopeMutex.h>
19 #include <DatabaseManager.h>
20 #include "DatabaseThread.h"
22 #define CONTEXT_DB_PATH tzplatform_mkpath(TZ_USER_DB, ".context-service.db")
26 static unsigned int __refCount = 0;
27 static GMutex __mutex;
28 static DatabaseThread *__databaseThread = NULL;
30 SO_EXPORT DatabaseManager::DatabaseManager()
32 ScopeMutex sm(&__mutex);
37 if (!__databaseThread->start())
38 _E("Failed to start Database thread");
41 SO_EXPORT DatabaseManager::~DatabaseManager()
43 ScopeMutex sm(&__mutex);
48 __databaseThread->stop();
51 SO_EXPORT bool DatabaseManager::__init()
53 __databaseThread = new(std::nothrow) DatabaseThread();
54 IF_FAIL_RETURN_TAG(__databaseThread, false, _E, "Memory allocation failed");
56 if (__databaseThread->open(CONTEXT_DB_PATH))
59 _E("Database initialization failed");
60 delete __databaseThread;
61 __databaseThread = NULL;
65 SO_EXPORT void DatabaseManager::__release()
67 if (!__databaseThread)
70 __databaseThread->stop();
71 __databaseThread->close();
73 delete __databaseThread;
74 __databaseThread = NULL;
77 SO_EXPORT bool DatabaseManager::createTable(unsigned int queryId, const char *tableName, const char *columns, const char *option, IDatabaseListener *listener)
79 return __databaseThread->createTable(queryId, tableName, columns, option, listener);
82 SO_EXPORT bool DatabaseManager::insert(unsigned int queryId, const char *tableName, CtxJson1 record, IDatabaseListener *listener)
84 return __databaseThread->insert(queryId, tableName, record, listener);
87 SO_EXPORT bool DatabaseManager::execute(unsigned int queryId, const char *query, IDatabaseListener *listener)
89 return __databaseThread->execute(queryId, query, listener);
92 SO_EXPORT bool DatabaseManager::createTableSync(const char *tableName, const char *columns, const char *option)
94 return __databaseThread->createTableSync(tableName, columns, option);
97 SO_EXPORT bool DatabaseManager::insertSync(const char *tableName, CtxJson1 record, int64_t *rowId)
99 return __databaseThread->insertSync(tableName, record, rowId);
102 SO_EXPORT bool DatabaseManager::executeSync(const char *query, std::vector<CtxJson1> *records)
104 return __databaseThread->executeSync(query, records);