1#![allow(unused)]
2#![doc = "データベースのスキーマ定義"]
3pub mod app_global;
4mod binder_helper;
5mod crud;
6mod from_row;
7mod load;
8mod prefixed_deserializer;
9#[cfg(test)]
10mod tests;
11
12use chrono::{DateTime, Utc};
13use indexmap::IndexMap;
14use sqlx::{Acquire, Database, FromRow, Row};
15use std::fmt::{Display, Formatter};
16
17pub const MAX_BIND_COUNT: usize = 30000;
20
21#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
22pub struct ReturningId {
24 pub id: i64,
25}
26
27#[derive(Default, Clone, Debug, FromRow, derive_more::Eq, derive_more::PartialEq)]
30pub struct BackgroundInfo {
32 pub id: i64,
33 pub body: String,
34 #[eq(skip)]
35 pub created_at: DateTime<Utc>,
36 #[eq(skip)]
37 pub updated_at: DateTime<Utc>,
38 #[sqlx(skip)]
39 #[eq(skip)]
40 pub references: Option<Vec<BackgroundReference>>,
41}
42
43#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
44pub struct Tag {
46 pub id: i64,
47 pub name: String,
48 pub memo: Option<String>,
49}
50
51#[derive(Default, Clone, Debug, Eq, PartialEq)]
52pub struct Publisher {
54 pub id: i64,
55 pub name: String,
56 pub memo: Option<String>,
57}
58
59#[derive(Default, Clone, Debug, derive_more::Eq, derive_more::PartialEq)]
62pub struct Bibliography {
64 pub id: i64,
65 pub isbn: Option<String>,
66 pub url: Option<String>,
67 pub title: String,
68 pub detail: Option<String>,
69 pub authors: Vec<BibliographyAuthor>,
70 pub publisher: Option<Publisher>,
71 pub publication_date: Option<String>,
72 #[eq(skip)]
73 pub created_at: DateTime<Utc>,
74 #[eq(skip)]
75 pub updated_at: DateTime<Utc>,
76 #[eq(skip)]
77 pub tmp_registration_id: Option<usize>,
78}
79
80#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
81pub struct BibliographyAuthor {
83 pub id: i64,
84 pub name: String,
85 pub memo: Option<String>,
86}
87
88#[derive(Clone, Debug, Eq, PartialEq)]
89pub enum ItemType {
91 Headline(Option<Headline>),
93 Paragraph(Option<Paragraph>),
95}
96
97impl ItemType {
98 pub fn headline_or_none(self) -> Option<Headline> {
100 match self {
101 ItemType::Headline(v) => v,
102 ItemType::Paragraph(_) => None,
103 }
104 }
105
106 pub fn paragraph_or_none(self) -> Option<Paragraph> {
108 match self {
109 ItemType::Headline(_) => None,
110 ItemType::Paragraph(v) => v,
111 }
112 }
113}
114
115impl Default for ItemType {
116 fn default() -> Self {
117 Self::Headline(None)
118 }
119}
120
121impl AsRef<str> for ItemType {
122 fn as_ref(&self) -> &str {
123 match self {
124 ItemType::Headline(_) => "headline",
125 ItemType::Paragraph(_) => "paragraph",
126 }
127 }
128}
129
130impl From<ItemType> for String {
131 fn from(value: ItemType) -> Self {
132 value.to_string()
133 }
134}
135
136impl Display for ItemType {
137 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
138 write!(f, "{}", self.as_ref())
139 }
140}
141
142#[derive(Default, Clone, Debug, derive_more::Eq, derive_more::PartialEq)]
145pub struct Item {
147 pub id: i64,
148 #[eq(skip)]
149 pub created_at: i64,
150 pub item_type: ItemType,
151 pub title: String,
152 #[eq(skip)]
153 pub references: Option<Vec<ItemReference>>,
154 #[eq(skip)]
155 pub tags: Option<Vec<Tag>>,
156 #[eq(skip)]
157 pub background_info_list: Option<Vec<BackgroundInfo>>,
158 #[eq(skip)]
159 pub tasks: Option<Vec<Task>>,
160}
161
162#[derive(Default, Clone, Debug, FromRow, derive_more::Eq, derive_more::PartialEq)]
165pub struct Headline {
167 pub id: i64,
168 pub item_id: i64,
169 pub parent_id: Option<i64>,
170 pub headline_pos: Option<i64>,
171 #[sqlx(skip)]
172 #[eq(skip)]
173 pub paragraph: Option<Vec<Paragraph>>,
174}
175
176#[derive(Default, Clone, Debug, Eq, PartialEq)]
177pub struct HeadlineChildren {
179 pub parent: Headline,
180 pub children: IndexMap<i64, Vec<Headline>>,
181}
182
183#[derive(Default, Clone, Debug, FromRow, derive_more::Eq, derive_more::PartialEq)]
186pub struct Draft {
188 pub id: i64,
189 pub paragraph_id: i64,
190 pub draft_pos: Option<i64>,
191 pub title: String,
192 pub body: String,
193 #[eq(skip)]
194 pub created_at: DateTime<Utc>,
195 #[eq(skip)]
196 pub updated_at: DateTime<Utc>,
197}
198
199#[derive(Default, Clone, Debug, derive_more::Eq, derive_more::PartialEq)]
202pub struct Paragraph {
204 pub id: i64,
205 pub item_id: i64,
206 pub headline: Headline,
207 pub accepted_draft: Option<Draft>,
208 pub paragraph_pos: Option<i64>,
209 #[eq(skip)]
210 pub draft: Option<Vec<Draft>>,
211 #[eq(skip)]
212 pub summary: Option<Vec<ParagraphSummary>>,
213}
214
215#[derive(Default, Clone, Debug, FromRow, derive_more::Eq, derive_more::PartialEq)]
218pub struct ParagraphSummary {
220 pub id: i64,
221 pub paragraph_id: i64,
222 pub title: String,
223 pub detail: String,
224 pub summary_pos: Option<i64>,
225 #[eq(skip)]
226 pub created_at: DateTime<Utc>,
227 #[eq(skip)]
228 pub updated_at: DateTime<Utc>,
229}
230
231#[derive(Default, Clone, Debug, Eq, PartialEq)]
232pub struct BackgroundReference {
234 pub id: i64,
235 pub background_info_id: i64,
236 pub bibliography: Bibliography,
237 pub location: String,
238}
239
240#[derive(Default, Clone, Debug, Eq, PartialEq)]
241pub struct ItemReference {
243 pub id: i64,
244 pub item_id: i64,
245 pub bibliography: Bibliography,
246 pub location: String,
247}
248
249#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
250pub struct TaskCategory {
252 pub id: i64,
253 pub name: String,
254 pub autocomplete_paragraph_link: bool,
255}
256
257#[derive(Default, Clone, Debug, Eq, PartialEq)]
258pub struct TaskTemplate {
260 pub id: i64,
261 pub task_category: Option<TaskCategory>,
262 pub title: String,
263 pub detail: Option<String>,
264}
265
266#[derive(Default, Clone, Debug, Eq, PartialEq)]
267pub struct Task {
269 pub id: i64,
270 pub item_id: i64,
271 pub task_category: Option<TaskCategory>,
272 pub title: String,
273 pub detail: Option<String>,
274 pub task_pos: Option<i64>,
275 pub is_finished: bool,
276}
277
278#[derive(Default, Clone, Debug, Eq, PartialEq)]
279pub struct ParagraphLink {
281 pub id: i64,
282 pub from_paragraph: Paragraph,
283 pub to_paragraph: Paragraph,
284 pub task: Option<Task>,
285 pub comment: Option<String>,
286}
287
288#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
289pub struct Setting {
291 pub id: i64,
292 pub setting_key: String,
293 pub setting_value: Option<String>,
294}
295
296#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
297pub struct RelBibliographyAuthor {
299 pub id: i64,
300 pub bibliography_id: i64,
301 pub bibliography_author_id: i64,
302}
303
304#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
305pub struct RelTagAndItem {
307 pub id: i64,
308 pub item_id: i64,
309 pub tag_id: i64,
310}
311
312#[derive(Default, Clone, Debug, FromRow, Eq, PartialEq)]
313pub struct RelBackgroundAndItem {
315 pub id: i64,
316 pub item_id: i64,
317 pub background_info_id: i64,
318}