• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SG_SQLITE_HELPER_H
17 #define SG_SQLITE_HELPER_H
18 
19 #include "sqlite_helper.h"
20 #include "generic_values.h"
21 #include <vector>
22 #include <memory>
23 #include "rwlock.h"
24 
25 namespace OHOS::Security::SecurityGuard {
26 class SgSqliteHelper : public SqliteHelper {
27 public:
28     explicit SgSqliteHelper(const std::string &dbName, const std::string &dbPath, int version,
29                         const std::vector<std::string> &createSqls);
30     int Insert(int64_t &outRowId, const std::string &table, const GenericValues &values);
31     int BatchInsert(int64_t &outInsertNum, const std::string &table,
32         const std::vector<GenericValues> &initialBatchValues);
33     int Update(int &changedRows, const std::string &table, const GenericValues &value);
34     int Delete(int &deletedRows, const std::string &table, const GenericValues &conditions = {});
35     int Query(const std::string &table, const GenericValues &conditions, std::vector<GenericValues> &results,
36         const QueryOptions &options = {});
37     int ExecuteSql(const std::string &sql);
38     int ExecuteAndGetLong(int64_t &outValue, const std::string &sql, const std::vector<std::string> &bindArgs);
39     int Count(int64_t &outValue, const std::string &table,  const GenericValues &conditions = {},
40          const QueryOptions &options = {});
41     int Attach(const std::string &alias, const std::string &pathName,
42         const std::vector<uint8_t> destEncryptKey);
43 
44     void OnCreate() override;
45     void OnUpdate() override;
46 
47 private:
48     bool EndWith(const std::string &str, const std::string &suffix) const;
49     GenericValues ExecuteRowData(Statement &stmt);
50     std::vector<std::string> Split(const std::string &s, char delimiter);
51     std::string Join(const std::vector<std::string> &items, const std::string &delimiter);
52 
53     std::string BuildInPlaceholders(const std::string& key, const std::string& values);
54     std::string BuildSelectSql(const std::string &table, const GenericValues &conditions,
55         const QueryOptions &options);
56     std::string BuildInsertSql(const std::string &table, const GenericValues &values);
57     std::string BuildUpdateSql(const std::string &table, const GenericValues &values,
58         const std::string &where);
59     std::string BuildWhereClause(const GenericValues &conditions);
60 
61     Statement PrepareCountStmt(const std::string &table, const GenericValues &conditions,
62         const QueryOptions &options);
63     Statement PrepareDeleteStmt(const std::string &table, const GenericValues &conditions);
64     Statement PrepareBoundStateStmt(const std::string &sql, const GenericValues &conditions);
65 
66     std::vector<std::string> createSqls_;
67     OHOS::Utils::RWLock rwLock_;
68 };
69 
70 } // OHOS::Security::SecurityGuard
71 #endif
72