• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef REQUEST_DATABASE_WRAPPER_H
16 
17 #define REQUEST_DATABASE_WRAPPER_H
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <memory>
22 #include <vector>
23 
24 #include "cxx.h"
25 #include "rdb_helper.h"
26 #include "rdb_open_callback.h"
27 #include "rdb_store.h"
28 #include "result_set.h"
29 #include "value_object.h"
30 namespace OHOS::Request {
31 using namespace OHOS::NativeRdb;
32 
33 struct OpenCallbackWrapper;
34 
BindI32(const int32_t value,std::vector<ValueObject> & args)35 inline void BindI32(const int32_t value, std::vector<ValueObject> &args)
36 {
37     args.push_back(ValueObject(value));
38 }
39 
BindI64(const int64_t value,std::vector<ValueObject> & args)40 inline void BindI64(const int64_t value, std::vector<ValueObject> &args)
41 {
42     args.push_back(ValueObject(value));
43 }
44 
BindDouble(const double value,std::vector<ValueObject> & args)45 inline void BindDouble(const double value, std::vector<ValueObject> &args)
46 {
47     args.push_back(ValueObject(value));
48 }
49 
BindBool(const bool value,std::vector<ValueObject> & args)50 inline void BindBool(const bool value, std::vector<ValueObject> &args)
51 {
52     args.push_back(ValueObject(value));
53 }
54 
BindString(const rust::str value,std::vector<ValueObject> & args)55 inline void BindString(const rust::str value, std::vector<ValueObject> &args)
56 {
57     args.push_back(ValueObject(std::string(value)));
58 }
59 
BindBlob(const rust::slice<const uint8_t> value,std::vector<ValueObject> & args)60 inline void BindBlob(const rust::slice<const uint8_t> value, std::vector<ValueObject> &args)
61 {
62     auto blob = std::vector<uint8_t>(value.begin(), value.end());
63     args.push_back(ValueObject(blob));
64 }
65 
BindNull(std::vector<ValueObject> & args)66 inline void BindNull(std::vector<ValueObject> &args)
67 {
68     args.push_back(ValueObject(std::monostate()));
69 }
70 
GetI32(RowEntity & rowEntity,int index,int32_t & value)71 inline int GetI32(RowEntity &rowEntity, int index, int32_t &value)
72 {
73     return rowEntity.Get(index).GetInt(value);
74 }
75 
GetI64(RowEntity & rowEntity,int index,int64_t & value)76 inline int GetI64(RowEntity &rowEntity, int index, int64_t &value)
77 {
78     return rowEntity.Get(index).GetLong(value);
79 }
80 
GetDouble(RowEntity & rowEntity,int index,double & value)81 inline int GetDouble(RowEntity &rowEntity, int index, double &value)
82 {
83     return rowEntity.Get(index).GetDouble(value);
84 }
85 
GetBool(RowEntity & rowEntity,int index,bool & value)86 inline int GetBool(RowEntity &rowEntity, int index, bool &value)
87 {
88     return rowEntity.Get(index).GetBool(value);
89 }
90 
IsNull(RowEntity & rowEntity,int index)91 inline bool IsNull(RowEntity &rowEntity, int index)
92 {
93     return rowEntity.Get(index).GetType() == ValueObject::TypeId::TYPE_NULL;
94 }
95 
96 int GetString(RowEntity &rowEntity, int index, rust::string &value);
97 
98 int GetBlob(RowEntity &rowEntity, int index, rust::vec<uint8_t> &value);
99 
NewVector()100 inline std::unique_ptr<std::vector<ValueObject>> NewVector()
101 {
102     return std::make_unique<std::vector<ValueObject>>();
103 }
104 
NewConfig(rust::str path)105 inline std::unique_ptr<RdbStoreConfig> NewConfig(rust::str path)
106 {
107     return std::make_unique<RdbStoreConfig>(std::string(path));
108 }
109 
NewRowEntity()110 inline std::unique_ptr<RowEntity> NewRowEntity()
111 {
112     return std::make_unique<RowEntity>();
113 }
114 
Execute(RdbStore & store,const rust::str sql,const std::unique_ptr<std::vector<ValueObject>> args)115 inline int32_t Execute(RdbStore &store, const rust::str sql, const std::unique_ptr<std::vector<ValueObject>> args)
116 {
117     return store.Execute(std::string(sql), *args).first;
118 }
119 
Query(RdbStore & store,const rust::str sql,const std::unique_ptr<std::vector<ValueObject>> args)120 inline std::shared_ptr<ResultSet> Query(
121     RdbStore &store, const rust::str sql, const std::unique_ptr<std::vector<ValueObject>> args)
122 {
123     return store.QueryByStep(std::string(sql), *args);
124 }
125 
126 std::shared_ptr<RdbStore> GetRdbStore(
127     const RdbStoreConfig &config, int version, rust::Box<OpenCallbackWrapper> callback, int &errCode);
128 
129 class OpenCallback : public RdbOpenCallback {
130 public:
131     OpenCallback(rust::Box<OpenCallbackWrapper> &&inner);
132     int OnCreate(RdbStore &store) override;
133     int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override;
134     int OnDowngrade(RdbStore &store, int currentVersion, int targetVersion) override;
135     int OnOpen(RdbStore &store) override;
136     int onCorruption(std::string databaseFile) override;
137 
138 private:
139     rust::Box<OpenCallbackWrapper> inner_;
140 };
141 
142 } // namespace OHOS::Request
143 
144 #endif // REQUEST_DATABASE_WRAPPER_H