Skip to content

Instantly share code, notes, and snippets.

@NurbsOtter
Created September 10, 2017 03:19
Show Gist options
  • Save NurbsOtter/4751101814669e634fa1d60bb36d11f2 to your computer and use it in GitHub Desktop.
Save NurbsOtter/4751101814669e634fa1d60bb36d11f2 to your computer and use it in GitHub Desktop.
Really bad DB porting thing
use std::path::Path;
extern crate rusqlite;
#[macro_use]
extern crate mysql;
use rusqlite::Connection;
use mysql as my;
#[derive(Debug)]
struct Warning{
id:i32,
user_id:i32,
warning_text:String,
warning_date:String
}
#[derive(Debug)]
struct ChatUser{
id:i32,
username:String,
tgid:i32,
ping_allowed:bool,
active_user:bool
}
#[derive(Debug)]
struct Alias{
id:i32,
name:String,
user_id:i32,
change_date:String
}
fn main() {
let path = Path::new("userdatabase.db");
let conn = Connection::open(path);
let conn = match conn{
Ok(c)=>c,
Err(e)=>{
panic!("{:?}", e)
}
};
//MySQL init
let pool = my::Pool::new("SECRETS");
let pool = match pool{
Ok(p)=>p,
Err(e)=>{
panic!("{:?}",e)
}
};
let mut write_stmt = pool.prepare("INSERT INTO chatuser(id,userName,tgID,pingAllowed,activeUser) VALUES (?,?,?,?,?)");
let mut write_stmt = match write_stmt{
Ok(w)=>w,
Err(e)=>{panic!("{:?}", e)}
};
let mut read_stmt = conn.prepare("SELECT id,userName,tgid,pingAllowed FROM chatUser").unwrap();
let cur_iter = read_stmt.query_map(&[],|row|{
ChatUser{
id:row.get(0),
username:row.get(1),
tgid:row.get(2),
ping_allowed:row.get(3),
active_user:true
}
}).unwrap();
for user in cur_iter{
let user = match user{
Ok(u)=>u,
Err(e)=>{
panic!("aaaa")
}
};
match write_stmt.execute((user.id,user.username,user.tgid,user.ping_allowed,user.active_user)){
Ok(u)=>u,
Err(e)=>{panic!("{:?}",e)}
};
}
let mut write_stmt = pool.prepare("INSERT INTO warning(id,userID,warningText,warnDate) VALUES(?,?,?,LEFT(?,19))");
let mut write_stmt = match write_stmt{
Ok(w)=>w,
Err(e)=>{panic!("{:?}", e);}
};
let mut read_stmt = conn.prepare("SELECT id,userID,warningText,warnDate FROM warning").unwrap();
let cur_iter = read_stmt.query_map(&[],|row|{
Warning{
id:row.get(0),
user_id:row.get(1),
warning_text:row.get(2),
warning_date:row.get(3)
}
}).unwrap();
for warning in cur_iter{
let warning = match warning{
Ok(w)=>w,
Err(e)=>{panic!("{:?}", e);}
};
write_stmt.execute((warning.id,warning.user_id,&warning.warning_text,&warning.warning_date)).expect(&format!("{:?}", warning));
}
let mut read_stmt = conn.prepare("SELECT id,name,userID,changeDate FROM aliases").unwrap();
let cur_iter = read_stmt.query_map(&[],|row|{
Alias{
id:row.get(0),
name:row.get(1),
user_id:row.get(2),
change_date:row.get(3)
}
}).unwrap();
let mut write_stmt = pool.prepare("INSERT INTO aliases(id,name,userID,changeDate) VALUES (?,?,?,LEFT(?,19))").expect("Failed to make write statement!");
for alias in cur_iter{
let alias = match alias{
Ok(a)=>a,
Err(e)=>{panic!("{:?}", e);}
};
write_stmt.execute((alias.id,&alias.name,alias.user_id,&alias.change_date)).expect(&format!("{:?}", alias));
}
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment