• 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     /**
44     * @brief Binds a text buffer to a parameter in a prepared SQL statement
45     *
46     * This function binds a text data buffer to a parameter placeholder in a SQL prepared statement.
47     * Typically used to replace "?" placeholders in SQL statements with actual values before execution.
48     *
49     * @param stmt      Pointer to the prepared statement object. Must be properly initialized.
50     * @param idx       1-based parameter index (SQL parameter numbering starts at 1).
51     * @param val       Pointer to the data buffer to bind. Can be text.
52     * @param len       The actual length of the val cannot be greater than the actual length of the val.
53     * @param freeFunc  Memory disposal callback
54     * @return int      Operation status code:
55     *                  - 0: Success
56     *                  - Non-zero: Error code (see implementation-specific definitions)
57     * @note The prepared statement must be in a state that accepts parameter binding.
58     */
59     static int RdSqlBindText(GRD_SqlStmt *stmt, uint32_t idx, const void *val, int32_t len, void (*freeFunc)(void *));
60     static int RdSqlBindInt(GRD_SqlStmt *stmt, uint32_t idx, int32_t val);
61     static int RdSqlBindInt64(GRD_SqlStmt *stmt, uint32_t idx, int64_t val);
62     static int RdSqlBindDouble(GRD_SqlStmt *stmt, uint32_t idx, double val);
63     static int RdSqlBindNull(GRD_SqlStmt *stmt, uint32_t idx);
64     static int RdSqlBindFloatVector(
65         GRD_SqlStmt *stmt, uint32_t idx, float *val, uint32_t dim, void (*freeFunc)(void *));
66 
67     static int RdSqlStep(GRD_SqlStmt *stmt);
68     static int RdSqlColCnt(GRD_SqlStmt *stmt);
69     static ColumnType RdSqlColType(GRD_SqlStmt *stmt, uint32_t idx);
70     static int RdSqlColBytes(GRD_SqlStmt *stmt, uint32_t idx);
71     static char *RdSqlColName(GRD_SqlStmt *stmt, uint32_t idx);
72     static GRD_DbValueT RdSqlColValue(GRD_SqlStmt *stmt, uint32_t idx);
73 
74     static const void *RdSqlColBlob(GRD_SqlStmt *stmt, uint32_t idx);
75     static const char *RdSqlColText(GRD_SqlStmt *stmt, uint32_t idx);
76     static int32_t RdSqlColInt(GRD_SqlStmt *stmt, uint32_t idx);
77     static int64_t RdSqlColInt64(GRD_SqlStmt *stmt, uint32_t idx);
78     static double RdSqlColDouble(GRD_SqlStmt *stmt, uint32_t idx);
79     static const float *RdSqlColumnFloatVector(GRD_SqlStmt *stmt, uint32_t idx, uint32_t *dim);
80 
81     static void ClearAndZeroString(std::string &str);
82     static const char *GetEncryptKey(const std::vector<uint8_t> &encryptedKey, char outBuff[], size_t outBufSize);
83     static int RdDbBackup(GRD_DB *db, const char *backupDbFile, const std::vector<uint8_t> &encryptedKey);
84     static int RdDbRestore(const char *dbFile, const char *backupDbFile, const std::vector<uint8_t> &encryptedKey);
85     static int RdDbRekey(const char *dbFile, const char *configStr, const std::vector<uint8_t> &encryptedKey);
86 
87     static int RdDbGetVersion(GRD_DB *db, GRD_ConfigTypeE type, int &version);
88     static int RdDbSetVersion(GRD_DB *db, GRD_ConfigTypeE type, int version);
89 
90     static int RdSqlRegistryThreadPool(GRD_DB *db);
91     static int RdSqlRegistryClusterAlgo(GRD_DB *db, const char *clstAlgoName, GRD_ClusterAlgoFunc func);
92 private:
93     static GRD_DbThreadPoolT threadPool_;
94 };
95 
96 } // namespace NativeRdb
97 } // namespace OHOS
98 #endif // RD_UTILS_H