pub fn insert_files(config: &models::Config, files: Vec<models::DataFile>) {
    // Loop over the vector and then check if each file exists in the database based on name.
    // if it does I should check the hash. If it's not the same just print out that there is 
    // a difference for now.
    let conn =  rusqlite::Connection::open(config.database.file.as_path()).unwrap();
    for a_file in files {
        let cand_path: String = match conn.query_row(&config.database.get_path,
        rusqlite::params![a_file.hash],|row| row.get(0)) {
            Ok(p) => p,
            Err(_) => String::from(""),
        };

        if cand_path.is_empty() {
            let a_path = match a_file.path.into_os_string().into_string() {
                Ok(p) => p,
                Err(_) => String::from(""),
            };
            match conn.execute(&config.database.insert_catalogue,
                                rusqlite::params![a_file.size, a_path, a_file.hash, a_file.extension,
                                                    a_file.created, a_file.read]) {
                                                        Ok(added) => println!("{} row added", added),
                                                        Err(err) => println!("Addition failed {}", err),
            };
        } else if Path::new(&cand_path) != a_file.path {
            if Path::new(&cand_path).exists() {
                    println!("Duplicate file original: {} new: {:?}", cand_path, a_file.path);
            } else {
                let a_path = match a_file.path.into_os_string().into_string() {
                    Ok(p) => p,
                    Err(_) => String::from(""),
                };

                match conn.execute(&config.database.update_catalogue,
                                    rusqlite::params!(a_path, a_file.hash)) {
                                        Ok(updated) => println!("{} row updated", updated),
                                        Err(err) => println!("Update failed {}", err)
                                    };
                }
        }       
    }
}
// Insert a list of files into a database
Insert a list of files into a database
Insert a list of files into a database
pub fn insert_files(config: &models::Config, files: Vec<models::DataFile>) {
    // Loop over the vector and then check if each file exists in the database based on name.
    // if it does I should check the hash. If it's not the same just print out that there is 
    // a difference for now.
    let conn =  rusqlite::Connection::open(config.database.file.as_path()).unwrap();
    for a_file in files {
        let cand_path: String = match conn.query_row(&config.database.get_path,
        rusqlite::params![a_file.hash],|row| row.get(0)) {
            Ok(p) => p,
            Err(_) => String::from(""),
        };
        if cand_path.is_empty() {
            let a_path = match a_file.path.into_os_string().into_string() {
                Ok(p) => p,
                Err(_) => String::from(""),
            };
            match conn.execute(&config.database.insert_catalogue,
                                rusqlite::params![a_file.size, a_path, a_file.hash, a_file.extension,
                                                    a_file.created, a_file.read]) {
                                                        Ok(added) => println!("{} row added", added),
                                                        Err(err) => println!("Addition failed {}", err),
            };
        } else if Path::new(&cand_path) != a_file.path {
            if Path::new(&cand_path).exists() {
                    println!("Duplicate file original: {} new: {:?}", cand_path, a_file.path);
            } else {
                let a_path = match a_file.path.into_os_string().into_string() {
                    Ok(p) => p,
                    Err(_) => String::from(""),
                };
                match conn.execute(&config.database.update_catalogue,
                                    rusqlite::params!(a_path, a_file.hash)) {
                                        Ok(updated) => println!("{} row updated", updated),
                                        Err(err) => println!("Update failed {}", err)
                                    };
                }
        }       
    }
}
pub fn insert_files ( config : & models :: Config , files : Vec < models :: DataFile > ) { // Loop over the vector and then check if each file exists in the database based on name. // if it does I should check the hash. If it's not the same just print out that there is  // a difference for now. let conn = rusqlite :: Connection :: open ( config . database . file . as_path ( ) ) . unwrap ( ) ; for a_file in files { let cand_path : String = match conn . query_row ( & config . database . get_path , rusqlite :: params ! [ a_file hash ] , | row | row . get ( 0 ) ) { Ok ( p ) => p , Err ( _ ) => String :: from ( " " ) , } ; if cand_path . is_empty ( ) { let a_path = match a_file . path . into_os_string ( ) . into_string ( ) { Ok ( p ) => p , Err ( _ ) => String :: from ( " " ) , } ; match conn . execute ( & config . database . insert_catalogue , rusqlite :: params ! [ a_file size a_path a_file hash a_file extension a_file created a_file read ] ) { Ok ( added ) => println ! ( " " added ) , Err ( err ) => println ! ( " " err ) , } ; } else if Path :: new ( & cand_path ) != a_file . path { if Path :: new ( & cand_path ) . exists ( ) { println ! ( " " cand_path a_file path ) ; } else { let a_path = match a_file . path . into_os_string ( ) . into_string ( ) { Ok ( p ) => p , Err ( _ ) => String :: from ( " " ) , } ; match conn . execute ( & config . database . update_catalogue , rusqlite :: params ! ( a_path a_file hash ) ) { Ok ( updated ) => println ! ( " " updated ) , Err ( err ) => println ! ( " " err ) } ; } } } }
Insert a list of files into a database
Insert a list of files into a database
config
&models::Config
files
Vec<models::DataFile>
config
&models::Config
files
Vec<models::DataFile>