1use sea_orm::entity::prelude::*;
16use serde::{Deserialize, Serialize};
17
18use crate::{DatabaseId, ObjectId, SchemaId, UserId};
19
20#[derive(Clone, Debug, PartialEq, Eq, Copy, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
21#[sea_orm(rs_type = "String", db_type = "string(None)")]
22pub enum ObjectType {
23 #[sea_orm(string_value = "DATABASE")]
24 Database,
25 #[sea_orm(string_value = "SCHEMA")]
26 Schema,
27 #[sea_orm(string_value = "TABLE")]
28 Table,
29 #[sea_orm(string_value = "SOURCE")]
30 Source,
31 #[sea_orm(string_value = "SINK")]
32 Sink,
33 #[sea_orm(string_value = "VIEW")]
34 View,
35 #[sea_orm(string_value = "INDEX")]
36 Index,
37 #[sea_orm(string_value = "FUNCTION")]
38 Function,
39 #[sea_orm(string_value = "CONNECTION")]
40 Connection,
41 #[sea_orm(string_value = "SUBSCRIPTION")]
42 Subscription,
43 #[sea_orm(string_value = "SECRET")]
44 Secret,
45}
46
47impl ObjectType {
48 pub fn as_str(&self) -> &'static str {
49 match self {
50 ObjectType::Database => "database",
51 ObjectType::Schema => "schema",
52 ObjectType::Table => "table",
53 ObjectType::Source => "source",
54 ObjectType::Sink => "sink",
55 ObjectType::View => "view",
56 ObjectType::Index => "index",
57 ObjectType::Function => "function",
58 ObjectType::Connection => "connection",
59 ObjectType::Subscription => "subscription",
60 ObjectType::Secret => "secret",
61 }
62 }
63}
64
65#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
66#[sea_orm(table_name = "object")]
67pub struct Model {
68 #[sea_orm(primary_key)]
69 pub oid: ObjectId,
70 pub obj_type: ObjectType,
71 pub owner_id: UserId,
72 pub schema_id: Option<SchemaId>,
73 pub database_id: Option<DatabaseId>,
74 pub initialized_at: DateTime,
75 pub created_at: DateTime,
76 pub initialized_at_cluster_version: Option<String>,
77 pub created_at_cluster_version: Option<String>,
78}
79
80#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
81pub enum Relation {
82 #[sea_orm(has_many = "super::connection::Entity")]
83 Connection,
84 #[sea_orm(has_many = "super::database::Entity")]
85 Database,
86 #[sea_orm(has_many = "super::fragment::Entity")]
87 Fragment,
88 #[sea_orm(has_many = "super::function::Entity")]
89 Function,
90 #[sea_orm(has_many = "super::index::Entity")]
91 Index,
92 #[sea_orm(
93 belongs_to = "Entity",
94 from = "Column::DatabaseId",
95 to = "Column::Oid",
96 on_update = "NoAction",
97 on_delete = "Cascade"
98 )]
99 SelfRef2,
100 #[sea_orm(
101 belongs_to = "Entity",
102 from = "Column::SchemaId",
103 to = "Column::Oid",
104 on_update = "NoAction",
105 on_delete = "Cascade"
106 )]
107 SelfRef1,
108 #[sea_orm(
109 belongs_to = "super::database::Entity",
110 from = "Column::DatabaseId",
111 to = "super::database::Column::DatabaseId",
112 on_update = "NoAction",
113 on_delete = "NoAction"
114 )]
115 Database2,
116 #[sea_orm(has_many = "super::schema::Entity")]
117 Schema,
118 #[sea_orm(has_many = "super::sink::Entity")]
119 Sink,
120 #[sea_orm(has_many = "super::subscription::Entity")]
121 Subscription,
122 #[sea_orm(has_many = "super::source::Entity")]
123 Source,
124 #[sea_orm(has_many = "super::table::Entity")]
125 Table,
126 #[sea_orm(has_many = "super::streaming_job::Entity")]
127 StreamingJob,
128 #[sea_orm(
129 belongs_to = "super::user::Entity",
130 from = "Column::OwnerId",
131 to = "super::user::Column::UserId",
132 on_update = "NoAction",
133 on_delete = "Cascade"
134 )]
135 User,
136 #[sea_orm(has_many = "super::user_privilege::Entity")]
137 UserPrivilege,
138 #[sea_orm(has_many = "super::view::Entity")]
139 View,
140}
141
142impl Related<super::connection::Entity> for Entity {
143 fn to() -> RelationDef {
144 Relation::Connection.def()
145 }
146}
147
148impl Related<super::database::Entity> for Entity {
149 fn to() -> RelationDef {
150 Relation::Database.def()
151 }
152}
153
154impl Related<super::fragment::Entity> for Entity {
155 fn to() -> RelationDef {
156 Relation::Fragment.def()
157 }
158}
159
160impl Related<super::function::Entity> for Entity {
161 fn to() -> RelationDef {
162 Relation::Function.def()
163 }
164}
165
166impl Related<super::index::Entity> for Entity {
167 fn to() -> RelationDef {
168 Relation::Index.def()
169 }
170}
171
172impl Related<super::schema::Entity> for Entity {
173 fn to() -> RelationDef {
174 Relation::Schema.def()
175 }
176}
177
178impl Related<super::sink::Entity> for Entity {
179 fn to() -> RelationDef {
180 Relation::Sink.def()
181 }
182}
183
184impl Related<super::subscription::Entity> for Entity {
185 fn to() -> RelationDef {
186 Relation::Subscription.def()
187 }
188}
189
190impl Related<super::source::Entity> for Entity {
191 fn to() -> RelationDef {
192 Relation::Source.def()
193 }
194}
195
196impl Related<super::table::Entity> for Entity {
197 fn to() -> RelationDef {
198 Relation::Table.def()
199 }
200}
201
202impl Related<super::streaming_job::Entity> for Entity {
203 fn to() -> RelationDef {
204 Relation::StreamingJob.def()
205 }
206}
207
208impl Related<super::user::Entity> for Entity {
209 fn to() -> RelationDef {
210 Relation::User.def()
211 }
212}
213
214impl Related<super::user_privilege::Entity> for Entity {
215 fn to() -> RelationDef {
216 Relation::UserPrivilege.def()
217 }
218}
219
220impl Related<super::view::Entity> for Entity {
221 fn to() -> RelationDef {
222 Relation::View.def()
223 }
224}
225
226impl ActiveModelBehavior for ActiveModel {}