Skip to content

Commit

Permalink
✨ NEW: golongan kata and cakupan filter in view
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed Sep 16, 2024
1 parent 8effd18 commit 7cf906c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
36 changes: 32 additions & 4 deletions backend/database/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::io::interface::IntoViewMap;
/// ```
/// will bind "lemma_1" to following query and calls it:
///```sql
#[doc = include_str!("../transactions/select_lemma_with_konsep_view.sql")]
#[doc = include_str!("../transactions/select_lemma_in_konsep_view.sql")]
///```
#[derive(Debug, Clone, sqlx::FromRow, Default)]
pub struct LemmaWithKonsepView {
Expand Down Expand Up @@ -64,11 +64,39 @@ impl LemmaWithKonsepView {
pub async fn query_lemma(
lemma: String,
pool: &sqlx::Pool<<Self as crate::io::interface::View>::SOURCE>,
) -> sqlx::Result<Vec<Self>> {
sqlx::query_file_as!(Self, "transactions/select_lemma_in_konsep_view.sql", lemma)
.fetch_all(pool)
.await
}

/// Query a single lemma using golongan kata with its associated konseps and attachments.
pub async fn query_lemma_with_golongan_kata(
lemma: Option<String>,
golongan_kata: String,
pool: &sqlx::Pool<<Self as crate::io::interface::View>::SOURCE>,
) -> sqlx::Result<Vec<Self>> {
sqlx::query_file_as!(
Self,
"transactions/select_lemma_with_golongan_kata_in_konsep_view.sql",
lemma,
golongan_kata
)
.fetch_all(pool)
.await
}

/// Query a single lemma using golongan kata with its associated konseps and attachments.
pub async fn query_lemma_with_cakupan(
lemma: Option<String>,
cakupan: String,
pool: &sqlx::Pool<<Self as crate::io::interface::View>::SOURCE>,
) -> sqlx::Result<Vec<Self>> {
sqlx::query_file_as!(
Self,
"transactions/select_lemma_with_konsep_view.sql",
lemma
"transactions/select_lemma_with_cakupan_in_konsep_view.sql",
lemma,
cakupan
)
.fetch_all(pool)
.await
Expand All @@ -82,7 +110,7 @@ impl LemmaWithKonsepView {
// TODO: And maybe sort by reverse chronology
sqlx::query_file_as!(
Self,
"transactions/select_lemma_with_konsep_view.sql",
"transactions/select_lemma_in_konsep_view.sql",
None::<String>
)
.fetch_all(pool)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
WITH items AS (
WITH params AS (SELECT ? as lemma, ? as cakupan)
SELECT
lemma.nama AS lemma
FROM lemma, params
LEFT JOIN konsep ON konsep.lemma_id = lemma.id
LEFT JOIN cakupan_x_konsep as cxk ON cxk.konsep_id = konsep.id
LEFT JOIN cakupan ON cakupan.id = cxk.cakupan_id
WHERE
(CASE
WHEN params.lemma IS NOT NULL THEN lemma.nama = params.lemma
ELSE lemma.nama IS NOT NULL
END) AND
cakupan.nama = params.cakupan
)
-- THE RESULTING TABLE THAT IS DESERIALIZED INTO STRUCT
SELECT
lemma.nama AS lemma,
konsep.keterangan AS konsep,
golongan_kata.nama AS golongan_kata,
cakupan.nama AS cakupan,
kata_asing.nama AS kata_asing,
kata_asing.bahasa AS bahasa_asing,
lemma.id AS l_id,
konsep.id AS k_id
FROM lemma, items
LEFT JOIN konsep ON konsep.lemma_id = lemma.id
LEFT JOIN golongan_kata ON konsep.golongan_id = golongan_kata.id
LEFT JOIN cakupan_x_konsep as cxk ON cxk.konsep_id = konsep.id
LEFT JOIN cakupan ON cakupan.id = cxk.cakupan_id
LEFT JOIN kata_asing_x_konsep as kaxk ON kaxk.konsep_id = konsep.id
LEFT JOIN kata_asing ON kata_asing.id = kaxk.kata_asing_id
WHERE
lemma.nama = items.lemma

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
WITH params AS (SELECT ? as lemma, ? as golkat)
-- THE RESULTING TABLE THAT IS DESERIALIZED INTO STRUCT
SELECT
lemma.nama AS lemma,
konsep.keterangan AS konsep,
golongan_kata.nama AS golongan_kata,
cakupan.nama AS cakupan,
kata_asing.nama AS kata_asing,
kata_asing.bahasa AS bahasa_asing,
lemma.id AS l_id,
konsep.id AS k_id
FROM lemma, params
LEFT JOIN konsep ON konsep.lemma_id = lemma.id
LEFT JOIN golongan_kata ON konsep.golongan_id = golongan_kata.id
LEFT JOIN cakupan_x_konsep as cxk ON cxk.konsep_id = konsep.id
LEFT JOIN cakupan ON cakupan.id = cxk.cakupan_id
LEFT JOIN kata_asing_x_konsep as kaxk ON kaxk.konsep_id = konsep.id
LEFT JOIN kata_asing ON kata_asing.id = kaxk.kata_asing_id
WHERE
(CASE
WHEN params.lemma IS NOT NULL THEN lemma.nama = params.lemma
ELSE lemma.nama IS NOT NULL
END) AND
golongan_kata.nama = params.golkat

0 comments on commit 7cf906c

Please sign in to comment.