1 /* 2 * WARNING: automatically generated by ort 0.14.1. 3 * DO NOT EDIT! 4 */ 5 #[macro_use] 6 extern crate num_derive; 7 8 pub mod ort { 9 use libsqlite3_sys; 10 use rusqlite::{Connection,Result,params,Row}; 11 use bcrypt::{hash,verify}; 12 use num_traits::{FromPrimitive,ToPrimitive}; 13 14 pub const VERSION: &str = "0.14.1"; 15 pub const VSTAMP: i64 = 11502; 16 17 pub mod data { 18 use base64; 19 fn json_escape(src: &str) -> String { 20 let mut escaped = String::with_capacity(src.len() + 2); 21 escaped.push('"'); 22 for c in src.chars() { 23 match c { 24 '\x08' => escaped += "\\b", 25 '\x0c' => escaped += "\\f", 26 '\n' => escaped += "\\n", 27 '\r' => escaped += "\\r", 28 '\t' => escaped += "\\t", 29 '"' => escaped += "\\\"", 30 '\\' => escaped += "\\", 31 c => escaped.push(c), 32 } 33 } 34 escaped.push('"'); 35 escaped 36 } 37 #[derive(FromPrimitive,ToPrimitive,PartialEq,Debug)] 38 pub enum Sex { 39 Male = 0, 40 Female = 1, 41 Other = 2, 42 } 43 pub struct Company { 44 pub name: String, 45 pub id: i64, 46 pub somenum: Option<i64>, 47 } 48 impl Company { 49 pub(super) fn to_json(&self) -> String { 50 let mut ret = String::new(); 51 ret += "\"name\":"; 52 ret += &json_escape(&self.name); 53 ret += ",\"id\":"; 54 ret += &format!("\"{}\"", self.id); 55 ret += ",\"somenum\":"; 56 if let Some(x) = self.somenum { 57 ret += &format!("\"{}\"", x); 58 } else { 59 ret += "null"; 60 } 61 ret 62 } 63 } 64 pub struct User { 65 pub company: Company, 66 pub cid: i64, 67 pub sex: Sex, 68 pub hash: String, 69 pub email: String, 70 pub image: Option<Vec<u8>>, 71 pub name: String, 72 pub uid: i64, 73 } 74 impl User { 75 pub(super) fn to_json(&self) -> String { 76 let mut ret = String::new(); 77 ret += "\"company\":"; 78 ret += "{"; 79 ret += &self.company.to_json(); 80 ret += "}"; 81 ret += ",\"cid\":"; 82 ret += &format!("\"{}\"", self.cid); 83 ret += ",\"sex\":"; 84 ret += &format!("\"{}\"", num_traits::ToPrimitive::to_i64(&self.sex).unwrap()); 85 ret += ",\"email\":"; 86 ret += &json_escape(&self.email); 87 ret += ",\"image\":"; 88 if let Some(ref x) = &self.image { 89 ret += "\""; 90 ret += &base64::encode(x); 91 ret += "\""; 92 } else { 93 ret += "null"; 94 } 95 ret += ",\"name\":"; 96 ret += &json_escape(&self.name); 97 ret += ",\"uid\":"; 98 ret += &format!("\"{}\"", self.uid); 99 ret 100 } 101 } 102 pub struct Session { 103 pub user: User, 104 pub userid: i64, 105 pub token: i64, 106 pub mtime: i64, 107 pub id: i64, 108 } 109 impl Session { 110 pub(super) fn to_json(&self) -> String { 111 let mut ret = String::new(); 112 ret += "\"user\":"; 113 ret += "{"; 114 ret += &self.user.to_json(); 115 ret += "}"; 116 ret += ",\"userid\":"; 117 ret += &format!("\"{}\"", self.userid); 118 ret += ",\"token\":"; 119 ret += &format!("\"{}\"", self.token); 120 ret += ",\"mtime\":"; 121 ret += &format!("\"{}\"", self.mtime); 122 ret += ",\"id\":"; 123 ret += &format!("\"{}\"", self.id); 124 ret 125 } 126 } 127 } 128 129 pub mod objs { 130 pub struct Company { 131 pub data: super::data::Company, 132 } 133 impl Company { 134 pub fn export(&self) -> String { 135 let mut ret = String::new(); 136 ret += "{"; 137 ret += &self.data.to_json(); 138 ret += "}"; 139 ret 140 } 141 } 142 pub struct User { 143 pub data: super::data::User, 144 } 145 impl User { 146 pub fn export(&self) -> String { 147 let mut ret = String::new(); 148 ret += "{"; 149 ret += &self.data.to_json(); 150 ret += "}"; 151 ret 152 } 153 } 154 pub struct Session { 155 pub data: super::data::Session, 156 } 157 impl Session { 158 pub fn export(&self) -> String { 159 let mut ret = String::new(); 160 ret += "{"; 161 ret += &self.data.to_json(); 162 ret += "}"; 163 ret 164 } 165 } 166 } 167 168 pub(self) mod stmt { 169 #[allow(dead_code)] 170 pub enum Ortstmt { 171 CompanyByUniqueId, 172 CompanyBySearch0, 173 CompanyInsert, 174 CompanyDelete0, 175 UserByUniqueEmail, 176 UserByUniqueUid, 177 UserBySearch0, 178 UserBySearch1, 179 UserBySearch2, 180 UserInsert, 181 UserUpdate0, 182 UserUpdate1, 183 UserDelete0, 184 SessionByUniqueId, 185 SessionBySearch0, 186 SessionInsert, 187 SessionDelete0, 188 } 189 190 fn stmt_company(v: &str) -> String { 191 let mut s = String::new(); 192 s += &format!("{}.name, ", v); 193 s += &format!("{}.id, ", v); 194 s += &format!("{}.somenum", v); 195 s 196 } 197 198 fn stmt_user(v: &str) -> String { 199 let mut s = String::new(); 200 s += &format!("{}.cid, ", v); 201 s += &format!("{}.sex, ", v); 202 s += &format!("{}.hash, ", v); 203 s += &format!("{}.email, ", v); 204 s += &format!("{}.image, ", v); 205 s += &format!("{}.name, ", v); 206 s += &format!("{}.uid", v); 207 s 208 } 209 210 fn stmt_session(v: &str) -> String { 211 let mut s = String::new(); 212 s += &format!("{}.userid, ", v); 213 s += &format!("{}.token, ", v); 214 s += &format!("{}.mtime, ", v); 215 s += &format!("{}.id", v); 216 s 217 } 218 219 pub fn stmt_fmt(v: Ortstmt) -> String { 220 let s; 221 match v { 222 Ortstmt::CompanyByUniqueId => { 223 s = String::new() + "SELECT " + &stmt_company("company") + " FROM company WHERE company.id = ?"; }, 224 Ortstmt::CompanyBySearch0 => { 225 s = String::new() + "SELECT " + &stmt_company("company") + " FROM company " 226 + "WHERE company.somenum ISNULL"; }, 227 Ortstmt::CompanyInsert => { 228 s = String::new() + "INSERT INTO company " 229 + "(name,somenum) VALUES (?," 230 + " ?)"; }, 231 Ortstmt::CompanyDelete0 => { 232 s = String::new() + "DELETE FROM company"; }, 233 Ortstmt::UserByUniqueEmail => { 234 s = String::new() + "SELECT " + &stmt_user("user") + "," 235 + &stmt_company("_a") + " FROM user " 236 + "INNER JOIN company AS _a ON _a.id=user.cid " 237 + "WHERE user.email = ?"; }, 238 Ortstmt::UserByUniqueUid => { 239 s = String::new() + "SELECT " + &stmt_user("user") + "," 240 + &stmt_company("_a") + " FROM user " 241 + "INNER JOIN company AS _a ON _a.id=user.cid " 242 + "WHERE user.uid = ?"; }, 243 Ortstmt::UserBySearch0 => { 244 s = String::new() + "SELECT " + &stmt_user("user") + "," 245 + &stmt_company("_a") + " FROM user " 246 + "INNER JOIN company AS _a ON _a.id=user.cid " 247 + "WHERE user.name = ? LIMIT 5"; }, 248 Ortstmt::UserBySearch1 => { 249 s = String::new() + "SELECT " + &stmt_user("user") + "," 250 + &stmt_company("_a") + " FROM user " 251 + "INNER JOIN company AS _a ON _a.id=user.cid " 252 + "WHERE user.email = ?"; }, 253 Ortstmt::UserBySearch2 => { 254 s = String::new() + "SELECT " + &stmt_user("user") + "," 255 + &stmt_company("_a") + " FROM user " 256 + "INNER JOIN company AS _a ON _a.id=user.cid " 257 + "WHERE user.uid = ? ORDER BY _a.name ASC"; }, 258 Ortstmt::UserInsert => { 259 s = String::new() + "INSERT INTO user " 260 + "(cid,sex,hash,email,image," 261 + " name) VALUES (?,?,?,?,?," 262 + " ?)"; }, 263 Ortstmt::UserUpdate0 => { 264 s = String::new() + "UPDATE user SET hash = ? WHERE uid = ?"; }, 265 Ortstmt::UserUpdate1 => { 266 s = String::new() + "UPDATE user SET email = ? WHERE uid = ?"; }, 267 Ortstmt::UserDelete0 => { 268 s = String::new() + "DELETE FROM user"; }, 269 Ortstmt::SessionByUniqueId => { 270 s = String::new() + "SELECT " + &stmt_session("session") + "," 271 + &stmt_user("_b") + "," 272 + &stmt_company("_c") + " FROM session " 273 + "INNER JOIN user AS _b ON _b.uid=session.userid " 274 + "INNER JOIN company AS _c ON _c.id=_b.cid " 275 + "WHERE session.id = ?"; }, 276 Ortstmt::SessionBySearch0 => { 277 s = String::new() + "SELECT " + &stmt_session("session") + "," 278 + &stmt_user("_b") + "," 279 + &stmt_company("_c") + " FROM session " 280 + "INNER JOIN user AS _b ON _b.uid=session.userid " 281 + "INNER JOIN company AS _c ON _c.id=_b.cid " 282 + "WHERE _c.name = ? AND session.mtime = ?"; }, 283 Ortstmt::SessionInsert => { 284 s = String::new() + "INSERT INTO session " 285 + "(userid,token,mtime) VALUES " 286 + "(?,?,?)"; }, 287 Ortstmt::SessionDelete0 => { 288 s = String::new() + "DELETE FROM session WHERE id = ?"; }, 289 } 290 s 291 } 292 } 293 294 pub struct Ortctx { 295 args: Ortargs, 296 conn: Connection, 297 } 298 299 impl Ortctx { 300 fn db_company_fill(&self, row: &Row, i: &mut usize) -> Result<data::Company> { 301 let ncol: usize = *i; 302 *i += 3; 303 Ok(data::Company { 304 name: row.get(ncol + 0)?, 305 id: row.get(ncol + 1)?, 306 somenum: row.get(ncol + 2)?, 307 }) 308 } 309 pub fn db_company_insert(&self, v1: &String, v2: Option<i64>) -> Result<i64> { 310 let sql = stmt::stmt_fmt(stmt::Ortstmt::CompanyInsert); 311 let mut stmt = self.conn.prepare(&sql)?; 312 match stmt.insert(params![ 313 v1, 314 v2, 315 ]) { 316 Ok(i) => Ok(i), 317 Err(e) => match e { 318 rusqlite::Error::SqliteFailure(err, ref _desc) => match err.code { 319 libsqlite3_sys::ErrorCode::ConstraintViolation => Ok(-1), 320 _ => Err(e), 321 }, 322 _ => Err(e), 323 }, 324 } 325 } 326 pub fn db_company_list_by_somenum_isnull(&self) -> Result<Vec<objs::Company>> { 327 let sql = stmt::stmt_fmt(stmt::Ortstmt::CompanyBySearch0); 328 let mut stmt = self.conn.prepare(&sql)?; 329 let mut rows = stmt.query(params![ 330 ])?; 331 let mut vec = Vec::new(); 332 while let Some(row) = rows.next()? { 333 let mut i = 0; 334 let obj = self.db_company_fill(&row, &mut i)?; 335 vec.push(objs::Company { 336 data: obj, 337 }); 338 } 339 Ok(vec) 340 } 341 pub fn db_company_delete(&self, ) -> Result<()> { 342 let sql = stmt::stmt_fmt(stmt::Ortstmt::CompanyDelete0); 343 let mut stmt = self.conn.prepare(&sql)?; 344 match stmt.execute(params![ 345 ]) { 346 Ok(_) => Ok(()), 347 Err(e) => Err(e), 348 } 349 } 350 fn db_user_fill(&self, row: &Row, i: &mut usize) -> Result<data::User> { 351 let ncol: usize = *i; 352 *i += 7; 353 let obj0 = self.db_company_fill(row, i)?; 354 let obj1 = row.get(ncol + 1)?; 355 Ok(data::User { 356 company: obj0, 357 cid: row.get(ncol + 0)?, 358 sex: FromPrimitive::from_i64(obj1).ok_or(rusqlite::Error::IntegralValueOutOfRange(ncol + 1, obj1))?, 359 hash: row.get(ncol + 2)?, 360 email: row.get(ncol + 3)?, 361 image: row.get(ncol + 4)?, 362 name: row.get(ncol + 5)?, 363 uid: row.get(ncol + 6)?, 364 }) 365 } 366 pub fn db_user_insert(&self, v1: i64, v2: data::Sex, v3: &String, v4: &String, v5: Option<&Vec<u8>>, v6: &String) -> Result<i64> { 367 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserInsert); 368 let hash1 = hash(v3, self.args.bcrypt_cost).unwrap(); 369 let mut stmt = self.conn.prepare(&sql)?; 370 match stmt.insert(params![ 371 v1, 372 ToPrimitive::to_i64(&v2).unwrap(), 373 hash1, 374 v4, 375 v5, 376 v6, 377 ]) { 378 Ok(i) => Ok(i), 379 Err(e) => match e { 380 rusqlite::Error::SqliteFailure(err, ref _desc) => match err.code { 381 libsqlite3_sys::ErrorCode::ConstraintViolation => Ok(-1), 382 _ => Err(e), 383 }, 384 _ => Err(e), 385 }, 386 } 387 } 388 pub fn db_user_iterate_by_name_eq(&self, cb: fn(res: objs::User), v1: &String) -> Result<()> { 389 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserBySearch0); 390 let mut stmt = self.conn.prepare(&sql)?; 391 let mut rows = stmt.query(params![ 392 v1, 393 ])?; 394 while let Some(row) = rows.next()? { 395 let mut i = 0; 396 let obj = self.db_user_fill(&row, &mut i)?; 397 cb(objs::User { 398 data: obj, 399 }); 400 } 401 Ok(()) 402 } 403 pub fn db_user_get_creds(&self, v1: &String, v2: &String) -> Result<Option<objs::User>> { 404 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserBySearch1); 405 let mut stmt = self.conn.prepare(&sql)?; 406 let mut rows = stmt.query(params![ 407 v1, 408 ])?; 409 if let Some(row) = rows.next()? { 410 let mut i = 0; 411 let obj = self.db_user_fill(&row, &mut i)?; 412 if !verify(v2, &obj.hash).unwrap() { 413 return Ok(None); 414 } 415 return Ok(Some(objs::User { 416 data: obj, 417 })); 418 } 419 Ok(None) 420 } 421 pub fn db_user_get_by_uid_eq(&self, v1: i64) -> Result<Option<objs::User>> { 422 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserBySearch2); 423 let mut stmt = self.conn.prepare(&sql)?; 424 let mut rows = stmt.query(params![ 425 v1, 426 ])?; 427 if let Some(row) = rows.next()? { 428 let mut i = 0; 429 let obj = self.db_user_fill(&row, &mut i)?; 430 return Ok(Some(objs::User { 431 data: obj, 432 })); 433 } 434 Ok(None) 435 } 436 pub fn db_user_delete(&self, ) -> Result<()> { 437 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserDelete0); 438 let mut stmt = self.conn.prepare(&sql)?; 439 match stmt.execute(params![ 440 ]) { 441 Ok(_) => Ok(()), 442 Err(e) => Err(e), 443 } 444 } 445 pub fn db_user_update_hash_set_by_uid_eq(&self, v1: &String, v2: i64) -> Result<bool> { 446 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserUpdate0); 447 let hash1 = hash(v1, self.args.bcrypt_cost).unwrap(); 448 let mut stmt = self.conn.prepare(&sql)?; 449 match stmt.execute(params![ 450 hash1, 451 v2, 452 ]) { 453 Ok(_) => Ok(true), 454 Err(e) => match e { 455 rusqlite::Error::SqliteFailure(err, ref _desc) => match err.code { 456 libsqlite3_sys::ErrorCode::ConstraintViolation => Ok(false), 457 _ => Err(e), 458 }, 459 _ => Err(e), 460 }, 461 } 462 } 463 pub fn db_user_update_email_set_by_uid_eq(&self, v1: &String, v2: i64) -> Result<bool> { 464 let sql = stmt::stmt_fmt(stmt::Ortstmt::UserUpdate1); 465 let mut stmt = self.conn.prepare(&sql)?; 466 match stmt.execute(params![ 467 v1, 468 v2, 469 ]) { 470 Ok(_) => Ok(true), 471 Err(e) => match e { 472 rusqlite::Error::SqliteFailure(err, ref _desc) => match err.code { 473 libsqlite3_sys::ErrorCode::ConstraintViolation => Ok(false), 474 _ => Err(e), 475 }, 476 _ => Err(e), 477 }, 478 } 479 } 480 fn db_session_fill(&self, row: &Row, i: &mut usize) -> Result<data::Session> { 481 let ncol: usize = *i; 482 *i += 4; 483 let obj0 = self.db_user_fill(row, i)?; 484 Ok(data::Session { 485 user: obj0, 486 userid: row.get(ncol + 0)?, 487 token: row.get(ncol + 1)?, 488 mtime: row.get(ncol + 2)?, 489 id: row.get(ncol + 3)?, 490 }) 491 } 492 pub fn db_session_insert(&self, v1: i64, v2: i64, v3: i64) -> Result<i64> { 493 let sql = stmt::stmt_fmt(stmt::Ortstmt::SessionInsert); 494 let mut stmt = self.conn.prepare(&sql)?; 495 match stmt.insert(params![ 496 v1, 497 v2, 498 v3, 499 ]) { 500 Ok(i) => Ok(i), 501 Err(e) => match e { 502 rusqlite::Error::SqliteFailure(err, ref _desc) => match err.code { 503 libsqlite3_sys::ErrorCode::ConstraintViolation => Ok(-1), 504 _ => Err(e), 505 }, 506 _ => Err(e), 507 }, 508 } 509 } 510 pub fn db_session_iterate_foo(&self, cb: fn(res: objs::Session), v1: &String, v2: i64) -> Result<()> { 511 let sql = stmt::stmt_fmt(stmt::Ortstmt::SessionBySearch0); 512 let mut stmt = self.conn.prepare(&sql)?; 513 let mut rows = stmt.query(params![ 514 v1, 515 v2, 516 ])?; 517 while let Some(row) = rows.next()? { 518 let mut i = 0; 519 let obj = self.db_session_fill(&row, &mut i)?; 520 cb(objs::Session { 521 data: obj, 522 }); 523 } 524 Ok(()) 525 } 526 pub fn db_session_delete_by_id_eq(&self, v1: i64) -> Result<()> { 527 let sql = stmt::stmt_fmt(stmt::Ortstmt::SessionDelete0); 528 let mut stmt = self.conn.prepare(&sql)?; 529 match stmt.execute(params![ 530 v1, 531 ]) { 532 Ok(_) => Ok(()), 533 Err(e) => Err(e), 534 } 535 } 536 pub(self) fn new(dbname: &str, args: &Ortargs) -> Result<Ortctx> { 537 let conn = Connection::open(dbname)?; 538 conn.execute("PRAGMA foreign_keys=ON", [])?; 539 Ok(Ortctx { 540 args: *args, 541 conn, 542 }) 543 } 544 } 545 546 #[derive(Copy, Clone)] 547 pub struct Ortargs { 548 pub bcrypt_cost: u32, 549 } 550 551 pub struct Ortdb { 552 dbname: String, 553 args: Ortargs, 554 } 555 556 impl Ortdb { 557 pub fn new(dbname: &str) -> Ortdb { 558 Ortdb { 559 dbname: dbname.to_string(), 560 args: Ortargs { 561 bcrypt_cost: bcrypt::DEFAULT_COST, 562 } 563 } 564 } 565 pub fn new_with_args(dbname: &str, args: Ortargs) -> Ortdb { 566 Ortdb { 567 dbname: dbname.to_string(), 568 args, 569 } 570 } 571 pub fn connect(&self) -> Result<Ortctx> { 572 Ortctx::new(&self.dbname, &self.args) 573 } 574 } 575 }