• 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};
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 1 number
29 pub const DB_DATA_VERSION_V1: u32 = 1;
30 /// Latest data version number.
31 pub const DB_DATA_VERSION: u32 = 2;
32 /// Column name of asset database.
33 pub mod column {
34     /// Column name of the primary key Id.
35     pub const ID: &str = "Id";
36     /// Column name of secret cipher.
37     pub const SECRET: &str = "Secret";
38     /// Column name of data alias.
39     pub const ALIAS: &str = "Alias";
40     /// Column name of data owner.
41     pub const OWNER: &str = "Owner";
42     /// Column name of owner type.
43     pub const OWNER_TYPE: &str = "OwnerType";
44     /// Column name of unique id of a group. (reserved)
45     pub const GROUP_ID: &str = "GroupId";
46     /// Column name of data synchronization type.
47     pub const SYNC_TYPE: &str = "SyncType";
48     /// Column name of data accessibility
49     pub const ACCESSIBILITY: &str = "Accessibility";
50     /// Column name of the user authentication type supported by the data
51     pub const AUTH_TYPE: &str = "AuthType";
52     /// Column name of data creation time.
53     pub const CREATE_TIME: &str = "CreateTime";
54     /// Column name of the data update time.
55     pub const UPDATE_TIME: &str = "UpdateTime";
56     /// Column name of the data persistence attribute.
57     pub const IS_PERSISTENT: &str = "IsPersistent";
58     /// Column name of the data version number.
59     pub const VERSION: &str = "Version";
60     /// Column name of if data require password set
61     pub const REQUIRE_PASSWORD_SET: &str = "RequirePasswordSet";
62     /// Column name of the first critical data label.
63     pub const CRITICAL1: &str = "DataLabelCritical_1";
64     /// Column name of the second critical data label.
65     pub const CRITICAL2: &str = "DataLabelCritical_2";
66     /// Column name of the third critical data label.
67     pub const CRITICAL3: &str = "DataLabelCritical_3";
68     /// Column name of the fourth critical data label.
69     pub const CRITICAL4: &str = "DataLabelCritical_4";
70     /// Column name of the first normal data label.
71     pub const NORMAL1: &str = "DataLabelNormal_1";
72     /// Column name of the second normal data label.
73     pub const NORMAL2: &str = "DataLabelNormal_2";
74     /// Column name of the third normal data label.
75     pub const NORMAL3: &str = "DataLabelNormal_3";
76     /// Column name of the fourth normal data label.
77     pub const NORMAL4: &str = "DataLabelNormal_4";
78 }
79 
80 #[repr(C)]
81 pub(crate) struct ColumnInfo {
82     pub(crate) name: &'static str,
83     pub(crate) data_type: DataType,
84     pub(crate) is_primary_key: bool,
85     pub(crate) not_null: bool,
86 }
87 
88 pub(crate) const COLUMN_INFO: &[ColumnInfo] = &[
89     ColumnInfo { name: column::ID, data_type: DataType::Number, is_primary_key: true, not_null: true },
90     ColumnInfo { name: column::SECRET, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
91     ColumnInfo { name: column::ALIAS, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
92     ColumnInfo { name: column::OWNER, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
93     ColumnInfo { name: column::OWNER_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
94     ColumnInfo { name: column::GROUP_ID, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
95     ColumnInfo { name: column::SYNC_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
96     ColumnInfo { name: column::ACCESSIBILITY, data_type: DataType::Number, is_primary_key: false, not_null: true },
97     ColumnInfo { name: column::AUTH_TYPE, data_type: DataType::Number, is_primary_key: false, not_null: true },
98     ColumnInfo { name: column::CREATE_TIME, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
99     ColumnInfo { name: column::UPDATE_TIME, data_type: DataType::Bytes, is_primary_key: false, not_null: true },
100     ColumnInfo { name: column::IS_PERSISTENT, data_type: DataType::Bool, is_primary_key: false, not_null: true },
101     ColumnInfo { name: column::VERSION, data_type: DataType::Number, is_primary_key: false, not_null: true },
102     ColumnInfo { name: column::REQUIRE_PASSWORD_SET, data_type: DataType::Bool, is_primary_key: false, not_null: true },
103     ColumnInfo { name: column::CRITICAL1, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
104     ColumnInfo { name: column::CRITICAL2, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
105     ColumnInfo { name: column::CRITICAL3, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
106     ColumnInfo { name: column::CRITICAL4, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
107     ColumnInfo { name: column::NORMAL1, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
108     ColumnInfo { name: column::NORMAL2, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
109     ColumnInfo { name: column::NORMAL3, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
110     ColumnInfo { name: column::NORMAL4, data_type: DataType::Bytes, is_primary_key: false, not_null: false },
111 ];
112 
113 /// Options for batch query.
114 #[repr(C)]
115 pub struct QueryOptions {
116     /// The offset of the query result.
117     pub offset: Option<u32>,
118     /// Maximum number of query results.
119     pub limit: Option<u32>,
120     /// ordering: Ordering::Greater => ASC and Ordering::Less => DESC
121     pub order: Option<Ordering>,
122     /// Columns used for sorting.
123     pub order_by: Option<Vec<&'static str>>,
124 }
125 
126 pub(crate) const SQLITE_OK: i32 = 0;
127 pub(crate) const SQLITE_NOMEM: i32 = 7;
128 pub(crate) const SQLITE_CORRUPT: i32 = 11;
129 pub(crate) const SQLITE_NOTADB: i32 = 26;
130 /// Another row willed queried by function: sqlite3_step().
131 pub(crate) const SQLITE_ROW: i32 = 100;
132 /// End the execution of function sqlite3_step().
133 pub(crate) const SQLITE_DONE: i32 = 101;
134 
sqlite_err_handle(ret: i32) -> ErrCode135 pub(crate) fn sqlite_err_handle(ret: i32) -> ErrCode {
136     match ret {
137         SQLITE_CORRUPT | SQLITE_NOTADB => ErrCode::DataCorrupted,
138         SQLITE_NOMEM => ErrCode::OutOfMemory,
139         _ => ErrCode::DatabaseError,
140     }
141 }
142