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_STORAGE_H 17 #define SQLITE_STORAGE_H 18 19 #include "data_storage.h" 20 #include "nocopyable.h" 21 #include "rwlock.h" 22 #include "sqlite_helper.h" 23 #include "token_field_const.h" 24 25 namespace OHOS { 26 namespace Security { 27 namespace AccessToken { 28 class SqliteStorage : public DataStorage, public SqliteHelper { 29 public: 30 enum ExecuteResult { FAILURE = -1, SUCCESS }; 31 32 struct SqliteTable { 33 public: 34 std::string tableName_; 35 std::vector<std::string> tableColumnNames_; 36 }; 37 38 static SqliteStorage& GetInstance(); 39 40 ~SqliteStorage() override; 41 42 int Add(const DataType type, const std::vector<GenericValues>& values) override; 43 44 int Remove(const DataType type, const GenericValues& conditions) override; 45 46 int Modify(const DataType type, const GenericValues& modifyValues, const GenericValues& conditions) override; 47 48 int Find(const DataType type, std::vector<GenericValues>& results) override; 49 50 int RefreshAll(const DataType type, const std::vector<GenericValues>& values) override; 51 52 void OnCreate() override; 53 void OnUpdate() override; 54 55 private: 56 SqliteStorage(); 57 DISALLOW_COPY_AND_MOVE(SqliteStorage); 58 59 std::map<DataType, SqliteTable> dataTypeToSqlTable_; 60 OHOS::Utils::RWLock rwLock_; 61 62 int CreateHapTokenInfoTable() const; 63 int CreateNativeTokenInfoTable() const; 64 int CreatePermissionDefinitionTable() const; 65 int CreatePermissionStateTable() const; 66 67 std::string CreateInsertPrepareSqlCmd(const DataType type) const; 68 std::string CreateDeletePrepareSqlCmd( 69 const DataType type, const std::vector<std::string>& columnNames = std::vector<std::string>()) const; 70 std::string CreateUpdatePrepareSqlCmd(const DataType type, const std::vector<std::string>& modifyColumns, 71 const std::vector<std::string>& conditionColumns) const; 72 std::string CreateSelectPrepareSqlCmd(const DataType type) const; 73 74 private: 75 inline static const std::string HAP_TOKEN_INFO_TABLE = "hap_token_info_table"; 76 inline static const std::string NATIVE_TOKEN_INFO_TABLE = "native_token_info_table"; 77 inline static const std::string PERMISSION_DEF_TABLE = "permission_definition_table"; 78 inline static const std::string PERMISSION_STATE_TABLE = "permission_state_table"; 79 inline static const std::string DATABASE_NAME = "access_token.db"; 80 inline static const std::string DATABASE_PATH = "/data/service/el1/public/access_token/"; 81 static const int DATABASE_VERSION = 1; 82 }; 83 } // namespace AccessToken 84 } // namespace Security 85 } // namespace OHOS 86 87 #endif // SQLITE_STORAGE_H 88