add check command and user object in user.rs file
This commit is contained in:
@@ -1,4 +1,18 @@
|
||||
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,
|
||||
@@ -10,22 +24,41 @@ impl DataBase {
|
||||
DataBase { connection }
|
||||
}
|
||||
|
||||
pub fn data_execute(&self, arg: String){
|
||||
fn execute(&self, arg: String){
|
||||
match self.connection.execute(arg){
|
||||
OK => println!("Ok"),
|
||||
_ => (),
|
||||
Err(e) => panic!("Error : {}",e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_table(&self, table: &str, var: &str){
|
||||
self.data_execute("CREATE TABLE ".to_owned() + &table.to_string() + "(" + &var.to_string() + ");");
|
||||
self.execute("CREATE TABLE ".to_owned() + &table.to_string() + "(" + &var.to_string() + ");");
|
||||
}
|
||||
|
||||
pub fn add_column(&self, table: &str, arg: &str){
|
||||
self.data_execute("ALTER TABLE ".to_owned() + &table.to_string() + " ADD " + &arg.to_string() + ";");
|
||||
self.execute("ALTER TABLE ".to_owned() + &table.to_string() + " ADD " + &arg.to_string() + ";");
|
||||
}
|
||||
|
||||
pub fn insert(&self, table: &str, arg: &str){
|
||||
self.data_execute("INSERT INTO ".to_owned() + &table.to_string() + " VALUES (" + &arg.to_string() + ")" + ";");
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user