Files
Projet_48h/backend/user.rs

64 lines
1.8 KiB
Rust

use sqlite::Connection;
use sqlite::State;
pub struct User {
name: String,
level: u32,
password: String,
}
impl User {
pub fn open(name: String, level: u32, password: String) -> User {
User {name, level, password}
}
}
pub struct DataBase {
connection: Connection,
}
impl DataBase {
pub fn open(name: String) -> DataBase {
let connection = sqlite::open(name + ".db").unwrap();
DataBase { connection }
}
fn execute(&self, arg: String){
match self.connection.execute(arg){
_ => (),
Err(e) => panic!("Error : {}",e),
}
}
pub fn create_table(&self, table: &str, var: &str){
self.execute("CREATE TABLE ".to_owned() + &table.to_string() + "(" + &var.to_string() + ");");
}
pub fn add_column(&self, table: &str, arg: &str){
self.execute("ALTER TABLE ".to_owned() + &table.to_string() + " ADD " + &arg.to_string() + ";");
}
pub fn insert(&self, table: &str, arg: &str){
self.execute("INSERT INTO ".to_owned() + &table.to_string() + " VALUES (" + &arg.to_string() + ")" + ";");
}
pub fn check(&self, table: &str, arg: &str) -> bool {
let mut end = false;
self.connection.iterate("Select name FROM ".to_owned() + &table.to_string() + " WHERE name = " + &arg.to_string(), |pairs| {
for &(name, value) in pairs.iter() {
println!("{} = {}", name, value.unwrap());
if name == "name" {
println!("True");
end = true;
}
}
return end;
});
return end;
}
pub fn update(&self, table: &str, arg: &str, where_is: &str, arg2: &str){
self.execute("UPDATE ".to_owned() + &table.to_string() + " SET " + &arg.to_string() + " WHERE " + &where_is.to_string() + " = " + &arg2.to_string());
}
}