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 };
Create(const RdbStoreConfig & config,bool isWriter)25 std::pair<int, std::shared_ptr<Connection>> Connection::Create(const RdbStoreConfig &config, bool isWriter)
26 {
27     auto dbType = config.GetDBType();
28     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
29         return { E_INVALID_ARGS, nullptr };
30     }
31 
32     auto creator = g_creators[dbType];
33     if (creator == nullptr) {
34         return { E_NOT_SUPPORT, nullptr };
35     }
36 
37     return creator(config, isWriter);
38 }
39 
Repair(const RdbStoreConfig & config)40 int32_t Connection::Repair(const RdbStoreConfig &config)
41 {
42     auto dbType = config.GetDBType();
43     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
44         return E_INVALID_ARGS;
45     }
46 
47     auto repairer = g_repairers[dbType];
48     if (repairer == nullptr) {
49         return E_NOT_SUPPORT;
50     }
51 
52     return repairer(config);
53 }
54 
Delete(const RdbStoreConfig & config)55 int32_t Connection::Delete(const RdbStoreConfig &config)
56 {
57     auto dbType = config.GetDBType();
58     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
59         return E_INVALID_ARGS;
60     }
61     auto deleter = g_fileDeleter[dbType];
62     if (deleter == nullptr) {
63         return E_NOT_SUPPORT;
64     }
65 
66     return deleter(config);
67 }
68 
Collect(const RdbStoreConfig & config)69 std::map<std::string, Connection::Info> Connection::Collect(const RdbStoreConfig &config)
70 {
71     auto dbType = config.GetDBType();
72     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
73         return {};
74     }
75 
76     auto collector = g_collectors[dbType];
77     if (collector == nullptr) {
78         return {};
79     }
80 
81     return collector(config);
82 }
83 
RegisterCreator(int32_t dbType,Creator creator)84 int32_t Connection::RegisterCreator(int32_t dbType, Creator creator)
85 {
86     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
87         return E_INVALID_ARGS;
88     }
89 
90     if (g_creators[dbType] != nullptr) {
91         return E_OK;
92     }
93 
94     g_creators[dbType] = creator;
95     return E_OK;
96 }
97 
RegisterRepairer(int32_t dbType,Repairer repairer)98 int32_t Connection::RegisterRepairer(int32_t dbType, Repairer repairer)
99 {
100     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
101         return E_INVALID_ARGS;
102     }
103 
104     if (g_repairers[dbType] != nullptr) {
105         return E_OK;
106     }
107 
108     g_repairers[dbType] = repairer;
109     return E_OK;
110 }
111 
RegisterDeleter(int32_t dbType,Deleter deleter)112 int32_t Connection::RegisterDeleter(int32_t dbType, Deleter deleter)
113 {
114     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
115         return E_INVALID_ARGS;
116     }
117 
118     if (g_fileDeleter[dbType] != nullptr) {
119         return E_OK;
120     }
121 
122     g_fileDeleter[dbType] = deleter;
123     return E_OK;
124 }
125 
RegisterCollector(int32_t dbType,Collector collector)126 int32_t Connection::RegisterCollector(int32_t dbType, Collector collector)
127 {
128     if (dbType < static_cast<int32_t>(DB_SQLITE) || dbType >= static_cast<int32_t>(DB_BUTT)) {
129         return E_INVALID_ARGS;
130     }
131 
132     if (g_collectors[dbType] != nullptr) {
133         return E_OK;
134     }
135 
136     g_collectors[dbType] = collector;
137     return E_OK;
138 }
139 
SetId(int id)140 int Connection::SetId(int id)
141 {
142     id_ = id;
143     return id_;
144 }
145 
GetId() const146 int Connection::GetId() const
147 {
148     return id_;
149 }
150 } // namespace OHOS::NativeRdb
151