• 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 
Replace(std::string & src,const std::string & rep,const std::string & dst)96 void SqliteUtils::Replace(std::string &src, const std::string &rep, const std::string &dst)
97 {
98     if (src.empty() || rep.empty()) {
99         return;
100     }
101     size_t pos = 0;
102     while ((pos = src.find(rep, pos)) != std::string::npos) {
103         src.replace(pos, rep.length(), dst);
104         pos += dst.length();
105     }
106 }
107 
IsSqlReadOnly(int sqlType)108 bool SqliteUtils::IsSqlReadOnly(int sqlType)
109 {
110     return (sqlType == STATEMENT_SELECT) ? true : false;
111 }
112 
IsSpecial(int sqlType)113 bool SqliteUtils::IsSpecial(int sqlType)
114 {
115     if (sqlType == STATEMENT_BEGIN || sqlType == STATEMENT_COMMIT || sqlType == STATEMENT_ROLLBACK) {
116         return true;
117     }
118     return false;
119 }
120 
121 const std::string SqliteUtils::ON_CONFLICT_CLAUSE[] = { "", " OR ROLLBACK", " OR ABORT", " OR FAIL", " OR IGNORE",
122     " OR REPLACE" };
GetConflictClause(int conflictResolution,std::string & conflictClause)123 int SqliteUtils::GetConflictClause(int conflictResolution, std::string &conflictClause)
124 {
125     if (conflictResolution < 0 || conflictResolution >= CONFLICT_CLAUSE_COUNT) {
126         return E_INVALID_CONFLICT_FLAG;
127     }
128     conflictClause = ON_CONFLICT_CLAUSE[conflictResolution];
129     return E_OK;
130 }
131 
DeleteFile(const std::string filePath)132 bool SqliteUtils::DeleteFile(const std::string filePath)
133 {
134     return remove(filePath.c_str()) == 0;
135 }
136 
RenameFile(const std::string srcFile,const std::string destFile)137 int SqliteUtils::RenameFile(const std::string srcFile, const std::string destFile)
138 {
139     return rename(srcFile.c_str(), destFile.c_str());
140 }
141 
Anonymous(const std::string & srcFile)142 std::string SqliteUtils::Anonymous(const std::string &srcFile)
143 {
144     if (srcFile.length() <= HEAD_SIZE) {
145         return DEFAULT_ANONYMOUS;
146     }
147 
148     if (srcFile.length() < MIN_SIZE) {
149         return (srcFile.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
150     }
151 
152     return (srcFile.substr(0, HEAD_SIZE) + REPLACE_CHAIN + srcFile.substr(srcFile.length() - END_SIZE, END_SIZE));
153 }
154 
GetFileSize(const std::string fileName)155 int SqliteUtils::GetFileSize(const std::string fileName)
156 {
157     if (fileName.empty() || access(fileName.c_str(), F_OK) != 0) {
158         return 0;
159     }
160 
161     struct stat fileStat;
162     if (stat(fileName.c_str(), &fileStat) < 0) {
163         LOG_ERROR("Failed to get file information, errno: %{public}d", errno);
164         return INT_MAX;
165     }
166 
167     return static_cast<int>(fileStat.st_size);
168 }
169 } // namespace NativeRdb
170 } // namespace OHOS
171