toasty-sql 0.8.0

SQL serialization layer for Toasty database drivers
Documentation
use super::{Ident, ToSql};

use crate::{serializer::Flavor, stmt};

impl ToSql for &stmt::ColumnDef {
    fn to_sql(self, f: &mut super::Formatter<'_>) {
        let name = Ident(&self.name);

        fmt!(f, name " " self.ty);

        if self.not_null {
            fmt!(f, " NOT NULL");
        }

        if self.auto_increment {
            match f.serializer.flavor {
                Flavor::Postgresql => fmt!(f, " GENERATED BY DEFAULT AS IDENTITY"),
                Flavor::Mysql => fmt!(f, " AUTO_INCREMENT"),
                Flavor::Sqlite => {
                    // Sqlite requires that only the primary key is auto
                    // increment. This is validated in ColumnsWithConstraints.
                    fmt!(f, " PRIMARY KEY AUTOINCREMENT")
                }
            }
        }

        if let Some(check) = &self.check {
            fmt!(f, " ");
            check.to_sql(f);
        }
    }
}

impl ToSql for &stmt::CheckConstraint {
    fn to_sql(self, f: &mut super::Formatter<'_>) {
        if let Some(name) = &self.name {
            fmt!(f, "CONSTRAINT " Ident(&name.0) " ");
        }

        fmt!(f, "CHECK (" self.expr ")");
    }
}