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 25 namespace OHOS { 26 namespace Security { 27 namespace AccessToken { 28 class SqliteHelper { 29 public: 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 54 private: 55 inline static const std::string PRAGMA_VERSION_COMMAND = "PRAGMA user_version"; 56 inline static const std::string PRAGMA_WAL_COMMAND = "PRAGMA journal_mode=WAL"; 57 static const int32_t GENERAL_ERROR = -1; 58 59 const std::string dbName_; 60 const std::string dbPath_; 61 int32_t currentVersion_; 62 sqlite3* db_; 63 64 void SetWal() const; 65 int32_t GetVersion() const; 66 void SetVersion() const; 67 }; 68 } // namespace AccessToken 69 } // namespace Security 70 } // namespace OHOS 71 #endif // SQLITE_HELPER_H 72