Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poc/new er model #1

Merged
merged 3 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion migrations/2022-07-31-130130_app/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ CREATE TABLE app (
try_exec VARCHAR,
icon_path VARCHAR NULL,

UNIQUE(id, path)
UNIQUE(id)
UNIQUE (path)
)
6 changes: 5 additions & 1 deletion migrations/2022-07-31-145605_comments/up.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
CREATE TABLE comments (
id INTEGER PRIMARY KEY NOT NULL,
app_id INTEGER NOT NULL,
locale_id INTEGER NOT NULL,
title VARCHAR NOT NULL,
lang VARCHAR NULL,

UNIQUE(id)
UNIQUE(app_id, title)
UNIQUE(locale_id, title)

FOREIGN KEY (app_id) REFERENCES app (id)
FOREIGN KEY (locale_id) REFERENCES locale (id)
)
5 changes: 4 additions & 1 deletion migrations/2022-07-31-145614_keywords/up.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
CREATE TABLE keywords (
id INTEGER PRIMARY KEY NOT NULL,
app_id INTEGER NOT NULL,
locale_id INTEGER NOT NULL,
key VARCHAR NOT NULL,
lang VARCHAR NULL,

UNIQUE(id)
UNIQUE(app_id, key)

FOREIGN KEY (app_id) REFERENCES app (id)
FOREIGN KEY (locale_id) REFERENCES locale (id)
)
1 change: 1 addition & 0 deletions migrations/2022-11-26-183044_locale/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE locale;
7 changes: 7 additions & 0 deletions migrations/2022-11-26-183044_locale/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE locale (
id INTEGER PRIMARY KEY NOT NULL,
key VARCHAR NOT NULL,

UNIQUE(id)
UNIQUE (key)
)
1 change: 1 addition & 0 deletions migrations/2022-11-26-190715_app_locale/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE app_locale
9 changes: 9 additions & 0 deletions migrations/2022-11-26-190715_app_locale/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE app_locale (
id INTEGER PRIMARY KEY NOT NULL,
app_id INTEGER NOT NULL,
locale_id INTEGER NOT NULL,

UNIQUE(id)
FOREIGN KEY (app_id) REFERENCES app (id)
FOREIGN KEY (locale_id) REFERENCES locale (id)
)
295 changes: 244 additions & 51 deletions src/db/populate.rs

Large diffs are not rendered by default.

63 changes: 33 additions & 30 deletions src/db/search.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use diesel::debug_query;
use diesel::sqlite::Sqlite;
use crate::diesel::JoinOnDsl;

use diesel::{
BoolExpressionMethods, ExpressionMethods, NullableExpressionMethods, QueryDsl, RunQueryDsl,
BoolExpressionMethods, ExpressionMethods, QueryDsl, RunQueryDsl,
TextExpressionMethods,
};

Expand All @@ -15,7 +16,7 @@ pub trait SearchDb {
text: &str,
locale: &str,
limit: u8,
) -> Result<Vec<Option<SearchResult>>, diesel::result::Error>;
) -> Result<Vec<SearchResult>, diesel::result::Error>;
}

impl SearchDb for DesktopDDb {
Expand All @@ -24,8 +25,8 @@ impl SearchDb for DesktopDDb {
text: &str,
locale: &str,
limit: u8,
) -> Result<Vec<Option<SearchResult>>, diesel::result::Error> {
use crate::schema::{app, comments, keywords};
) -> Result<Vec<SearchResult>, diesel::result::Error> {
use crate::schema::{app, app_locale, locale, comments, keywords};

let selection = (
app::title,
Expand All @@ -52,41 +53,43 @@ impl SearchDb for DesktopDDb {
let location = locale.get(0..5).unwrap_or("en_EN");

let query = app::dsl::app
.left_join(keywords::dsl::keywords)
.inner_join(comments::dsl::comments)
.filter(
app::title
.like(format!("{}%", text))
.or(
comments::title
.like(format!("%{}%", text))
.and(
comments::lang.eq(lang)
.or(
comments::lang.eq(location)
)
)
.inner_join(
app_locale::dsl::app_locale.on(app_locale::app_id.eq(app::id))
)
.inner_join(
locale::dsl::locale.on(locale::id.eq(app_locale::locale_id))
)
.inner_join(keywords::dsl::keywords)
.inner_join(comments::dsl::comments.on(
comments::app_id.eq(app::id)
.and(
comments::locale_id.eq(locale::id)
)
.or(
keywords::key
.like(format!("{}%", text))
.and(
keywords::lang.eq(lang)
.or(
keywords::lang.eq(location)
)
)
)
)
.filter(
keywords::key
.like(format!("%{}%", text))
.and(
locale::key.eq(lang)
.or(
locale::key.eq(location)
)
.or(
locale::key.eq("__NO_LOCALE__")
)
)
)
//.limit(limit.into())
.select(selection.nullable())
.group_by(selection);
.select(selection)
.group_by(selection)
;

if self.debug {
let sql_debug = debug_query::<Sqlite, _>(&query);
println!("{}", sql_debug);
}

query.load::<Option<SearchResult>>(&mut self.connection)
query.load::<SearchResult>(&mut self.connection)
}
}
25 changes: 20 additions & 5 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::schema::{app, comments, keywords};
use crate::schema::{app, comments, keywords, locale, app_locale};

#[derive(Debug, Queryable)]
pub struct SearchResult {
pub title: String,
pub path: String,
pub generic_title: Option<String>,
pub exec: String,
pub exec: Option<String>,
pub try_exec: Option<String>,
pub icon_path: Option<String>,

pub comment: Option<String>,
pub comment: String,
}

#[derive(Insertable)]
Expand All @@ -23,18 +23,33 @@ pub struct NewApp<'a> {
pub icon_path: Option<&'a str>,
}

#[derive(Insertable)]
#[diesel(table_name = app_locale)]
pub struct NewAppLocale {
pub app_id: i32,
pub locale_id: i32,
}

#[derive(Insertable)]
#[diesel(table_name = locale)]
pub struct NewLocale<'a> {
pub key: &'a str,
}

#[derive(Insertable)]
#[diesel(table_name = comments)]
pub struct NewComments<'a> {
pub title: &'a str,

pub app_id: i32,
pub lang: Option<&'a str>,
pub locale_id: i32,
}

#[derive(Insertable)]
#[diesel(table_name = keywords)]
pub struct NewKeywords<'a> {
pub key: &'a str,

pub app_id: i32,
pub lang: Option<&'a str>,
pub locale_id: i32,
}
45 changes: 35 additions & 10 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
table! {
app (id) {
id -> Int4,
id -> Integer,
title -> Text,
path -> Text,
generic_title -> Nullable<Text>,
exec -> Text,
exec -> Nullable<Text>,
try_exec -> Nullable<Text>,
icon_path -> Nullable<Text>,
}
}

table! {
app_locale (id) {
id -> Integer,
app_id -> Integer,
locale_id -> Integer,
}
}

table! {
comments (id) {
id -> Int4,
app_id -> Int4,
title -> Nullable<Text>,
lang -> Text,
id -> Integer,
app_id -> Integer,
locale_id -> Integer,
title -> Text,
}
}

table! {
keywords (id) {
id -> Int4,
app_id -> Int4,
id -> Integer,
app_id -> Integer,
locale_id -> Integer,
key -> Text,
}
}

table! {
locale (id) {
id -> Integer,
key -> Text,
lang -> Nullable<Text>,
}
}

joinable!(app_locale -> app (app_id));
joinable!(app_locale -> locale (locale_id));
joinable!(comments -> app (app_id));
joinable!(comments -> locale (locale_id));
joinable!(keywords -> app (app_id));
joinable!(keywords -> locale (locale_id));

allow_tables_to_appear_in_same_query!(app, comments, keywords,);
allow_tables_to_appear_in_same_query!(
app,
app_locale,
comments,
keywords,
locale,
);