• 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 "rdb_data_handler.h"
17 #include <sstream>
18 #include "dfs_error.h"
19 #include "data_convertor.h"
20 #include "unistd.h"
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudSync {
24 using namespace std;
25 using namespace NativeRdb;
26 
BatchInsert(int64_t & outRowId,const string & table,const vector<ValuesBucket> & initialBatchValues)27 int32_t RdbDataHandler::BatchInsert(int64_t &outRowId, const string &table,
28                                     const vector<ValuesBucket> &initialBatchValues)
29 {
30     RETURN_ON_ERR(IsStop());
31     const uint32_t TRY_TIMES = 5;
32     const uint32_t SLEEP_TIME = 200 * 1000;
33     int32_t ret = E_OK;
34     uint32_t tryCount = 0;
35     if (initialBatchValues.size() == 0) {
36         return ret;
37     }
38     while (tryCount <= TRY_TIMES) {
39         usleep(SLEEP_TIME * tryCount);
40         {
41             std::lock_guard<std::mutex> lock(rdbMutex_);
42             ret = rdb_->BatchInsert(outRowId, table, initialBatchValues);
43         }
44         if (ret != 0) {
45             LOGW("betch insert fail try next time, retry time is tryCount %{public}d", tryCount);
46             tryCount++;
47         } else {
48             return ret;
49         }
50     }
51     LOGE("betch insert fail, try too many times");
52     return ret;
53 }
54 
BatchDetete(const string & whichTable,const string & whichColumn,const std::vector<NativeRdb::ValueObject> & bindArgs)55 int32_t RdbDataHandler::BatchDetete(const string &whichTable,
56                                     const string &whichColumn,
57                                     const std::vector<NativeRdb::ValueObject> &bindArgs)
58 {
59     if (bindArgs.size() > BATCH_LIMIT_SIZE || bindArgs.size() < 0) {
60         return E_INVAL_ARG;
61     }
62     std::stringstream ss;
63     for (unsigned int i = 0; i < bindArgs.size(); i++) {
64         if (ss.tellp() != 0) {
65             ss << ",";
66         }
67         ss <<" ? ";
68     }
69     string SQL = "DELETE FROM " + whichTable + " WHERE " + whichColumn + " IN (" + ss.str() + ")";
70     std::lock_guard<std::mutex> lock(rdbMutex_);
71     return rdb_->ExecuteSql(SQL, bindArgs);
72 }
73 
BatchUpdate(const string & sql,const string & whichColumn,std::vector<NativeRdb::ValueObject> & bindArgs,uint64_t & count)74 int32_t RdbDataHandler::BatchUpdate(const string &sql,
75                                     const string &whichColumn,
76                                     std::vector<NativeRdb::ValueObject> &bindArgs,
77                                     uint64_t &count)
78 {
79     int32_t ret = E_OK;
80     if (bindArgs.size() < 0) {
81         return E_INVALID_ARGS;
82     }
83     do {
84         uint32_t size = bindArgs.size() > BATCH_LIMIT_SIZE ? BATCH_LIMIT_SIZE : bindArgs.size();
85         std::stringstream ss;
86         for (unsigned int i = 0; i < size; i++) {
87             if (ss.tellp() != 0) {
88                 ss << " ,";
89             }
90             ss<<" ?";
91         }
92         vector<ValueObject> tmp(bindArgs.begin(), bindArgs.begin() + size);
93         string newSql = sql + " WHERE " + whichColumn + " IN (" + ss.str() + ")";
94         {
95             std::lock_guard<std::mutex> lock(rdbMutex_);
96             ret = rdb_->ExecuteSql(newSql, tmp);
97         }
98         if (ret == E_OK) {
99             bindArgs.erase(bindArgs.begin(), bindArgs.begin() + size);
100             count += size;
101         } else {
102             LOGW("update fail try next time, ret is %{public}d", ret);
103             return ret;
104         }
105     } while (bindArgs.size() > BATCH_LIMIT_SIZE);
106     return E_OK;
107 }
108 
GetRaw()109 shared_ptr<NativeRdb::RdbStore> RdbDataHandler::GetRaw()
110 {
111     return rdb_;
112 }
113 
BeginTransaction()114 int32_t RdbDataHandler::BeginTransaction()
115 {
116     return rdb_->BeginTransaction();
117 }
118 
RollBack()119 int32_t RdbDataHandler::RollBack()
120 {
121     return rdb_->RollBack();
122 }
123 
Commit()124 int32_t RdbDataHandler::Commit()
125 {
126     return rdb_->Commit();
127 }
128 
Insert(int64_t & outRowId,const ValuesBucket & initiavalues)129 int32_t RdbDataHandler::Insert(int64_t &outRowId, const ValuesBucket &initiavalues)
130 {
131     RETURN_ON_ERR(IsStop());
132     std::lock_guard<std::mutex> lock(rdbMutex_);
133     return rdb_->Insert(outRowId, tableName_, initiavalues);
134 }
135 
Update(int & changedRows,const ValuesBucket & values,const string & whereClause,const vector<string> & whereArgs)136 int32_t RdbDataHandler::Update(int &changedRows, const ValuesBucket &values,
137     const string &whereClause, const vector<string> &whereArgs)
138 {
139     std::lock_guard<std::mutex> lock(rdbMutex_);
140     return rdb_->Update(changedRows, tableName_, values, whereClause, whereArgs);
141 }
142 
Delete(int & deletedRows,const string & whereClause,const vector<string> & whereArgs)143 int32_t RdbDataHandler::Delete(int &deletedRows, const string &whereClause, const vector<string> &whereArgs)
144 {
145     std::lock_guard<std::mutex> lock(rdbMutex_);
146     return rdb_->Delete(deletedRows, tableName_, whereClause, whereArgs);
147 }
148 
Query(const NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> & columns)149 shared_ptr<NativeRdb::ResultSet> RdbDataHandler::Query(
150     const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns)
151 {
152     return rdb_->Query(predicates, columns);
153 }
154 
Insert(int64_t & outRowId,const std::string & tableName,const ValuesBucket & initiavalues)155 int32_t RdbDataHandler::Insert(int64_t &outRowId, const std::string &tableName, const ValuesBucket &initiavalues)
156 {
157     RETURN_ON_ERR(IsStop());
158     return rdb_->Insert(outRowId, tableName, initiavalues);
159 }
160 
Update(int & changedRows,const std::string & tableName,const ValuesBucket & values,const string & whereClause,const vector<string> & whereArgs)161 int32_t RdbDataHandler::Update(int &changedRows, const std::string &tableName, const ValuesBucket &values,
162     const string &whereClause, const vector<string> &whereArgs)
163 {
164     return rdb_->Update(changedRows, tableName, values, whereClause, whereArgs);
165 }
166 
Delete(int & deletedRows,const std::string & tableName,const string & whereClause,const vector<string> & whereArgs)167 int32_t RdbDataHandler::Delete(int &deletedRows, const std::string &tableName,
168     const string &whereClause, const vector<string> &whereArgs)
169 {
170     return rdb_->Delete(deletedRows, tableName, whereClause, whereArgs);
171 }
172 
ExecuteSql(const std::string & sql,const std::vector<NativeRdb::ValueObject> & bindArgs)173 int32_t RdbDataHandler::ExecuteSql(const std::string &sql, const std::vector<NativeRdb::ValueObject> &bindArgs)
174 {
175     int32_t ret = rdb_->ExecuteSql(sql, bindArgs);
176     if (ret != E_OK) {
177         LOGE("err sql is %{public}s", sql.c_str());
178     }
179     return ret;
180 }
181 
IsStop()182 int32_t RdbDataHandler::IsStop()
183 {
184     if (stopFlag_ == nullptr) {
185         stopFlag_ = make_shared<bool>(false);
186     }
187     if (*stopFlag_) {
188         return E_STOP;
189     }
190     return E_OK;
191 }
192 } // namespace CloudSync
193 } // namespace FileManagement
194 } // namespace OHOS