tweaks to add sqlx

This commit is contained in:
Chris Cochrun 2024-09-24 10:12:14 -05:00
parent 186142a012
commit bfb723a413
3 changed files with 455 additions and 411 deletions

1
.envrc
View file

@ -4,6 +4,7 @@ export CMAKE_EXPORT_COMPILE_COMMANDS=1
export CMAKE_BUILD_TYPE=Debug
export CXX=g++
export CC=gcc
export DATABASE_URL=sqlite:///home/chris/.local/share/lumina/library-db.sqlite3
# export CXXQT_INCLUDE_PATH=$GUIX_ENVIRONMENT/include
# export INCLUDEPATH="/gnu/store/pkjvij1f6rvx42xv2kygicr7fsch41dl-profile/include"
use flake . --impure

View file

@ -60,6 +60,7 @@ mkShell rec {
cargo
rustfmt
rust-analyzer
sqlx-cli
corrosion
];

View file

@ -289,13 +289,41 @@ impl Default for Song {
}
}
#[derive(Default, Debug)]
pub struct SongModelRust {
#[derive(Debug)]
pub struct SongModelRust<'a> {
count: i32,
highest_id: i32,
songs: Vec<Song>,
inner_songs: Vec<Song>,
db: SqliteConnection,
db: Option<&'a SqliteConnection>,
}
impl Default for SongModelRust<'a> {
fn default() -> Self {
Self {
count: 0,
highest_id: 0,
songs: vec![],
inner_songs: vec![],
db: {
let rt = tokio::runtime::Runtime::new().unwrap();
let mut data = dirs::data_local_dir().unwrap();
data.push("lumina");
data.push("library-db.sqlite3");
let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap());
rt.block_on(async {
match SqliteConnection::connect(&db_url).await {
Ok(db) => Some(&db),
Err(e) => {
error!("Error connecting to db: {e}");
None
}
}
})
}
}
}
}
impl song_model::SongModel {
@ -315,102 +343,50 @@ impl song_model::SongModel {
let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap());
println!("DB: {:?}", db_url);
static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3";
let thread = self.qt_thread();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
match SqliteConnection::connect(&db_url).await {
Ok(db) => {
thread.queue(|s| s.as_mut().rust_mut().db = db);
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;
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;
match result {
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 the db: {e}"),
}
});
// run_migrations(db);
// let results = songs
// .load::<crate::models::Song>(db)
// .expect("NO TABLE?????????????");
// self.as_mut().rust_mut().highest_id = 0;
// println!("SHOWING SONGS");
// println!("--------------");
// for song in results {
// println!("{}", song.title);
// println!("{}", song.id);
// println!(
// "{}",
// song.background.clone().unwrap_or_default()
// );
// println!("--------------");
// if self.as_mut().highest_id < song.id {
// self.as_mut().rust_mut().highest_id = song.id;
// }
// let song = Song {
// id: song.id,
// title: song.title,
// lyrics: song.lyrics.unwrap_or_default(),
// author: song.author.unwrap_or_default(),
// ccli: song.ccli.unwrap_or_default(),
// audio: song.audio.unwrap_or_default(),
// verse_order: song.verse_order.unwrap_or_default(),
// background: song.background.unwrap_or_default(),
// background_type: song
// .background_type
// .unwrap_or_default(),
// horizontal_text_alignment: song
// .horizontal_text_alignment
// .unwrap_or_default(),
// vertical_text_alignment: song
// .vertical_text_alignment
// .unwrap_or_default(),
// font: song.font.unwrap_or_default(),
// font_size: song.font_size.unwrap_or_default(),
// };
// self.as_mut().add_song(song);
// }
// println!("--------------------------------------");
// println!("{:?}", self.as_mut().songs);
// println!("--------------------------------------");
}
pub fn remove_item(mut self: Pin<&mut Self>, index: i32) -> bool {
if index < 0 || (index as usize) >= self.songs.len() {
return false;
}
let db = &mut self.as_mut().rust_mut().db;
let song_id = self.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 db = &mut self.as_mut().rust_mut().db.unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("delete from songs where id = ?", song_id).fetch_optional(db).await;
match result {
Ok(_) => {
thread.queue(move |mut song_model| {
unsafe {
self.as_mut().begin_remove_rows(
song_model.as_mut().begin_remove_rows(
&QModelIndex::default(),
index,
index,
);
self.as_mut()
song_model.as_mut()
.rust_mut()
.songs
.remove(index as usize);
self.as_mut()
song_model.as_mut()
.rust_mut()
.inner_songs
.remove(index as usize);
self.as_mut().end_remove_rows();
song_model.as_mut().end_remove_rows();
}
});
debug!("removed-item-at-index: {:?}", song_id);
true
},
@ -430,7 +406,7 @@ impl song_model::SongModel {
pub fn new_song(mut self: Pin<&mut Self>) -> bool {
let song_id = self.rust().highest_id + 1;
let song_title = String::from("title");
let db = &mut self.as_mut().get_db();
let rt = tokio::runtime::Runtime::new().unwrap();
let song = Song {
id: song_id,
@ -438,26 +414,8 @@ impl song_model::SongModel {
..Default::default()
};
let result = insert_into(songs)
.values((
id.eq(&song_id),
title.eq(&song_title),
lyrics.eq(&song.lyrics),
author.eq(&song.author),
ccli.eq(&song.ccli),
audio.eq(&song.audio),
verse_order.eq(&song.verse_order),
background.eq(&song.background),
background_type.eq(&song.background_type),
horizontal_text_alignment
.eq(&song.horizontal_text_alignment),
vertical_text_alignment
.eq(&song.vertical_text_alignment),
font.eq(&song.font),
font_size.eq(&song.font_size),
))
.execute(db);
println!("{:?}", result);
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;
match result {
Ok(_i) => {
@ -470,6 +428,8 @@ impl song_model::SongModel {
false
}
}
});
true
}
fn add_song(mut self: Pin<&mut Self>, song: self::Song) {
@ -520,13 +480,15 @@ impl song_model::SongModel {
) -> bool {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Title);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(title.eq(updated_title.to_string()))
.execute(db);
let rt = tokio::runtime::Runtime::new().unwrap();
let db_string = updated_title.clone().to_string();
rt.block_on(async {
let result = query!("UPDATE songs SET title = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
debug!(_i, index);
if let Some(song) =
self.as_mut().rust_mut().songs.get_mut(index)
{
@ -542,8 +504,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating title in db: {e}");
false
},
}
});
true
}
pub fn update_lyrics(
@ -553,10 +520,13 @@ impl song_model::SongModel {
) -> bool {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Lyrics);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(lyrics.eq(updated_lyrics.to_string()))
.execute(db);
let db_string = updated_lyrics.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET lyrics = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -577,8 +547,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating lyrics in db: {e}");
false
},
}
});
true
}
pub fn update_author(
@ -589,10 +564,13 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Author);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(author.eq(updated_author.to_string()))
.execute(db);
let db_string = updated_author.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET author = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -613,8 +591,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating author in db: {e}");
false
},
}
});
true
}
pub fn update_audio(
@ -625,20 +608,23 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Audio);
debug!(?updated_audio);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(audio.eq(updated_audio.to_string()))
.execute(db);
debug!(?result);
let db_string = updated_audio.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET audio = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
self.as_mut().rust_mut().songs.get_mut(index)
{
debug!(?song, audio = updated_audio.to_string());
debug!(
?song,
audio = updated_audio.to_string()
);
song.audio = updated_audio.to_string();
debug!(?song);
self.as_mut().data_changed(
&model_index,
&model_index,
@ -649,8 +635,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating audio in db: {e}");
false
},
}
});
true
}
pub fn update_ccli(
@ -661,16 +652,22 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Ccli);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(ccli.eq(updated_ccli.to_string()))
.execute(db);
let db_string = updated_ccli.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET ccli = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
self.as_mut().rust_mut().songs.get_mut(index)
{
debug!(?song, ccli = updated_ccli.to_string());
debug!(
?song,
ccli = updated_ccli.to_string()
);
song.ccli = updated_ccli.to_string();
self.as_mut().data_changed(
&model_index,
@ -682,8 +679,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating ccli in db: {e}");
false
},
}
});
true
}
pub fn update_verse_order(
@ -694,10 +696,13 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::VerseOrder);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(verse_order.eq(updated_verse_order.to_string()))
.execute(db);
let db_string = updated_verse_order.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET vorder = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -707,8 +712,7 @@ impl song_model::SongModel {
?song,
verse_order = updated_verse_order.to_string()
);
song.verse_order =
updated_verse_order.to_string();
song.verse_order = updated_verse_order.to_string();
self.as_mut().data_changed(
&model_index,
&model_index,
@ -719,8 +723,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating verse_order in db: {e}");
false
},
}
});
true
}
pub fn update_background(
@ -731,34 +740,40 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Background);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(background.eq(updated_background.to_string()))
.execute(db);
let db_string = updated_background.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET background = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
self.as_mut().rust_mut().songs.get_mut(index)
{
song.background = updated_background.to_string();
debug!(
background = ?updated_background,
model_index = ?model_index,
roles = vector_roles.get(0)
?song,
background = updated_background.to_string()
);
song.background = updated_background.to_string();
self.as_mut().data_changed(
&model_index,
&model_index,
&vector_roles,
);
self.as_mut().background_changed();
true
} else {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating background in db: {e}");
false
},
}
});
true
}
pub fn update_background_type(
@ -770,13 +785,13 @@ impl song_model::SongModel {
.as_mut()
.get_indices(song_id, SongRoles::BackgroundType);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(
background_type
.eq(updated_background_type.to_string()),
)
.execute(db);
let db_string = updated_background_type.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET backgroundType = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -784,11 +799,9 @@ impl song_model::SongModel {
{
debug!(
?song,
background_type =
updated_background_type.to_string()
background_type = updated_background_type.to_string()
);
song.background_type =
updated_background_type.to_string();
song.background_type = updated_background_type.to_string();
self.as_mut().data_changed(
&model_index,
&model_index,
@ -799,8 +812,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating background_type in db: {e}");
false
},
}
});
true
}
pub fn update_horizontal_text_alignment(
@ -812,13 +830,13 @@ impl song_model::SongModel {
.as_mut()
.get_indices(song_id, SongRoles::HorizontalTextAlignment);
let db = &mut self.as_mut().get_db();
let result =
update(songs.filter(id.eq(song_id)))
.set(horizontal_text_alignment.eq(
updated_horizontal_text_alignment.to_string(),
))
.execute(db);
let db_string = updated_horizontal_text_alignment.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET horizontalTextAlignment = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -826,12 +844,9 @@ impl song_model::SongModel {
{
debug!(
?song,
horizontal =
updated_horizontal_text_alignment
.to_string()
horizontal_text_alignment = updated_horizontal_text_alignment.to_string()
);
song.horizontal_text_alignment =
updated_horizontal_text_alignment.to_string();
song.horizontal_text_alignment = updated_horizontal_text_alignment.to_string();
self.as_mut().data_changed(
&model_index,
&model_index,
@ -842,8 +857,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating horizontal_text_alignment in db: {e}");
false
},
}
});
true
}
pub fn update_vertical_text_alignment(
@ -855,13 +875,13 @@ impl song_model::SongModel {
.as_mut()
.get_indices(song_id, SongRoles::VerticalTextAlignment);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(
vertical_text_alignment
.eq(updated_vertical_text_alignment.to_string()),
)
.execute(db);
let db_string = updated_vertical_text_alignment.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET verticalTextAlignment = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -869,11 +889,9 @@ impl song_model::SongModel {
{
debug!(
?song,
vertical = updated_vertical_text_alignment
.to_string()
vertical_text_alignment = updated_vertical_text_alignment.to_string()
);
song.vertical_text_alignment =
updated_vertical_text_alignment.to_string();
song.vertical_text_alignment = updated_vertical_text_alignment.to_string();
self.as_mut().data_changed(
&model_index,
&model_index,
@ -884,8 +902,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating vertical_text_alignment in db: {e}");
false
},
}
});
true
}
pub fn update_font(
@ -896,17 +919,23 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::Font);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(font.eq(updated_font.to_string()))
.execute(db);
let db_string = updated_font.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET font = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
self.as_mut().rust_mut().songs.get_mut(index)
{
debug!(
?song,
font = updated_font.to_string()
);
song.font = updated_font.to_string();
debug!(?song, font = updated_font.to_string());
self.as_mut().data_changed(
&model_index,
&model_index,
@ -917,8 +946,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating font in db: {e}");
false
},
}
});
true
}
pub fn update_font_size(
@ -929,10 +963,13 @@ impl song_model::SongModel {
let (index, model_index, vector_roles) =
self.as_mut().get_indices(song_id, SongRoles::FontSize);
let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(song_id)))
.set(font_size.eq(updated_font_size))
.execute(db);
let db_string = updated_font_size.clone().to_string();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE songs SET fontSize = ?", db_string)
.execute(&mut self.as_mut().rust_mut().db.unwrap())
.await;
match result {
Ok(_i) => {
if let Some(song) =
@ -940,7 +977,7 @@ impl song_model::SongModel {
{
debug!(
?song,
font_size = updated_font_size.to_string()
font_size = updated_font_size
);
song.font_size = updated_font_size;
self.as_mut().data_changed(
@ -953,8 +990,13 @@ impl song_model::SongModel {
false
}
}
Err(_e) => false,
Err(e) => {
error!("There was an error updating font_size in db: {e}");
false
},
}
});
true
}
pub fn get_item(&self, index: i32) -> QMap_QString_QVariant {