presentation_model using sqlx
This commit is contained in:
parent
d871f68ca5
commit
ee03777c10
1 changed files with 156 additions and 177 deletions
|
@ -182,15 +182,12 @@ mod presentation_model {
|
||||||
|
|
||||||
use crate::presentation_model::presentation_model::QMap_QString_QVariant;
|
use crate::presentation_model::presentation_model::QMap_QString_QVariant;
|
||||||
use crate::reveal_js;
|
use crate::reveal_js;
|
||||||
use crate::schema::presentations::dsl::*;
|
|
||||||
use cxx_qt::CxxQtType;
|
use cxx_qt::CxxQtType;
|
||||||
use cxx_qt_lib::{QModelIndex, QString, QUrl, QVariant};
|
use cxx_qt_lib::{QModelIndex, QString, QUrl, QVariant};
|
||||||
use diesel::sqlite::SqliteConnection;
|
use sqlx::{query, query_as, Connection, SqliteConnection};
|
||||||
use diesel::{delete, insert_into, prelude::*, update};
|
|
||||||
// use sqlx::Connection;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use tracing::debug;
|
use tracing::{debug, error};
|
||||||
|
|
||||||
use self::presentation_model::{
|
use self::presentation_model::{
|
||||||
PresRoles, QHash_i32_QByteArray, QVector_i32,
|
PresRoles, QHash_i32_QByteArray, QVector_i32,
|
||||||
|
@ -205,12 +202,35 @@ pub struct Presentation {
|
||||||
page_count: i32,
|
page_count: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PresentationModelRust {
|
pub struct PresentationModelRust {
|
||||||
count: i32,
|
count: i32,
|
||||||
highest_id: i32,
|
highest_id: i32,
|
||||||
presentations: Vec<Presentation>,
|
presentations: Vec<Presentation>,
|
||||||
inner_presentations: Vec<Presentation>,
|
inner_presentations: Vec<Presentation>,
|
||||||
|
db: SqliteConnection
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PresentationModelRust {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
count: 0,
|
||||||
|
highest_id: 0,
|
||||||
|
presentations: vec![],
|
||||||
|
inner_presentations: 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 {
|
||||||
|
SqliteConnection::connect(&db_url).await.expect("problems")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl presentation_model::PresentationModel {
|
impl presentation_model::PresentationModel {
|
||||||
|
@ -223,56 +243,24 @@ impl presentation_model::PresentationModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup(mut self: Pin<&mut Self>) {
|
pub fn setup(mut self: Pin<&mut Self>) {
|
||||||
let db = &mut self.as_mut().get_db();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
// let table_info = diesel::sql_query("PRAGMA table_info(presentations)").load(db);
|
rt.block_on(async {
|
||||||
// println!("{:?}", table_info);
|
let result = query_as!(Presentation, r#"SELECT id as "id: i32", title as "title!", filePath as "path!", html as "html!", pageCount as "page_count!: i32" from presentations"#).fetch_all(&mut self.as_mut().rust_mut().db).await;
|
||||||
let results = presentations
|
match result {
|
||||||
.load::<crate::models::Presentation>(db)
|
Ok(p) => p.into_iter().for_each(|p| self.as_mut().add_presentation(p)),
|
||||||
.expect("Error loading presentations");
|
Err(e) => error!("There was an error in converting songs: {e}"),
|
||||||
self.as_mut().rust_mut().highest_id = 0;
|
|
||||||
|
|
||||||
println!("SHOWING PRESENTATIONS");
|
|
||||||
println!("--------------");
|
|
||||||
for presentation in results {
|
|
||||||
println!("{}", presentation.title);
|
|
||||||
println!("{}", presentation.id);
|
|
||||||
println!("{}", presentation.path);
|
|
||||||
println!("{}", presentation.html);
|
|
||||||
println!("--------------");
|
|
||||||
if &self.as_mut().highest_id < &presentation.id {
|
|
||||||
self.as_mut().rust_mut().highest_id = presentation.id;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
let pres = self::Presentation {
|
|
||||||
id: presentation.id,
|
|
||||||
title: presentation.title,
|
|
||||||
html: presentation.path.ends_with(".html"),
|
|
||||||
path: presentation.path,
|
|
||||||
page_count: presentation.page_count.unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let count = self.as_ref().count;
|
|
||||||
self.as_mut().set_count(count + 1);
|
|
||||||
self.as_mut().add_presentation(pres);
|
|
||||||
}
|
|
||||||
println!("--------------------------------------");
|
|
||||||
println!("{:?}", self.as_mut().presentations);
|
|
||||||
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.presentations.len() {
|
if index < 0 || (index as usize) >= self.presentations.len() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let db = &mut self.as_mut().get_db();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
|
|
||||||
let presentation_id =
|
|
||||||
self.presentations.get(index as usize).unwrap().id;
|
|
||||||
|
|
||||||
let result =
|
|
||||||
delete(presentations.filter(id.eq(presentation_id)))
|
|
||||||
.execute(db);
|
|
||||||
|
|
||||||
|
rt.block_on(async {
|
||||||
|
let result = query!("delete from presentations where id = ?", index).execute(&mut self.as_mut().rust_mut().db).await;
|
||||||
match result {
|
match result {
|
||||||
Ok(_i) => {
|
Ok(_i) => {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -291,34 +279,16 @@ impl presentation_model::PresentationModel {
|
||||||
.remove(index as usize);
|
.remove(index as usize);
|
||||||
self.as_mut().end_remove_rows();
|
self.as_mut().end_remove_rows();
|
||||||
}
|
}
|
||||||
println!(
|
debug!("removed-item-at-index: {:?}", index);
|
||||||
"removed-item-at-index: {:?}",
|
|
||||||
presentation_id
|
|
||||||
);
|
|
||||||
println!(
|
|
||||||
"new-Vec: {:?}",
|
|
||||||
self.as_mut().presentations
|
|
||||||
);
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(_e) => {
|
Err(e) => {
|
||||||
println!("Cannot connect to database");
|
error!("Cannot connect to database: {e}");
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
true
|
||||||
fn get_db(self: Pin<&mut Self>) -> SqliteConnection {
|
|
||||||
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());
|
|
||||||
println!("DB: {:?}", db_url);
|
|
||||||
|
|
||||||
SqliteConnection::establish(&db_url).unwrap_or_else(|_| {
|
|
||||||
panic!("error connecting to {}", db_url)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_item(
|
pub fn new_item(
|
||||||
|
@ -361,8 +331,6 @@ impl presentation_model::PresentationModel {
|
||||||
presentation_html: bool,
|
presentation_html: bool,
|
||||||
new_page_count: i32,
|
new_page_count: i32,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let db = &mut self.as_mut().get_db();
|
|
||||||
// println!("{:?}", db);
|
|
||||||
let mut actual_page_count = new_page_count;
|
let mut actual_page_count = new_page_count;
|
||||||
if presentation_html {
|
if presentation_html {
|
||||||
let actual_path =
|
let actual_path =
|
||||||
|
@ -375,37 +343,40 @@ impl presentation_model::PresentationModel {
|
||||||
html = presentation_html
|
html = presentation_html
|
||||||
);
|
);
|
||||||
|
|
||||||
let presentation = self::Presentation {
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
id: presentation_id,
|
rt.block_on(async {
|
||||||
title: presentation_title.clone().to_string(),
|
let presentation_title = presentation_title.to_string();
|
||||||
html: presentation_html,
|
let presentation_path = presentation_path.to_string();
|
||||||
path: presentation_path.clone().to_string(),
|
let result = query!(r#"INSERT into presentations (id, title, filePath, html, pageCount) VALUES (?, ?, ?, ?, ?)"#,
|
||||||
page_count: actual_page_count,
|
presentation_id,
|
||||||
};
|
presentation_title,
|
||||||
println!("{:?}", presentation);
|
presentation_path,
|
||||||
|
presentation_html,
|
||||||
let result = insert_into(presentations)
|
actual_page_count)
|
||||||
.values((
|
.execute(&mut self.as_mut().rust_mut().db).await;
|
||||||
id.eq(&presentation_id),
|
|
||||||
title.eq(&presentation_title.to_string()),
|
|
||||||
path.eq(&presentation_path.to_string()),
|
|
||||||
html.eq(&presentation_html),
|
|
||||||
page_count.eq(&presentation.page_count),
|
|
||||||
))
|
|
||||||
.execute(db);
|
|
||||||
println!("{:?}", result);
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_i) => {
|
Ok(_i) => {
|
||||||
|
let presentation = Presentation {
|
||||||
|
id: presentation_id,
|
||||||
|
title: presentation_title.to_string(),
|
||||||
|
path: presentation_path.to_string(),
|
||||||
|
html: presentation_html,
|
||||||
|
page_count: actual_page_count,
|
||||||
|
};
|
||||||
self.as_mut().add_presentation(presentation);
|
self.as_mut().add_presentation(presentation);
|
||||||
println!("{:?}", self.as_mut().presentations);
|
debug!("{:?}", self.as_mut().presentations);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(_e) => {
|
Err(e) => {
|
||||||
println!("Cannot connect to database");
|
error!(
|
||||||
|
"Cannot connect to database: {e}"
|
||||||
|
);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_presentation(
|
fn add_presentation(
|
||||||
|
@ -511,10 +482,12 @@ impl presentation_model::PresentationModel {
|
||||||
let model_index =
|
let model_index =
|
||||||
&self.as_ref().index(index, 0, &QModelIndex::default());
|
&self.as_ref().index(index, 0, &QModelIndex::default());
|
||||||
|
|
||||||
let db = &mut self.as_mut().get_db();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
let result = update(presentations.filter(id.eq(index)))
|
rt.block_on(async {
|
||||||
.set(title.eq(updated_title.to_string()))
|
let title = updated_title.to_string();
|
||||||
.execute(db);
|
let result = query!("UPDATE presentations SET title = ? where id = ?", title, index)
|
||||||
|
.execute(&mut self.as_mut().rust_mut().db)
|
||||||
|
.await;
|
||||||
match result {
|
match result {
|
||||||
Ok(_i) => {
|
Ok(_i) => {
|
||||||
for presentation in self
|
for presentation in self
|
||||||
|
@ -524,21 +497,25 @@ impl presentation_model::PresentationModel {
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.filter(|x| x.id == index)
|
.filter(|x| x.id == index)
|
||||||
{
|
{
|
||||||
presentation.title = updated_title.to_string();
|
presentation.title = title.clone();
|
||||||
println!("rust-title: {:?}", presentation.title);
|
debug!(title = presentation.title,
|
||||||
|
title = title,
|
||||||
|
"updated presentation title");
|
||||||
}
|
}
|
||||||
// TODO this seems to not be updating in the actual list
|
|
||||||
self.as_mut().data_changed(
|
self.as_mut().data_changed(
|
||||||
model_index,
|
model_index,
|
||||||
model_index,
|
model_index,
|
||||||
&vector_roles,
|
&vector_roles,
|
||||||
);
|
);
|
||||||
// self.as_mut().emit_title_changed();
|
|
||||||
println!("rust-title: {:?}", updated_title);
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(_e) => false,
|
Err(e) => {
|
||||||
|
error!("Error connecting to db: {e}");
|
||||||
|
false
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_page_count(
|
pub fn update_page_count(
|
||||||
|
@ -552,10 +529,11 @@ impl presentation_model::PresentationModel {
|
||||||
let model_index =
|
let model_index =
|
||||||
&self.as_ref().index(index, 0, &QModelIndex::default());
|
&self.as_ref().index(index, 0, &QModelIndex::default());
|
||||||
|
|
||||||
let db = &mut self.as_mut().get_db();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
let result = update(presentations.filter(id.eq(index)))
|
rt.block_on(async {
|
||||||
.set(page_count.eq(updated_page_count))
|
let result = query!("UPDATE presentations SET pageCount = ? where id = ?", updated_page_count, index)
|
||||||
.execute(db);
|
.execute(&mut self.as_mut().rust_mut().db)
|
||||||
|
.await;
|
||||||
match result {
|
match result {
|
||||||
Ok(_i) => {
|
Ok(_i) => {
|
||||||
for presentation in self
|
for presentation in self
|
||||||
|
@ -565,24 +543,25 @@ impl presentation_model::PresentationModel {
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.filter(|x| x.id == index)
|
.filter(|x| x.id == index)
|
||||||
{
|
{
|
||||||
presentation.page_count = updated_page_count;
|
presentation.page_count = updated_page_count.clone();
|
||||||
println!(
|
debug!(title = presentation.title,
|
||||||
"rust-page_count: {:?}",
|
page_count = updated_page_count,
|
||||||
presentation.page_count
|
"updated presentation page_count");
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// TODO this seems to not be updating in the actual list
|
|
||||||
self.as_mut().data_changed(
|
self.as_mut().data_changed(
|
||||||
model_index,
|
model_index,
|
||||||
model_index,
|
model_index,
|
||||||
&vector_roles,
|
&vector_roles,
|
||||||
);
|
);
|
||||||
// self.as_mut().emit_page_count_changed();
|
|
||||||
println!("rust-page_count: {:?}", updated_page_count);
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(_e) => false,
|
Err(e) => {
|
||||||
|
error!("Error connecting to db: {e}");
|
||||||
|
false
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search(mut self: Pin<&mut Self>, search_term: QString) {
|
fn search(mut self: Pin<&mut Self>, search_term: QString) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue