1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Types returned by [`Connection`][crate::Connection] schema-inspection methods.
/// A table visible to the current connection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableInfo {
/// The table name.
pub name: String,
/// The schema (namespace) the table belongs to, if the backend supports it.
pub schema: Option<String>,
/// The kind of table object.
pub table_type: TableType,
}
/// The kind of table object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TableType {
/// A regular base table.
Base,
/// A view.
View,
/// Something else (foreign table, materialised view, etc.).
Other(String),
}
impl From<&str> for TableType {
fn from(s: &str) -> Self {
match s.to_ascii_uppercase().as_str() {
"BASE TABLE" | "TABLE" => TableType::Base,
"VIEW" => TableType::View,
other => TableType::Other(other.to_string()),
}
}
}
/// A column in a table.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnInfo {
/// The column name.
pub name: String,
/// The 1-based position of the column in the table definition.
pub ordinal_position: u32,
/// The SQL data type name as reported by the backend.
pub data_type: String,
/// Whether the column accepts NULL values.
pub nullable: bool,
/// The column default expression, if any.
pub default: Option<String>,
/// Maximum character/byte length, if applicable.
pub max_length: Option<u64>,
/// Numeric precision, if applicable.
pub numeric_precision: Option<u32>,
/// Numeric scale, if applicable.
pub numeric_scale: Option<u32>,
}
/// An index on a table.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexInfo {
/// The index name.
pub name: String,
/// Ordered list of column names that form the index key.
pub columns: Vec<String>,
/// Whether the index enforces uniqueness.
pub unique: bool,
/// Whether this index is the table's primary key.
pub primary: bool,
}
/// A foreign key constraint.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ForeignKeyInfo {
/// The constraint name.
pub constraint_name: String,
/// The column on the referencing (child) table.
pub column: String,
/// The referenced (parent) table name.
pub foreign_table: String,
/// The referenced column in the parent table.
pub foreign_column: String,
/// Referential action on `UPDATE` of parent row (`CASCADE`, `SET NULL`, etc.).
///
/// `None` when the backend does not surface this information.
pub on_update: Option<String>,
/// Referential action on `DELETE` of parent row.
///
/// `None` when the backend does not surface this information.
pub on_delete: Option<String>,
}