1 /* 2 * Copyright (c) 2024 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 ACCOUNT_SQLITE_HELPER_H 17 #define ACCOUNT_SQLITE_HELPER_H 18 19 #include <string> 20 21 #include "statement.h" 22 23 #include "sqlite3sym.h" 24 25 namespace OHOS { 26 namespace AccountSA { 27 class SqliteHelper { 28 public: 29 SqliteHelper(const std::string& dbName, const std::string& dbPath, int32_t version); 30 virtual ~SqliteHelper(); 31 enum DataBaseVersion { 32 VERISION_0 = 0, 33 VERISION_1, 34 VERISION_2, 35 VERISION_3, 36 VERISION_4 37 }; 38 39 void Open(); 40 void Close(); 41 42 int32_t BeginTransaction() const; 43 int32_t CommitTransaction() const; 44 int32_t RollbackTransaction() const; 45 46 Statement Prepare(const std::string& sql) const; 47 int32_t ExecuteSql(const std::string& sql) const; 48 std::string SpitError() const; 49 bool CheckReady() 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 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 AccountSA 67 } // namespace OHOS 68 #endif // ACCOUNT_SQLITE_HELPER_H 69