Rust traits describing SQL-like objects.
This library provides a set of traits to abstract over SQL database schemas, allowing generic code to operate on database definitions regardless of the underlying representation (e.g., parsed SQL ASTs or active database connection metadata).
- Generic Schema Abstraction: Traits such as
DatabaseLike,TableLike,ColumnLike,ForeignKeyLike, andUniqueIndexLikedefine a common interface for schema introspection. - SQL Parser Integration: The
ParserDBstruct implements these traits usingsqlparser-rs, enabling the construction of an introspectable database model directly from SQL DDL strings. - Metadata Support: Comprehensive support for table attributes, indices, and constraints.
This crate is currently not published to crates.io. You can add it to your project via git:
[dependencies]
sql-traits = { git = "https://github.com/connetto-rs/sql-traits" }The following example demonstrates how to parse SQL CREATE TABLE statements and inspect the resulting schema model using ParserDB.
use sql_traits::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse SQL DDL into a database model
let db = ParserDB::parse::<GenericDialect>("
CREATE TABLE users (
id INT PRIMARY KEY,
username TEXT NOT NULL
);
")?;
// Retrieve a table by the name a statement writes for it, compared the way
// the engine compares it
let users_table = db
.table_by_target(TargetName::new("users", false), IdentifierCase::AsWritten)?
.expect("Table not found");
assert_eq!(users_table.table_name(), "users");
// Inspect columns
let columns: Vec<_> = users_table.columns(&db)?.collect();
assert_eq!(columns.len(), 2);
assert_eq!(columns[0].column_name(), "id");
assert_eq!(columns[1].column_name(), "username");
Ok(())
}sqlparserAST: Included in this crate (ParserDB), useful for static analysis and schema validation of SQL files.pg_diesel: An external, downstream crate that implements these traits for inspecting an existing PostgreSQL database.
This crate is not yet published to crates.io because we still need to finalize the upstream crate geometric-traits.