Make Rust models for database schema

This commit is contained in:
2023-01-03 16:11:43 +01:00
parent a9e300ede2
commit 60988cff24
10 changed files with 142 additions and 7 deletions

2
src/db/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod models;
pub mod schema;

View File

@@ -0,0 +1,53 @@
use diesel::prelude::*;
use super::super::schema::{languages, langandagents};
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Release"]
pub enum Release {
Public,
NonCommercial,
Research,
Private
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Dictgenre"]
pub enum DictGenre {
General,
Learning,
Etymology,
Specialized,
Historical,
Orthography,
Terminology
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Agentlanguagerelation"]
pub enum AgentLanguageRelation {
Publisher,
Author
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct Language {
release: Release,
created: chrono::NaiveDateTime,
name: String,
owner: String,
targetlanguage: Vec<String>,
genre: Vec<DictGenre>,
native: Option<String>,
abstract_: Option<String>,
description: Option<String>,
rights: Option<String>,
license: Option<String>,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = langandagents)]
pub struct LangAndAgent {
id: i32,
agent: String,
language: String,
}

3
src/db/models/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod users;
pub mod languages;
pub mod words;

16
src/db/models/users.rs Normal file
View File

@@ -0,0 +1,16 @@
use diesel::prelude::*;
use super::super::schema::{userfollows, users};
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct User {
pub id: String,
pub username: String,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = userfollows)]
pub struct UserFollow {
pub id: i32,
pub follower: String,
pub following: String,
}

56
src/db/models/words.rs Normal file
View File

@@ -0,0 +1,56 @@
use diesel::prelude::*;
use super::super::schema::{wordrelation, words};
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Wordrelationship"]
pub enum WordRelationship {
Definition,
Related
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Partofspeech"]
pub enum PartOfSpeech {
Adjective,
Adposition,
Adverb,
Auxilliary,
CoordConj,
Determiner,
Interjection,
Noun,
Numeral,
Particle,
Pronoun,
ProperNoun,
Punctuation,
SubjConj,
Symbol,
Verb,
Other
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
struct Word {
norm: String,
native: Option<String>,
lemma: Option<String>,
language: String,
partofspeech: PartOfSpeech,
audio: Option<String>,
video: Option<String>,
image: Option<String>,
description: Option<String>,
etymology: Option<String>,
lusage: Option<String>,
morphology: Option<String>,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = wordrelation)]
struct WordRelation {
id: i32,
wordsource: String,
wordtarget: String,
relationship: WordRelationship,
}

117
src/db/schema.rs Normal file
View File

@@ -0,0 +1,117 @@
// @generated automatically by Diesel CLI.
pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "agentlanguagerelation"))]
pub struct Agentlanguagerelation;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "dictgenre"))]
pub struct Dictgenre;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "partofspeech"))]
pub struct Partofspeech;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "release"))]
pub struct Release;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "wordrelationship"))]
pub struct Wordrelationship;
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::Agentlanguagerelation;
langandagents (id) {
id -> Int4,
agent -> Varchar,
language -> Varchar,
relationship -> Agentlanguagerelation,
}
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::Release;
use super::sql_types::Dictgenre;
languages (name) {
name -> Varchar,
native -> Nullable<Varchar>,
release -> Release,
targetlanguage -> Array<Nullable<Text>>,
genre -> Array<Nullable<Dictgenre>>,
#[sql_name = "abstract"]
abstract_ -> Nullable<Text>,
created -> Timestamp,
description -> Nullable<Text>,
rights -> Nullable<Text>,
license -> Nullable<Text>,
owner -> Varchar,
}
}
diesel::table! {
userfollows (id) {
id -> Int4,
follower -> Varchar,
following -> Varchar,
}
}
diesel::table! {
users (id) {
id -> Varchar,
username -> Varchar,
}
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::Wordrelationship;
wordrelation (id) {
id -> Int4,
wordsource -> Varchar,
wordtarget -> Varchar,
relationship -> Wordrelationship,
}
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::Partofspeech;
words (norm) {
norm -> Varchar,
native -> Nullable<Varchar>,
lemma -> Nullable<Varchar>,
language -> Varchar,
partofspeech -> Partofspeech,
audio -> Nullable<Varchar>,
video -> Nullable<Varchar>,
image -> Nullable<Varchar>,
description -> Nullable<Text>,
etymology -> Nullable<Text>,
lusage -> Nullable<Text>,
morphology -> Nullable<Text>,
}
}
diesel::joinable!(langandagents -> languages (language));
diesel::joinable!(langandagents -> users (agent));
diesel::joinable!(languages -> users (owner));
diesel::joinable!(words -> languages (language));
diesel::allow_tables_to_appear_in_same_query!(
langandagents,
languages,
userfollows,
users,
wordrelation,
words,
);