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