• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::pin::Pin;
15 
16 use cxx::SharedPtr;
17 use ffi::{GetRdbStore, RdbStore};
18 
19 use crate::config::OpenCallback;
20 use crate::OpenConfig;
21 
22 pub struct OpenCallbackWrapper {
23     inner: Box<dyn OpenCallback>,
24 }
25 
26 impl OpenCallbackWrapper {
on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i3227     fn on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i32 {
28         let mut rdb = crate::RdbStore::from_ffi(rdb);
29         self.inner.on_create(&mut rdb)
30     }
31 
on_upgrade(&mut self, rdb: Pin<&mut RdbStore>, old_version: i32, new_version: i32) -> i3232     fn on_upgrade(&mut self, rdb: Pin<&mut RdbStore>, old_version: i32, new_version: i32) -> i32 {
33         let mut rdb = crate::RdbStore::from_ffi(rdb);
34         self.inner.on_upgrade(&mut rdb, old_version, new_version)
35     }
36 
on_downgrade( &mut self, rdb: Pin<&mut RdbStore>, current_version: i32, target_version: i32, ) -> i3237     fn on_downgrade(
38         &mut self,
39         rdb: Pin<&mut RdbStore>,
40         current_version: i32,
41         target_version: i32,
42     ) -> i32 {
43         let mut rdb = crate::RdbStore::from_ffi(rdb);
44         self.inner
45             .on_downgrade(&mut rdb, current_version, target_version)
46     }
47 
on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i3248     fn on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i32 {
49         let mut rdb = crate::RdbStore::from_ffi(rdb);
50         self.inner.on_open(&mut rdb)
51     }
52 
on_corrupt(&mut self, database_file: &str) -> i3253     fn on_corrupt(&mut self, database_file: &str) -> i32 {
54         self.inner.on_corrupt(database_file)
55     }
56 }
57 
open_rdb_store(config: OpenConfig) -> Result<SharedPtr<RdbStore>, i32>58 pub(crate) fn open_rdb_store(config: OpenConfig) -> Result<SharedPtr<RdbStore>, i32> {
59     let mut code = 0;
60     let rdb = GetRdbStore(
61         &config.inner,
62         config.version,
63         Box::new(OpenCallbackWrapper {
64             inner: config.callback,
65         }),
66         &mut code,
67     );
68     match code {
69         0 => Ok(rdb),
70         err => Err(err),
71     }
72 }
73 
74 unsafe impl Send for RdbStore {}
75 unsafe impl Sync for RdbStore {}
76 #[cxx::bridge(namespace = "OHOS::Request")]
77 pub mod ffi {
78 
79     #[repr(i32)]
80     enum SecurityLevel {
81         S1 = 1,
82         S2,
83         S3,
84         S4,
85         LAST,
86     }
87 
88     #[repr(i32)]
89     enum ColumnType {
90         TYPE_NULL = 0,
91         TYPE_INTEGER,
92         TYPE_FLOAT,
93         TYPE_STRING,
94         TYPE_BLOB,
95         TYPE_ASSET,
96         TYPE_ASSETS,
97         TYPE_FLOAT32_ARRAY,
98         TYPE_BIGINT,
99     }
100 
101     extern "Rust" {
102         type OpenCallbackWrapper;
on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i32103         fn on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i32;
on_upgrade( &mut self, rdb: Pin<&mut RdbStore>, old_version: i32, new_version: i32, ) -> i32104         fn on_upgrade(
105             &mut self,
106             rdb: Pin<&mut RdbStore>,
107             old_version: i32,
108             new_version: i32,
109         ) -> i32;
on_downgrade( &mut self, rdb: Pin<&mut RdbStore>, current_version: i32, target_version: i32, ) -> i32110         fn on_downgrade(
111             &mut self,
112             rdb: Pin<&mut RdbStore>,
113             current_version: i32,
114             target_version: i32,
115         ) -> i32;
on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i32116         fn on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i32;
on_corrupt(&mut self, database_file: &str) -> i32117         fn on_corrupt(&mut self, database_file: &str) -> i32;
118     }
119 
120     unsafe extern "C++" {
121         include!("rdb_store.h");
122         include!("result_set.h");
123         include!("remote_result_set.h");
124         include!("wrapper.h");
125         #[namespace = "OHOS::NativeRdb"]
126         type RdbStoreConfig;
127         #[namespace = "OHOS::NativeRdb"]
128         type RdbStore;
129         #[namespace = "OHOS::NativeRdb"]
130         type ValueObject;
131         #[namespace = "OHOS::NativeRdb"]
132         type SecurityLevel;
133         #[namespace = "OHOS::NativeRdb"]
134         type StorageMode;
135         #[namespace = "OHOS::NativeRdb"]
136         type ResultSet;
137         #[namespace = "OHOS::NativeRdb"]
138         type RowEntity;
139         #[namespace = "OHOS::NativeRdb"]
140         type ColumnType;
141 
142         #[namespace = "OHOS::NativeRdb"]
GetColumnType( self: Pin<&mut ResultSet>, column_index: i32, column_type: Pin<&mut ColumnType>, ) -> i32143         fn GetColumnType(
144             self: Pin<&mut ResultSet>,
145             column_index: i32,
146             column_type: Pin<&mut ColumnType>,
147         ) -> i32;
148         #[namespace = "OHOS::NativeRdb"]
GetColumnCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32149         fn GetColumnCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32;
150         #[namespace = "OHOS::NativeRdb"]
GetRowCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32151         fn GetRowCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32;
152         #[namespace = "OHOS::NativeRdb"]
GoToNextRow(self: Pin<&mut ResultSet>) -> i32153         fn GoToNextRow(self: Pin<&mut ResultSet>) -> i32;
154         #[namespace = "OHOS::NativeRdb"]
GetRow(self: Pin<&mut ResultSet>, row: Pin<&mut RowEntity>) -> i32155         fn GetRow(self: Pin<&mut ResultSet>, row: Pin<&mut RowEntity>) -> i32;
156 
NewVector() -> UniquePtr<CxxVector<ValueObject>>157         fn NewVector() -> UniquePtr<CxxVector<ValueObject>>;
NewConfig(path: &str) -> UniquePtr<RdbStoreConfig>158         fn NewConfig(path: &str) -> UniquePtr<RdbStoreConfig>;
NewRowEntity() -> UniquePtr<RowEntity>159         fn NewRowEntity() -> UniquePtr<RowEntity>;
160 
BindI32(value: i32, values: Pin<&mut CxxVector<ValueObject>>)161         fn BindI32(value: i32, values: Pin<&mut CxxVector<ValueObject>>);
BindI64(value: i64, values: Pin<&mut CxxVector<ValueObject>>)162         fn BindI64(value: i64, values: Pin<&mut CxxVector<ValueObject>>);
BindBool(value: bool, values: Pin<&mut CxxVector<ValueObject>>)163         fn BindBool(value: bool, values: Pin<&mut CxxVector<ValueObject>>);
BindDouble(value: f64, values: Pin<&mut CxxVector<ValueObject>>)164         fn BindDouble(value: f64, values: Pin<&mut CxxVector<ValueObject>>);
BindString(value: &str, values: Pin<&mut CxxVector<ValueObject>>)165         fn BindString(value: &str, values: Pin<&mut CxxVector<ValueObject>>);
BindBlob(value: &[u8], values: Pin<&mut CxxVector<ValueObject>>)166         fn BindBlob(value: &[u8], values: Pin<&mut CxxVector<ValueObject>>);
BindNull(values: Pin<&mut CxxVector<ValueObject>>)167         fn BindNull(values: Pin<&mut CxxVector<ValueObject>>);
168 
GetI32(row: Pin<&mut RowEntity>, index: i32, value: &mut i32) -> i32169         fn GetI32(row: Pin<&mut RowEntity>, index: i32, value: &mut i32) -> i32;
GetI64(row: Pin<&mut RowEntity>, index: i32, value: &mut i64) -> i32170         fn GetI64(row: Pin<&mut RowEntity>, index: i32, value: &mut i64) -> i32;
GetBool(row: Pin<&mut RowEntity>, index: i32, value: &mut bool) -> i32171         fn GetBool(row: Pin<&mut RowEntity>, index: i32, value: &mut bool) -> i32;
GetDouble(row: Pin<&mut RowEntity>, index: i32, value: &mut f64) -> i32172         fn GetDouble(row: Pin<&mut RowEntity>, index: i32, value: &mut f64) -> i32;
GetString(row: Pin<&mut RowEntity>, index: i32, value: &mut String) -> i32173         fn GetString(row: Pin<&mut RowEntity>, index: i32, value: &mut String) -> i32;
GetBlob(row: Pin<&mut RowEntity>, index: i32, value: &mut Vec<u8>) -> i32174         fn GetBlob(row: Pin<&mut RowEntity>, index: i32, value: &mut Vec<u8>) -> i32;
IsNull(row: Pin<&mut RowEntity>, index: i32) -> bool175         fn IsNull(row: Pin<&mut RowEntity>, index: i32) -> bool;
Execute( rdb: Pin<&mut RdbStore>, sql: &str, values: UniquePtr<CxxVector<ValueObject>>, ) -> i32176         fn Execute(
177             rdb: Pin<&mut RdbStore>,
178             sql: &str,
179             values: UniquePtr<CxxVector<ValueObject>>,
180         ) -> i32;
181 
Query( rdb: Pin<&mut RdbStore>, sql: &str, values: UniquePtr<CxxVector<ValueObject>>, ) -> SharedPtr<ResultSet>182         fn Query(
183             rdb: Pin<&mut RdbStore>,
184             sql: &str,
185             values: UniquePtr<CxxVector<ValueObject>>,
186         ) -> SharedPtr<ResultSet>;
187 
SetSecurityLevel(self: Pin<&mut RdbStoreConfig>, level: SecurityLevel)188         fn SetSecurityLevel(self: Pin<&mut RdbStoreConfig>, level: SecurityLevel);
SetEncryptStatus(self: Pin<&mut RdbStoreConfig>, status: bool)189         fn SetEncryptStatus(self: Pin<&mut RdbStoreConfig>, status: bool);
SetBundleName(self: Pin<&mut RdbStoreConfig>, bundleName: &CxxString) -> i32190         fn SetBundleName(self: Pin<&mut RdbStoreConfig>, bundleName: &CxxString) -> i32;
GetRdbStore( config: &RdbStoreConfig, version: i32, openCallback: Box<OpenCallbackWrapper>, errCode: &mut i32, ) -> SharedPtr<RdbStore>191         fn GetRdbStore(
192             config: &RdbStoreConfig,
193             version: i32,
194             openCallback: Box<OpenCallbackWrapper>,
195             errCode: &mut i32,
196         ) -> SharedPtr<RdbStore>;
197     }
198 }
199