@@ -13,11 +13,14 @@ use crate::databases::database::{Database, Error};
1313use crate :: protocol:: common:: { InfoHash , AUTH_KEY_LENGTH } ;
1414use crate :: tracker:: key:: AuthKey ;
1515
16- pub struct MysqlDatabase {
16+ pub struct Mysql {
1717 pool : Pool < MysqlConnectionManager > ,
1818}
1919
20- impl MysqlDatabase {
20+ impl Mysql {
21+ /// # Errors
22+ ///
23+ /// Will return `r2d2::Error` if `db_path` is not able to create `MySQL` database.
2124 pub fn new ( db_path : & str ) -> Result < Self , r2d2:: Error > {
2225 let opts = Opts :: from_url ( db_path) . expect ( "Failed to connect to MySQL database." ) ;
2326 let builder = OptsBuilder :: from_opts ( opts) ;
@@ -31,7 +34,7 @@ impl MysqlDatabase {
3134}
3235
3336#[ async_trait]
34- impl Database for MysqlDatabase {
37+ impl Database for Mysql {
3538 fn create_database_tables ( & self ) -> Result < ( ) , database:: Error > {
3639 let create_whitelist_table = "
3740 CREATE TABLE IF NOT EXISTS whitelist (
@@ -57,7 +60,7 @@ impl Database for MysqlDatabase {
5760 PRIMARY KEY (`id`),
5861 UNIQUE (`key`)
5962 );" ,
60- AUTH_KEY_LENGTH as i8
63+ i8 :: try_from ( AUTH_KEY_LENGTH ) . expect ( "Auth Key Length Should fit within a i8!" )
6164 ) ;
6265
6366 let mut conn = self . pool . get ( ) . map_err ( |_| database:: Error :: DatabaseError ) ?;
@@ -95,7 +98,7 @@ impl Database for MysqlDatabase {
9598 "SELECT `key`, valid_until FROM `keys`" ,
9699 |( key, valid_until) : ( String , i64 ) | AuthKey {
97100 key,
98- valid_until : Some ( Duration :: from_secs ( valid_until as u64 ) ) ,
101+ valid_until : Some ( Duration :: from_secs ( valid_until. unsigned_abs ( ) ) ) ,
99102 } ,
100103 )
101104 . map_err ( |_| database:: Error :: QueryReturnedNoRows ) ?;
@@ -188,7 +191,7 @@ impl Database for MysqlDatabase {
188191 {
189192 Some ( ( key, valid_until) ) => Ok ( AuthKey {
190193 key,
191- valid_until : Some ( Duration :: from_secs ( valid_until as u64 ) ) ,
194+ valid_until : Some ( Duration :: from_secs ( valid_until. unsigned_abs ( ) ) ) ,
192195 } ) ,
193196 None => Err ( database:: Error :: InvalidQuery ) ,
194197 }
0 commit comments