• 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 cxx::{let_cxx_string, UniquePtr};
15 
16 use crate::database::RdbStore;
17 use crate::wrapper::ffi::{NewConfig, RdbStoreConfig, SecurityLevel};
18 
19 /// Open options of `RDB`.
20 pub struct OpenConfig {
21     pub(crate) inner: UniquePtr<RdbStoreConfig>,
22     pub(crate) version: i32,
23     pub(crate) callback: Box<dyn OpenCallback>,
24 }
25 
26 impl OpenConfig {
27     /// Creates a new `OpenConfig`.
new(path: &str) -> Self28     pub fn new(path: &str) -> Self {
29         Self {
30             inner: NewConfig(path),
31             version: 1,
32             callback: Box::new(DefaultCallback),
33         }
34     }
35 
36     /// Sets the security level of the database.
security_level(&mut self, level: SecurityLevel) -> &mut Self37     pub fn security_level(&mut self, level: SecurityLevel) -> &mut Self {
38         self.inner.pin_mut().SetSecurityLevel(level);
39         self
40     }
41 
42     /// Sets the encrypt status of the database.
encrypt_status(&mut self, status: bool) -> &mut Self43     pub fn encrypt_status(&mut self, status: bool) -> &mut Self {
44         self.inner.pin_mut().SetEncryptStatus(status);
45         self
46     }
47 
48     /// Sets the bundle name of the database.
bundle_name(&mut self, name: &str) -> &mut Self49     pub fn bundle_name(&mut self, name: &str) -> &mut Self {
50         let_cxx_string!(name = name);
51         self.inner.pin_mut().SetBundleName(&name);
52         self
53     }
54 
55     /// Sets the open callback of the database.
callback(&mut self, callback: Box<dyn OpenCallback>) -> &mut Self56     pub fn callback(&mut self, callback: Box<dyn OpenCallback>) -> &mut Self {
57         self.callback = callback;
58         self
59     }
60 
61     /// Sets the version of the database.
version(&mut self, version: i32) -> &mut Self62     pub fn version(&mut self, version: i32) -> &mut Self {
63         self.version = version;
64         self
65     }
66 }
67 
68 /// Trait for database callbacks.
69 pub trait OpenCallback {
70     /// Callback for creating the database.
on_create(&mut self, _rdb: &mut RdbStore) -> i3271     fn on_create(&mut self, _rdb: &mut RdbStore) -> i32 {
72         0
73     }
74 
75     /// Callback for upgrading the database.
on_upgrade(&mut self, _rdb: &mut RdbStore, _old_version: i32, _new_version: i32) -> i3276     fn on_upgrade(&mut self, _rdb: &mut RdbStore, _old_version: i32, _new_version: i32) -> i32 {
77         0
78     }
79 
80     /// Callback for downgrading the database.
on_downgrade( &mut self, _rdb: &mut RdbStore, _current_version: i32, _target_version: i32, ) -> i3281     fn on_downgrade(
82         &mut self,
83         _rdb: &mut RdbStore,
84         _current_version: i32,
85         _target_version: i32,
86     ) -> i32 {
87         0
88     }
89 
90     /// Callback for opening the database.
on_open(&mut self, _rdb: &mut RdbStore) -> i3291     fn on_open(&mut self, _rdb: &mut RdbStore) -> i32 {
92         0
93     }
94 
95     /// Callback when the database is corrupted.
on_corrupt(&mut self, _database_file: &str) -> i3296     fn on_corrupt(&mut self, _database_file: &str) -> i32 {
97         0
98     }
99 }
100 
101 struct DefaultCallback;
102 
103 impl OpenCallback for DefaultCallback {}
104