1 /*
2 * Copyright (C) 2025 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 "database.h"
17 #include <thread>
18 #include "security_guard_log.h"
19 #include "security_guard_define.h"
20
21 namespace OHOS::Security::SecurityGuard {
22 namespace {
23 constexpr int32_t MAX_TIMES = 5;
24 constexpr int32_t SLEEP_INTERVAL = 500;
25 }
26
CreateRdbStore(const std::string & dbName,const std::string & dbPath,int version,const std::vector<std::string> & createSqls,int & errCode)27 void Database::CreateRdbStore(const std::string &dbName, const std::string &dbPath, int version,
28 const std::vector<std::string> &createSqls, int &errCode)
29 {
30 SGLOGI("EventStore::CreateRdbStore");
31 store_ = std::make_shared<SgSqliteHelper>(dbName, dbPath, version, createSqls);
32 errCode = 0;
33 }
34
Insert(int64_t & outRowId,const std::string & table,const GenericValues & value)35 int Database::Insert(int64_t &outRowId, const std::string &table, const GenericValues &value)
36 {
37 int ret = IsExistStore();
38 if (ret == SUCCESS) {
39 ret = store_->Insert(outRowId, table, value);
40 }
41 return ret;
42 }
43
44 // LCOV_EXCL_START
BatchInsert(int64_t & outInsertNum,const std::string & table,const std::vector<GenericValues> & initialBatchValues)45 int Database::BatchInsert(int64_t &outInsertNum, const std::string &table,
46 const std::vector<GenericValues> &initialBatchValues)
47 {
48 int ret = IsExistStore();
49 if (ret == SUCCESS) {
50 ret = store_->BatchInsert(outInsertNum, table, initialBatchValues);
51 }
52 return ret;
53 }
54
Update(int & changedRows,const std::string & table,const GenericValues & value)55 int Database::Update(int &changedRows, const std::string &table, const GenericValues &value)
56 {
57 int ret = IsExistStore();
58 if (ret == SUCCESS) {
59 ret = store_->Update(changedRows, table, value);
60 }
61 return ret;
62 }
63 // LCOV_EXCL_STOP
64
Delete(int & deletedRows,const std::string & table,const GenericValues & conditions)65 int Database::Delete(int &deletedRows, const std::string &table, const GenericValues &conditions)
66 {
67 int ret = IsExistStore();
68 if (ret == SUCCESS) {
69 ret = store_->Delete(deletedRows, table, conditions);
70 }
71 return ret;
72 }
73
Query(const std::string & table,const GenericValues & conditions,std::vector<GenericValues> & results,const QueryOptions & options)74 int Database::Query(const std::string &table, const GenericValues &conditions,
75 std::vector<GenericValues> &results, const QueryOptions &options)
76 {
77 int ret = IsExistStore();
78 if (ret == SUCCESS) {
79 return store_->Query(table, conditions, results, options);
80 }
81 return ret;
82 }
83
84 // LCOV_EXCL_START
ExecuteSql(const std::string & sql)85 int Database::ExecuteSql(const std::string &sql)
86 {
87 int ret = IsExistStore();
88 if (ret == SUCCESS) {
89 ret = store_->ExecuteSql(sql);
90 }
91 return ret;
92 }
93
ExecuteAndGetLong(int64_t & outValue,const std::string & sql,const std::vector<std::string> & bindArgs)94 int Database::ExecuteAndGetLong(int64_t &outValue, const std::string &sql, const std::vector<std::string> &bindArgs)
95 {
96 int ret = IsExistStore();
97 if (ret == SUCCESS) {
98 ret = store_->ExecuteAndGetLong(outValue, sql, bindArgs);
99 }
100 return ret;
101 }
102 // LCOV_EXCL_STOP
103
Count(int64_t & outValue,const std::string & table,const GenericValues & conditions,const QueryOptions & options)104 int Database::Count(int64_t &outValue, const std::string &table, const GenericValues &conditions,
105 const QueryOptions &options)
106 {
107 int ret = IsExistStore();
108 if (ret == SUCCESS) {
109 ret = store_->Count(outValue, table, conditions, options);
110 }
111 return ret;
112 }
113
114 // LCOV_EXCL_START
BeginTransaction()115 int Database::BeginTransaction()
116 {
117 int ret = IsExistStore();
118 if (ret == SUCCESS) {
119 ret = store_->BeginTransaction();
120 }
121 return ret;
122 }
123
RollBack()124 int Database::RollBack()
125 {
126 int ret = IsExistStore();
127 if (ret == SUCCESS) {
128 ret = store_->RollbackTransaction();
129 }
130 return ret;
131 }
132
Commit()133 int Database::Commit()
134 {
135 int ret = IsExistStore();
136 if (ret == SUCCESS) {
137 ret = store_->CommitTransaction();
138 }
139 return ret;
140 }
141
Attach(const std::string & alias,const std::string & pathName,const std::vector<uint8_t> destEncryptKey)142 int Database::Attach(const std::string &alias, const std::string &pathName,
143 const std::vector<uint8_t> destEncryptKey)
144 {
145 int ret = IsExistStore();
146 if (ret == SUCCESS) {
147 ret = store_->Attach(alias, pathName, destEncryptKey);
148 }
149 return ret;
150 }
151 // LCOV_EXCL_STOP
152
IsExistStore()153 int Database::IsExistStore()
154 {
155 if (store_ != nullptr) {
156 return SUCCESS;
157 }
158 int32_t tryTimes = MAX_TIMES;
159 while (tryTimes > 0) {
160 if (store_ != nullptr) {
161 return SUCCESS;
162 }
163
164 SGLOGW("tryTimes = %{public}d", tryTimes);
165 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_INTERVAL));
166 tryTimes--;
167 }
168 if (store_ == nullptr) {
169 SGLOGE("EventStore::IsExistStore NativeRdb::RdbStore is null!");
170 return FAILED;
171 }
172 return SUCCESS;
173 }
174 }