sea_schema/postgres/query/
table.rs1use super::{InformationSchema, SchemaQueryBuilder, select_base_table_and_view};
2use crate::sqlx_types::SqlxRow;
3use sea_query::{DynIden, Expr, ExprTrait, Iden, Query, SelectStatement};
4
5#[derive(Debug, sea_query::Iden)]
6pub enum TablesFields {
8 TableCatalog,
9 TableSchema,
10 TableName,
11 TableType,
12 UserDefinedTypeSchema,
13 UserDefinedTypeName,
14 IsInsertableInto,
16 IsTyped,
17}
18
19#[derive(Debug, sea_query::Iden)]
20pub enum TableType {
21 #[iden = "BASE TABLE"]
22 BaseTable,
23 #[iden = "VIEW"]
24 View,
25 #[iden = "FOREIGN"]
26 Foreign,
27 #[iden = "LOCAL TEMPORARY"]
28 Temporary,
29}
30
31#[derive(Debug, Default)]
32pub struct TableQueryResult {
33 pub table_name: String,
34 pub user_defined_type_schema: Option<String>,
35 pub user_defined_type_name: Option<String>,
36}
37
38impl SchemaQueryBuilder {
39 pub fn query_tables(&self, schema: DynIden) -> SelectStatement {
40 Query::select()
41 .columns(vec![
42 TablesFields::TableName,
43 TablesFields::UserDefinedTypeSchema,
44 TablesFields::UserDefinedTypeName,
45 ])
46 .from((InformationSchema::Schema, InformationSchema::Tables))
47 .and_where(Expr::col(TablesFields::TableSchema).eq(schema.to_string()))
48 .and_where(Expr::col(TablesFields::TableType).eq(TableType::BaseTable.to_string()))
49 .and_where(
50 Expr::col(TablesFields::TableName).not_in_subquery(select_base_table_and_view()),
51 )
52 .take()
53 }
54}
55
56#[cfg(feature = "sqlx-postgres")]
57impl From<SqlxRow> for TableQueryResult {
58 fn from(row: SqlxRow) -> Self {
59 use crate::sqlx_types::Row;
60 let row = row.postgres();
61 Self {
62 table_name: row.get(0),
63 user_defined_type_schema: row.get(1),
64 user_defined_type_name: row.get(2),
65 }
66 }
67}
68
69#[cfg(not(feature = "sqlx-postgres"))]
70impl From<SqlxRow> for TableQueryResult {
71 fn from(_: SqlxRow) -> Self {
72 Self::default()
73 }
74}