1 /*
2  * Copyright (c) 2023 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 SQLITE_HELPER_H
17 #define SQLITE_HELPER_H
18 
19 #include <string>
20 #include "statement.h"
21 #include "sqlite3sym.h"
22 
23 namespace OHOS {
24 namespace AccessControl {
25 namespace SandboxManager {
26 class SqliteHelper {
27 public:
28     explicit SqliteHelper(const std::string &dbName, const std::string &dbPath, int32_t version);
29     virtual ~SqliteHelper();
30 
31     void Open();
32     void Close();
33 
34     int32_t BeginTransaction() const;
35     int32_t CommitTransaction() const;
36     int32_t RollbackTransaction() const;
37 
38     Statement Prepare(const std::string &sql) const;
39     int32_t ExecuteSql(const std::string &sql) const;
40     std::string SpitError() const;
41 
42     virtual void OnCreate() = 0;
43     virtual void OnUpdate() = 0;
44 
45 private:
46     inline static const std::string PRAGMA_VERSION_COMMAND = "PRAGMA user_version";
47     static const int32_t GENERAL_ERROR = -1;
48 
49     const std::string dbName_;
50     const std::string dbPath_;
51     int32_t currentVersion_;
52     sqlite3* db_;
53 
54     int32_t GetVersion() const;
55     void SetVersion() const;
56 };
57 } // namespace SandboxManager
58 } // namespace AccessControl
59 } // namespace OHOS
60 #endif // SQLITE_HELPER_H
61