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 #include "connection.h"
16
17 #include "rdb_common.h"
18 #include "rdb_errno.h"
19 #include "rdb_store_config.h"
20 namespace OHOS::NativeRdb {
21 static Connection::Creator g_creators[DB_BUTT] = { nullptr, nullptr };
22 static Connection::Repairer g_repairers[DB_BUTT] = { nullptr, nullptr };
23 static Connection::Deleter g_fileDeleter[DB_BUTT] = { nullptr, nullptr };
24 static Connection::Collector g_collectors[DB_BUTT] = { nullptr, nullptr };
25 static Connection::ReplicaChecker g_replicaCheckers[DB_BUTT] = { nullptr, nullptr };
Create(const RdbStoreConfig & config,bool isWriter)26 std::pair<int, std::shared_ptr<Connection>> Connection::Create(const RdbStoreConfig &config, bool isWriter)
27 {
28 auto dbType = config.GetDBType();
29 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
30 return { E_INVALID_ARGS, nullptr };
31 }
32
33 auto creator = g_creators[dbType];
34 if (creator == nullptr) {
35 return { E_NOT_SUPPORT, nullptr };
36 }
37
38 return creator(config, isWriter);
39 }
40
Repair(const RdbStoreConfig & config)41 int32_t Connection::Repair(const RdbStoreConfig &config)
42 {
43 auto dbType = config.GetDBType();
44 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
45 return E_INVALID_ARGS;
46 }
47
48 auto repairer = g_repairers[dbType];
49 if (repairer == nullptr) {
50 return E_NOT_SUPPORT;
51 }
52
53 return repairer(config);
54 }
55
Delete(const RdbStoreConfig & config)56 int32_t Connection::Delete(const RdbStoreConfig &config)
57 {
58 auto dbType = config.GetDBType();
59 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
60 return E_INVALID_ARGS;
61 }
62 auto deleter = g_fileDeleter[dbType];
63 if (deleter == nullptr) {
64 return E_NOT_SUPPORT;
65 }
66
67 return deleter(config);
68 }
69
Collect(const RdbStoreConfig & config)70 std::map<std::string, Connection::Info> Connection::Collect(const RdbStoreConfig &config)
71 {
72 auto dbType = config.GetDBType();
73 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
74 return {};
75 }
76
77 auto collector = g_collectors[dbType];
78 if (collector == nullptr) {
79 return {};
80 }
81
82 return collector(config);
83 }
84
CheckReplicaIntegrity(const RdbStoreConfig & config)85 int32_t Connection::CheckReplicaIntegrity(const RdbStoreConfig &config)
86 {
87 auto dbType = config.GetDBType();
88 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
89 return E_INVALID_ARGS;
90 }
91 auto replicaChecker = g_replicaCheckers[dbType];
92 if (replicaChecker == nullptr) {
93 return E_NOT_SUPPORT;
94 }
95
96 return replicaChecker(config);
97 }
98
RegisterCreator(int32_t dbType,Creator creator)99 int32_t Connection::RegisterCreator(int32_t dbType, Creator creator)
100 {
101 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
102 return E_INVALID_ARGS;
103 }
104
105 if (g_creators[dbType] != nullptr) {
106 return E_OK;
107 }
108
109 g_creators[dbType] = creator;
110 return E_OK;
111 }
112
RegisterRepairer(int32_t dbType,Repairer repairer)113 int32_t Connection::RegisterRepairer(int32_t dbType, Repairer repairer)
114 {
115 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
116 return E_INVALID_ARGS;
117 }
118
119 if (g_repairers[dbType] != nullptr) {
120 return E_OK;
121 }
122
123 g_repairers[dbType] = repairer;
124 return E_OK;
125 }
126
RegisterDeleter(int32_t dbType,Deleter deleter)127 int32_t Connection::RegisterDeleter(int32_t dbType, Deleter deleter)
128 {
129 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
130 return E_INVALID_ARGS;
131 }
132
133 if (g_fileDeleter[dbType] != nullptr) {
134 return E_OK;
135 }
136
137 g_fileDeleter[dbType] = deleter;
138 return E_OK;
139 }
140
RegisterCollector(int32_t dbType,Collector collector)141 int32_t Connection::RegisterCollector(int32_t dbType, Collector collector)
142 {
143 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
144 return E_INVALID_ARGS;
145 }
146
147 if (g_collectors[dbType] != nullptr) {
148 return E_OK;
149 }
150
151 g_collectors[dbType] = collector;
152 return E_OK;
153 }
154
RegisterReplicaChecker(int32_t dbType,ReplicaChecker replicaChecker)155 int32_t Connection::RegisterReplicaChecker(int32_t dbType, ReplicaChecker replicaChecker)
156 {
157 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
158 return E_INVALID_ARGS;
159 }
160
161 if (g_replicaCheckers[dbType] != nullptr) {
162 return E_OK;
163 }
164
165 g_replicaCheckers[dbType] = replicaChecker;
166 return E_OK;
167 }
168
SetId(int id)169 int Connection::SetId(int id)
170 {
171 id_ = id;
172 return id_;
173 }
174
GetId() const175 int Connection::GetId() const
176 {
177 return id_;
178 }
179
SetIsRecyclable(bool isRecyclable)180 void Connection::SetIsRecyclable(bool isRecyclable)
181 {
182 isRecyclable_ = isRecyclable;
183 }
184
IsRecyclable() const185 bool Connection::IsRecyclable() const
186 {
187 return isRecyclable_;
188 }
189 } // namespace OHOS::NativeRdb
190