sea-schema-sync 0.17.0-rc.16

🌿 SQL schema definition and discovery
Documentation
use sea_query::{Condition, Expr, ExprTrait, Query, SelectStatement, SimpleExpr};

use super::Sqlite;
use super::query::{SqliteMaster, SqliteSchema};
use crate::probe::{Has, Schema, SchemaProbe};

impl SchemaProbe for Sqlite {
    fn get_current_schema() -> SimpleExpr {
        unimplemented!()
    }

    fn query_tables(&self) -> SelectStatement {
        Query::select()
            .expr_as(Expr::col(SqliteSchema::Name), Schema::TableName)
            .from(SqliteMaster)
            .cond_where(
                Condition::all()
                    .add(Expr::col(SqliteSchema::Type).eq("table"))
                    .add(Expr::col(SqliteSchema::Name).ne("sqlite_sequence")),
            )
            .take()
    }

    fn has_column<T, C>(&self, table: T, column: C) -> SelectStatement
    where
        T: AsRef<str>,
        C: AsRef<str>,
    {
        Query::select()
            .expr(Expr::cust_with_values(
                "COUNT(*) > 0 AS \"has_column\" FROM pragma_table_info(?)",
                [table.as_ref()],
            ))
            .and_where(Expr::col(SqliteSchema::Name).eq(column.as_ref()))
            .take()
    }

    fn has_index<T, C>(&self, table: T, index: C) -> SelectStatement
    where
        T: AsRef<str>,
        C: AsRef<str>,
    {
        Query::select()
            .expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
            .from(SqliteMaster)
            .cond_where(
                Condition::all()
                    .add(Expr::col(SqliteSchema::Type).eq("index"))
                    .add(Expr::col(SqliteSchema::TblName).eq(table.as_ref()))
                    .add(Expr::col(SqliteSchema::Name).eq(index.as_ref())),
            )
            .take()
    }
}