• 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();
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     bool ReadOnly() const override;
58     bool SupportBlockInfo() const override;
59     int32_t FillBlockInfo(SharedBlockInfo *info) const override;
60     int ModifyLockStatus(
61         const std::string &table, const std::vector<std::vector<uint8_t>> &hashKeys, bool isLock) override;
62 
63 private:
64     friend class SqliteConnection;
65     using Asset = ValueObject::Asset;
66     using Assets = ValueObject::Assets;
67     using BigInt = ValueObject::BigInt;
68     using Floats = ValueObject::FloatVector;
69     using Action = int32_t (*)(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
70     static int32_t BindNil(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
71     static int32_t BindInteger(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
72     static int32_t BindDouble(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
73     static int32_t BindText(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
74     static int32_t BindBool(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
75     static int32_t BindBlob(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
76     static int32_t BindAsset(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
77     static int32_t BindAssets(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
78     static int32_t BindFloats(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
79     static int32_t BindBigInt(sqlite3_stmt *stat, int index, const ValueObject::Type &object);
80     static const int SQLITE_SET_SHAREDBLOCK = 2004;
81     static const int SQLITE_USE_SHAREDBLOCK = 2005;
82     static constexpr Action ACTIONS[ValueObject::TYPE_MAX] = { BindNil, BindInteger, BindDouble, BindText, BindBool,
83         BindBlob, BindAsset, BindAssets, BindFloats, BindBigInt };
84 
85     int Prepare(sqlite3 *dbHandle, const std::string &sql);
86     int BindArgs(const std::vector<ValueObject> &bindArgs);
87     int BindArgs(const std::vector<std::reference_wrapper<ValueObject>> &bindArgs);
88     int IsValid(int index) const;
89     int InnerStep();
90     int InnerFinalize();
91     ValueObject GetValueFromBlob(int32_t index, int32_t type) const;
92     void ReadFile2Buffer();
93     void PrintInfoForDbError(int errCode, const std::string &sql);
94     void TableReport(const std::string &errMsg, const std::string &bundleName, ErrMsgState state);
95     void ColumnReport(const std::string &errMsg, const std::string &bundleName, ErrMsgState state);
96     void HandleErrMsg(const std::string &errMsg, const std::string &dbPath, const std::string &bundleName);
97 
98     static constexpr uint32_t BUFFER_LEN = 16;
99 
100     static constexpr int MAX_RETRY_TIMES = 50;
101     // Interval of retrying query in millisecond
102     static constexpr int RETRY_INTERVAL = 1000;
103 
104     bool readOnly_;
105     bool bound_ = false;
106     int columnCount_ = -1;
107     int numParameters_;
108     uint32_t seqId_ = 0;
109     sqlite3_stmt *stmt_;
110     std::shared_ptr<Connection> conn_;
111     std::string sql_;
112     mutable std::vector<int32_t> types_;
113     std::shared_ptr<Statement> slave_;
114     const RdbStoreConfig *config_ = nullptr;
115 };
116 } // namespace NativeRdb
117 } // namespace OHOS
118 #endif
119