• 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 <algorithm>
19 #include <cstdio>
20 
21 #include "logger.h"
22 #include "rdb_errno.h"
23 
24 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
25 #include "rdb_store_config.h"
26 #include <unistd.h>
27 #endif
28 
29 namespace OHOS {
30 namespace NativeRdb {
31 const int SqliteUtils::STATEMENT_SELECT = 1;
32 const int SqliteUtils::STATEMENT_UPDATE = 2;
33 const int SqliteUtils::STATEMENT_ATTACH = 3;
34 const int SqliteUtils::STATEMENT_DETACH = 4;
35 const int SqliteUtils::STATEMENT_BEGIN = 5;
36 const int SqliteUtils::STATEMENT_COMMIT = 6;
37 const int SqliteUtils::STATEMENT_ROLLBACK = 7;
38 const int SqliteUtils::STATEMENT_PRAGMA = 8;
39 const int SqliteUtils::STATEMENT_DDL = 9;
40 const int SqliteUtils::STATEMENT_OTHER = 99;
41 
42 const std::map<std::string, int> SqliteUtils::SQL_TYPE_MAP = {
43     { "SEL", SqliteUtils::STATEMENT_SELECT },
44     { "INS", SqliteUtils::STATEMENT_UPDATE },
45     { "UPD", SqliteUtils::STATEMENT_UPDATE },
46     { "REP", SqliteUtils::STATEMENT_UPDATE },
47     { "DEL", SqliteUtils::STATEMENT_UPDATE },
48     { "ATT", SqliteUtils::STATEMENT_ATTACH },
49     { "DET", SqliteUtils::STATEMENT_DETACH },
50     { "COM", SqliteUtils::STATEMENT_COMMIT },
51     { "END", SqliteUtils::STATEMENT_COMMIT },
52     { "ROL", SqliteUtils::STATEMENT_ROLLBACK },
53     { "BEG", SqliteUtils::STATEMENT_BEGIN },
54     { "PRA", SqliteUtils::STATEMENT_PRAGMA },
55     { "CRE", SqliteUtils::STATEMENT_DDL },
56     { "DRO", SqliteUtils::STATEMENT_DDL },
57     { "ALT", SqliteUtils::STATEMENT_DDL },
58 };
59 
GetSqlStatementType(const std::string & sql)60 int SqliteUtils::GetSqlStatementType(const std::string &sql)
61 {
62     std::string sqlStr = sql;
63     /* the sql string length less than 3 can not be any type sql */
64     if (sqlStr.length() < 3) {
65         return STATEMENT_OTHER;
66     }
67     /* analyze the sql type through first 3 character */
68     std::string prefixSql = StrToUpper(sqlStr.substr(0, 3));
69     auto iter = SQL_TYPE_MAP.find(prefixSql);
70     if (iter != SQL_TYPE_MAP.end()) {
71         return iter->second;
72     }
73 
74     return STATEMENT_OTHER;
75 }
76 
StrToUpper(std::string s)77 std::string SqliteUtils::StrToUpper(std::string s)
78 {
79     std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
80     return s;
81 }
82 
IsSqlReadOnly(int sqlType)83 bool SqliteUtils::IsSqlReadOnly(int sqlType)
84 {
85     return (sqlType == STATEMENT_SELECT) ? true : false;
86 }
87 
IsSpecial(int sqlType)88 bool SqliteUtils::IsSpecial(int sqlType)
89 {
90     if (sqlType == STATEMENT_BEGIN || sqlType == STATEMENT_COMMIT || sqlType == STATEMENT_ROLLBACK) {
91         return true;
92     }
93     return false;
94 }
95 
96 const std::string SqliteUtils::ON_CONFLICT_CLAUSE[] = { "", " OR ROLLBACK", " OR ABORT", " OR FAIL", " OR IGNORE",
97     " OR REPLACE" };
GetConflictClause(int conflictResolution,std::string & conflictClause)98 int SqliteUtils::GetConflictClause(int conflictResolution, std::string &conflictClause)
99 {
100     if (conflictResolution < 0 || conflictResolution >= CONFLICT_CLAUSE_COUNT) {
101         return E_INVALID_CONFLICT_FLAG;
102     }
103     conflictClause = ON_CONFLICT_CLAUSE[conflictResolution];
104     return E_OK;
105 }
106 
DeleteFile(const std::string filePath)107 bool SqliteUtils::DeleteFile(const std::string filePath)
108 {
109     return remove(filePath.c_str()) == 0;
110 }
111 
RenameFile(const std::string srcFile,const std::string destFile)112 int SqliteUtils::RenameFile(const std::string srcFile, const std::string destFile)
113 {
114     return rename(srcFile.c_str(), destFile.c_str());
115 }
116 } // namespace NativeRdb
117 } // namespace OHOS
118