• 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 {
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(const std::string & device,const std::string & tableName)161 int SQLiteRelationalStoreConnection::RemoveDeviceData(const std::string &device, const std::string &tableName)
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(device, tableName);
170     if (errCode != E_OK) {
171         LOGE("[RelationalConnection] remove device data failed. %d", errCode);
172     }
173     return errCode;
174 }
175 
Pragma(int cmd,void * parameter)176 int SQLiteRelationalStoreConnection::Pragma(int cmd, void *parameter) // reserve for interface function fix
177 {
178     (void)cmd;
179     (void)parameter;
180     return E_OK;
181 }
182 
TriggerAutoSync()183 int SQLiteRelationalStoreConnection::TriggerAutoSync()
184 {
185     return E_OK;
186 }
187 
SyncToDevice(SyncInfo & info)188 int SQLiteRelationalStoreConnection::SyncToDevice(SyncInfo &info)
189 {
190     auto *store = GetDB<SQLiteRelationalStore>();
191     if (store == nullptr) {
192         LOGE("[RelationalConnection] store is null, get executor failed!");
193         return -E_INVALID_CONNECTION;
194     }
195 
196     {
197         AutoLock lockGuard(this);
198         IncObjRef(this);
199         ISyncer::SyncParma syncParam;
200         syncParam.devices = info.devices;
201         syncParam.mode = info.mode;
202         syncParam.wait = info.wait;
203         syncParam.isQuerySync = true;
204         syncParam.relationOnComplete = info.onComplete;
205         syncParam.syncQuery = QuerySyncObject(info.query);
206         syncParam.onFinalize =  [this]() { DecObjRef(this); };
207         if (syncParam.syncQuery.GetSortType() != SortType::NONE) {
208             LOGE("not support order by timestamp");
209             return -E_NOT_SUPPORT;
210         }
211         int errCode = store->Sync(syncParam, GetConnectionId());
212         if (errCode != E_OK) {
213             DecObjRef(this);
214             return errCode;
215         }
216     }
217     return E_OK;
218 }
219 
RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier & notifier)220 int SQLiteRelationalStoreConnection::RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier)
221 {
222     auto *store = GetDB<SQLiteRelationalStore>();
223     if (store == nullptr) {
224         LOGE("[RelationalConnection] store is null, get executor failed!");
225         return -E_INVALID_CONNECTION;
226     }
227 
228     return store->RegisterLifeCycleCallback(notifier);
229 }
230 
RegisterObserverAction(const RelationalObserverAction & action)231 void SQLiteRelationalStoreConnection::RegisterObserverAction(const RelationalObserverAction &action)
232 {
233     static_cast<SQLiteRelationalStore *>(store_)->RegisterObserverAction(action);
234 }
235 
RemoteQuery(const std::string & device,const RemoteCondition & condition,uint64_t timeout,std::shared_ptr<ResultSet> & result)236 int SQLiteRelationalStoreConnection::RemoteQuery(const std::string &device, const RemoteCondition &condition,
237     uint64_t timeout, std::shared_ptr<ResultSet> &result)
238 {
239     auto *store = GetDB<SQLiteRelationalStore>();
240     if (store == nullptr) {
241         LOGE("[RelationalConnection] store is null, get executor failed!");
242         return -E_INVALID_CONNECTION;
243     }
244     return store->RemoteQuery(device, condition, timeout, GetConnectionId(), result);
245 }
246 }
247 #endif