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 {
24 OnKill([this]() {
25 auto *store = GetDB<SQLiteRelationalStore>();
26 if (store == nullptr) {
27 return;
28 }
29 UnlockObj();
30 store->StopSync(GetConnectionId());
31 LockObj();
32 });
33 }
34 // Close and release the connection.
Close()35 int SQLiteRelationalStoreConnection::Close()
36 {
37 if (store_ == nullptr) {
38 return -E_INVALID_CONNECTION;
39 }
40
41 if (isExclusive_.load()) {
42 return -E_BUSY;
43 }
44
45 // check if transaction closed
46 {
47 std::lock_guard<std::mutex> transactionLock(transactionMutex_);
48 if (writeHandle_ != nullptr) {
49 LOGW("Transaction started, need to rollback before close.");
50 int errCode = RollBack();
51 if (errCode != E_OK) {
52 LOGE("Rollback transaction failed, %d.", errCode);
53 }
54 ReleaseExecutor(writeHandle_);
55 }
56 }
57
58 static_cast<SQLiteRelationalStore *>(store_)->ReleaseDBConnection(this);
59 return E_OK;
60 }
61
GetIdentifier()62 std::string SQLiteRelationalStoreConnection::GetIdentifier()
63 {
64 return store_->GetProperties().GetStringProp(RelationalDBProperties::IDENTIFIER_DATA, "");
65 }
66
GetExecutor(bool isWrite,int & errCode) const67 SQLiteSingleVerRelationalStorageExecutor *SQLiteRelationalStoreConnection::GetExecutor(bool isWrite, int &errCode) const
68 {
69 auto *store = GetDB<SQLiteRelationalStore>();
70 if (store == nullptr) {
71 errCode = -E_NOT_INIT;
72 LOGE("[RelationalConnection] store is null, get executor failed! errCode = [%d]", errCode);
73 return nullptr;
74 }
75
76 return store->GetHandle(isWrite, errCode);
77 }
78
ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor * & executor) const79 void SQLiteRelationalStoreConnection::ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&executor) const
80 {
81 auto *store = GetDB<SQLiteRelationalStore>();
82 if (store != nullptr) {
83 store->ReleaseHandle(executor);
84 }
85 }
86
StartTransaction()87 int SQLiteRelationalStoreConnection::StartTransaction()
88 {
89 std::lock_guard<std::mutex> lock(transactionMutex_);
90 if (writeHandle_ != nullptr) {
91 LOGD("Transaction started already.");
92 return -E_TRANSACT_STATE;
93 }
94
95 int errCode = E_OK;
96 auto *handle = GetExecutor(true, errCode);
97 if (handle == nullptr) {
98 return errCode;
99 }
100
101 errCode = handle->StartTransaction(TransactType::DEFERRED);
102 if (errCode != E_OK) {
103 ReleaseExecutor(handle);
104 return errCode;
105 }
106
107 writeHandle_ = handle;
108 return E_OK;
109 }
110
111 // Commit the transaction
Commit()112 int SQLiteRelationalStoreConnection::Commit()
113 {
114 std::lock_guard<std::mutex> lock(transactionMutex_);
115 if (writeHandle_ == nullptr) {
116 LOGE("single version database is null or the transaction has not been started");
117 return -E_INVALID_DB;
118 }
119
120 int errCode = writeHandle_->Commit();
121 ReleaseExecutor(writeHandle_);
122 LOGD("connection commit transaction!");
123 return errCode;
124 }
125
126 // Roll back the transaction
RollBack()127 int SQLiteRelationalStoreConnection::RollBack()
128 {
129 std::lock_guard<std::mutex> lock(transactionMutex_);
130 if (writeHandle_ == nullptr) {
131 LOGE("Invalid handle for rollback or the transaction has not been started.");
132 return -E_INVALID_DB;
133 }
134
135 int errCode = writeHandle_->Rollback();
136 ReleaseExecutor(writeHandle_);
137 LOGI("connection rollback transaction!");
138 return errCode;
139 }
140
CreateDistributedTable(const std::string & tableName)141 int SQLiteRelationalStoreConnection::CreateDistributedTable(const std::string &tableName)
142 {
143 auto *store = GetDB<SQLiteRelationalStore>();
144 if (store == nullptr) {
145 LOGE("[RelationalConnection] store is null, get DB failed!");
146 return -E_INVALID_CONNECTION;
147 }
148
149 int errCode = store->CreateDistributedTable(tableName);
150 if (errCode != E_OK) {
151 LOGE("[RelationalConnection] create distributed table failed. %d", errCode);
152 }
153 return errCode;
154 }
155
RemoveDeviceData(const std::string & device)156 int SQLiteRelationalStoreConnection::RemoveDeviceData(const std::string &device)
157 {
158 return RemoveDeviceData(device, {});
159 }
160
RemoveDeviceData()161 int SQLiteRelationalStoreConnection::RemoveDeviceData()
162 {
163 auto *store = GetDB<SQLiteRelationalStore>();
164 if (store == nullptr) {
165 LOGE("[RelationalConnection] store is null, get DB failed!");
166 return -E_INVALID_CONNECTION;
167 }
168
169 int errCode = store->RemoveDeviceData();
170 if (errCode != E_OK) {
171 LOGE("[RelationalConnection] remove device data failed. %d", errCode);
172 }
173 return errCode;
174 }
175
RemoveDeviceData(const std::string & device,const std::string & tableName)176 int SQLiteRelationalStoreConnection::RemoveDeviceData(const std::string &device, const std::string &tableName)
177 {
178 auto *store = GetDB<SQLiteRelationalStore>();
179 if (store == nullptr) {
180 LOGE("[RelationalConnection] store is null, get DB failed!");
181 return -E_INVALID_CONNECTION;
182 }
183
184 int errCode = store->RemoveDeviceData(device, tableName);
185 if (errCode != E_OK) {
186 LOGE("[RelationalConnection] remove device data failed. %d", errCode);
187 }
188 return errCode;
189 }
190
Pragma(int cmd,void * parameter)191 int SQLiteRelationalStoreConnection::Pragma(int cmd, void *parameter) // reserve for interface function fix
192 {
193 (void)cmd;
194 (void)parameter;
195 return E_OK;
196 }
197
TriggerAutoSync()198 int SQLiteRelationalStoreConnection::TriggerAutoSync()
199 {
200 return E_OK;
201 }
202
SyncToDevice(SyncInfo & info)203 int SQLiteRelationalStoreConnection::SyncToDevice(SyncInfo &info)
204 {
205 auto *store = GetDB<SQLiteRelationalStore>();
206 if (store == nullptr) {
207 LOGE("[RelationalConnection] store is null, get executor failed!");
208 return -E_INVALID_CONNECTION;
209 }
210
211 {
212 AutoLock lockGuard(this);
213 IncObjRef(this);
214 ISyncer::SyncParma syncParam;
215 syncParam.devices = info.devices;
216 syncParam.mode = info.mode;
217 syncParam.wait = info.wait;
218 syncParam.isQuerySync = true;
219 syncParam.relationOnComplete = info.onComplete;
220 syncParam.syncQuery = QuerySyncObject(info.query);
221 syncParam.onFinalize = [this]() { DecObjRef(this); };
222 if (syncParam.syncQuery.GetSortType() != SortType::NONE) {
223 LOGE("not support order by timestamp");
224 return -E_NOT_SUPPORT;
225 }
226 int errCode = store->Sync(syncParam, GetConnectionId());
227 if (errCode != E_OK) {
228 DecObjRef(this);
229 return errCode;
230 }
231 }
232 return E_OK;
233 }
234
RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier & notifier)235 int SQLiteRelationalStoreConnection::RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier)
236 {
237 auto *store = GetDB<SQLiteRelationalStore>();
238 if (store == nullptr) {
239 LOGE("[RelationalConnection] store is null, get executor failed!");
240 return -E_INVALID_CONNECTION;
241 }
242
243 return store->RegisterLifeCycleCallback(notifier);
244 }
245
RegisterObserverAction(const RelationalObserverAction & action)246 void SQLiteRelationalStoreConnection::RegisterObserverAction(const RelationalObserverAction &action)
247 {
248 static_cast<SQLiteRelationalStore *>(store_)->RegisterObserverAction(action);
249 }
250
RemoteQuery(const std::string & device,const RemoteCondition & condition,uint64_t timeout,std::shared_ptr<ResultSet> & result)251 int SQLiteRelationalStoreConnection::RemoteQuery(const std::string &device, const RemoteCondition &condition,
252 uint64_t timeout, std::shared_ptr<ResultSet> &result)
253 {
254 auto *store = GetDB<SQLiteRelationalStore>();
255 if (store == nullptr) {
256 LOGE("[RelationalConnection] store is null, get executor failed!");
257 return -E_INVALID_CONNECTION;
258 }
259 return store->RemoteQuery(device, condition, timeout, GetConnectionId(), result);
260 }
261 }
262 #endif