• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "data_syncer_rdb_store.h"
17 
18 #include "data_convertor.h"
19 #include "data_syncer_rdb_col.h"
20 #include "dfs_error.h"
21 #include "utils_log.h"
22 #include "rdb_helper.h"
23 #include "rdb_sql_utils.h"
24 #include "rdb_store_config.h"
25 
26 namespace OHOS::FileManagement::CloudSync {
27 using namespace std;
28 
GetInstance()29 DataSyncerRdbStore &DataSyncerRdbStore::GetInstance()
30 {
31     static DataSyncerRdbStore instance;
32     return instance;
33 }
34 
RdbInit()35 int32_t DataSyncerRdbStore::RdbInit()
36 {
37     LOGI("Init rdb store");
38     int32_t errCode = 0;
39     string databasePath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(EL1_CLOUDFILE_DIR, DATA_SYNCER_DB, errCode);
40 
41     if (errCode != E_OK) {
42         LOGE("Create Default Database Path is failed, errCode = %{public}d", errCode);
43         return E_RDB;
44     }
45     NativeRdb::RdbStoreConfig config{""};
46     config.SetName(move(DATA_SYNCER_DB));
47     config.SetPath(move(databasePath));
48     errCode = E_OK;
49     DataSyncerRdbCallBack rdbDataCallBack;
50     rdb_ = NativeRdb::RdbHelper::GetRdbStore(config, CLOUD_DISK_RDB_VERSION, rdbDataCallBack, errCode);
51     if (rdb_ == nullptr) {
52         LOGE("GetRdbStore is failed,  errCode = %{public}d", errCode);
53         return E_RDB;
54     }
55 
56     return E_OK;
57 }
58 
Insert(int32_t userId,const std::string & bundleName)59 int32_t DataSyncerRdbStore::Insert(int32_t userId, const std::string &bundleName)
60 {
61     LOGI("datasyncer insert userId %d, bundleName %s", userId, bundleName.c_str());
62     string uniqueId = to_string(userId) + bundleName;
63     NativeRdb::AbsRdbPredicates predicates = NativeRdb::AbsRdbPredicates(DATA_SYNCER_TABLE);
64     predicates.EqualTo(DATA_SYNCER_UNIQUE_ID, uniqueId);
65     std::shared_ptr<NativeRdb::ResultSet> resultSet = nullptr;
66     RETURN_ON_ERR(Query(predicates, resultSet));
67     if (resultSet->GoToNextRow() == E_OK) {
68         return E_OK;
69     }
70     NativeRdb::ValuesBucket values;
71     values.PutInt(USER_ID, userId);
72     values.PutString(BUNDLE_NAME, bundleName);
73     values.PutString(DATA_SYNCER_UNIQUE_ID, uniqueId);
74     int32_t ret = 0;
75     {
76         std::lock_guard<std::mutex> lck(rdbMutex_);
77         int64_t rowId;
78         ret = rdb_->Insert(rowId, DATA_SYNCER_TABLE, values);
79     }
80     if (ret != E_OK) {
81         LOGE("fail to insert userId %d bundleName %s, ret %{public}d", userId, bundleName.c_str(), ret);
82         return E_RDB;
83     }
84 
85     return E_OK;
86 }
87 
UpdateSyncState(int32_t userId,const string & bundleName,SyncState syncState)88 int32_t DataSyncerRdbStore::UpdateSyncState(int32_t userId, const string &bundleName, SyncState syncState)
89 {
90     LOGI("update syncstate %{public}d", syncState);
91     int updateRows;
92     NativeRdb::ValuesBucket values;
93     values.PutInt(SYNC_STATE, static_cast<int32_t>(syncState));
94     string whereClause = USER_ID + " = ? AND " + BUNDLE_NAME + " = ?";
95     vector<string> whereArgs = { to_string(userId), bundleName };
96     int32_t ret = rdb_->Update(updateRows, DATA_SYNCER_TABLE, values, whereClause, whereArgs);
97     if (ret != E_OK) {
98         LOGE("update sync state failed: %{public}d", ret);
99         return E_OK;
100     }
101     return E_OK;
102 }
103 
QueryDataSyncer(int32_t userId,std::shared_ptr<NativeRdb::ResultSet> & resultSet)104 int32_t DataSyncerRdbStore::QueryDataSyncer(int32_t userId, std::shared_ptr<NativeRdb::ResultSet> &resultSet)
105 {
106     NativeRdb::AbsRdbPredicates predicates = NativeRdb::AbsRdbPredicates(DATA_SYNCER_TABLE);
107     predicates.EqualTo(USER_ID, userId);
108     return Query(predicates, resultSet);
109 }
110 
Query(NativeRdb::AbsRdbPredicates predicates,std::shared_ptr<NativeRdb::ResultSet> & resultSet)111 int32_t DataSyncerRdbStore::Query(NativeRdb::AbsRdbPredicates predicates,
112     std::shared_ptr<NativeRdb::ResultSet> &resultSet)
113 {
114     while (rdb_ == nullptr) {
115         if (RdbInit() != E_OK) {
116             LOGE("Data Syner init rdb failed");
117             return E_RDB;
118         }
119     }
120     resultSet = rdb_->QueryByStep(predicates, {});
121     if (resultSet == nullptr) {
122         LOGE("DataSyncerRdbStore query failed");
123         return E_RDB;
124     }
125     return E_OK;
126 }
127 
128 
OnCreate(NativeRdb::RdbStore & store)129 int32_t DataSyncerRdbCallBack::OnCreate(NativeRdb::RdbStore &store)
130 {
131     vector<string> executeSqlStrs = {
132         CREATE_DATA_SYNCER_TABLE,
133         CREATE_DATA_SYNCER_UNIQUE_INDEX,
134     };
135 
136     for (const string& sqlStr : executeSqlStrs) {
137         if (store.ExecuteSql(sqlStr) != E_OK) {
138             LOGE("create table failed.");
139             return E_RDB;
140         }
141     }
142     return E_OK;
143 }
144 
VersionAddDataSyncerUniqueIndex(NativeRdb::RdbStore & store)145 static void VersionAddDataSyncerUniqueIndex(NativeRdb::RdbStore &store)
146 {
147     const string addDataSyncerUniqueIdColumn =
148         "ALTER TABLE " + DATA_SYNCER_TABLE + " ADD COLUMN " +
149         DATA_SYNCER_UNIQUE_ID + " TEXT";
150     const string initUniqueIdColumn =
151         "UPDATE " + DATA_SYNCER_TABLE + " SET " +
152         DATA_SYNCER_UNIQUE_ID + " = userId || bundleName";
153     const string addDataSyncerUniqueIndex = CREATE_DATA_SYNCER_UNIQUE_INDEX;
154     const vector<string> addUniqueIndex = { addDataSyncerUniqueIdColumn, initUniqueIdColumn,
155         addDataSyncerUniqueIndex};
156     for (size_t i = 0; i < addUniqueIndex.size(); i++) {
157         if (store.ExecuteSql(addUniqueIndex[i]) != NativeRdb::E_OK) {
158             LOGE("upgrade fail idx:%{public}zu", i);
159         }
160     }
161 }
162 
OnUpgrade(NativeRdb::RdbStore & store,int32_t oldVersion,int32_t newVersion)163 int32_t DataSyncerRdbCallBack::OnUpgrade(NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion)
164 {
165     LOGD("OnUpgrade old:%d, new:%d", oldVersion, newVersion);
166     if (oldVersion < VERSION_ADD_DATA_SYNCER_UNIQUE_INDEX) {
167         VersionAddDataSyncerUniqueIndex(store);
168     }
169     return E_OK;
170 }
171 } // namespace OHOS::FileManagement::CloudSync
172