• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DbRegistryThreadPool");
66     GRD_DBApiInfo.DBSqlRegistryClusterAlgo = (DBSqlRegistryClusterAlgo)dlsym(g_library, "GRD_SqlRegistryClusterAlgo");
67 #endif
68 }
69 
IsUsingArkData()70 bool IsUsingArkData()
71 {
72 #ifndef _WIN32
73     if (g_library == nullptr) {
74         GetApiInfoInstance();
75     }
76     return g_library != nullptr;
77 #else
78     return false;
79 #endif
80 }
81 
GetApiInfoInstance()82 GRD_APIInfo GetApiInfoInstance()
83 {
84     GRD_APIInfo GRD_TempApiStruct;
85 #ifndef _WIN32
86     g_library = dlopen("libarkdata_db_core.z.so", RTLD_LAZY);
87     if (g_library != nullptr) {
88         GRD_DBApiInitEnhance(GRD_TempApiStruct);
89     } else {
90         LOG_INFO("use default db kernel");
91     }
92 #endif
93     return GRD_TempApiStruct;
94 }
95 
96 } // namespace NativeRdb
97 } // namespace OHOS
98