1 /*
2  * Copyright (c) 2021 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_SQLITE_STATEMENT_H
17 #define NATIVE_RDB_SQLITE_STATEMENT_H
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "share_block.h"
23 #include "sqlite3sym.h"
24 #include "statement.h"
25 #include "value_object.h"
26 #include "rdb_store_config.h"
27 
28 namespace OHOS {
29 namespace NativeRdb {
30 class Connection;
31 class SqliteStatement : public Statement {
32 public:
33     static constexpr int COLUMN_TYPE_ASSET = 1000;
34     static constexpr int COLUMN_TYPE_ASSETS = 1001;
35     static constexpr int COLUMN_TYPE_FLOATS = 1002;
36     static constexpr int COLUMN_TYPE_BIGINT = 1003;
37 
38     SqliteStatement();
39     ~SqliteStatement();
40     int Prepare(const std::string &sql) override;
41     int Bind(const std::vector<ValueObject> &args) override;
42     std::pair<int32_t, int32_t> Count() override;
43     int Step() override;
44     int Reset() override;
45     int Finalize() override;
46     int Execute(const std::vector<ValueObject> &args) override;
47     int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>> &args) override;
48     std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject> &args) override;
49     int Changes() const override;
50     int64_t LastInsertRowId() const override;
51     int32_t GetColumnCount() const override;
52     std::pair<int32_t, std::string> GetColumnName(int index) const override;
53     std::pair<int32_t, int32_t> GetColumnType(int index) const override;
54     std::pair<int32_t, size_t> GetSize(int index) const override;
55     std::pair<int32_t, ValueObject> GetColumn(int index) const override;
56     bool ReadOnly() const override;
57     bool SupportBlockInfo() const override;
58     int32_t FillBlockInfo(SharedBlockInfo *info) const override;
59     int ModifyLockStatus(const std::string &table, const std::vector<std::vector<uint8_t>> &hashKeys,
60         bool isLock) override;
61 
62 private:
63     friend class SqliteConnection;
64     using Asset = ValueObject::Asset;
65     using Assets = ValueObject::Assets;
66     using BigInt = ValueObject::BigInt;
67     using Floats = ValueObject::FloatVector;
68     using Action = int32_t (*)(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
69     static int32_t BindNil(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
70     static int32_t BindInteger(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
71     static int32_t BindDouble(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
72     static int32_t BindText(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
73     static int32_t BindBool(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
74     static int32_t BindBlob(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
75     static int32_t BindAsset(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
76     static int32_t BindAssets(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
77     static int32_t BindFloats(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
78     static int32_t BindBigInt(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
79     static const int SQLITE_SET_SHAREDBLOCK = 2004;
80     static const int SQLITE_USE_SHAREDBLOCK = 2005;
81     static constexpr Action ACTIONS[ValueObject::TYPE_MAX] = { BindNil, BindInteger, BindDouble, BindText, BindBool,
82         BindBlob, BindAsset, BindAssets, BindFloats, BindBigInt };
83 
84     int Prepare(sqlite3 *dbHandle, const std::string &sql);
85     int BindArgs(const std::vector<ValueObject> &bindArgs);
86     int BindArgs(const std::vector<std::reference_wrapper<ValueObject>> &bindArgs);
87     int IsValid(int index) const;
88     int InnerStep();
89     int InnerFinalize();
90     ValueObject GetValueFromBlob(int32_t index, int32_t type) const;
91     void ReadFile2Buffer();
92     void PrintInfoForDbError(int errCode, const std::string &sql);
93 
94     static constexpr uint32_t BUFFER_LEN = 16;
95 
96     static constexpr int MAX_RETRY_TIMES = 50;
97     // Interval of retrying query in millisecond
98     static constexpr int RETRY_INTERVAL = 1000;
99 
100     bool readOnly_;
101     bool bound_ = false;
102     int columnCount_ = -1;
103     int numParameters_;
104     uint32_t seqId_ = 0;
105     sqlite3_stmt *stmt_;
106     std::shared_ptr<Connection> conn_;
107     std::string sql_;
108     mutable std::vector<int32_t> types_;
109     std::shared_ptr<Statement> slave_;
110     const RdbStoreConfig *config_ = nullptr;
111 };
112 } // namespace NativeRdb
113 } // namespace OHOS
114 #endif
115