• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 //! This module defines the common data structure of the database module.
17 
18 use std::{cmp::Ordering, collections::HashMap};
19 
20 use asset_definition::{DataType, ErrCode, Value, WrapType};
21 
22 /// A Map type containing tag-value pairs that describe the attributes of an DB field.
23 pub type DbMap = HashMap<&'static str, Value>;
24 
25 /// Table name of asset database.
26 pub const TABLE_NAME: &str = "asset_table";
27 
28 /// Version V1 number for upgrade database
29 pub const DB_UPGRADE_VERSION_V1: u32 = 0;
30 /// Version V2 number for upgrade database
31 pub const DB_UPGRADE_VERSION_V2: u32 = 1;
32 /// Version V3 number for upgrade database
33 pub const DB_UPGRADE_VERSION_V3: u32 = 2;
34 /// Version V4 number for upgrade database
35 pub const DB_UPGRADE_VERSION_V4: u32 = 3;
36 /// Latest version number for upgrade database
37 pub const DB_UPGRADE_VERSION: u32 = 4;
38 
39 /// Version 1 number
40 pub const DB_DATA_VERSION_V1: u32 = 1;
41 /// Version 2 number
42 pub const DB_DATA_VERSION_V2: u32 = 2;
43 /// Version 3 number
44 pub const DB_DATA_VERSION_V3: u32 = 3;
45 /// Latest data version number.
46 pub const DB_DATA_VERSION: u32 = 4;
47 /// Column name of asset database.
48 pub mod column {
49     /// Column name of the primary key Id.
50     pub const ID: &str = "Id";
51     /// Column name of secret cipher.
52     pub const SECRET: &str = "Secret";
53     /// Column name of data alias.
54     pub const ALIAS: &str = "Alias";
55     /// Column name of data owner.
56     pub const OWNER: &str = "Owner";
57     /// Column name of owner type.
58     pub const OWNER_TYPE: &str = "OwnerType";
59     /// Column name of (developerId + "," + groupId) of a group.
60     pub const GROUP_ID: &str = "GroupId";
61     /// Column name of data synchronization type.
62     pub const SYNC_TYPE: &str = "SyncType";
63     /// Column name of data accessibility
64     pub const ACCESSIBILITY: &str = "Accessibility";
65     /// Column name of the user authentication type supported by the data
66     pub const AUTH_TYPE: &str = "AuthType";
67     /// Column name of data creation time.
68     pub const CREATE_TIME: &str = "CreateTime";
69     /// Column name of the data update time.
70     pub const UPDATE_TIME: &str = "UpdateTime";
71     /// Column name of the data persistence attribute.
72     pub const IS_PERSISTENT: &str = "IsPersistent";
73     /// Column name of the data version number.
74     pub const VERSION: &str = "Version";
75     /// Column name of if data require password set
76     pub const REQUIRE_PASSWORD_SET: &str = "RequirePasswordSet";
77     /// Column name of the first critical data label.
78     pub const CRITICAL1: &str = "DataLabelCritical_1";
79     /// Column name of the second critical data label.
80     pub const CRITICAL2: &str = "DataLabelCritical_2";
81     /// Column name of the third critical data label.
82     pub const CRITICAL3: &str = "DataLabelCritical_3";
83     /// Column name of the fourth critical data label.
84     pub const CRITICAL4: &str = "DataLabelCritical_4";
85     /// Column name of the first normal data label.
86     pub const NORMAL1: &str = "DataLabelNormal_1";
87     /// Column name of the second normal data label.
88     pub const NORMAL2: &str = "DataLabelNormal_2";
89     /// Column name of the third normal data label.
90     pub const NORMAL3: &str = "DataLabelNormal_3";
91     /// Column name of the fourth normal data label.
92     pub const NORMAL4: &str = "DataLabelNormal_4";
93     /// Column name of the first normal local data label.
94     pub const NORMAL_LOCAL1: &str = "DataLabelNormalLocal_1";
95     /// Column name of the second normal local data label.
96     pub const NORMAL_LOCAL2: &str = "DataLabelNormalLocal_2";
97     /// Column name of the third normal local data label.
98     pub const NORMAL_LOCAL3: &str = "DataLabelNormalLocal_3";
99     /// Column name of the fourth normal local data label.
100     pub const NORMAL_LOCAL4: &str = "DataLabelNormalLocal_4";
101     /// Column name of the first normal local data label.
102     pub const GLOBAL_ID: &str = "GlobalId";
103     /// Column name of the second normal local data label.
104     pub const CLOUD_VERSION: &str = "CloudVersion";
105     /// Column name of the third normal local data label.
106     pub const LOCAL_STATUS: &str = "LocalStatus";
107     /// Column name of the fourth normal local data label.
108     pub const SYNC_STATUS: &str = "SyncStatus";
109     /// Column name of the ext data info.
110     pub const EXT_INFO: &str = "ExtInfo";
111     /// Column name of the wrap type info.
112     pub const WRAP_TYPE: &str = "WrapType";
113 }
114 
115 #[repr(C)]
116 pub(crate) struct ColumnInfo {
117     pub(crate) name: &'static str,
118     pub(crate) data_type: DataType,
119     pub(crate) is_primary_key: bool,
120     pub(crate) not_null: bool,
121 }
122 
123 pub(crate) const COLUMN_INFO: &[ColumnInfo] = &[
124     ColumnInfo { name: column::ID, data_type: DataType::Number, is_primary_key: true, not_null: true },
125     ColumnInfo { name: column::SECRET, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
126     ColumnInfo { name: column::ALIAS, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
127     ColumnInfo { name: column::OWNER, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
128     ColumnInfo { name: column::OWNER_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
129     ColumnInfo { name: column::GROUP_ID, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
130     ColumnInfo { name: column::SYNC_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
131     ColumnInfo { name: column::ACCESSIBILITY, data_type: DataType::Number, is_primary_key: false, not_null: true },
132     ColumnInfo { name: column::AUTH_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
133     ColumnInfo { name: column::CREATE_TIME, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
134     ColumnInfo { name: column::UPDATE_TIME, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
135     ColumnInfo { name: column::IS_PERSISTENT, data_type: DataType::Bool, is_primary_key: false, not_null: true },
136     ColumnInfo { name: column::VERSION, data_type: DataType::Number, is_primary_key: false, not_null: true },
137     ColumnInfo { name: column::REQUIRE_PASSWORD_SET, data_type: DataType::Bool, is_primary_key: false, not_null: true },
138     ColumnInfo { name: column::CRITICAL1, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
139     ColumnInfo { name: column::CRITICAL2, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
140     ColumnInfo { name: column::CRITICAL3, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
141     ColumnInfo { name: column::CRITICAL4, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
142     ColumnInfo { name: column::NORMAL1, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
143     ColumnInfo { name: column::NORMAL2, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
144     ColumnInfo { name: column::NORMAL3, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
145     ColumnInfo { name: column::NORMAL4, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
146     ColumnInfo { name: column::NORMAL_LOCAL1, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
147     ColumnInfo { name: column::NORMAL_LOCAL2, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
148     ColumnInfo { name: column::NORMAL_LOCAL3, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
149     ColumnInfo { name: column::NORMAL_LOCAL4, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
150     ColumnInfo { name: column::GLOBAL_ID, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
151     ColumnInfo { name: column::CLOUD_VERSION, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
152     ColumnInfo { name: column::LOCAL_STATUS, data_type: DataType::Number, is_primary_key: false, not_null: true },
153     ColumnInfo { name: column::SYNC_STATUS, data_type: DataType::Number, is_primary_key: false, not_null: true },
154     ColumnInfo { name: column::EXT_INFO, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
155     ColumnInfo { name: column::WRAP_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
156 ];
157 
158 pub(crate) struct UpgradeColumnInfo {
159     pub(crate) base_info: ColumnInfo,
160     pub(crate) default_value: Option<Value>,
161 }
162 
163 pub(crate) const UPGRADE_COLUMN_INFO_V2: &[UpgradeColumnInfo] = &[
164     UpgradeColumnInfo {
165         base_info: ColumnInfo {
166             name: column::NORMAL_LOCAL1,
167             data_type: DataType::Bytes,
168             is_primary_key: false,
169             not_null: false,
170         },
171         default_value: None,
172     },
173     UpgradeColumnInfo {
174         base_info: ColumnInfo {
175             name: column::NORMAL_LOCAL2,
176             data_type: DataType::Bytes,
177             is_primary_key: false,
178             not_null: false,
179         },
180         default_value: None,
181     },
182     UpgradeColumnInfo {
183         base_info: ColumnInfo {
184             name: column::NORMAL_LOCAL3,
185             data_type: DataType::Bytes,
186             is_primary_key: false,
187             not_null: false,
188         },
189         default_value: None,
190     },
191     UpgradeColumnInfo {
192         base_info: ColumnInfo {
193             name: column::NORMAL_LOCAL4,
194             data_type: DataType::Bytes,
195             is_primary_key: false,
196             not_null: false,
197         },
198         default_value: None,
199     },
200     UpgradeColumnInfo {
201         base_info: ColumnInfo {
202             name: column::GLOBAL_ID,
203             data_type: DataType::Bytes,
204             is_primary_key: false,
205             not_null: false,
206         },
207         default_value: None,
208     },
209     UpgradeColumnInfo {
210         base_info: ColumnInfo {
211             name: column::CLOUD_VERSION,
212             data_type: DataType::Bytes,
213             is_primary_key: false,
214             not_null: false,
215         },
216         default_value: None,
217     },
218     UpgradeColumnInfo {
219         base_info: ColumnInfo {
220             name: column::LOCAL_STATUS,
221             data_type: DataType::Number,
222             is_primary_key: false,
223             not_null: true,
224         },
225         default_value: Some(Value::Number(0)),
226     },
227     UpgradeColumnInfo {
228         base_info: ColumnInfo {
229             name: column::SYNC_STATUS,
230             data_type: DataType::Number,
231             is_primary_key: false,
232             not_null: true,
233         },
234         default_value: Some(Value::Number(0)),
235     },
236 ];
237 
238 pub(crate) const UPGRADE_COLUMN_INFO_V3: &[UpgradeColumnInfo] = &[UpgradeColumnInfo {
239     base_info: ColumnInfo {
240         name: column::EXT_INFO,
241         data_type: DataType::Bytes,
242         is_primary_key: false,
243         not_null: false,
244     },
245     default_value: None,
246 }];
247 
248 pub(crate) const UPGRADE_COLUMN_INFO_V4: &[UpgradeColumnInfo] = &[];
249 
250 pub(crate) const UPGRADE_COLUMN_INFO: &[UpgradeColumnInfo] = &[UpgradeColumnInfo {
251     base_info: ColumnInfo {
252         name: column::WRAP_TYPE,
253         data_type: DataType::Number,
254         is_primary_key: false,
255         not_null: true,
256     },
257     default_value: Some(Value::Number(WrapType::Never as u32)),
258 }];
259 
260 /// Options for batch query.
261 #[repr(C)]
262 pub struct QueryOptions {
263     /// The offset of the query result.
264     pub offset: Option<u32>,
265     /// Maximum number of query results.
266     pub limit: Option<u32>,
267     /// ordering: Ordering::Greater => ASC and Ordering::Less => DESC
268     pub order: Option<Ordering>,
269     /// Columns used for sorting.
270     pub order_by: Option<Vec<&'static str>>,
271 }
272 
273 pub(crate) const SQLITE_OK: i32 = 0;
274 pub(crate) const SQLITE_NOMEM: i32 = 7;
275 pub(crate) const SQLITE_CORRUPT: i32 = 11;
276 pub(crate) const SQLITE_NOTADB: i32 = 26;
277 /// Another row willed queried by function: sqlite3_step().
278 pub(crate) const SQLITE_ROW: i32 = 100;
279 /// End the execution of function sqlite3_step().
280 pub(crate) const SQLITE_DONE: i32 = 101;
281 
sqlite_err_handle(ret: i32) -> ErrCode282 pub(crate) fn sqlite_err_handle(ret: i32) -> ErrCode {
283     match ret {
284         SQLITE_CORRUPT | SQLITE_NOTADB => ErrCode::DataCorrupted,
285         SQLITE_NOMEM => ErrCode::OutOfMemory,
286         _ => ErrCode::DatabaseError,
287     }
288 }
289