1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "GRD_API_MANAGER"
16 #include "grd_api_manager.h"
17
18 #include "logger.h"
19
20 #ifndef _WIN32
21 #include <dlfcn.h>
22 #endif
23
24 #ifndef _WIN32
25 static void *g_library = nullptr;
26 #endif
27
28 namespace OHOS {
29 namespace NativeRdb {
30 using namespace OHOS::Rdb;
31
GRD_DBApiInitEnhance(GRD_APIInfo & GRD_DBApiInfo)32 void GRD_DBApiInitEnhance(GRD_APIInfo &GRD_DBApiInfo)
33 {
34 #ifndef _WIN32
35 GRD_DBApiInfo.DBOpenApi = (DBOpen)dlsym(g_library, "GRD_DBOpen");
36 GRD_DBApiInfo.DBCloseApi = (DBClose)dlsym(g_library, "GRD_DBClose");
37 GRD_DBApiInfo.DBRepairApi = (DBRepair)dlsym(g_library, "GRD_DBRepair");
38 GRD_DBApiInfo.DBSqlPrepare = (DBSqlPrepare)dlsym(g_library, "GRD_SqlPrepare");
39 GRD_DBApiInfo.DBSqlReset = (DBSqlReset)dlsym(g_library, "GRD_SqlReset");
40 GRD_DBApiInfo.DBSqlFinalize = (DBSqlFinalize)dlsym(g_library, "GRD_SqlFinalize");
41 GRD_DBApiInfo.DBSqlBindBlob = (DBSqlBindBlob)dlsym(g_library, "GRD_SqlBindBlob");
42 GRD_DBApiInfo.DBSqlBindText = (DBSqlBindText)dlsym(g_library, "GRD_SqlBindText");
43 GRD_DBApiInfo.DBSqlBindInt = (DBSqlBindInt)dlsym(g_library, "GRD_SqlBindInt");
44 GRD_DBApiInfo.DBSqlBindInt64 = (DBSqlBindInt64)dlsym(g_library, "GRD_SqlBindInt64");
45 GRD_DBApiInfo.DBSqlBindDouble = (DBSqlBindDouble)dlsym(g_library, "GRD_SqlBindDouble");
46 GRD_DBApiInfo.DBSqlBindNull = (DBSqlBindNull)dlsym(g_library, "GRD_SqlBindNull");
47 GRD_DBApiInfo.DBSqlBindFloatVector = (DBSqlBindFloatVector)dlsym(g_library, "GRD_SqlBindFloatVector");
48 GRD_DBApiInfo.DBSqlStep = (DBSqlStep)dlsym(g_library, "GRD_SqlStep");
49 GRD_DBApiInfo.DBSqlColCnt = (DBSqlColCnt)dlsym(g_library, "GRD_SqlColumnCount");
50 GRD_DBApiInfo.DBSqlColType = (DBSqlColType)dlsym(g_library, "GRD_SqlColumnType");
51 GRD_DBApiInfo.DBSqlColBytes = (DBSqlColBytes)dlsym(g_library, "GRD_SqlColumnBytes");
52 GRD_DBApiInfo.DBSqlColName = (DBSqlColName)dlsym(g_library, "GRD_SqlColumnName");
53 GRD_DBApiInfo.DBSqlColValue = (DBSqlColValue)dlsym(g_library, "GRD_SqlColumnValue");
54 GRD_DBApiInfo.DBSqlColBlob = (DBSqlColBlob)dlsym(g_library, "GRD_SqlColumnBlob");
55 GRD_DBApiInfo.DBSqlColText = (DBSqlColText)dlsym(g_library, "GRD_SqlColumnText");
56 GRD_DBApiInfo.DBSqlColInt = (DBSqlColInt)dlsym(g_library, "GRD_SqlColumnInt");
57 GRD_DBApiInfo.DBSqlColInt64 = (DBSqlColInt64)dlsym(g_library, "GRD_SqlColumnInt64");
58 GRD_DBApiInfo.DBSqlColDouble = (DBSqlColDouble)dlsym(g_library, "GRD_SqlColumnDouble");
59 GRD_DBApiInfo.DBSqlColumnFloatVector = (DBSqlColumnFloatVector)dlsym(g_library, "GRD_SqlColumnFloatVector");
60 GRD_DBApiInfo.DBBackupApi = (DBBackup)dlsym(g_library, "GRD_DBBackup");
61 GRD_DBApiInfo.DBRestoreApi = (DBRestore)dlsym(g_library, "GRD_DBRestore");
62 GRD_DBApiInfo.DBReKeyApi = (DBReKey)dlsym(g_library, "GRD_DBRekey");
63 GRD_DBApiInfo.DBGetConfigApi = (DBGetConfig)dlsym(g_library, "GRD_GetConfig");
64 GRD_DBApiInfo.DBSetConfigApi = (DBSetConfig)dlsym(g_library, "GRD_SetConfig");
65 GRD_DBApiInfo.DBSqlRegistryThreadPool = (DBSqlRegistryThreadPool)dlsym(g_library, "GRD_SqlRegistryThreadPool");
66 #endif
67 }
68
IsUsingArkData()69 bool IsUsingArkData()
70 {
71 #ifndef _WIN32
72 if (g_library == nullptr) {
73 GetApiInfoInstance();
74 }
75 return g_library != nullptr;
76 #else
77 return false;
78 #endif
79 }
80
GetApiInfoInstance()81 GRD_APIInfo GetApiInfoInstance()
82 {
83 GRD_APIInfo GRD_TempApiStruct;
84 #ifndef _WIN32
85 g_library = dlopen("libarkdata_db_core.z.so", RTLD_LAZY);
86 if (g_library != nullptr) {
87 GRD_DBApiInitEnhance(GRD_TempApiStruct);
88 } else {
89 LOG_INFO("use default db kernel");
90 }
91 #endif
92 return GRD_TempApiStruct;
93 }
94
95 } // namespace NativeRdb
96 } // namespace OHOS
97