pub(crate) mod mysql;
pub(crate) mod psql;
pub(crate) mod sqlite;
use crate::config::{Config, Dialect};
use crate::error::{GenError, Result};
use crate::schema::Schema;
pub fn introspect(config: &Config) -> Result<Schema> {
let url = config.url.as_deref().ok_or_else(|| {
GenError::Config("no `url` in the config (and none given on the command line)".to_owned())
})?;
match config.dialect {
Dialect::Sqlite => sqlite::introspect(url),
Dialect::Psql => psql::introspect(url, &config.schema),
Dialect::Mysql => {
if config.schema != "public" {
return Err(GenError::Unsupported(format!(
"dialect = \"mysql\" ignores `schema` (here `{}`): a MySQL schema is a \
database, so name it in the connection URL instead",
config.schema
)));
}
mysql::introspect(url)
}
}
}
pub(crate) fn canonicalise(schema: &mut Schema) {
schema.tables.sort_by(|a, b| a.name.cmp(&b.name));
for t in &mut schema.tables {
t.foreign_keys.sort_by(|a, b| a.columns.cmp(&b.columns));
t.unique_keys.sort();
}
}