• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #ifndef NATIVE_RDB_RDB_STORE_IMPL_H
17 #define NATIVE_RDB_RDB_STORE_IMPL_H
18 
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <thread>
24 #include <shared_mutex>
25 
26 #include "dataobs_mgr_client.h"
27 #include "data_ability_observer_stub.h"
28 #include "rdb_service.h"
29 #include "rdb_store.h"
30 #include "rdb_store_config.h"
31 #include "refbase.h"
32 #include "sqlite_connection_pool.h"
33 #include "sqlite_statement.h"
34 
35 namespace OHOS {
36 class ExecutorPool;
37 }
38 
39 namespace OHOS::NativeRdb {
40 class DelayNotify;
41 class RdbStoreLocalObserver {
42 public:
RdbStoreLocalObserver(DistributedRdb::RdbStoreObserver * observer)43     explicit RdbStoreLocalObserver(DistributedRdb::RdbStoreObserver *observer) : observer_(observer) {};
~RdbStoreLocalObserver()44     virtual ~RdbStoreLocalObserver() {};
OnChange()45     void OnChange()
46     {
47         observer_->OnChange();
48     }
getObserver()49     DistributedRdb::RdbStoreObserver *getObserver()
50     {
51         return observer_;
52     }
53 private:
54     DistributedRdb::RdbStoreObserver *observer_ = nullptr;
55 };
56 
57 class RdbStoreLocalSharedObserver : public AAFwk::DataAbilityObserverStub {
58 public:
RdbStoreLocalSharedObserver(DistributedRdb::RdbStoreObserver * observer)59     explicit RdbStoreLocalSharedObserver(DistributedRdb::RdbStoreObserver *observer) : observer_(observer) {};
~RdbStoreLocalSharedObserver()60     virtual ~RdbStoreLocalSharedObserver() {};
OnChange()61     void OnChange() override
62     {
63         observer_->OnChange();
64     }
getObserver()65     DistributedRdb::RdbStoreObserver *getObserver()
66     {
67         return observer_;
68     }
69 private:
70     DistributedRdb::RdbStoreObserver *observer_ = nullptr;
71 };
72 
73 class RdbStoreImpl : public RdbStore, public std::enable_shared_from_this<RdbStoreImpl> {
74 public:
75     RdbStoreImpl(const RdbStoreConfig &config, int &errCode);
76     ~RdbStoreImpl() override;
77     const RdbStoreConfig &GetConfig();
78     int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) override;
79     int BatchInsert(int64_t &outInsertNum, const std::string &table,
80         const std::vector<ValuesBucket> &initialBatchValues) override;
81     int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) override;
82     int InsertWithConflictResolution(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues,
83         ConflictResolution conflictResolution) override;
84     int Update(int &changedRows, const std::string &table, const ValuesBucket &values, const std::string &whereClause,
85         const std::vector<std::string> &whereArgs) override;
86     int Update(int &changedRows, const std::string &table, const ValuesBucket &values, const std::string &whereClause,
87         const std::vector<ValueObject> &bindArgs) override;
88     int UpdateWithConflictResolution(int &changedRows, const std::string &table, const ValuesBucket &values,
89         const std::string &whereClause, const std::vector<std::string> &whereArgs,
90         ConflictResolution conflictResolution) override;
91     int UpdateWithConflictResolution(int &changedRows, const std::string &table, const ValuesBucket &values,
92         const std::string &whereClause, const std::vector<ValueObject> &bindArgs,
93         ConflictResolution conflictResolution) override;
94     int Delete(int &deletedRows, const std::string &table, const std::string &whereClause,
95         const std::vector<std::string> &whereArgs) override;
96     int Delete(int &deletedRows, const std::string &table, const std::string &whereClause,
97         const std::vector<ValueObject> &bindArgs) override;
98     std::shared_ptr<AbsSharedResultSet> Query(int &errCode, bool distinct,
99         const std::string &table, const std::vector<std::string> &columns,
100         const std::string &whereClause, const std::vector<ValueObject> &bindArgs, const std::string &groupBy,
101         const std::string &indexName, const std::string &orderBy, const int &limit, const int &offset) override;
102     std::shared_ptr<AbsSharedResultSet> QuerySql(const std::string &sql,
103         const std::vector<std::string> &sqlArgs) override;
104     std::shared_ptr<AbsSharedResultSet> QuerySql(const std::string &sql,
105         const std::vector<ValueObject> &bindArgs) override;
106     int ExecuteSql(
107         const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()) override;
108     int ExecuteAndGetLong(int64_t &outValue, const std::string &sql, const std::vector<ValueObject> &bindArgs) override;
109     int ExecuteAndGetString(std::string &outValue, const std::string &sql,
110         const std::vector<ValueObject> &bindArgs) override;
111     int ExecuteForLastInsertedRowId(int64_t &outValue, const std::string &sql,
112         const std::vector<ValueObject> &bindArgs) override;
113     int ExecuteForChangedRowCount(int64_t &outValue, const std::string &sql,
114         const std::vector<ValueObject> &bindArgs) override;
115     int Backup(const std::string databasePath,
116         const std::vector<uint8_t> destEncryptKey = std::vector<uint8_t>()) override;
117     int Attach(const std::string &alias, const std::string &pathName,
118         const std::vector<uint8_t> destEncryptKey) override;
119     int GetVersion(int &version) override;
120     int SetVersion(int version) override;
121     int BeginTransaction() override;
122     int RollBack() override;
123     int Commit() override;
124     bool IsInTransaction() override;
125     bool IsOpen() const override;
126     std::string GetPath() override;
127     bool IsReadOnly() const override;
128     bool IsMemoryRdb() const override;
129     bool IsHoldingConnection() override;
130 #ifdef RDB_SUPPORT_ICU
131     int ConfigLocale(const std::string localeStr);
132 #endif
133     int Restore(const std::string backupPath, const std::vector<uint8_t> &newKey = std::vector<uint8_t>()) override;
134     void GetSchema(const RdbStoreConfig &config);
135     std::string GetName();
136     std::string GetOrgPath();
137     std::string GetFileType();
138     std::shared_ptr<ResultSet> QueryByStep(const std::string &sql,
139         const std::vector<std::string> &sqlArgs) override;
140     std::shared_ptr<ResultSet> QueryByStep(const std::string &sql, const std::vector<ValueObject> &args) override;
141     std::shared_ptr<ResultSet> QueryByStep(
142         const AbsRdbPredicates &predicates, const std::vector<std::string> &columns) override;
143     std::shared_ptr<AbsSharedResultSet> Query(
144         const AbsRdbPredicates &predicates, const std::vector<std::string> &columns) override;
145     std::pair<int32_t, std::shared_ptr<ResultSet>> QuerySharingResource(
146         const AbsRdbPredicates &predicates, const std::vector<std::string> &columns) override;
147     int Count(int64_t &outValue, const AbsRdbPredicates &predicates) override;
148     int Update(int &changedRows, const ValuesBucket &values, const AbsRdbPredicates &predicates) override;
149     int Delete(int &deletedRows, const AbsRdbPredicates &predicates) override;
150 
151     std::shared_ptr<ResultSet> RemoteQuery(const std::string &device, const AbsRdbPredicates &predicates,
152         const std::vector<std::string> &columns, int &errCode) override;
153 
154     int SetDistributedTables(const std::vector<std::string> &tables, int32_t type,
155         const DistributedRdb::DistributedConfig &distributedConfig) override;
156 
157     std::string ObtainDistributedTableName(const std::string& device, const std::string& table, int &errCode) override;
158 
159     int Sync(const SyncOption &option, const AbsRdbPredicates &predicate, const AsyncBrief &async) override;
160 
161     int Sync(const SyncOption &option, const std::vector<std::string> &tables, const AsyncDetail &async) override;
162 
163     int Sync(const SyncOption &option, const AbsRdbPredicates &predicate, const AsyncDetail &async) override;
164 
165     int Subscribe(const SubscribeOption& option, RdbStoreObserver *observer) override;
166 
167     int UnSubscribe(const SubscribeOption& option, RdbStoreObserver *observer) override;
168 
169     int RegisterAutoSyncCallback(std::shared_ptr<DetailProgressObserver> observer) override;
170 
171     int UnregisterAutoSyncCallback(std::shared_ptr<DetailProgressObserver> observer) override;
172 
173     int Notify(const std::string &event) override;
174 
175     ModifyTime GetModifyTime(const std::string& table, const std::string& columnName,
176         std::vector<PRIKey>& keys) override;
177 
178     int CleanDirtyData(const std::string &table, uint64_t cursor = UINT64_MAX) override;
179 
180 private:
181     using ExecuteSqls = std::vector<std::pair<std::string, std::vector<std::vector<ValueObject>>>>;
182     int InnerOpen();
183     int CheckAttach(const std::string &sql);
184     int BeginExecuteSql(const std::string &sql, std::shared_ptr<SqliteConnection> &connection);
185     int FreeTransaction(std::shared_ptr<SqliteConnection> connection, const std::string &sql);
186     ExecuteSqls GenerateSql(
187         const std::string &table, const std::vector<ValuesBucket> &initialBatchValues, int limitVariableNumber);
188     ExecuteSqls MakeExecuteSqls(
189         const std::string &sql, const std::vector<ValueObject> &args, int fieldSize, int limitVariableNumber);
190     int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath);
191     int ExecuteSqlInner(const std::string &sql, const std::vector<ValueObject> &bindArgs);
192     int ExecuteGetLongInner(const std::string &sql, const std::vector<ValueObject> &bindArgs);
193     void SetAssetStatus(const ValueObject &val, int32_t status);
194     void DoCloudSync(const std::string &table);
195     int InnerSync(const DistributedRdb::RdbService::Option &option, const DistributedRdb::PredicatesMemo &predicates,
196         const AsyncDetail &async);
197     int InnerBackup(const std::string databasePath,
198         const std::vector<uint8_t> destEncryptKey = std::vector<uint8_t>());
199     ModifyTime GetModifyTimeByRowId(const std::string& logTable, std::vector<PRIKey>& keys);
200     inline std::string GetSqlArgs(size_t size);
201     Uri GetUri(const std::string &event);
202     int SubscribeLocal(const SubscribeOption& option, RdbStoreObserver *observer);
203     int SubscribeLocalShared(const SubscribeOption& option, RdbStoreObserver *observer);
204     int SubscribeRemote(const SubscribeOption& option, RdbStoreObserver *observer);
205 
206     int UnSubscribeLocal(const SubscribeOption& option, RdbStoreObserver *observer);
207     int UnSubscribeLocalAll(const SubscribeOption& option);
208     int UnSubscribeLocalShared(const SubscribeOption& option, RdbStoreObserver *observer);
209     int UnSubscribeLocalSharedAll(const SubscribeOption& option);
210     int UnSubscribeRemote(const SubscribeOption& option, RdbStoreObserver *observer);
211     int RegisterDataChangeCallback();
212     void InitDelayNotifier();
213 
214     const RdbStoreConfig rdbStoreConfig;
215     SqliteConnectionPool *connectionPool;
216     bool isOpen;
217     std::string path;
218     std::string orgPath;
219     bool isReadOnly;
220     bool isMemoryRdb;
221     std::string name;
222     std::string fileType;
223     DistributedRdb::RdbSyncerParam syncerParam_;
224     bool isEncrypt_;
225     std::shared_ptr<ExecutorPool> pool_;
226     std::shared_ptr<DelayNotify> delayNotifier_ = nullptr;
227 
228     mutable std::shared_mutex rwMutex_;
229     static inline constexpr uint32_t INTERVAL = 200;
230     static constexpr const char *ROW_ID = "ROWID";
231     std::set<std::string> cloudTables_;
232 
233     std::mutex mutex_;
234     std::shared_ptr<std::set<std::string>> syncTables_;
235     static constexpr char SCHEME_RDB[] = "rdb://";
236     std::map<std::string, std::list<std::shared_ptr<RdbStoreLocalObserver>>> localObservers_;
237     std::map<std::string, std::list<sptr<RdbStoreLocalSharedObserver>>> localSharedObservers_;
238     static constexpr uint32_t EXPANSION = 2;
239     static constexpr uint32_t AUTO_SYNC_MAX_INTERVAL = 3000;
240 };
241 } // namespace OHOS::NativeRdb
242 #endif
243