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