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_RD_STATEMENT_H
17 #define NATIVE_RDB_RD_STATEMENT_H
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "connection.h"
23 #include "rd_utils.h"
24 #include "statement.h"
25 #include "value_object.h"
26 #include "rd_connection.h"
27 #include "rdb_store_config.h"
28 
29 namespace OHOS {
30 namespace NativeRdb {
31 class RdStatement final : public Statement {
32 public:
33     RdStatement();
34     ~RdStatement();
35     int Finalize() override;
36     int32_t Prepare(const std::string& sql) override;
37     int32_t Bind(const std::vector<ValueObject>& args) override;
38     std::pair<int32_t, int32_t> Count() override;
39     int32_t Step() override;
40     int32_t Reset() override;
41     int32_t Execute(const std::vector<ValueObject>& args) override;
42     int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>>& args) override;
43     std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject>& args) override;
44     int32_t Changes() const override;
45     int64_t LastInsertRowId() const override;
46     int32_t GetColumnCount() const override;
47     std::pair<int32_t, std::string> GetColumnName(int32_t index) const override;
48     std::pair<int32_t, int32_t> GetColumnType(int32_t index) const override;
49     std::pair<int32_t, size_t> GetSize(int32_t index) const override;
50     std::pair<int32_t, ValueObject> GetColumn(int32_t index) const override;
51     bool ReadOnly() const override;
52     bool SupportBlockInfo() const override;
53     int32_t FillBlockInfo(SharedBlockInfo* info) const override;
54     void GetProperties();
55 
56 private:
57     friend class RdConnection;
58     int Prepare(GRD_DB *db, const std::string &sql);
59     int32_t Bind(const std::vector<std::reference_wrapper<ValueObject>>& args);
60     int InnerBindBlobTypeArgs(const ValueObject &bindArg, uint32_t index) const;
61     int IsValid(int index) const;
62 
63     bool readOnly_ = false;
64     bool isStepInPrepare_ = false;
65     int stepCnt_ = 0;
66     std::string sql_ = "";
67     GRD_SqlStmt *stmtHandle_ = nullptr;
68     GRD_DB *dbHandle_ = nullptr;
69     std::shared_ptr<Connection> conn_;
70     int columnCount_ = 0;
71 
72     std::map<std::string, std::function<int32_t(const int &value)>> setPragmas_;
73     std::map<std::string, std::function<int32_t(int &version)>> getPragmas_;
74     const RdbStoreConfig *config_ = nullptr;
75 };
76 } // namespace NativeRdb
77 } // namespace OHOS
78 #endif // NATIVE_RDB_RD_STATEMENT_H
79