some really stupid changes that I probably shouldn't commit
So I'm trying to switch to sqlx...........
This commit is contained in:
parent
4659cb6de5
commit
186142a012
3 changed files with 145 additions and 133 deletions
|
@ -243,12 +243,13 @@ pub mod song_model {
|
|||
use crate::models::*;
|
||||
use crate::schema::songs::dsl::*;
|
||||
use crate::songs::song_editor::song_editor::QList_QString;
|
||||
use cxx_qt::CxxQtType;
|
||||
use cxx_qt::{CxxQtType, Threading};
|
||||
use cxx_qt_lib::{
|
||||
QByteArray, QModelIndex, QString, QStringList, QVariant,
|
||||
};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
// use diesel::sqlite::SqliteConnection;
|
||||
use diesel::{delete, insert_into, prelude::*, update};
|
||||
use sqlx::{query, query_as, Connection, SqliteConnection};
|
||||
use std::collections::HashMap;
|
||||
use std::pin::Pin;
|
||||
use std::slice::Iter;
|
||||
|
@ -294,6 +295,7 @@ pub struct SongModelRust {
|
|||
highest_id: i32,
|
||||
songs: Vec<Song>,
|
||||
inner_songs: Vec<Song>,
|
||||
db: SqliteConnection,
|
||||
}
|
||||
|
||||
impl song_model::SongModel {
|
||||
|
@ -306,97 +308,7 @@ impl song_model::SongModel {
|
|||
}
|
||||
|
||||
pub fn setup(mut self: Pin<&mut Self>) {
|
||||
let db = &mut self.as_mut().get_db();
|
||||
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().get_db();
|
||||
|
||||
let song_id = self.songs.get(index as usize).unwrap().id;
|
||||
|
||||
let result = delete(songs.filter(id.eq(song_id))).execute(db);
|
||||
|
||||
match result {
|
||||
Ok(_i) => {
|
||||
unsafe {
|
||||
self.as_mut().begin_remove_rows(
|
||||
&QModelIndex::default(),
|
||||
index,
|
||||
index,
|
||||
);
|
||||
self.as_mut()
|
||||
.rust_mut()
|
||||
.songs
|
||||
.remove(index as usize);
|
||||
self.as_mut()
|
||||
.rust_mut()
|
||||
.inner_songs
|
||||
.remove(index as usize);
|
||||
self.as_mut().end_remove_rows();
|
||||
}
|
||||
println!("removed-item-at-index: {:?}", song_id);
|
||||
println!("new-Vec: {:?}", self.as_mut().songs);
|
||||
true
|
||||
}
|
||||
Err(_e) => {
|
||||
println!("Cannot connect to database");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_db(self: Pin<&mut Self>) -> SqliteConnection {
|
||||
let mut data = dirs::data_local_dir().unwrap();
|
||||
data.push("lumina");
|
||||
data.push("library-db.sqlite3");
|
||||
|
@ -404,9 +316,115 @@ impl song_model::SongModel {
|
|||
db_url.push_str(data.to_str().unwrap());
|
||||
println!("DB: {:?}", db_url);
|
||||
|
||||
SqliteConnection::establish(&db_url).unwrap_or_else(|_| {
|
||||
panic!("error connecting to {}", 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;
|
||||
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 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(_) => {
|
||||
unsafe {
|
||||
self.as_mut().begin_remove_rows(
|
||||
&QModelIndex::default(),
|
||||
index,
|
||||
index,
|
||||
);
|
||||
self.as_mut()
|
||||
.rust_mut()
|
||||
.songs
|
||||
.remove(index as usize);
|
||||
self.as_mut()
|
||||
.rust_mut()
|
||||
.inner_songs
|
||||
.remove(index as usize);
|
||||
self.as_mut().end_remove_rows();
|
||||
}
|
||||
debug!("removed-item-at-index: {:?}", song_id);
|
||||
true
|
||||
},
|
||||
Err(e) => {
|
||||
error!("There was an error deleting the row {song_id}: {e}");
|
||||
false
|
||||
}
|
||||
}
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
fn get_db(self: Pin<&mut Self>) -> SqliteConnection {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn new_song(mut self: Pin<&mut Self>) -> bool {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue