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