some really stupid changes that I probably shouldn't commit

So I'm trying to switch to sqlx...........
This commit is contained in:
Chris Cochrun 2024-09-18 15:59:28 -05:00
parent 4659cb6de5
commit 186142a012
3 changed files with 145 additions and 133 deletions

68
Cargo.lock generated
View file

@ -24,7 +24,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"getrandom",
"once_cell", "once_cell",
"version_check", "version_check",
"zerocopy", "zerocopy",
@ -827,7 +826,7 @@ checksum = "c5d9abe6314103864cc2d8901b7ae224e0ab1a103a0a416661b4097b0779b607"
dependencies = [ dependencies = [
"darling", "darling",
"either", "either",
"heck 0.5.0", "heck",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.77", "syn 2.0.77",
@ -1181,22 +1180,13 @@ dependencies = [
[[package]] [[package]]
name = "hashlink" name = "hashlink"
version = "0.8.4" version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af"
dependencies = [ dependencies = [
"hashbrown 0.14.5", "hashbrown 0.14.5",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
@ -1510,9 +1500,9 @@ dependencies = [
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.27.0" version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4e226dcd58b4be396f7bd3c20da8fdee2911400705297ba7d2d7cc2c30f716" checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149"
dependencies = [ dependencies = [
"cc", "cc",
"pkg-config", "pkg-config",
@ -2553,6 +2543,9 @@ name = "smallvec"
version = "1.13.2" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
@ -2605,9 +2598,9 @@ dependencies = [
[[package]] [[package]]
name = "sqlx" name = "sqlx"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9a2ccff1a000a5a59cd33da541d9f2fdcd9e6e8229cc200565942bff36d0aaa" checksum = "93334716a037193fac19df402f8571269c84a00852f6a7066b5d2616dcd64d3e"
dependencies = [ dependencies = [
"sqlx-core", "sqlx-core",
"sqlx-macros", "sqlx-macros",
@ -2618,23 +2611,23 @@ dependencies = [
[[package]] [[package]]
name = "sqlx-core" name = "sqlx-core"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24ba59a9342a3d9bab6c56c118be528b27c9b60e490080e9711a04dccac83ef6" checksum = "d4d8060b456358185f7d50c55d9b5066ad956956fddec42ee2e8567134a8936e"
dependencies = [ dependencies = [
"ahash",
"atoi", "atoi",
"byteorder", "byteorder",
"bytes", "bytes",
"crc", "crc",
"crossbeam-queue", "crossbeam-queue",
"either", "either",
"event-listener 2.5.3", "event-listener 5.3.1",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-intrusive", "futures-intrusive",
"futures-io", "futures-io",
"futures-util", "futures-util",
"hashbrown 0.14.5",
"hashlink", "hashlink",
"hex", "hex",
"indexmap 2.5.0", "indexmap 2.5.0",
@ -2657,26 +2650,26 @@ dependencies = [
[[package]] [[package]]
name = "sqlx-macros" name = "sqlx-macros"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea40e2345eb2faa9e1e5e326db8c34711317d2b5e08d0d5741619048a803127" checksum = "cac0692bcc9de3b073e8d747391827297e075c7710ff6276d9f7a1f3d58c6657"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"sqlx-core", "sqlx-core",
"sqlx-macros-core", "sqlx-macros-core",
"syn 1.0.109", "syn 2.0.77",
] ]
[[package]] [[package]]
name = "sqlx-macros-core" name = "sqlx-macros-core"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5833ef53aaa16d860e92123292f1f6a3d53c34ba8b1969f152ef1a7bb803f3c8" checksum = "1804e8a7c7865599c9c79be146dc8a9fd8cc86935fa641d3ea58e5f0688abaa5"
dependencies = [ dependencies = [
"dotenvy", "dotenvy",
"either", "either",
"heck 0.4.1", "heck",
"hex", "hex",
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
@ -2686,8 +2679,9 @@ dependencies = [
"sha2", "sha2",
"sqlx-core", "sqlx-core",
"sqlx-mysql", "sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite", "sqlx-sqlite",
"syn 1.0.109", "syn 2.0.77",
"tempfile", "tempfile",
"tokio", "tokio",
"url", "url",
@ -2695,12 +2689,12 @@ dependencies = [
[[package]] [[package]]
name = "sqlx-mysql" name = "sqlx-mysql"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ed31390216d20e538e447a7a9b959e06ed9fc51c37b514b46eb758016ecd418" checksum = "64bb4714269afa44aef2755150a0fc19d756fb580a67db8885608cf02f47d06a"
dependencies = [ dependencies = [
"atoi", "atoi",
"base64 0.21.7", "base64 0.22.1",
"bitflags 2.6.0", "bitflags 2.6.0",
"byteorder", "byteorder",
"bytes", "bytes",
@ -2737,12 +2731,12 @@ dependencies = [
[[package]] [[package]]
name = "sqlx-postgres" name = "sqlx-postgres"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c824eb80b894f926f89a0b9da0c7f435d27cdd35b8c655b114e58223918577e" checksum = "6fa91a732d854c5d7726349bb4bb879bb9478993ceb764247660aee25f67c2f8"
dependencies = [ dependencies = [
"atoi", "atoi",
"base64 0.21.7", "base64 0.22.1",
"bitflags 2.6.0", "bitflags 2.6.0",
"byteorder", "byteorder",
"crc", "crc",
@ -2775,9 +2769,9 @@ dependencies = [
[[package]] [[package]]
name = "sqlx-sqlite" name = "sqlx-sqlite"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b244ef0a8414da0bed4bb1910426e890b19e5e9bccc27ada6b797d05c55ae0aa" checksum = "d5b2cf34a45953bfd3daaf3db0f7a7878ab9b7a6b91b422d24a7a9e4c857b680"
dependencies = [ dependencies = [
"atoi", "atoi",
"flume", "flume",
@ -2790,10 +2784,10 @@ dependencies = [
"log", "log",
"percent-encoding", "percent-encoding",
"serde", "serde",
"serde_urlencoded",
"sqlx-core", "sqlx-core",
"tracing", "tracing",
"url", "url",
"urlencoding",
] ]
[[package]] [[package]]

View file

@ -32,7 +32,7 @@ zstd = "0.12.4"
serde_json = "1.0.104" serde_json = "1.0.104"
fastrand = "2.0.0" fastrand = "2.0.0"
rfd = { version = "0.12.1", features = ["xdg-portal"], default-features = false } rfd = { version = "0.12.1", features = ["xdg-portal"], default-features = false }
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio"] } sqlx = { version = "0.8.2", features = ["sqlite", "runtime-tokio", "macros"] }
tokio = { version = "1.32.0", features = ["full"] } tokio = { version = "1.32.0", features = ["full"] }
tracing-subscriber = { version = "0.3.17", features = ["fmt", "std", "chrono", "time", "local-time", "env-filter"] } tracing-subscriber = { version = "0.3.17", features = ["fmt", "std", "chrono", "time", "local-time", "env-filter"] }
tracing = "0.1.37" tracing = "0.1.37"

View file

@ -243,12 +243,13 @@ pub mod song_model {
use crate::models::*; use crate::models::*;
use crate::schema::songs::dsl::*; use crate::schema::songs::dsl::*;
use crate::songs::song_editor::song_editor::QList_QString; use crate::songs::song_editor::song_editor::QList_QString;
use cxx_qt::CxxQtType; use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::{ use cxx_qt_lib::{
QByteArray, QModelIndex, QString, QStringList, QVariant, QByteArray, QModelIndex, QString, QStringList, QVariant,
}; };
use diesel::sqlite::SqliteConnection; // use diesel::sqlite::SqliteConnection;
use diesel::{delete, insert_into, prelude::*, update}; use diesel::{delete, insert_into, prelude::*, update};
use sqlx::{query, query_as, Connection, SqliteConnection};
use std::collections::HashMap; use std::collections::HashMap;
use std::pin::Pin; use std::pin::Pin;
use std::slice::Iter; use std::slice::Iter;
@ -294,6 +295,7 @@ pub struct SongModelRust {
highest_id: i32, highest_id: i32,
songs: Vec<Song>, songs: Vec<Song>,
inner_songs: Vec<Song>, inner_songs: Vec<Song>,
db: SqliteConnection,
} }
impl song_model::SongModel { impl song_model::SongModel {
@ -306,69 +308,93 @@ impl song_model::SongModel {
} }
pub fn setup(mut self: Pin<&mut Self>) { pub fn setup(mut self: Pin<&mut Self>) {
let db = &mut self.as_mut().get_db();
run_migrations(db);
let results = songs let mut data = dirs::data_local_dir().unwrap();
.load::<crate::models::Song>(db) data.push("lumina");
.expect("NO TABLE?????????????"); data.push("library-db.sqlite3");
self.as_mut().rust_mut().highest_id = 0; let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap());
println!("DB: {:?}", db_url);
println!("SHOWING SONGS"); static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3";
println!("--------------"); let thread = self.qt_thread();
for song in results { let rt = tokio::runtime::Runtime::new().unwrap();
println!("{}", song.title); rt.block_on(async {
println!("{}", song.id); match SqliteConnection::connect(&db_url).await {
println!( Ok(db) => {
"{}", thread.queue(|s| s.as_mut().rust_mut().db = db);
song.background.clone().unwrap_or_default() 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 {
println!("--------------"); Ok(s) => s.into_iter().for_each(|s| self.as_mut().add_song(s)),
if self.as_mut().highest_id < song.id { Err(e) => error!("There was an error in converting songs: {e}"),
self.as_mut().rust_mut().highest_id = song.id;
} }
},
let song = Song { Err(e) => error!("There was an error in the db: {e}"),
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!("--------------------------------------"); // 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 { pub fn remove_item(mut self: Pin<&mut Self>, index: i32) -> bool {
if index < 0 || (index as usize) >= self.songs.len() { if index < 0 || (index as usize) >= self.songs.len() {
return false; return false;
} }
let db = &mut self.as_mut().get_db(); let db = &mut self.as_mut().rust_mut().db;
let song_id = self.songs.get(index as usize).unwrap().id; let song_id = self.songs.get(index as usize).unwrap().id;
let result = delete(songs.filter(id.eq(song_id))).execute(db); 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 { match result {
Ok(_i) => { Ok(_) => {
unsafe { unsafe {
self.as_mut().begin_remove_rows( self.as_mut().begin_remove_rows(
&QModelIndex::default(), &QModelIndex::default(),
@ -385,28 +411,20 @@ impl song_model::SongModel {
.remove(index as usize); .remove(index as usize);
self.as_mut().end_remove_rows(); self.as_mut().end_remove_rows();
} }
println!("removed-item-at-index: {:?}", song_id); debug!("removed-item-at-index: {:?}", song_id);
println!("new-Vec: {:?}", self.as_mut().songs);
true true
} },
Err(_e) => { Err(e) => {
println!("Cannot connect to database"); error!("There was an error deleting the row {song_id}: {e}");
false false
} }
} }
});
true
} }
fn get_db(self: Pin<&mut Self>) -> SqliteConnection { fn get_db(self: Pin<&mut Self>) -> SqliteConnection {
let mut data = dirs::data_local_dir().unwrap(); todo!()
data.push("lumina");
data.push("library-db.sqlite3");
let mut db_url = String::from("sqlite://");
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)
})
} }
pub fn new_song(mut self: Pin<&mut Self>) -> bool { pub fn new_song(mut self: Pin<&mut Self>) -> bool {