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 <sys/types.h>
19 #include "account_log_wrapper.h"
20 #include "sqlite3ext.h"
21
22 namespace OHOS {
23 namespace AccountSA {
SqliteHelper(const std::string & dbName,const std::string & dbPath,int32_t version)24 SqliteHelper::SqliteHelper(const std::string& dbName, const std::string& dbPath, int32_t version)
25 : dbName_(dbName), dbPath_(dbPath), currentVersion_(version), db_(nullptr)
26 {}
27
~SqliteHelper()28 SqliteHelper::~SqliteHelper()
29 {}
30
Open()31 void SqliteHelper::Open() __attribute__((no_sanitize("cfi")))
32 {
33 if (db_ != nullptr) {
34 ACCOUNT_LOGW("Db s already open");
35 return;
36 }
37 if (dbName_.empty() || dbPath_.empty() || currentVersion_ < 0) {
38 ACCOUNT_LOGE("Param invalid, dbName: %{public}s, dbPath: %{public}s, currentVersion: %{public}d",
39 dbName_.c_str(), dbPath_.c_str(), currentVersion_);
40 return;
41 }
42 // set soft heap limit as 10KB
43 const int32_t heapLimit = 10 * 1024;
44 sqlite3_soft_heap_limit64(heapLimit);
45 std::string fileName = dbPath_ + dbName_;
46 int32_t res = sqlite3_open(fileName.c_str(), &db_);
47 if (res != SQLITE_OK) {
48 ACCOUNT_LOGE("Failed to open db: %{public}s", sqlite3_errmsg(db_));
49 return;
50 }
51
52 int32_t version = GetVersion();
53 if (version == currentVersion_) {
54 return;
55 }
56
57 BeginTransaction();
58 if (version == DataBaseVersion::VERISION_0) {
59 OnCreate();
60 } else {
61 if (version < currentVersion_) {
62 OnUpdate(version);
63 }
64 }
65 SetVersion();
66 CommitTransaction();
67 }
68
Close()69 void SqliteHelper::Close()
70 {
71 if (db_ == nullptr) {
72 ACCOUNT_LOGW("Do open data base first!");
73 return;
74 }
75 int32_t ret = sqlite3_close(db_);
76 if (ret != SQLITE_OK) {
77 ACCOUNT_LOGW("Sqlite3_close error, ret=%{public}d", ret);
78 return;
79 }
80 db_ = nullptr;
81 ACCOUNT_LOGI("Close sqlite database.");
82 }
83
BeginTransaction() const84 int32_t SqliteHelper::BeginTransaction() const
85 {
86 if (db_ == nullptr) {
87 ACCOUNT_LOGW("Do open data base first!");
88 return GENERAL_ERROR;
89 }
90 char* errorMessage = nullptr;
91 int32_t result = 0;
92 int32_t ret = sqlite3_exec(db_, "BEGIN;", nullptr, nullptr, &errorMessage);
93 if (ret != SQLITE_OK) {
94 ACCOUNT_LOGE("Failed, errorMsg: %{public}s", errorMessage);
95 result = GENERAL_ERROR;
96 }
97 sqlite3_free(errorMessage);
98 return result;
99 }
100
CommitTransaction() const101 int32_t SqliteHelper::CommitTransaction() const
102 {
103 if (db_ == nullptr) {
104 ACCOUNT_LOGW("Do open data base first!");
105 return GENERAL_ERROR;
106 }
107 char* errorMessage = nullptr;
108 int32_t result = 0;
109 int32_t ret = sqlite3_exec(db_, "COMMIT;", nullptr, nullptr, &errorMessage);
110 if (ret != SQLITE_OK) {
111 ACCOUNT_LOGE("Failed, errorMsg: %{public}s", errorMessage);
112 result = GENERAL_ERROR;
113 }
114 sqlite3_free(errorMessage);
115 sqlite3_db_cacheflush(db_);
116 return result;
117 }
118
RollbackTransaction() const119 int32_t SqliteHelper::RollbackTransaction() const
120 {
121 if (db_ == nullptr) {
122 ACCOUNT_LOGW("Do open data base first!");
123 return GENERAL_ERROR;
124 }
125 int32_t result = 0;
126 char* errorMessage = nullptr;
127 int32_t ret = sqlite3_exec(db_, "ROLLBACK;", nullptr, nullptr, &errorMessage);
128 if (ret != SQLITE_OK) {
129 ACCOUNT_LOGE("Failed, errorMsg: %{public}s", errorMessage);
130 result = GENERAL_ERROR;
131 }
132 sqlite3_free(errorMessage);
133 return result;
134 }
135
Prepare(const std::string & sql) const136 Statement SqliteHelper::Prepare(const std::string& sql) const
137 {
138 return Statement(db_, sql);
139 }
140
ExecuteSql(const std::string & sql) const141 int32_t SqliteHelper::ExecuteSql(const std::string& sql) const
142 {
143 if (db_ == nullptr) {
144 ACCOUNT_LOGW("Do open data base first!");
145 return GENERAL_ERROR;
146 }
147 char* errorMessage = nullptr;
148 int32_t result = 0;
149 int32_t res = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errorMessage);
150 if (res != SQLITE_OK) {
151 ACCOUNT_LOGE("Failed, errorMsg: %{public}s", errorMessage);
152 result = GENERAL_ERROR;
153 }
154 sqlite3_free(errorMessage);
155 return result;
156 }
157
GetVersion() const158 int32_t SqliteHelper::GetVersion() const __attribute__((no_sanitize("cfi")))
159 {
160 if (db_ == nullptr) {
161 ACCOUNT_LOGW("Do open data base first!");
162 return GENERAL_ERROR;
163 }
164 auto statement = Prepare(PRAGMA_VERSION_COMMAND);
165 int32_t version = 0;
166 while (statement.Step() == Statement::State::ROW) {
167 version = statement.GetColumnInt(0);
168 }
169 ACCOUNT_LOGI("Version: %{public}d", version);
170 return version;
171 }
172
SetVersion() const173 void SqliteHelper::SetVersion() const
174 {
175 if (db_ == nullptr) {
176 ACCOUNT_LOGW("Do open data base first!");
177 return;
178 }
179 auto statement = Prepare(PRAGMA_VERSION_COMMAND + " = " + std::to_string(currentVersion_));
180 statement.Step();
181 }
182
SpitError() const183 std::string SqliteHelper::SpitError() const
184 {
185 if (db_ == nullptr) {
186 ACCOUNT_LOGW("Do open data base first!");
187 return "";
188 }
189 return sqlite3_errmsg(db_);
190 }
191
CheckReady() const192 bool SqliteHelper::CheckReady() const
193 {
194 return db_ != nullptr;
195 }
196 } // namespace AccountSA
197 } // namespace OHOS
198