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 33 void Open(); 34 void Close(); 35 36 int32_t BeginTransaction() const; 37 int32_t CommitTransaction() const; 38 int32_t RollbackTransaction() const; 39 40 Statement Prepare(const std::string& sql) const; 41 int32_t ExecuteSql(const std::string& sql) const; 42 std::string SpitError() const; 43 44 virtual void OnCreate() = 0; 45 virtual void OnUpdate() = 0; 46 47 private: 48 inline static const std::string PRAGMA_VERSION_COMMAND = "PRAGMA user_version"; 49 static const int32_t GENERAL_ERROR = -1; 50 51 const std::string dbName_; 52 const std::string dbPath_; 53 int32_t currentVersion_; 54 sqlite3* db_; 55 56 int32_t GetVersion() const; 57 void SetVersion() const; 58 }; 59 } // namespace AccessToken 60 } // namespace Security 61 } // namespace OHOS 62 #endif // SQLITE_HELPER_H 63