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::Restorer g_restorer[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
Restore(const RdbStoreConfig & config,const std::string & srcPath,const std::string & destPath)85 int32_t Connection::Restore(const RdbStoreConfig &config, const std::string &srcPath, const std::string &destPath)
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
92 auto restorer = g_restorer[dbType];
93 if (restorer == nullptr) {
94 return E_NOT_SUPPORT;
95 }
96
97 return restorer(config, srcPath, destPath);
98 }
99
RegisterCreator(int32_t dbType,Creator creator)100 int32_t Connection::RegisterCreator(int32_t dbType, Creator creator)
101 {
102 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
103 return E_INVALID_ARGS;
104 }
105
106 if (g_creators[dbType] != nullptr) {
107 return E_OK;
108 }
109
110 g_creators[dbType] = creator;
111 return E_OK;
112 }
113
RegisterRepairer(int32_t dbType,Repairer repairer)114 int32_t Connection::RegisterRepairer(int32_t dbType, Repairer repairer)
115 {
116 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
117 return E_INVALID_ARGS;
118 }
119
120 if (g_repairers[dbType] != nullptr) {
121 return E_OK;
122 }
123
124 g_repairers[dbType] = repairer;
125 return E_OK;
126 }
127
RegisterDeleter(int32_t dbType,Deleter deleter)128 int32_t Connection::RegisterDeleter(int32_t dbType, Deleter deleter)
129 {
130 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
131 return E_INVALID_ARGS;
132 }
133
134 if (g_fileDeleter[dbType] != nullptr) {
135 return E_OK;
136 }
137
138 g_fileDeleter[dbType] = deleter;
139 return E_OK;
140 }
141
RegisterCollector(int32_t dbType,Collector collector)142 int32_t Connection::RegisterCollector(int32_t dbType, Collector collector)
143 {
144 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
145 return E_INVALID_ARGS;
146 }
147
148 if (g_collectors[dbType] != nullptr) {
149 return E_OK;
150 }
151
152 g_collectors[dbType] = collector;
153 return E_OK;
154 }
155
RegisterRestorer(int32_t dbType,Restorer restorer)156 int32_t Connection::RegisterRestorer(int32_t dbType, Restorer restorer)
157 {
158 if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
159 return E_INVALID_ARGS;
160 }
161
162 if (g_restorer[dbType] != nullptr) {
163 return E_OK;
164 }
165
166 g_restorer[dbType] = restorer;
167 return E_OK;
168 }
169
SetId(int id)170 int Connection::SetId(int id)
171 {
172 id_ = id;
173 return id_;
174 }
175
GetId() const176 int Connection::GetId() const
177 {
178 return id_;
179 }
180
SetIsRecyclable(bool isRecyclable)181 void Connection::SetIsRecyclable(bool isRecyclable)
182 {
183 isRecyclable_ = isRecyclable;
184 }
185
IsRecyclable() const186 bool Connection::IsRecyclable() const
187 {
188 return isRecyclable_;
189 }
190 } // namespace OHOS::NativeRdb
191