• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 RD_UTILS_H
17 #define RD_UTILS_H
18 
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include <vector>
23 
24 #include "grd_type_export.h"
25 #include "rdb_errno.h"
26 #include "remote_result_set.h"
27 
28 namespace OHOS {
29 namespace NativeRdb {
30 
31 class RdUtils {
32 public:
33     static int TransferGrdErrno(int err);
34     static ColumnType TransferGrdTypeToColType(int grdColType);
35     static int RdDbOpen(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db);
36     static int RdDbRepair(const char *dbPath, const char *configStr);
37     static int RdDbClose(GRD_DB *db, uint32_t flags);
38     static int RdSqlPrepare(GRD_DB *db, const char *str, uint32_t strLen, GRD_SqlStmt **stmt, const char **unusedStr);
39     static int RdSqlReset(GRD_SqlStmt *stmt);
40     static int RdSqlFinalize(GRD_SqlStmt *stmt);
41 
42     static int RdSqlBindBlob(GRD_SqlStmt *stmt, uint32_t idx, const void *val, int32_t len, void (*freeFunc)(void *));
43     static int RdSqlBindText(GRD_SqlStmt *stmt, uint32_t idx, const void *val, int32_t len, void (*freeFunc)(void *));
44     static int RdSqlBindInt(GRD_SqlStmt *stmt, uint32_t idx, int32_t val);
45     static int RdSqlBindInt64(GRD_SqlStmt *stmt, uint32_t idx, int64_t val);
46     static int RdSqlBindDouble(GRD_SqlStmt *stmt, uint32_t idx, double val);
47     static int RdSqlBindNull(GRD_SqlStmt *stmt, uint32_t idx);
48     static int RdSqlBindFloatVector(
49         GRD_SqlStmt *stmt, uint32_t idx, float *val, uint32_t dim, void (*freeFunc)(void *));
50 
51     static int RdSqlStep(GRD_SqlStmt *stmt);
52     static int RdSqlColCnt(GRD_SqlStmt *stmt);
53     static ColumnType RdSqlColType(GRD_SqlStmt *stmt, uint32_t idx);
54     static int RdSqlColBytes(GRD_SqlStmt *stmt, uint32_t idx);
55     static char *RdSqlColName(GRD_SqlStmt *stmt, uint32_t idx);
56     static GRD_DbValueT RdSqlColValue(GRD_SqlStmt *stmt, uint32_t idx);
57 
58     static const void *RdSqlColBlob(GRD_SqlStmt *stmt, uint32_t idx);
59     static const char *RdSqlColText(GRD_SqlStmt *stmt, uint32_t idx);
60     static int32_t RdSqlColInt(GRD_SqlStmt *stmt, uint32_t idx);
61     static int64_t RdSqlColInt64(GRD_SqlStmt *stmt, uint32_t idx);
62     static double RdSqlColDouble(GRD_SqlStmt *stmt, uint32_t idx);
63     static const float *RdSqlColumnFloatVector(GRD_SqlStmt *stmt, uint32_t idx, uint32_t *dim);
64 
65     static void ClearAndZeroString(std::string &str);
66     static const char *GetEncryptKey(const std::vector<uint8_t> &encryptedKey, char outBuff[], size_t outBufSize);
67     static int RdDbBackup(GRD_DB *db, const char *backupDbFile, const std::vector<uint8_t> &encryptedKey);
68     static int RdDbRestore(const char *dbFile, const char *backupDbFile, const std::vector<uint8_t> &encryptedKey);
69     static int RdDbRekey(const char *dbFile, const char *configStr, const std::vector<uint8_t> &encryptedKey);
70 
71     static int RdDbGetVersion(GRD_DB *db, GRD_ConfigTypeE type, int &version);
72     static int RdDbSetVersion(GRD_DB *db, GRD_ConfigTypeE type, int version);
73 
74     static int RdSqlRegistryThreadPool(GRD_DB *db);
75 private:
76     static GRD_ThreadPoolT threadPool_;
77 };
78 
79 } // namespace NativeRdb
80 } // namespace OHOS
81 #endif // RD_UTILS_H