• 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, OpenConfig};
20 use crate::database;
21 
22 /// OpenCallback ffi.
23 pub struct OpenCallbackWrapper {
24     inner: Box<dyn OpenCallback>,
25 }
26 
27 impl OpenCallbackWrapper {
on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i3228     fn on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i32 {
29         let mut rdb = database::RdbStore::from_ffi(rdb);
30         self.inner.on_create(&mut rdb)
31     }
32 
on_upgrade(&mut self, rdb: Pin<&mut RdbStore>, old_version: i32, new_version: i32) -> i3233     fn on_upgrade(&mut self, rdb: Pin<&mut RdbStore>, old_version: i32, new_version: i32) -> i32 {
34         let mut rdb = database::RdbStore::from_ffi(rdb);
35         self.inner.on_upgrade(&mut rdb, old_version, new_version)
36     }
37 
on_downgrade( &mut self, rdb: Pin<&mut RdbStore>, current_version: i32, target_version: i32, ) -> i3238     fn on_downgrade(
39         &mut self,
40         rdb: Pin<&mut RdbStore>,
41         current_version: i32,
42         target_version: i32,
43     ) -> i32 {
44         let mut rdb = database::RdbStore::from_ffi(rdb);
45         self.inner
46             .on_downgrade(&mut rdb, current_version, target_version)
47     }
48 
on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i3249     fn on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i32 {
50         let mut rdb = database::RdbStore::from_ffi(rdb);
51         self.inner.on_open(&mut rdb)
52     }
53 
on_corrupt(&mut self, database_file: &str) -> i3254     fn on_corrupt(&mut self, database_file: &str) -> i32 {
55         self.inner.on_corrupt(database_file)
56     }
57 }
58 
open_rdb_store(config: OpenConfig) -> Result<SharedPtr<RdbStore>, i32>59 pub(crate) fn open_rdb_store(config: OpenConfig) -> Result<SharedPtr<RdbStore>, i32> {
60     let mut code = 0;
61     let rdb = GetRdbStore(
62         &config.inner,
63         config.version,
64         Box::new(OpenCallbackWrapper {
65             inner: config.callback,
66         }),
67         &mut code,
68     );
69     match code {
70         0 => Ok(rdb),
71         err => Err(err),
72     }
73 }
74 
75 unsafe impl Send for RdbStore {}
76 unsafe impl Sync for RdbStore {}
77 
78 #[allow(unused, missing_docs)]
79 #[cxx::bridge(namespace = "OHOS::Request")]
80 pub mod ffi {
81     #[repr(i32)]
82     enum SecurityLevel {
83         S1 = 1,
84         S2,
85         S3,
86         S4,
87         LAST,
88     }
89 
90     #[repr(i32)]
91     enum ColumnType {
92         TYPE_NULL = 0,
93         TYPE_INTEGER,
94         TYPE_FLOAT,
95         TYPE_STRING,
96         TYPE_BLOB,
97         TYPE_ASSET,
98         TYPE_ASSETS,
99         TYPE_FLOAT32_ARRAY,
100         TYPE_BIGINT,
101     }
102 
103     extern "Rust" {
104         type OpenCallbackWrapper;
on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i32105         fn on_create(&mut self, rdb: Pin<&mut RdbStore>) -> i32;
on_upgrade( &mut self, rdb: Pin<&mut RdbStore>, old_version: i32, new_version: i32, ) -> i32106         fn on_upgrade(
107             &mut self,
108             rdb: Pin<&mut RdbStore>,
109             old_version: i32,
110             new_version: i32,
111         ) -> i32;
on_downgrade( &mut self, rdb: Pin<&mut RdbStore>, current_version: i32, target_version: i32, ) -> i32112         fn on_downgrade(
113             &mut self,
114             rdb: Pin<&mut RdbStore>,
115             current_version: i32,
116             target_version: i32,
117         ) -> i32;
on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i32118         fn on_open(&mut self, rdb: Pin<&mut RdbStore>) -> i32;
on_corrupt(&mut self, database_file: &str) -> i32119         fn on_corrupt(&mut self, database_file: &str) -> i32;
120     }
121 
122     unsafe extern "C++" {
123         include!("rdb_store.h");
124         include!("result_set.h");
125         include!("remote_result_set.h");
126         include!("wrapper.h");
127         #[namespace = "OHOS::NativeRdb"]
128         type RdbStoreConfig;
129         #[namespace = "OHOS::NativeRdb"]
130         type RdbStore;
131         #[namespace = "OHOS::NativeRdb"]
132         type ValueObject;
133         #[namespace = "OHOS::NativeRdb"]
134         type SecurityLevel;
135         #[namespace = "OHOS::NativeRdb"]
136         type StorageMode;
137         #[namespace = "OHOS::NativeRdb"]
138         type ResultSet;
139         #[namespace = "OHOS::NativeRdb"]
140         type RowEntity;
141         #[namespace = "OHOS::NativeRdb"]
142         type ColumnType;
143 
144         #[namespace = "OHOS::NativeRdb"]
GetColumnType( self: Pin<&mut ResultSet>, column_index: i32, column_type: Pin<&mut ColumnType>, ) -> i32145         fn GetColumnType(
146             self: Pin<&mut ResultSet>,
147             column_index: i32,
148             column_type: Pin<&mut ColumnType>,
149         ) -> i32;
150         #[namespace = "OHOS::NativeRdb"]
GetColumnCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32151         fn GetColumnCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32;
152         #[namespace = "OHOS::NativeRdb"]
GetRowCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32153         fn GetRowCount(self: Pin<&mut ResultSet>, count: &mut i32) -> i32;
154         #[namespace = "OHOS::NativeRdb"]
GoToNextRow(self: Pin<&mut ResultSet>) -> i32155         fn GoToNextRow(self: Pin<&mut ResultSet>) -> i32;
156         #[namespace = "OHOS::NativeRdb"]
GetRow(self: Pin<&mut ResultSet>, row: Pin<&mut RowEntity>) -> i32157         fn GetRow(self: Pin<&mut ResultSet>, row: Pin<&mut RowEntity>) -> i32;
158 
NewVector() -> UniquePtr<CxxVector<ValueObject>>159         fn NewVector() -> UniquePtr<CxxVector<ValueObject>>;
NewConfig(path: &str) -> UniquePtr<RdbStoreConfig>160         fn NewConfig(path: &str) -> UniquePtr<RdbStoreConfig>;
NewRowEntity() -> UniquePtr<RowEntity>161         fn NewRowEntity() -> UniquePtr<RowEntity>;
162 
BindI32(value: i32, values: Pin<&mut CxxVector<ValueObject>>)163         fn BindI32(value: i32, values: Pin<&mut CxxVector<ValueObject>>);
BindI64(value: i64, values: Pin<&mut CxxVector<ValueObject>>)164         fn BindI64(value: i64, values: Pin<&mut CxxVector<ValueObject>>);
BindBool(value: bool, values: Pin<&mut CxxVector<ValueObject>>)165         fn BindBool(value: bool, values: Pin<&mut CxxVector<ValueObject>>);
BindDouble(value: f64, values: Pin<&mut CxxVector<ValueObject>>)166         fn BindDouble(value: f64, values: Pin<&mut CxxVector<ValueObject>>);
BindString(value: &str, values: Pin<&mut CxxVector<ValueObject>>)167         fn BindString(value: &str, values: Pin<&mut CxxVector<ValueObject>>);
BindBlob(value: &[u8], values: Pin<&mut CxxVector<ValueObject>>)168         fn BindBlob(value: &[u8], values: Pin<&mut CxxVector<ValueObject>>);
BindNull(values: Pin<&mut CxxVector<ValueObject>>)169         fn BindNull(values: Pin<&mut CxxVector<ValueObject>>);
170 
GetI32(row: Pin<&mut RowEntity>, index: i32, value: &mut i32) -> i32171         fn GetI32(row: Pin<&mut RowEntity>, index: i32, value: &mut i32) -> i32;
GetI64(row: Pin<&mut RowEntity>, index: i32, value: &mut i64) -> i32172         fn GetI64(row: Pin<&mut RowEntity>, index: i32, value: &mut i64) -> i32;
GetBool(row: Pin<&mut RowEntity>, index: i32, value: &mut bool) -> i32173         fn GetBool(row: Pin<&mut RowEntity>, index: i32, value: &mut bool) -> i32;
GetDouble(row: Pin<&mut RowEntity>, index: i32, value: &mut f64) -> i32174         fn GetDouble(row: Pin<&mut RowEntity>, index: i32, value: &mut f64) -> i32;
GetString(row: Pin<&mut RowEntity>, index: i32, value: &mut String) -> i32175         fn GetString(row: Pin<&mut RowEntity>, index: i32, value: &mut String) -> i32;
GetBlob(row: Pin<&mut RowEntity>, index: i32, value: &mut Vec<u8>) -> i32176         fn GetBlob(row: Pin<&mut RowEntity>, index: i32, value: &mut Vec<u8>) -> i32;
IsNull(row: Pin<&mut RowEntity>, index: i32) -> bool177         fn IsNull(row: Pin<&mut RowEntity>, index: i32) -> bool;
Execute( rdb: Pin<&mut RdbStore>, sql: &str, values: UniquePtr<CxxVector<ValueObject>>, ) -> i32178         fn Execute(
179             rdb: Pin<&mut RdbStore>,
180             sql: &str,
181             values: UniquePtr<CxxVector<ValueObject>>,
182         ) -> i32;
183 
Query( rdb: Pin<&mut RdbStore>, sql: &str, values: UniquePtr<CxxVector<ValueObject>>, ) -> SharedPtr<ResultSet>184         fn Query(
185             rdb: Pin<&mut RdbStore>,
186             sql: &str,
187             values: UniquePtr<CxxVector<ValueObject>>,
188         ) -> SharedPtr<ResultSet>;
189 
SetSecurityLevel(self: Pin<&mut RdbStoreConfig>, level: SecurityLevel)190         fn SetSecurityLevel(self: Pin<&mut RdbStoreConfig>, level: SecurityLevel);
SetEncryptStatus(self: Pin<&mut RdbStoreConfig>, status: bool)191         fn SetEncryptStatus(self: Pin<&mut RdbStoreConfig>, status: bool);
SetBundleName(self: Pin<&mut RdbStoreConfig>, bundleName: &CxxString) -> i32192         fn SetBundleName(self: Pin<&mut RdbStoreConfig>, bundleName: &CxxString) -> i32;
GetRdbStore( config: &RdbStoreConfig, version: i32, openCallback: Box<OpenCallbackWrapper>, errCode: &mut i32, ) -> SharedPtr<RdbStore>193         fn GetRdbStore(
194             config: &RdbStoreConfig,
195             version: i32,
196             openCallback: Box<OpenCallbackWrapper>,
197             errCode: &mut i32,
198         ) -> SharedPtr<RdbStore>;
199     }
200 }
201