• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifdef RELATIONAL_STORE
16 #include "sqlite_relational_store_connection.h"
17 #include "db_errno.h"
18 #include "log_print.h"
19 
20 namespace DistributedDB {
SQLiteRelationalStoreConnection(SQLiteRelationalStore * store)21 SQLiteRelationalStoreConnection::SQLiteRelationalStoreConnection(SQLiteRelationalStore *store)
22     : RelationalStoreConnection(store) {}
23 // Close and release the connection.
Close()24 int SQLiteRelationalStoreConnection::Close()
25 {
26     if (store_ == nullptr) {
27         return -E_INVALID_CONNECTION;
28     }
29 
30     if (isExclusive_.load()) {
31         return -E_BUSY;
32     }
33 
34     // check if transaction closed
35     {
36         std::lock_guard<std::mutex> transactionLock(transactionMutex_);
37         if (writeHandle_ != nullptr) {
38             LOGW("Transaction started, need to rollback before close.");
39             int errCode = RollBack();
40             if (errCode != E_OK) {
41                 LOGE("Rollback transaction failed, %d.", errCode);
42             }
43             ReleaseExecutor(writeHandle_);
44         }
45     }
46 
47     static_cast<SQLiteRelationalStore *>(store_)->ReleaseDBConnection(this);
48     return E_OK;
49 }
50 
GetIdentifier()51 std::string SQLiteRelationalStoreConnection::GetIdentifier()
52 {
53     return store_->GetProperties().GetStringProp(RelationalDBProperties::IDENTIFIER_DATA, "");
54 }
55 
GetExecutor(bool isWrite,int & errCode) const56 SQLiteSingleVerRelationalStorageExecutor *SQLiteRelationalStoreConnection::GetExecutor(bool isWrite, int &errCode) const
57 {
58     auto *store = GetDB<SQLiteRelationalStore>();
59     if (store == nullptr) {
60         errCode = -E_NOT_INIT;
61         LOGE("[RelationalConnection] store is null, get executor failed! errCode = [%d]", errCode);
62         return nullptr;
63     }
64 
65     return store->GetHandle(isWrite, errCode);
66 }
67 
ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor * & executor) const68 void SQLiteRelationalStoreConnection::ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&executor) const
69 {
70     auto *store = GetDB<SQLiteRelationalStore>();
71     if (store != nullptr) {
72         store->ReleaseHandle(executor);
73     }
74 }
75 
StartTransaction()76 int SQLiteRelationalStoreConnection::StartTransaction()
77 {
78     std::lock_guard<std::mutex> lock(transactionMutex_);
79     if (writeHandle_ != nullptr) {
80         LOGD("Transaction started already.");
81         return -E_TRANSACT_STATE;
82     }
83 
84     int errCode = E_OK;
85     auto *handle = GetExecutor(true, errCode);
86     if (handle == nullptr) {
87         return errCode;
88     }
89 
90     errCode = handle->StartTransaction(TransactType::DEFERRED);
91     if (errCode != E_OK) {
92         ReleaseExecutor(handle);
93         return errCode;
94     }
95 
96     writeHandle_ = handle;
97     return E_OK;
98 }
99 
100 // Commit the transaction
Commit()101 int SQLiteRelationalStoreConnection::Commit()
102 {
103     std::lock_guard<std::mutex> lock(transactionMutex_);
104     if (writeHandle_ == nullptr) {
105         LOGE("single version database is null or the transaction has not been started");
106         return -E_INVALID_DB;
107     }
108 
109     int errCode = writeHandle_->Commit();
110     ReleaseExecutor(writeHandle_);
111     LOGD("connection commit transaction!");
112     return errCode;
113 }
114 
115 // Roll back the transaction
RollBack()116 int SQLiteRelationalStoreConnection::RollBack()
117 {
118     std::lock_guard<std::mutex> lock(transactionMutex_);
119     if (writeHandle_ == nullptr) {
120         LOGE("Invalid handle for rollback or the transaction has not been started.");
121         return -E_INVALID_DB;
122     }
123 
124     int errCode = writeHandle_->Rollback();
125     ReleaseExecutor(writeHandle_);
126     LOGI("connection rollback transaction!");
127     return errCode;
128 }
129 
CreateDistributedTable(const std::string & tableName)130 int SQLiteRelationalStoreConnection::CreateDistributedTable(const std::string &tableName)
131 {
132     auto *store = GetDB<SQLiteRelationalStore>();
133     if (store == nullptr) {
134         LOGE("[RelationalConnection] store is null, get DB failed!");
135         return -E_INVALID_CONNECTION;
136     }
137 
138     int errCode = store->CreateDistributedTable(tableName);
139     if (errCode != E_OK) {
140         LOGE("[RelationalConnection] crete distributed table failed. %d", errCode);
141     }
142     return errCode;
143 }
144 
RemoveDeviceData(const std::string & device)145 int SQLiteRelationalStoreConnection::RemoveDeviceData(const std::string &device)
146 {
147     return RemoveDeviceData(device, {});
148 }
149 
RemoveDeviceData(const std::string & device,const std::string & tableName)150 int SQLiteRelationalStoreConnection::RemoveDeviceData(const std::string &device, const std::string &tableName)
151 {
152     auto *store = GetDB<SQLiteRelationalStore>();
153     if (store == nullptr) {
154         LOGE("[RelationalConnection] store is null, get DB failed!");
155         return -E_INVALID_CONNECTION;
156     }
157 
158     int errCode = store->RemoveDeviceData(device, tableName);
159     if (errCode != E_OK) {
160         LOGE("[RelationalConnection] remove device data failed. %d", errCode);
161     }
162     return errCode;
163 }
164 
Pragma(int cmd,void * parameter)165 int SQLiteRelationalStoreConnection::Pragma(int cmd, void *parameter) // reserve for interface function fix
166 {
167     return E_OK;
168 }
169 
TriggerAutoSync()170 int SQLiteRelationalStoreConnection::TriggerAutoSync()
171 {
172     return E_OK;
173 }
174 
SyncToDevice(SyncInfo & info)175 int SQLiteRelationalStoreConnection::SyncToDevice(SyncInfo &info)
176 {
177     auto *store = GetDB<SQLiteRelationalStore>();
178     if (store == nullptr) {
179         LOGE("[RelationalConnection] store is null, get executor failed!");
180         return -E_INVALID_CONNECTION;
181     }
182 
183     {
184         AutoLock lockGuard(this);
185         IncObjRef(this);
186         ISyncer::SyncParma syncParam;
187         syncParam.devices = info.devices;
188         syncParam.mode = info.mode;
189         syncParam.wait = info.wait;
190         syncParam.isQuerySync = true;
191         syncParam.relationOnComplete = info.onComplete;
192         syncParam.syncQuery = QuerySyncObject(info.query);
193         syncParam.onFinalize =  [this]() { DecObjRef(this); };
194 
195         int errCode = store->Sync(syncParam);
196         if (errCode != E_OK) {
197             DecObjRef(this);
198             return errCode;
199         }
200     }
201     return E_OK;
202 }
203 
RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier & notifier)204 int SQLiteRelationalStoreConnection::RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier)
205 {
206     auto *store = GetDB<SQLiteRelationalStore>();
207     if (store == nullptr) {
208         LOGE("[RelationalConnection] store is null, get executor failed!");
209         return -E_INVALID_CONNECTION;
210     }
211 
212     return store->RegisterLifeCycleCallback(notifier);
213 }
214 
RegisterObserverAction(const RelationalObserverAction & action)215 void SQLiteRelationalStoreConnection::RegisterObserverAction(const RelationalObserverAction &action)
216 {
217     static_cast<SQLiteRelationalStore *>(store_)->RegisterObserverAction(action);
218 }
219 }
220 #endif