• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rdb_types.h"
23 #include "rdb_store_config.h"
24 
25 namespace OHOS {
26 namespace NativeRdb {
27 
28 struct ErrMsgState {
29     bool isCreated = false;
30     bool isDeleted = false;
31     bool isRenamed = false;
32 };
33 
34 using DebugInfo = OHOS::DistributedRdb::RdbDebugInfo;
35 using DfxInfo = OHOS::DistributedRdb::RdbDfxInfo;
36 class SqliteUtils {
37 public:
38     static constexpr int STATEMENT_SELECT = 1;
39     static constexpr int STATEMENT_UPDATE = 2;
40     static constexpr int STATEMENT_ATTACH = 3;
41     static constexpr int STATEMENT_DETACH = 4;
42     static constexpr int STATEMENT_BEGIN = 5;
43     static constexpr int STATEMENT_COMMIT = 6;
44     static constexpr int STATEMENT_ROLLBACK = 7;
45     static constexpr int STATEMENT_PRAGMA = 8;
46     static constexpr int STATEMENT_DDL = 9;
47     static constexpr int STATEMENT_INSERT = 10;
48     static constexpr int STATEMENT_ERROR = 11;
49     static constexpr int STATEMENT_OTHER = 99;
50     static constexpr int CONFLICT_CLAUSE_COUNT = 6;
51     static constexpr int DISABLE_LOAD_EXTENSION = 0;
52     static constexpr int ENABLE_LOAD_EXTENSION = 1;
53     static constexpr int MAX_LOAD_EXTENSION_COUNT = 16;
54     static constexpr int PATH_DEPTH = 3;
55     static constexpr const char *REP = "#_";
56     static constexpr const char *SLAVE_FAILURE = "-slaveFailure";
57     static constexpr const char *SLAVE_INTERRUPT = "-syncInterrupt";
58     static constexpr const char *SLAVE_RESTORE = "-restoring";
59     static constexpr ssize_t SLAVE_ASYNC_REPAIR_CHECK_LIMIT = 1073741824; // 1073741824 = 1024 * 1024 * 1024
60 
61     static int GetSqlStatementType(const std::string &sql);
62     static bool IsSupportSqlForExecute(int sqlType);
63     static bool IsSqlReadOnly(int sqlType);
64     static bool IsSpecial(int sqlType);
65     static const char *GetConflictClause(int conflictResolution);
66     static std::string StrToUpper(const std::string &s);
67     static std::string Replace(const std::string &src, const std::string &rep, const std::string &dst);
68     static bool DeleteFile(const std::string &filePath);
69     static bool RenameFile(const std::string &srcFile, const std::string &destFile);
70     static bool CopyFile(const std::string &srcFile, const std::string &destFile);
71     static size_t DeleteFolder(const std::string &folderPath);
72     API_EXPORT static std::string Anonymous(const std::string &srcFile);
73     static std::string SqlAnonymous(const std::string &sql);
74     static std::string GetArea(const std::string &srcFile);
75     static ssize_t GetFileSize(const std::string &fileName);
76     static bool IsSlaveDbName(const std::string &fileName);
77     static std::string GetSlavePath(const std::string &name);
78     static int SetSlaveInvalid(const std::string &dbPath);
79     static int SetSlaveInterrupted(const std::string &dbPath);
80     static int SetSlaveRestoring(const std::string &dbPath, bool isRestore = true);
81     static bool IsSlaveRestoring(const std::string &dbPath);
82     static ssize_t GetDecompressedSize(const std::string &dbPath);
83     static bool IsSlaveLarge(const std::string &dbPath);
84     static bool IsSlaveInvalid(const std::string &dbPath);
85     static bool IsSlaveInterrupted(const std::string &dbPath);
86     static void SetSlaveValid(const std::string &dbPath);
87     static const char *HmacAlgoDescription(int32_t hmacAlgo);
88     static const char *KdfAlgoDescription(int32_t kdfAlgo);
89     static const char *EncryptAlgoDescription(int32_t encryptAlgo);
90     static bool DeleteDirtyFiles(const std::string &backupFilePath);
91     static std::pair<int32_t, DistributedRdb::RdbDebugInfo> Stat(const std::string &path);
92     static void WriteSqlToFile(const std::string &comparePath, const std::string &sql);
93     static bool CleanFileContent(const std::string &filePath);
94     static std::string GetErrInfoFromMsg(const std::string &message, const std::string &errStr);
95     static ErrMsgState CompareTableFileContent(const std::string &dbPath, const std::string &bundleName,
96         const std::string &tableName);
97     static ErrMsgState CompareColumnFileContent(const std::string &dbPath, const std::string &bundleName,
98         const std::string &columnName);
99     static std::string ReadFileHeader(const std::string &filePath);
100     static std::string FormatDebugInfo(const std::map<std::string, DebugInfo> &debugs, const std::string &header);
101     static std::string FormatDebugInfoBrief(const std::map<std::string, DebugInfo> &debugs, const std::string &header);
102     static std::string FormatDfxInfo(const DfxInfo &dfxInfo);
103     static std::string GetParentModes(const std::string &path, int pathDepth = PATH_DEPTH);
104     static std::string GetFileStatInfo(const DebugInfo &debugInfo);
105 
106 private:
107     struct SqlType {
108         const char *sql;
109         int32_t type;
110     };
111     static constexpr SqlType SQL_TYPE_MAP[] = {
112         { "ALT", SqliteUtils::STATEMENT_DDL },
113         { "ATT", SqliteUtils::STATEMENT_ATTACH },
114         { "BEG", SqliteUtils::STATEMENT_BEGIN },
115         { "COM", SqliteUtils::STATEMENT_COMMIT },
116         { "CRE", SqliteUtils::STATEMENT_DDL },
117         { "DEL", SqliteUtils::STATEMENT_UPDATE },
118         { "DET", SqliteUtils::STATEMENT_DETACH },
119         { "DRO", SqliteUtils::STATEMENT_DDL },
120         { "END", SqliteUtils::STATEMENT_COMMIT },
121         { "INS", SqliteUtils::STATEMENT_INSERT },
122         { "PRA", SqliteUtils::STATEMENT_PRAGMA },
123         { "REP", SqliteUtils::STATEMENT_UPDATE },
124         { "ROL", SqliteUtils::STATEMENT_ROLLBACK },
125         { "SAV", SqliteUtils::STATEMENT_BEGIN },
126         { "SEL", SqliteUtils::STATEMENT_SELECT },
127         { "UPD", SqliteUtils::STATEMENT_UPDATE }
128     };
129     static constexpr size_t TYPE_SIZE = sizeof(SQL_TYPE_MAP) / sizeof(SqlType);
130     static constexpr const char *ON_CONFLICT_CLAUSE[CONFLICT_CLAUSE_COUNT] = { "", " OR ROLLBACK", " OR ABORT",
131         " OR FAIL", " OR IGNORE", " OR REPLACE" };
132 
133     static std::string GetAnonymousName(const std::string &fileName);
134     static std::string AnonymousDigits(const std::string &digits);
135     static bool IsKeyword(const std::string& word);
136     static std::string GetModeInfo(uint32_t st_mode);
137     static int GetPageCountCallback(void *data, int argc, char **argv, char **azColName);
138 };
139 
140 } // namespace NativeRdb
141 } // namespace OHOS
142 #endif