1 /* 2 * Copyright (c) 2021 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 NATIVE_RDB_SQLITE_UTILS_H 17 #define NATIVE_RDB_SQLITE_UTILS_H 18 19 #include <map> 20 #include <string> 21 22 #include "sqlite3sym.h" 23 24 namespace OHOS { 25 namespace NativeRdb { 26 27 class SqliteUtils { 28 public: 29 static constexpr int STATEMENT_SELECT = 1; 30 static constexpr int STATEMENT_UPDATE = 2; 31 static constexpr int STATEMENT_ATTACH = 3; 32 static constexpr int STATEMENT_DETACH = 4; 33 static constexpr int STATEMENT_BEGIN = 5; 34 static constexpr int STATEMENT_COMMIT = 6; 35 static constexpr int STATEMENT_ROLLBACK = 7; 36 static constexpr int STATEMENT_PRAGMA = 8; 37 static constexpr int STATEMENT_DDL = 9; 38 static constexpr int STATEMENT_INSERT = 10; 39 static constexpr int STATEMENT_ERROR = 11; 40 static constexpr int STATEMENT_OTHER = 99; 41 static constexpr int CONFLICT_CLAUSE_COUNT = 6; 42 static constexpr int DISABLE_LOAD_EXTENSION = 0; 43 static constexpr int ENABLE_LOAD_EXTENSION = 1; 44 static constexpr int MAX_LOAD_EXTENSION_COUNT = 16; 45 static constexpr const char* REP = "#_"; 46 47 static int GetSqlStatementType(const std::string &sql); 48 static bool IsSupportSqlForExecute(int sqlType); 49 static bool IsSqlReadOnly(int sqlType); 50 static bool IsSpecial(int sqlType); 51 static const char *GetConflictClause(int conflictResolution); 52 static std::string StrToUpper(std::string s); 53 static void Replace(std::string &src, const std::string &rep, const std::string &dst); 54 static bool DeleteFile(const std::string &filePath); 55 static bool RenameFile(const std::string &srcFile, const std::string &destFile); 56 static bool CopyFile(const std::string &srcFile, const std::string &destFile); 57 static std::string Anonymous(const std::string &srcFile); 58 static ssize_t GetFileSize(const std::string &fileName); 59 static bool IsSlaveDbName(const std::string &fileName); 60 static std::string GetSlavePath(const std::string& name); 61 static bool TryAccessSlaveLock(const std::string &dbPath, bool isDelete, bool needCreate, 62 bool isSlaveFailure = false); 63 static const char *HmacAlgoDescription(int32_t hmacAlgo); 64 static const char *KdfAlgoDescription(int32_t kdfAlgo); 65 static const char *EncryptAlgoDescription(int32_t encryptAlgo); 66 67 private: 68 struct SqlType { 69 const char *sql; 70 int32_t type; 71 }; 72 static constexpr SqlType SQL_TYPE_MAP[] = { 73 { "ALT", SqliteUtils::STATEMENT_DDL }, 74 { "ATT", SqliteUtils::STATEMENT_ATTACH }, 75 { "BEG", SqliteUtils::STATEMENT_BEGIN }, 76 { "COM", SqliteUtils::STATEMENT_COMMIT }, 77 { "CRE", SqliteUtils::STATEMENT_DDL }, 78 { "DEL", SqliteUtils::STATEMENT_UPDATE }, 79 { "DET", SqliteUtils::STATEMENT_DETACH }, 80 { "DRO", SqliteUtils::STATEMENT_DDL }, 81 { "END", SqliteUtils::STATEMENT_COMMIT }, 82 { "INS", SqliteUtils::STATEMENT_INSERT }, 83 { "PRA", SqliteUtils::STATEMENT_PRAGMA }, 84 { "REP", SqliteUtils::STATEMENT_UPDATE }, 85 { "ROL", SqliteUtils::STATEMENT_ROLLBACK }, 86 { "SAV", SqliteUtils::STATEMENT_BEGIN }, 87 { "SEL", SqliteUtils::STATEMENT_SELECT }, 88 { "UPD", SqliteUtils::STATEMENT_UPDATE } 89 }; 90 static constexpr size_t TYPE_SIZE = sizeof(SQL_TYPE_MAP) / sizeof(SqlType); 91 static constexpr const char* ON_CONFLICT_CLAUSE[CONFLICT_CLAUSE_COUNT] = { "", " OR ROLLBACK", " OR ABORT", 92 " OR FAIL", " OR IGNORE", " OR REPLACE" }; 93 94 static std::string GetAnonymousName(const std::string& fileName); 95 static std::string AnonyDigits(const std::string& fileName); 96 }; 97 98 } // namespace NativeRdb 99 } // namespace OHOS 100 #endif