use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use crate::{DatabaseId, ObjectId, SchemaId, UserId};
#[derive(Clone, Debug, PartialEq, Eq, Copy, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "String", db_type = "string(None)")]
pub enum ObjectType {
#[sea_orm(string_value = "DATABASE")]
Database,
#[sea_orm(string_value = "SCHEMA")]
Schema,
#[sea_orm(string_value = "TABLE")]
Table,
#[sea_orm(string_value = "SOURCE")]
Source,
#[sea_orm(string_value = "SINK")]
Sink,
#[sea_orm(string_value = "VIEW")]
View,
#[sea_orm(string_value = "INDEX")]
Index,
#[sea_orm(string_value = "FUNCTION")]
Function,
#[sea_orm(string_value = "CONNECTION")]
Connection,
#[sea_orm(string_value = "SUBSCRIPTION")]
Subscription,
#[sea_orm(string_value = "SECRET")]
Secret,
}
impl ObjectType {
pub fn as_str(&self) -> &'static str {
match self {
ObjectType::Database => "database",
ObjectType::Schema => "schema",
ObjectType::Table => "table",
ObjectType::Source => "source",
ObjectType::Sink => "sink",
ObjectType::View => "view",
ObjectType::Index => "index",
ObjectType::Function => "function",
ObjectType::Connection => "connection",
ObjectType::Subscription => "subscription",
ObjectType::Secret => "secret",
}
}
}
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "object")]
pub struct Model {
#[sea_orm(primary_key)]
pub oid: ObjectId,
pub obj_type: ObjectType,
pub owner_id: UserId,
pub schema_id: Option<SchemaId>,
pub database_id: Option<DatabaseId>,
pub initialized_at: DateTime,
pub created_at: DateTime,
pub initialized_at_cluster_version: Option<String>,
pub created_at_cluster_version: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::connection::Entity")]
Connection,
#[sea_orm(has_many = "super::database::Entity")]
Database,
#[sea_orm(has_many = "super::fragment::Entity")]
Fragment,
#[sea_orm(has_many = "super::function::Entity")]
Function,
#[sea_orm(has_many = "super::index::Entity")]
Index,
#[sea_orm(
belongs_to = "Entity",
from = "Column::DatabaseId",
to = "Column::Oid",
on_update = "NoAction",
on_delete = "Cascade"
)]
SelfRef2,
#[sea_orm(
belongs_to = "Entity",
from = "Column::SchemaId",
to = "Column::Oid",
on_update = "NoAction",
on_delete = "Cascade"
)]
SelfRef1,
#[sea_orm(
belongs_to = "super::database::Entity",
from = "Column::DatabaseId",
to = "super::database::Column::DatabaseId",
on_update = "NoAction",
on_delete = "NoAction"
)]
Database2,
#[sea_orm(has_many = "super::schema::Entity")]
Schema,
#[sea_orm(has_many = "super::sink::Entity")]
Sink,
#[sea_orm(has_many = "super::subscription::Entity")]
Subscription,
#[sea_orm(has_many = "super::source::Entity")]
Source,
#[sea_orm(has_many = "super::table::Entity")]
Table,
#[sea_orm(has_many = "super::streaming_job::Entity")]
StreamingJob,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::OwnerId",
to = "super::user::Column::UserId",
on_update = "NoAction",
on_delete = "Cascade"
)]
User,
#[sea_orm(has_many = "super::user_privilege::Entity")]
UserPrivilege,
#[sea_orm(has_many = "super::view::Entity")]
View,
}
impl Related<super::connection::Entity> for Entity {
fn to() -> RelationDef {
Relation::Connection.def()
}
}
impl Related<super::database::Entity> for Entity {
fn to() -> RelationDef {
Relation::Database.def()
}
}
impl Related<super::fragment::Entity> for Entity {
fn to() -> RelationDef {
Relation::Fragment.def()
}
}
impl Related<super::function::Entity> for Entity {
fn to() -> RelationDef {
Relation::Function.def()
}
}
impl Related<super::index::Entity> for Entity {
fn to() -> RelationDef {
Relation::Index.def()
}
}
impl Related<super::schema::Entity> for Entity {
fn to() -> RelationDef {
Relation::Schema.def()
}
}
impl Related<super::sink::Entity> for Entity {
fn to() -> RelationDef {
Relation::Sink.def()
}
}
impl Related<super::subscription::Entity> for Entity {
fn to() -> RelationDef {
Relation::Subscription.def()
}
}
impl Related<super::source::Entity> for Entity {
fn to() -> RelationDef {
Relation::Source.def()
}
}
impl Related<super::table::Entity> for Entity {
fn to() -> RelationDef {
Relation::Table.def()
}
}
impl Related<super::streaming_job::Entity> for Entity {
fn to() -> RelationDef {
Relation::StreamingJob.def()
}
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl Related<super::user_privilege::Entity> for Entity {
fn to() -> RelationDef {
Relation::UserPrivilege.def()
}
}
impl Related<super::view::Entity> for Entity {
fn to() -> RelationDef {
Relation::View.def()
}
}
impl ActiveModelBehavior for ActiveModel {}