Type Definitions (def)
Relevant source files
The following files were used as context for generating this wiki page:
The def module serves as the foundational layer of SeaSchema, providing the data structures and type definitions required to represent database schemas in a language-native format. It maps database objects—such as tables, columns, indexes, and foreign keys—directly to Rust structs and enums. This allows for a database-agnostic representation (where applicable) or a highly specialized representation for specific backends like MySQL, Postgres, and SQLite.
The primary purpose of these definitions is to facilitate schema discovery (mapping raw database metadata into Rust objects) and schema writing (converting Rust objects back into SQL statements via SeaQuery). By maintaining these strictly typed structures, the library ensures that schema manipulations are validated at compile time.
Sources: README.md:15-20, src/lib.rs:4-15
Core Schema Components
The schema representation is hierarchical, starting from the top-level Schema or SystemInfo and descending into specific table components.
Schema Hierarchy
The following diagram illustrates the relationship between the major data structures within the def module.
The diagram shows how a single Schema encapsulates the entire database state, while TableDef acts as the container for all relational constraints and metadata.
Sources: src/mysql/writer/table.rs:6-23, tests/discovery/mysql/schema.rs:1-30
Key Data Structures
| Structure | Description | Relevant Fields |
|---|---|---|
SystemInfo | Contains metadata about the database engine and version. | version, system, suffix |
TableInfo | High-level table metadata. | name, engine, char_set, collation |
ColumnInfo | Detailed specification of a single table column. | name, col_type, null, extra |
IndexInfo | Definition of a database index. | name, unique, idx_type, parts |
ForeignKeyInfo | Definition of a relationship between tables. | referenced_table, on_update, on_delete |
Sources: src/mysql/writer/table.rs:6-23, src/mysql/parser/system.rs:18-30
Column and Data Type Definitions
The def module includes comprehensive enums for SQL data types, particularly specialized for the MySQL backend. Types are often accompanied by attributes that define their constraints, such as length, precision, or character sets.
MySQL Type Mapping
The Type enum (or ColumnType in some contexts) represents the variety of data types available in the database.
- Numeric Types:
TinyInt,SmallInt,Int,BigInt,Decimal,Float,Double,Bit. - String Types:
Char,Varchar,Text,TinyText,MediumText,LongText,NChar,NVarchar. - Temporal Types:
Date,Time,DateTime,Timestamp,Year. - Blob/Binary Types:
Blob,TinyBlob,MediumBlob,LongBlob,Binary,Varbinary. - Special Types:
Enum,Set,Json,Geometry,Point,LineString,Polygon.
Sources: src/mysql/writer/types.rs:10-100, src/mysql/writer/column.rs:49-160
Type Attributes
Attributes are used to further refine the data types.
- NumericAttr: Handles
maximum(display width or precision),decimal(scale),unsigned, andzero_fill. - StringAttr: Handles
length,charset, andcollation. - TimeAttr: Handles
fractionalseconds precision.
Sources: src/mysql/writer/types.rs:141-175, src/mysql/writer/column.rs:188-208
Index and Foreign Key Definitions
Indexes and Foreign Keys are defined through complex structures that allow for multi-column configurations and specific database behaviors.
Index Configuration
Indexes are composed of one or more IndexPart objects. Each part specifies the column name, the sort order (Ascending, Descending, Unordered), and optional sub-part lengths (prefix indexing).
The flowchart describes the internal composition of an index definition, highlighting the flexibility in column ordering and prefixing.
Sources: src/mysql/writer/index.rs:6-30, src/mysql/writer/table.rs:175-210
Foreign Key Actions
Foreign keys track both the local columns and the referenced remote table/columns. They include enums for referential integrity actions:
RestrictCascadeSetNullNoActionSetDefault
Sources: src/mysql/writer/table.rs:211-235
System Information and Versioning
SeaSchema tracks system-level information to handle version-specific SQL syntax. The SystemInfo struct stores the parsed version of the database.
Version Parsing Logic
MySQL version strings (e.g., 8.0.23-0ubuntu0.20.04.1) are decomposed into a u32 numeric representation for easy comparison.
// Logic from src/mysql/parser/system.rs
pub fn parse_version_number(string: &str) -> u32 {
let mut number: u32 = 0;
let numbers: Vec<&str> = string.split('.').collect();
if numbers.len() > 0 {
number += numbers[0].parse::<u32>().unwrap() * 10000
}
if numbers.len() > 1 {
number += numbers[1].parse::<u32>().unwrap() * 100
}
if numbers.len() > 2 {
number += numbers[2].parse::<u32>().unwrap()
}
number
}
Example: Version "5.1.10" is converted to the integer 50110.
Sources: src/mysql/parser/system.rs:32-48
Summary
The def module provides a robust, type-safe blueprint of a database schema. By encapsulating complex SQL concepts into Rust structures like TableDef, ColumnInfo, and specialized attribute types (NumericAttr, StringAttr), it enables the rest of the SeaSchema library to perform discovery and synchronization tasks with high accuracy across different database backends.