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