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