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
16 #include "wrapper.h"
17
18 #include <cstdint>
19
20 #include "base/request/request/common/include/log.h"
21 #include "cxx.h"
22 #include "wrapper.rs.h"
23 namespace OHOS::Request {
24
OpenCallback(rust::Box<OpenCallbackWrapper> && inner)25 OpenCallback::OpenCallback(rust::Box<OpenCallbackWrapper> &&inner) : inner_(std::move(inner))
26 {
27 }
OnCreate(RdbStore & store)28 int OpenCallback::OnCreate(RdbStore &store)
29 {
30 return inner_->on_create(store);
31 }
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)32 int OpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
33 {
34 return inner_->on_upgrade(store, oldVersion, newVersion);
35 }
OnDowngrade(RdbStore & store,int oldVersion,int newVersion)36 int OpenCallback::OnDowngrade(RdbStore &store, int oldVersion, int newVersion)
37 {
38 return inner_->on_downgrade(store, oldVersion, newVersion);
39 }
OnOpen(RdbStore & store)40 int OpenCallback::OnOpen(RdbStore &store)
41 {
42 return inner_->on_open(store);
43 }
onCorruption(std::string databaseFile)44 int OpenCallback::onCorruption(std::string databaseFile)
45 {
46 return inner_->on_corrupt(databaseFile);
47 }
48
GetString(RowEntity & rowEntity,int index,rust::string & value)49 int GetString(RowEntity &rowEntity, int index, rust::string &value)
50 {
51 std::string val;
52 int ret = rowEntity.Get(index).GetString(val);
53 value = val;
54 return ret;
55 }
56
GetBlob(RowEntity & rowEntity,int index,rust::vec<uint8_t> & value)57 int GetBlob(RowEntity &rowEntity, int index, rust::vec<uint8_t> &value)
58 {
59 std::vector<uint8_t> val;
60 int ret = rowEntity.Get(index).GetBlob(val);
61 for (auto &v : val) {
62 value.push_back(v);
63 }
64 return ret;
65 }
66
GetRdbStore(const RdbStoreConfig & config,int version,rust::Box<OpenCallbackWrapper> openCallbackWrapper,int & errCode)67 std::shared_ptr<RdbStore> GetRdbStore(
68 const RdbStoreConfig &config, int version, rust::Box<OpenCallbackWrapper> openCallbackWrapper, int &errCode)
69 {
70 OpenCallback callback(std::move(openCallbackWrapper));
71 return RdbHelper::GetRdbStore(config, version, callback, errCode);
72 }
73
74 } // namespace OHOS::Request