• 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 #include "sqlite_utils.h"
17 
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include <algorithm>
23 #include <cerrno>
24 #include <climits>
25 #include <cstdio>
26 
27 #include "logger.h"
28 #include "rdb_errno.h"
29 
30 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
31 #include "rdb_store_config.h"
32 #endif
33 
34 namespace OHOS {
35 namespace NativeRdb {
36 using namespace OHOS::Rdb;
37 
38 const int SqliteUtils::STATEMENT_SELECT = 1;
39 const int SqliteUtils::STATEMENT_UPDATE = 2;
40 const int SqliteUtils::STATEMENT_ATTACH = 3;
41 const int SqliteUtils::STATEMENT_DETACH = 4;
42 const int SqliteUtils::STATEMENT_BEGIN = 5;
43 const int SqliteUtils::STATEMENT_COMMIT = 6;
44 const int SqliteUtils::STATEMENT_ROLLBACK = 7;
45 const int SqliteUtils::STATEMENT_PRAGMA = 8;
46 const int SqliteUtils::STATEMENT_DDL = 9;
47 const int SqliteUtils::STATEMENT_OTHER = 99;
48 
49 constexpr int32_t HEAD_SIZE = 3;
50 constexpr int32_t END_SIZE = 3;
51 constexpr int32_t MIN_SIZE = HEAD_SIZE + END_SIZE + 3;
52 constexpr const char *REPLACE_CHAIN = "***";
53 constexpr const char *DEFAULT_ANONYMOUS = "******";
54 
55 const std::map<std::string, int> SqliteUtils::SQL_TYPE_MAP = {
56     { "SEL", SqliteUtils::STATEMENT_SELECT },
57     { "INS", SqliteUtils::STATEMENT_UPDATE },
58     { "UPD", SqliteUtils::STATEMENT_UPDATE },
59     { "REP", SqliteUtils::STATEMENT_UPDATE },
60     { "DEL", SqliteUtils::STATEMENT_UPDATE },
61     { "ATT", SqliteUtils::STATEMENT_ATTACH },
62     { "DET", SqliteUtils::STATEMENT_DETACH },
63     { "COM", SqliteUtils::STATEMENT_COMMIT },
64     { "END", SqliteUtils::STATEMENT_COMMIT },
65     { "ROL", SqliteUtils::STATEMENT_ROLLBACK },
66     { "BEG", SqliteUtils::STATEMENT_BEGIN },
67     { "PRA", SqliteUtils::STATEMENT_PRAGMA },
68     { "CRE", SqliteUtils::STATEMENT_DDL },
69     { "DRO", SqliteUtils::STATEMENT_DDL },
70     { "ALT", SqliteUtils::STATEMENT_DDL },
71 };
72 
GetSqlStatementType(const std::string & sql)73 int SqliteUtils::GetSqlStatementType(const std::string &sql)
74 {
75     std::string sqlStr = sql;
76     /* the sql string length less than 3 can not be any type sql */
77     if (sqlStr.length() < 3) {
78         return STATEMENT_OTHER;
79     }
80     /* analyze the sql type through first 3 character */
81     std::string prefixSql = StrToUpper(sqlStr.substr(0, 3));
82     auto iter = SQL_TYPE_MAP.find(prefixSql);
83     if (iter != SQL_TYPE_MAP.end()) {
84         return iter->second;
85     }
86 
87     return STATEMENT_OTHER;
88 }
89 
StrToUpper(std::string s)90 std::string SqliteUtils::StrToUpper(std::string s)
91 {
92     std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
93     return s;
94 }
95 
IsSqlReadOnly(int sqlType)96 bool SqliteUtils::IsSqlReadOnly(int sqlType)
97 {
98     return (sqlType == STATEMENT_SELECT) ? true : false;
99 }
100 
IsSpecial(int sqlType)101 bool SqliteUtils::IsSpecial(int sqlType)
102 {
103     if (sqlType == STATEMENT_BEGIN || sqlType == STATEMENT_COMMIT || sqlType == STATEMENT_ROLLBACK) {
104         return true;
105     }
106     return false;
107 }
108 
109 const std::string SqliteUtils::ON_CONFLICT_CLAUSE[] = { "", " OR ROLLBACK", " OR ABORT", " OR FAIL", " OR IGNORE",
110     " OR REPLACE" };
GetConflictClause(int conflictResolution,std::string & conflictClause)111 int SqliteUtils::GetConflictClause(int conflictResolution, std::string &conflictClause)
112 {
113     if (conflictResolution < 0 || conflictResolution >= CONFLICT_CLAUSE_COUNT) {
114         return E_INVALID_CONFLICT_FLAG;
115     }
116     conflictClause = ON_CONFLICT_CLAUSE[conflictResolution];
117     return E_OK;
118 }
119 
DeleteFile(const std::string filePath)120 bool SqliteUtils::DeleteFile(const std::string filePath)
121 {
122     return remove(filePath.c_str()) == 0;
123 }
124 
RenameFile(const std::string srcFile,const std::string destFile)125 int SqliteUtils::RenameFile(const std::string srcFile, const std::string destFile)
126 {
127     return rename(srcFile.c_str(), destFile.c_str());
128 }
129 
Anonymous(const std::string & srcFile)130 std::string SqliteUtils::Anonymous(const std::string &srcFile)
131 {
132     if (srcFile.length() <= HEAD_SIZE) {
133         return DEFAULT_ANONYMOUS;
134     }
135 
136     if (srcFile.length() < MIN_SIZE) {
137         return (srcFile.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
138     }
139 
140     return (srcFile.substr(0, HEAD_SIZE) + REPLACE_CHAIN + srcFile.substr(srcFile.length() - END_SIZE, END_SIZE));
141 }
142 
GetFileSize(const std::string fileName)143 int SqliteUtils::GetFileSize(const std::string fileName)
144 {
145     if (fileName.empty() || access(fileName.c_str(), F_OK) != 0) {
146         return 0;
147     }
148 
149     struct stat fileStat;
150     if (stat(fileName.c_str(), &fileStat) < 0) {
151         LOG_ERROR("Failed to get file information, errno: %{public}d", errno);
152         return INT_MAX;
153     }
154 
155     return static_cast<int>(fileStat.st_size);
156 }
157 } // namespace NativeRdb
158 } // namespace OHOS
159