• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_CONNECTION_H
17 #define NATIVE_RDB_SQLITE_CONNECTION_H
18 
19 #include <mutex>
20 #include <memory>
21 #include <vector>
22 
23 #include "sqlite3sym.h"
24 #include "rdb_store_config.h"
25 #include "sqlite_statement.h"
26 #include "value_object.h"
27 #include "shared_block.h"
28 
29 typedef struct ClientChangedData ClientChangedData;
30 namespace OHOS {
31 namespace NativeRdb {
32 
33 /**
34  * @brief Use DataChangeCallback replace std::function<void(ClientChangedData &clientChangedData)>.
35  */
36 using DataChangeCallback = std::function<void(ClientChangedData &clientChangedData)>;
37 
38 class SqliteConnection {
39 public:
40     static std::shared_ptr<SqliteConnection> Open(const RdbStoreConfig &config, bool isWriteConnection, int &errCode);
41     ~SqliteConnection();
42     bool IsWriteConnection() const;
43     int Prepare(const std::string &sql, bool &outIsReadOnly);
44     int ExecuteSql(const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>());
45     int ExecuteForChangedRowCount(int &changedRows, const std::string &sql, const std::vector<ValueObject> &bindArgs);
46     int ExecuteForLastInsertedRowId(int64_t &outRowId, const std::string &sql,
47         const std::vector<ValueObject> &bindArgs);
48     int ExecuteGetLong(int64_t &outValue, const std::string &sql,
49         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>());
50     int ExecuteGetString(std::string &outValue, const std::string &sql,
51         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>());
52     std::shared_ptr<SqliteStatement> BeginStepQuery(int &errCode, const std::string &sql,
53         const std::vector<ValueObject> &args) const;
54     int ExecuteEncryptSql(const RdbStoreConfig &config, uint32_t iter);
55     int ReSetKey(const RdbStoreConfig &config);
56     int DesFinalize();
57     int EndStepQuery();
58     void SetInTransaction(bool transaction);
59     bool IsInTransaction();
60     int TryCheckPoint();
61     int LimitWalSize();
62 #ifdef RDB_SUPPORT_ICU
63     int ConfigLocale(const std::string localeStr);
64 #endif
65     int ExecuteForSharedBlock(int &rowNum, std::string sql, const std::vector<ValueObject> &bindArgs,
66         AppDataFwk::SharedBlock *sharedBlock, int startPos, int requiredPos, bool isCountAllRows);
67     int CleanDirtyData(const std::string &table, uint64_t cursor);
68     int RegisterCallBackObserver(const DataChangeCallback &clientChangedData);
69     int GetMaxVariableNumber();
70 private:
71     static constexpr const char *MERGE_ASSETS_FUNC = "merge_assets";
72     explicit SqliteConnection(bool isWriteConnection);
73     int InnerOpen(const RdbStoreConfig &config, uint32_t retry);
74     int GetDbPath(const RdbStoreConfig &config, std::string &dbPath);
75     int Configure(const RdbStoreConfig &config, uint32_t retry, std::string &dbPath);
76     int SetPageSize(const RdbStoreConfig &config);
77     int SetEncryptKey(const RdbStoreConfig &config, uint32_t iter);
78     int SetJournalMode(const RdbStoreConfig &config);
79     int SetJournalSizeLimit(const RdbStoreConfig &config);
80     int SetAutoCheckpoint(const RdbStoreConfig &config);
81     int SetWalSyncMode(const std::string &syncMode);
82     int PrepareAndBind(const std::string &sql, const std::vector<ValueObject> &bindArgs);
83     void LimitPermission(const std::string &dbPath) const;
84 
85     int SetPersistWal();
86     int SetBusyTimeout(int timeout);
87 
88     int RegDefaultFunctions(sqlite3 *dbHandle);
89     static void MergeAssets(sqlite3_context *ctx, int argc, sqlite3_value **argv);
90     static void CompAssets(std::map<std::string, ValueObject::Asset> &oldAssets, std::map<std::string,
91         ValueObject::Asset> &newAssets);
92     static void MergeAsset(ValueObject::Asset &oldAsset, ValueObject::Asset &newAsset);
93 
94     int SetCustomFunctions(const RdbStoreConfig &config);
95     int SetCustomScalarFunction(const std::string &functionName, int argc, ScalarFunction *function);
96 
97     friend class SqliteStatement;
98 
99     sqlite3 *dbHandle;
100     bool isWriteConnection;
101     bool isReadOnly;
102     SqliteStatement statement;
103     std::shared_ptr<SqliteStatement> stepStatement;
104     std::string filePath;
105     int openFlags;
106     std::mutex rdbMutex;
107     bool inTransaction_;
108     std::map<std::string, ScalarFunctionInfo> customScalarFunctions_;
109 
110     static constexpr int DEFAULT_BUSY_TIMEOUT_MS = 2000;
111     static constexpr uint32_t NO_ITER = 0;
112     static constexpr uint32_t ITER_V1 = 5000;
113     static constexpr uint32_t ITERS[] = {NO_ITER, ITER_V1};
114     static constexpr uint32_t ITERS_COUNT = sizeof(ITERS) / sizeof(ITERS[0]);
115 
116     bool isConfigured_ = false;
117     int maxVariableNumber_;
118 };
119 
120 } // namespace NativeRdb
121 } // namespace OHOS
122 #endif