1 /*
2 * Copyright (c) 2021-2022 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_helper.h"
17
18 #include "accesstoken_log.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SqliteHelper"};
25 }
26
SqliteHelper(const std::string & dbName,const std::string & dbPath,int32_t version)27 SqliteHelper::SqliteHelper(const std::string& dbName, const std::string& dbPath, int32_t version)
28 : dbName_(dbName), dbPath_(dbPath), currentVersion_(version), db_(nullptr)
29 {}
30
~SqliteHelper()31 SqliteHelper::~SqliteHelper()
32 {}
33
Open()34 void SqliteHelper::Open()
35 {
36 if (db_ != nullptr) {
37 return;
38 }
39 if (dbName_.empty() || dbPath_.empty() || currentVersion_ < 0) {
40 return;
41 }
42 std::string fileName = dbPath_ + dbName_;
43 int32_t res = sqlite3_open(fileName.c_str(), &db_);
44 if (res != SQLITE_OK) {
45 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to open db: %{public}s", sqlite3_errmsg(db_));
46 return;
47 }
48
49 int32_t version = GetVersion();
50 if (version == currentVersion_) {
51 return;
52 }
53
54 BeginTransaction();
55 if (version == 0) {
56 OnCreate();
57 } else {
58 if (version < currentVersion_) {
59 OnUpdate();
60 }
61 }
62 SetVersion();
63 CommitTransaction();
64 }
65
Close()66 void SqliteHelper::Close()
67 {
68 if (db_ == nullptr) {
69 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
70 return;
71 }
72 int32_t ret = sqlite3_close(db_);
73 if (ret != SQLITE_OK) {
74 ACCESSTOKEN_LOG_WARN(LABEL, "sqlite3_close error, ret=%{public}d", ret);
75 return;
76 }
77 db_ = nullptr;
78 }
79
BeginTransaction() const80 int32_t SqliteHelper::BeginTransaction() const
81 {
82 if (db_ == nullptr) {
83 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
84 return GENERAL_ERROR;
85 }
86 char* errorMessage = nullptr;
87 int32_t result = 0;
88 int32_t ret = sqlite3_exec(db_, "BEGIN;", nullptr, nullptr, &errorMessage);
89 if (ret != SQLITE_OK) {
90 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
91 result = GENERAL_ERROR;
92 }
93 sqlite3_free(errorMessage);
94 return result;
95 }
96
CommitTransaction() const97 int32_t SqliteHelper::CommitTransaction() const
98 {
99 if (db_ == nullptr) {
100 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
101 return GENERAL_ERROR;
102 }
103 char* errorMessage = nullptr;
104 int32_t result = 0;
105 int32_t ret = sqlite3_exec(db_, "COMMIT;", nullptr, nullptr, &errorMessage);
106 if (ret != SQLITE_OK) {
107 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
108 result = GENERAL_ERROR;
109 }
110 sqlite3_free(errorMessage);
111 return result;
112 }
113
RollbackTransaction() const114 int32_t SqliteHelper::RollbackTransaction() const
115 {
116 if (db_ == nullptr) {
117 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
118 return GENERAL_ERROR;
119 }
120 int32_t result = 0;
121 char* errorMessage = nullptr;
122 int32_t ret = sqlite3_exec(db_, "ROLLBACK;", nullptr, nullptr, &errorMessage);
123 if (ret != SQLITE_OK) {
124 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
125 result = GENERAL_ERROR;
126 }
127 sqlite3_free(errorMessage);
128 return result;
129 }
130
Prepare(const std::string & sql) const131 Statement SqliteHelper::Prepare(const std::string& sql) const
132 {
133 return Statement(db_, sql);
134 }
135
ExecuteSql(const std::string & sql) const136 int32_t SqliteHelper::ExecuteSql(const std::string& sql) const
137 {
138 if (db_ == nullptr) {
139 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
140 return GENERAL_ERROR;
141 }
142 char* errorMessage = nullptr;
143 int32_t result = 0;
144 int32_t res = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errorMessage);
145 if (res != SQLITE_OK) {
146 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
147 result = GENERAL_ERROR;
148 }
149 sqlite3_free(errorMessage);
150 return result;
151 }
152
GetVersion() const153 int32_t SqliteHelper::GetVersion() const
154 {
155 if (db_ == nullptr) {
156 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
157 return GENERAL_ERROR;
158 }
159 auto statement = Prepare(PRAGMA_VERSION_COMMAND);
160 int32_t version = 0;
161 while (statement.Step() == Statement::State::ROW) {
162 version = statement.GetColumnInt(0);
163 }
164 ACCESSTOKEN_LOG_INFO(LABEL, "version: %{public}d", version);
165 return version;
166 }
167
SetVersion() const168 void SqliteHelper::SetVersion() const
169 {
170 if (db_ == nullptr) {
171 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
172 return;
173 }
174 auto statement = Prepare(PRAGMA_VERSION_COMMAND + " = " + std::to_string(currentVersion_));
175 statement.Step();
176 }
177
SpitError() const178 std::string SqliteHelper::SpitError() const
179 {
180 if (db_ == nullptr) {
181 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
182 return "";
183 }
184 return sqlite3_errmsg(db_);
185 }
186 } // namespace AccessToken
187 } // namespace Security
188 } // namespace OHOS
189