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