Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update database #20

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
},
"prettier.tabWidth": 4,
"css.lint.unknownProperties": "ignore",
"rust-analyzer.showUnlinkedFileNotification": false
"rust-analyzer.showUnlinkedFileNotification": false,
}
4 changes: 2 additions & 2 deletions src/data/app_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::{
TagsData,
},
database::prelude::{
AudioFile, CollectionID, Database, DatabaseAudioFileHandler, DatabaseCollectionHandler,
AudioFile, CollectionID, Database, DatabaseAudioFileHandler, DatabaseCollection,
},
engine::{SamplePlayerController, Waveform},
AudioData, Collection, DatabaseTagHandler, Tag,
AudioData, Collection, DatabaseTags, Tag,
};

use super::{SettingsData, TableData};
Expand Down
59 changes: 56 additions & 3 deletions src/database/audio_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use vizia::prelude::*;

use super::{CollectionID, Database, DatabaseConnectionHandle, DatabaseError};
use super::{CollectionID, Database, DatabaseConnection, DatabaseError, AUDIO_FILE_EXTENSIONS};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use vizia::prelude::*;

pub type AudioFileID = usize;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Data, Lens)]
Expand All @@ -18,6 +20,20 @@ pub struct AudioFile {
}

impl AudioFile {
pub fn from_path(path: &PathBuf, id: AudioFileID) -> Option<Self> {
let extension = path.extension().map(|v| v.to_str().unwrap()).unwrap_or("");

if !AUDIO_FILE_EXTENSIONS.contains(&extension) {
return None;
}

let name = path.file_name().unwrap().to_str().unwrap();

let audio_file = AudioFile::new(id, name.to_string(), id, 0.0, 0.0, 0.0, None, None, 0.0);

Some(audio_file)
}

pub fn new(
id: AudioFileID,
name: String,
Expand All @@ -35,8 +51,10 @@ impl AudioFile {

pub trait DatabaseAudioFileHandler {
fn get_all_audio_files(&self) -> Result<Vec<AudioFile>, DatabaseError>;
fn get_audio_file_by_path(&self, path: &PathBuf) -> Result<AudioFile, DatabaseError>;
fn get_child_audio_files(&self, parent: CollectionID) -> Result<Vec<AudioFile>, DatabaseError>;
fn insert_audio_file(&mut self, audio_file: AudioFile) -> Result<(), DatabaseError>;
fn remove_audio_file(&mut self, audio_file: AudioFileID) -> Result<(), DatabaseError>;
}

impl DatabaseAudioFileHandler for Database {
Expand Down Expand Up @@ -65,13 +83,40 @@ impl DatabaseAudioFileHandler for Database {
Err(DatabaseError::ConnectionClosed)
}

fn get_audio_file_by_path(&self, path: &PathBuf) -> Result<AudioFile, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, name, collection, duration, sample_rate, bit_depth, bpm, key, size FROM audio_files WHERE path = (?1)",
)?;

let col: AudioFile = query.query_row([path.to_str().unwrap()], |row| {
let path: String = row.get(3)?;
Ok(AudioFile {
id: row.get(0)?,
name: row.get(1)?,
collection: row.get(2)?,
duration: row.get(3)?,
sample_rate: row.get(4)?,
bit_depth: row.get(5)?,
bpm: row.get(6)?,
key: row.get(7)?,
size: row.get(8)?,
})
})?;

return Ok(col);
}

Err(DatabaseError::ConnectionClosed)
}

fn get_child_audio_files(&self, parent: CollectionID) -> Result<Vec<AudioFile>, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, name, collection, duration, sample_rate, bit_depth, bpm, key, size FROM audio_files WHERE collection = (?1)",
)?;

let collections = query.query_map([parent], |row| {
let audio_files = query.query_map([parent], |row| {
Ok(AudioFile {
id: row.get(0)?,
name: row.get(1)?,
Expand All @@ -85,7 +130,7 @@ impl DatabaseAudioFileHandler for Database {
})
})?;

return Ok(collections.map(|v| v.unwrap()).collect());
return Ok(audio_files.map(|v| v.unwrap()).collect());
}
Err(DatabaseError::ConnectionClosed)
}
Expand All @@ -110,6 +155,14 @@ impl DatabaseAudioFileHandler for Database {

Ok(())
}

fn remove_audio_file(&mut self, audio_file: AudioFileID) -> Result<(), DatabaseError> {
if let Some(connection) = self.get_connection() {
connection.execute("DELETE FROM audio_files WHERE id = (?1)", [audio_file])?;
}

Ok(())
}
}

impl From<AudioFile> for usize {
Expand Down
77 changes: 66 additions & 11 deletions src/database/collection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use super::{Database, DatabaseConnectionHandle, DatabaseError};
use super::{Database, DatabaseConnection, DatabaseError};
use serde::{Deserialize, Serialize};

pub type CollectionID = usize;
Expand Down Expand Up @@ -39,23 +39,26 @@ impl Collection {
}
}

pub trait DatabaseCollectionHandler {
fn get_collection(&self, id: CollectionID) -> Result<Collection, DatabaseError>;
pub trait DatabaseCollection {
fn get_root_collection(&self) -> Result<Collection, DatabaseError>;
fn get_collection(&self, collection: CollectionID) -> Result<Collection, DatabaseError>;
fn get_collection_by_name(&self, name: &str) -> Result<Collection, DatabaseError>;
fn get_collection_by_path(&self, path: &PathBuf) -> Result<Collection, DatabaseError>;
fn get_all_collections(&self) -> Result<Vec<Collection>, DatabaseError>;
fn get_child_collections(&self, parent: CollectionID)
-> Result<Vec<Collection>, DatabaseError>;
fn insert_collection(&mut self, collection: Collection) -> Result<(), DatabaseError>;
fn remove_collection(&mut self, colletion: CollectionID) -> Result<(), DatabaseError>;
}

impl DatabaseCollectionHandler for Database {
fn get_collection(&self, id: CollectionID) -> Result<Collection, DatabaseError> {
impl DatabaseCollection for Database {
fn get_root_collection(&self) -> Result<Collection, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, parent_collection, name, path FROM collections WHERE id = (?1)",
"SELECT id, parent_collection, name, path FROM collections WHERE parent_collection IS NULL",
)?;

let collection = query.query_row([id], |row| {
let col: Collection = query.query_row([], |row| {
let path: String = row.get(3)?;
Ok(Collection::new(
row.get(0)?,
Expand All @@ -65,19 +68,63 @@ impl DatabaseCollectionHandler for Database {
))
})?;

return Ok(collection);
return Ok(col);
}

Err(DatabaseError::ConnectionClosed)
}

fn get_root_collection(&self) -> Result<Collection, DatabaseError> {
fn get_collection(&self, collection: CollectionID) -> Result<Collection, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, parent_collection, name, path FROM collections WHERE parent_collection IS NULL",
"SELECT id, parent_collection, name, path FROM collections WHERE id IS (?1)",
)?;

let col: Collection = query.query_row([], |row| {
let col: Collection = query.query_row([collection], |row| {
let path: String = row.get(3)?;
Ok(Collection::new(
row.get(0)?,
row.get(1)?,
row.get(2)?,
Path::new(&path).to_path_buf(),
))
})?;

return Ok(col);
}

Err(DatabaseError::ConnectionClosed)
}

fn get_collection_by_name(&self, name: &str) -> Result<Collection, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, parent_collection, name, path FROM collections WHERE name IS (?1)",
)?;

let col: Collection = query.query_row([name], |row| {
let path: String = row.get(3)?;
Ok(Collection::new(
row.get(0)?,
row.get(1)?,
row.get(2)?,
Path::new(&path).to_path_buf(),
))
})?;

return Ok(col);
}

Err(DatabaseError::ConnectionClosed)
}

fn get_collection_by_path(&self, path: &PathBuf) -> Result<Collection, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, parent_collection, name, path FROM collections WHERE path IS (?1)",
)?;

let col: Collection = query.query_row([path.to_str().unwrap()], |row| {
let path: String = row.get(3)?;
Ok(Collection::new(
row.get(0)?,
Expand Down Expand Up @@ -152,6 +199,14 @@ impl DatabaseCollectionHandler for Database {

Ok(())
}

fn remove_collection(&mut self, collection: CollectionID) -> Result<(), DatabaseError> {
if let Some(connection) = self.get_connection() {
connection.execute("DELETE FROM collections WHERE id = (?1)", [collection])?;
}

Ok(())
}
}

impl From<Collection> for usize {
Expand Down
Loading
Loading