• 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 "sqlite_config.h"
25 #include "sqlite_statement.h"
26 #include "value_object.h"
27 #include "shared_block.h"
28 
29 namespace OHOS {
30 namespace NativeRdb {
31 
32 class SqliteConnection {
33 public:
34     static SqliteConnection *Open(const SqliteConfig &config, bool isWriteConnection, int &errCode);
35     ~SqliteConnection();
36     bool IsWriteConnection() const;
37     int Prepare(const std::string &sql, bool &outIsReadOnly);
38     int PrepareAndGetInfo(const std::string &sql, bool &outIsReadOnly, int &numParameters,
39         std::vector<std::string> &columnNames);
40     int ExecuteSql(const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>());
41     int ExecuteForChangedRowCount(int &changedRows, const std::string &sql, const std::vector<ValueObject> &bindArgs);
42     int ExecuteForLastInsertedRowId(int64_t &outRowId, const std::string &sql,
43         const std::vector<ValueObject> &bindArgs);
44     int ExecuteGetLong(int64_t &outValue, const std::string &sql,
45         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>());
46     int ExecuteGetString(std::string &outValue, const std::string &sql,
47         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>());
48     std::shared_ptr<SqliteStatement> BeginStepQuery(int &errCode, const std::string &sql,
49         const std::vector<std::string> &selectionArgs) const;
50     int EndStepQuery();
51 #ifdef RDB_SUPPORT_ICU
52     int ConfigLocale(const std::string localeStr);
53 #endif
54     int ExecuteForSharedBlock(int &rowNum, std::string sql, const std::vector<ValueObject> &bindArgs,
55         AppDataFwk::SharedBlock *sharedBlock, int startPos, int requiredPos, bool isCountAllRows);
56 
57 private:
58     explicit SqliteConnection(bool isWriteConnection);
59     int InnerOpen(const SqliteConfig &config);
60     int Config(const SqliteConfig &config);
61     int SetPageSize(const SqliteConfig &config);
62     int SetPageSize();
63     int SetEncryptAlgo();
64     int SetEncryptKey(const std::vector<uint8_t> &encryptKey);
65     int SetJournalMode(const std::string &journalMode, const std::string &synclMode);
66     int SetJournalSizeLimit();
67     int SetAutoCheckpoint();
68     int SetWalSyncMode(const std::string &syncMode);
69     int PrepareAndBind(const std::string &sql, const std::vector<ValueObject> &bindArgs);
70     void LimitPermission(const std::string &dbPath) const;
71     int ManageKey(const SqliteConfig &config);
72     int InitKey();
73     int GetKeyFromFile();
74 
75     int SetPersistWal();
76     int SetBusyTimeout(int timeout);
77 
78     sqlite3 *dbHandle;
79     bool isWriteConnection;
80     bool isReadOnly;
81     SqliteStatement statement;
82     std::shared_ptr<SqliteStatement> stepStatement;
83     std::string filePath;
84     int openFlags;
85     std::mutex rdbMutex;
86 
87     static constexpr int DEFAULT_BUSY_TIMEOUT_MS = 2000;
88 };
89 
90 } // namespace NativeRdb
91 } // namespace OHOS
92 #endif
93