song_model is using sqlx now

This commit is contained in:
Chris Cochrun 2024-09-24 14:19:57 -05:00
parent bfb723a413
commit 0a1800ad71

View file

@ -290,15 +290,15 @@ impl Default for Song {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct SongModelRust<'a> { pub struct SongModelRust {
count: i32, count: i32,
highest_id: i32, highest_id: i32,
songs: Vec<Song>, songs: Vec<Song>,
inner_songs: Vec<Song>, inner_songs: Vec<Song>,
db: Option<&'a SqliteConnection>, db: SqliteConnection,
} }
impl Default for SongModelRust<'a> { impl Default for SongModelRust {
fn default() -> Self { fn default() -> Self {
Self { Self {
count: 0, count: 0,
@ -313,13 +313,7 @@ impl Default for SongModelRust<'a> {
let mut db_url = String::from("sqlite://"); let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap()); db_url.push_str(data.to_str().unwrap());
rt.block_on(async { rt.block_on(async {
match SqliteConnection::connect(&db_url).await { SqliteConnection::connect(&db_url).await.expect("problems")
Ok(db) => Some(&db),
Err(e) => {
error!("Error connecting to db: {e}");
None
}
}
}) })
} }
} }
@ -347,7 +341,7 @@ impl song_model::SongModel {
let thread = self.qt_thread(); let thread = self.qt_thread();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query_as!(Song, r#"SELECT vorder as "verse_order!", fontSize as "font_size!: i32", backgroundType as "background_type!", horizontalTextAlignment as "horizontal_text_alignment!", verticalTextAlignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs"#).fetch_all(&mut self.as_mut().rust_mut().db.unwrap()).await; let result = query_as!(Song, r#"SELECT vorder as "verse_order!", fontSize as "font_size!: i32", backgroundType as "background_type!", horizontalTextAlignment as "horizontal_text_alignment!", verticalTextAlignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs"#).fetch_all(&mut self.as_mut().rust_mut().db).await;
match result { match result {
Ok(s) => s.into_iter().for_each(|s| self.as_mut().add_song(s)), Ok(s) => s.into_iter().for_each(|s| self.as_mut().add_song(s)),
Err(e) => error!("There was an error in converting songs: {e}"), Err(e) => error!("There was an error in converting songs: {e}"),
@ -362,14 +356,14 @@ impl song_model::SongModel {
let song_id = self.as_mut().rust_mut().songs.get(index as usize).unwrap().id; let song_id = self.as_mut().rust_mut().songs.get(index as usize).unwrap().id;
let thread = self.qt_thread(); let thread = self.qt_thread();
let db = &mut self.as_mut().rust_mut().db.unwrap(); let db = &mut self.as_mut().rust_mut().db;
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("delete from songs where id = ?", song_id).fetch_optional(db).await; let result = query!("delete from songs where id = ?", song_id).fetch_optional(db).await;
match result { match result {
Ok(_) => { Ok(_) => {
thread.queue(move |mut song_model| { let _ = thread.queue(move |mut song_model| {
unsafe { unsafe {
song_model.as_mut().begin_remove_rows( song_model.as_mut().begin_remove_rows(
&QModelIndex::default(), &QModelIndex::default(),
@ -415,7 +409,21 @@ impl song_model::SongModel {
}; };
rt.block_on(async { rt.block_on(async {
let result = query!(r#"INSERT into songs (vorder, fontSize, backgroundType, horizontalTextAlignment, verticalTextAlignment, title, font, background, lyrics, ccli, author, audio, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#, song.verse_order, song.font_size, song.background_type, song.horizontal_text_alignment, song.vertical_text_alignment, song.title, song.font, song.background, song.lyrics, song.ccli, song.author, song.audio, song.id).fetch_all(&mut self.as_mut().rust_mut().db.unwrap()).await; let result = query!(r#"INSERT into songs (vorder, fontSize, backgroundType, horizontalTextAlignment, verticalTextAlignment, title, font, background, lyrics, ccli, author, audio, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
song.verse_order,
song.font_size,
song.background_type,
song.horizontal_text_alignment,
song.vertical_text_alignment,
song.title,
song.font,
song.background,
song.lyrics,
song.ccli,
song.author,
song.audio,
song.id)
.fetch_all(&mut self.as_mut().rust_mut().db).await;
match result { match result {
Ok(_i) => { Ok(_i) => {
@ -483,8 +491,8 @@ impl song_model::SongModel {
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
let db_string = updated_title.clone().to_string(); let db_string = updated_title.clone().to_string();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET title = ?", db_string) let result = query!("UPDATE songs SET title = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -523,8 +531,8 @@ impl song_model::SongModel {
let db_string = updated_lyrics.clone().to_string(); let db_string = updated_lyrics.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET lyrics = ?", db_string) let result = query!("UPDATE songs SET lyrics = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -567,8 +575,8 @@ impl song_model::SongModel {
let db_string = updated_author.clone().to_string(); let db_string = updated_author.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET author = ?", db_string) let result = query!("UPDATE songs SET author = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -611,8 +619,8 @@ impl song_model::SongModel {
let db_string = updated_audio.clone().to_string(); let db_string = updated_audio.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET audio = ?", db_string) let result = query!("UPDATE songs SET audio = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -655,8 +663,8 @@ impl song_model::SongModel {
let db_string = updated_ccli.clone().to_string(); let db_string = updated_ccli.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET ccli = ?", db_string) let result = query!("UPDATE songs SET ccli = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -699,8 +707,8 @@ impl song_model::SongModel {
let db_string = updated_verse_order.clone().to_string(); let db_string = updated_verse_order.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET vorder = ?", db_string) let result = query!("UPDATE songs SET vorder = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -743,8 +751,8 @@ impl song_model::SongModel {
let db_string = updated_background.clone().to_string(); let db_string = updated_background.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET background = ?", db_string) let result = query!("UPDATE songs SET background = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -788,8 +796,8 @@ impl song_model::SongModel {
let db_string = updated_background_type.clone().to_string(); let db_string = updated_background_type.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET backgroundType = ?", db_string) let result = query!("UPDATE songs SET backgroundType = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -833,8 +841,8 @@ impl song_model::SongModel {
let db_string = updated_horizontal_text_alignment.clone().to_string(); let db_string = updated_horizontal_text_alignment.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET horizontalTextAlignment = ?", db_string) let result = query!("UPDATE songs SET horizontalTextAlignment = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -878,8 +886,8 @@ impl song_model::SongModel {
let db_string = updated_vertical_text_alignment.clone().to_string(); let db_string = updated_vertical_text_alignment.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET verticalTextAlignment = ?", db_string) let result = query!("UPDATE songs SET verticalTextAlignment = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -922,8 +930,8 @@ impl song_model::SongModel {
let db_string = updated_font.clone().to_string(); let db_string = updated_font.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET font = ?", db_string) let result = query!("UPDATE songs SET font = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {
@ -966,8 +974,8 @@ impl song_model::SongModel {
let db_string = updated_font_size.clone().to_string(); let db_string = updated_font_size.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
let result = query!("UPDATE songs SET fontSize = ?", db_string) let result = query!("UPDATE songs SET fontSize = ? where id = ?", db_string, song_id)
.execute(&mut self.as_mut().rust_mut().db.unwrap()) .execute(&mut self.as_mut().rust_mut().db)
.await; .await;
match result { match result {