1 /* 2 * Copyright (c) 2024 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_TRANS_DB_H 17 #define NATIVE_RDB_TRANS_DB_H 18 #include <memory> 19 20 #include "connection.h" 21 #include "rdb_store.h" 22 #include "statement.h" 23 namespace OHOS::NativeRdb { 24 class TransDB : public RdbStore { 25 public: 26 TransDB(std::shared_ptr<Connection> conn, const std::string &path); 27 std::pair<int, int64_t> Insert(const std::string &table, const Row &row, Resolution resolution) override; 28 std::pair<int, int64_t> BatchInsert(const std::string &table, const RefRows &rows) override; 29 std::pair<int32_t, Results> BatchInsert(const std::string &table, const RefRows &rows, 30 const std::vector<std::string> &returningFields, Resolution resolution) override; 31 std::pair<int32_t, Results> Update(const Row &row, const AbsRdbPredicates &predicates, 32 const std::vector<std::string> &returningFields, Resolution resolution) override; 33 std::pair<int32_t, Results> Delete( 34 const AbsRdbPredicates &predicates, const std::vector<std::string> &returningFields) override; 35 std::shared_ptr<AbsSharedResultSet> QuerySql(const std::string &sql, const Values &args) override; 36 std::shared_ptr<ResultSet> QueryByStep(const std::string &sql, const Values &args, bool preCount) override; 37 std::pair<int32_t, ValueObject> Execute(const std::string &sql, const Values &args, int64_t trxId) override; 38 std::pair<int32_t, Results> ExecuteExt(const std::string &sql, const Values &args) override; 39 int GetVersion(int &version) override; 40 int SetVersion(int version) override; 41 int Sync(const SyncOption &option, const std::vector<std::string> &tables, const AsyncDetail &async) override; 42 43 private: 44 std::pair<int32_t, std::shared_ptr<Statement>> GetStatement(const std::string &sql) const; 45 void HandleSchemaDDL(std::shared_ptr<Statement> statement); 46 static inline constexpr uint32_t MAX_RETURNING_ROWS = 1024; 47 static Results GenerateResult(int32_t code, std::shared_ptr<Statement> statement, bool isDML = true); 48 static std::shared_ptr<ResultSet> GetValues(std::shared_ptr<Statement> statement); 49 50 int32_t maxArgs_ = 0; 51 int64_t vSchema_ = 0; 52 std::weak_ptr<Connection> conn_; 53 std::string path_; 54 }; 55 } // namespace OHOS::NativeRdb 56 #endif // NATIVE_RDB_TRANS_DB_H