• 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 "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 
64 private:
65     struct SqlType {
66         const char *sql;
67         int32_t type;
68     };
69     static constexpr SqlType SQL_TYPE_MAP[] = {
70         { "ALT", SqliteUtils::STATEMENT_DDL },
71         { "ATT", SqliteUtils::STATEMENT_ATTACH },
72         { "BEG", SqliteUtils::STATEMENT_BEGIN },
73         { "COM", SqliteUtils::STATEMENT_COMMIT },
74         { "CRE", SqliteUtils::STATEMENT_DDL },
75         { "DEL", SqliteUtils::STATEMENT_UPDATE },
76         { "DET", SqliteUtils::STATEMENT_DETACH },
77         { "DRO", SqliteUtils::STATEMENT_DDL },
78         { "END", SqliteUtils::STATEMENT_COMMIT },
79         { "INS", SqliteUtils::STATEMENT_INSERT },
80         { "PRA", SqliteUtils::STATEMENT_PRAGMA },
81         { "REP", SqliteUtils::STATEMENT_UPDATE },
82         { "ROL", SqliteUtils::STATEMENT_ROLLBACK },
83         { "SAV", SqliteUtils::STATEMENT_BEGIN },
84         { "SEL", SqliteUtils::STATEMENT_SELECT },
85         { "UPD", SqliteUtils::STATEMENT_UPDATE }
86     };
87     static constexpr size_t TYPE_SIZE = sizeof(SQL_TYPE_MAP) / sizeof(SqlType);
88     static constexpr const char* ON_CONFLICT_CLAUSE[CONFLICT_CLAUSE_COUNT] = { "", " OR ROLLBACK", " OR ABORT",
89         " OR FAIL", " OR IGNORE", " OR REPLACE" };
90 
91     static std::string GetAnonymousName(const std::string& fileName);
92     static std::string AnonyDigits(const std::string& fileName);
93 };
94 
95 } // namespace NativeRdb
96 } // namespace OHOS
97 #endif