generated from TobiasDeBruijn/BaseBukkitPlugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Switch to SQLx - Cleanup code - Fix bugs - Support custom database port
- Loading branch information
1 parent
3c50504
commit 2c12eac
Showing
19 changed files
with
783 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pluginVersion = 1.7.9 | ||
pluginVersion = 1.7.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "skinfixer" | ||
version = "0.1.0" | ||
authors = ["Tobias de Bruijn <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "skinfixer" | ||
|
@@ -14,26 +14,11 @@ jni = "0.19.0" | |
postgres = "0.19.1" | ||
bincode = "1.3.3" | ||
lazy_static = "1.4.0" | ||
|
||
[dependencies.mysql] | ||
version = "23.0.0" | ||
default-features = false | ||
|
||
[dependencies.flate2] | ||
version = "1.0.22" | ||
features = ["zlib"] | ||
|
||
[dependencies.rusqlite] | ||
version = "0.25.1" | ||
features = ["bundled"] | ||
|
||
[dependencies.serde] | ||
version = "1.0.126" | ||
features = ["derive"] | ||
|
||
[dependencies.typenum] | ||
version = "1.14.0" | ||
features = ["no_std"] | ||
sqlx = { version = "0.7.2", features = ["any", "mysql", "postgres", "sqlite", "runtime-tokio-rustls"] } | ||
serde = { version = "1.0.188", features = ["derive"] } | ||
typenum = { version = "1.17.0", features = ["no_std"] } | ||
tokio = { version = "1.33.0", features = ["fs", "rt", "rt-multi-thread"] } | ||
thiserror = "1.0.49" | ||
|
||
[profile.release] | ||
lto = true | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use crate::database::profile::BinProfile; | ||
use serde::de::DeserializeOwned; | ||
use serde::Serialize; | ||
use sqlx::mysql::MySqlConnectOptions; | ||
use sqlx::postgres::PgConnectOptions; | ||
use sqlx::sqlite::SqliteConnectOptions; | ||
use sqlx::{MySqlPool, PgPool, SqlitePool}; | ||
use std::path::{Path, PathBuf}; | ||
use thiserror::Error; | ||
use tokio::fs; | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
|
||
mod profile; | ||
pub use profile::*; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum DatabaseError { | ||
#[error("{0}")] | ||
Sql(#[from] sqlx::Error), | ||
#[error("{0}")] | ||
Bin(#[from] BinError), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum Driver { | ||
Mysql(MySqlPool), | ||
Postgres(PgPool), | ||
Sqlite(SqlitePool), | ||
Bin(PathBuf), | ||
} | ||
|
||
pub enum DriverType<'a> { | ||
Mysql(DatabaseOptions<'a>), | ||
Postgres(DatabaseOptions<'a>), | ||
Sqlite(PathBuf), | ||
Bin(PathBuf), | ||
} | ||
|
||
pub struct DatabaseOptions<'a> { | ||
pub name: &'a str, | ||
pub user: &'a str, | ||
pub passw: &'a str, | ||
pub host: &'a str, | ||
pub port: u16, | ||
} | ||
|
||
impl Driver { | ||
pub async fn new(driver_type: DriverType<'_>) -> Result<Self, DatabaseError> { | ||
let driver = match driver_type { | ||
DriverType::Mysql(c) => Self::new_mysql(c).await?, | ||
DriverType::Postgres(c) => Self::new_postgres(c).await?, | ||
DriverType::Sqlite(c) => Self::new_sqlite(&c).await?, | ||
DriverType::Bin(c) => Self::new_bin(c).await?, | ||
}; | ||
|
||
Ok(driver) | ||
} | ||
|
||
async fn new_mysql(options: DatabaseOptions<'_>) -> Result<Self, sqlx::Error> { | ||
let opts = MySqlConnectOptions::new() | ||
.database(options.name) | ||
.username(options.user) | ||
.password(options.passw) | ||
.host(options.host) | ||
.port(options.port); | ||
|
||
let pool = MySqlPool::connect_with(opts).await?; | ||
Ok(Self::Mysql(pool)) | ||
} | ||
|
||
async fn new_postgres(options: DatabaseOptions<'_>) -> Result<Self, sqlx::Error> { | ||
let opts = PgConnectOptions::new() | ||
.database(options.name) | ||
.username(options.user) | ||
.password(options.passw) | ||
.host(options.host) | ||
.port(options.port); | ||
|
||
let pool = PgPool::connect_with(opts).await?; | ||
Ok(Self::Postgres(pool)) | ||
} | ||
|
||
async fn new_sqlite(path: &Path) -> Result<Self, sqlx::Error> { | ||
let opts = SqliteConnectOptions::new().filename(path); | ||
|
||
let pool = SqlitePool::connect_with(opts).await?; | ||
Ok(Self::Sqlite(pool)) | ||
} | ||
|
||
async fn new_bin(path: PathBuf) -> Result<Self, BinError> { | ||
if !path.exists() { | ||
if let Some(parent) = path.parent() { | ||
fs::create_dir_all(parent).await?; | ||
} | ||
|
||
BinFile::write::<Vec<BinProfile>>(&path, &vec![]).await?; | ||
} | ||
|
||
Ok(Self::Bin(path)) | ||
} | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum BinError { | ||
#[error("IO Error: {0}")] | ||
Io(#[from] std::io::Error), | ||
#[error("Binary (de)serialization error: {0}")] | ||
Bin(#[from] bincode::Error), | ||
} | ||
|
||
pub struct BinFile; | ||
|
||
impl BinFile { | ||
async fn read<T: DeserializeOwned>(path: &Path) -> Result<T, BinError> { | ||
let mut file = fs::File::open(path).await?; | ||
let mut buf = Vec::new(); | ||
file.read_to_end(&mut buf).await?; | ||
|
||
let deserialized: T = bincode::deserialize(&buf)?; | ||
|
||
Ok(deserialized) | ||
} | ||
|
||
async fn write<T: Serialize>(path: &Path, content: &T) -> Result<(), BinError> { | ||
let mut file = fs::File::create(path).await?; | ||
let buf = bincode::serialize(content)?; | ||
|
||
file.write_all(&buf).await?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.