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 #ifndef OHOS_CLOUD_SYNC_SERVICE_RDB_DATA_HANDLER_H 17 #define OHOS_CLOUD_SYNC_SERVICE_RDB_DATA_HANDLER_H 18 19 #include "abs_rdb_predicates.h" 20 #include "rdb_helper.h" 21 #include "result_set.h" 22 #include "values_bucket.h" 23 #include "data_handler.h" 24 namespace OHOS { 25 namespace FileManagement { 26 namespace CloudSync { 27 class RdbDataHandler : public DataHandler { 28 protected: 29 /* init */ RdbDataHandler(int32_t userId,const std::string & bundleName,const std::string & table,std::shared_ptr<NativeRdb::RdbStore> rdb,std::shared_ptr<bool> stopFlag)30 RdbDataHandler(int32_t userId, const std::string &bundleName, const std::string &table, 31 std::shared_ptr<NativeRdb::RdbStore> rdb, std::shared_ptr<bool> stopFlag) 32 : DataHandler(userId, bundleName, table), 33 stopFlag_(stopFlag), 34 rdb_(rdb), 35 tableName_(table) {} 36 virtual ~RdbDataHandler() = default; 37 38 std::shared_ptr<NativeRdb::RdbStore> GetRaw(); 39 40 virtual int32_t BeginTransaction(); 41 virtual int32_t RollBack(); 42 virtual int32_t Commit(); 43 44 /* insert, delete, update, query */ 45 virtual int32_t BatchInsert(int64_t &outRowId, 46 const std::string &table, 47 const std::vector<NativeRdb::ValuesBucket> &initialBatchValues); 48 virtual int32_t BatchDetete(const std::string &whichTable, 49 const std::string &whichColumn, 50 const std::vector<NativeRdb::ValueObject> &bindArgs); 51 virtual int32_t BatchUpdate(const std::string &whichTable, 52 const std::string &whichColumn, 53 std::vector<NativeRdb::ValueObject> &bindArgs, 54 uint64_t &count); 55 virtual int32_t Insert(int64_t &outRowId, const NativeRdb::ValuesBucket &initialValues); 56 virtual int32_t Update(int &changedRows, const NativeRdb::ValuesBucket &values, 57 const std::string &whereClause, const std::vector<std::string> &whereArgs); 58 virtual int32_t Delete(int &deletedRows, const std::string &whereClause, 59 const std::vector<std::string> &whereArgs); 60 virtual std::shared_ptr<NativeRdb::ResultSet> Query(const NativeRdb::AbsRdbPredicates &predicates, 61 const std::vector<std::string> &columns); 62 63 /* insert, delete, update with tableName */ 64 virtual int32_t Insert(int64_t &outRowId, const std::string &tableName, 65 const NativeRdb::ValuesBucket &initialValues); 66 virtual int32_t Update(int &changedRows, const std::string &tableName, 67 const NativeRdb::ValuesBucket &values, const std::string &whereClause, 68 const std::vector<std::string> &whereArgs); 69 virtual int32_t Delete(int &deletedRows, const std::string &tableName, 70 const std::string &whereClause, const std::vector<std::string> &whereArgs); 71 72 /* raw */ 73 virtual int32_t ExecuteSql(const std::string &sql, 74 const std::vector<NativeRdb::ValueObject> &bindArgs = std::vector<NativeRdb::ValueObject>()); 75 int32_t IsStop(); 76 77 static const int32_t BATCH_LIMIT_SIZE = 500; 78 std::shared_ptr<bool> stopFlag_; 79 80 private: 81 std::shared_ptr<NativeRdb::RdbStore> rdb_; 82 std::string tableName_; 83 std::mutex rdbMutex_; 84 }; 85 } // namespace CloudSync 86 } // namespace FileManagement 87 } // namespace OHOS 88 #endif // OHOS_CLOUD_SYNC_SERVICE_RDB_DATA_HANDLER_H