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