1fc0b0055Sopenharmony_ci/*
2fc0b0055Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at
6fc0b0055Sopenharmony_ci *
7fc0b0055Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc0b0055Sopenharmony_ci *
9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and
13fc0b0055Sopenharmony_ci * limitations under the License.
14fc0b0055Sopenharmony_ci */
15fc0b0055Sopenharmony_ci
16fc0b0055Sopenharmony_ci#ifndef SQLITE_HELPER_H
17fc0b0055Sopenharmony_ci#define SQLITE_HELPER_H
18fc0b0055Sopenharmony_ci
19fc0b0055Sopenharmony_ci#include <string>
20fc0b0055Sopenharmony_ci
21fc0b0055Sopenharmony_ci#include "statement.h"
22fc0b0055Sopenharmony_ci
23fc0b0055Sopenharmony_ci#include "sqlite3sym.h"
24fc0b0055Sopenharmony_ci
25fc0b0055Sopenharmony_cinamespace OHOS {
26fc0b0055Sopenharmony_cinamespace Security {
27fc0b0055Sopenharmony_cinamespace AccessToken {
28fc0b0055Sopenharmony_ciclass SqliteHelper {
29fc0b0055Sopenharmony_cipublic:
30fc0b0055Sopenharmony_ci    explicit SqliteHelper(const std::string& dbName, const std::string& dbPath, int32_t version);
31fc0b0055Sopenharmony_ci    virtual ~SqliteHelper();
32fc0b0055Sopenharmony_ci    enum DataBaseVersion {
33fc0b0055Sopenharmony_ci        VERISION_0 = 0,
34fc0b0055Sopenharmony_ci        VERISION_1,
35fc0b0055Sopenharmony_ci        VERISION_2,
36fc0b0055Sopenharmony_ci        VERISION_3,
37fc0b0055Sopenharmony_ci        VERISION_4
38fc0b0055Sopenharmony_ci    };
39fc0b0055Sopenharmony_ci
40fc0b0055Sopenharmony_ci    void Open();
41fc0b0055Sopenharmony_ci    void Close();
42fc0b0055Sopenharmony_ci
43fc0b0055Sopenharmony_ci    int32_t BeginTransaction() const;
44fc0b0055Sopenharmony_ci    int32_t CommitTransaction() const;
45fc0b0055Sopenharmony_ci    int32_t RollbackTransaction() const;
46fc0b0055Sopenharmony_ci
47fc0b0055Sopenharmony_ci    Statement Prepare(const std::string& sql) const;
48fc0b0055Sopenharmony_ci    int32_t ExecuteSql(const std::string& sql) const;
49fc0b0055Sopenharmony_ci    std::string SpitError() const;
50fc0b0055Sopenharmony_ci
51fc0b0055Sopenharmony_ci    virtual void OnCreate() = 0;
52fc0b0055Sopenharmony_ci    virtual void OnUpdate(int32_t version) = 0;
53fc0b0055Sopenharmony_ci
54fc0b0055Sopenharmony_ciprivate:
55fc0b0055Sopenharmony_ci    inline static const std::string PRAGMA_VERSION_COMMAND = "PRAGMA user_version";
56fc0b0055Sopenharmony_ci    static const int32_t GENERAL_ERROR = -1;
57fc0b0055Sopenharmony_ci
58fc0b0055Sopenharmony_ci    const std::string dbName_;
59fc0b0055Sopenharmony_ci    const std::string dbPath_;
60fc0b0055Sopenharmony_ci    int32_t currentVersion_;
61fc0b0055Sopenharmony_ci    sqlite3* db_;
62fc0b0055Sopenharmony_ci
63fc0b0055Sopenharmony_ci    int32_t GetVersion() const;
64fc0b0055Sopenharmony_ci    void SetVersion() const;
65fc0b0055Sopenharmony_ci};
66fc0b0055Sopenharmony_ci} // namespace AccessToken
67fc0b0055Sopenharmony_ci} // namespace Security
68fc0b0055Sopenharmony_ci} // namespace OHOS
69fc0b0055Sopenharmony_ci#endif // SQLITE_HELPER_H
70