Skip to content

Commit

Permalink
Merge pull request #3 from Jupeyy/pr_sqlite_support
Browse files Browse the repository at this point in the history
Add sqlite support (to game server & base-sql lib).
  • Loading branch information
Jupeyy authored Oct 9, 2024
2 parents e71c088 + 54dd75c commit 394c342
Show file tree
Hide file tree
Showing 37 changed files with 564 additions and 148 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ jobs:
run: |
cargo build -p ddnet-account-client
cargo build -p ddnet-account-game-server
cargo build -p ddnet-account-game-server --no-default-features --features sqlite
cargo build -p ddnet-account-game-server --no-default-features --features sqlite,mysql
cargo build -p ddnet-accounts-shared
cargo build -p ddnet-account-sql
cargo build -p ddnet-account-sql --no-default-features --features sqlite
cargo build -p ddnet-account-sql --no-default-features --features sqlite,mysql
cargo build -p ddnet-accounts-types
cargo build -p ddnet-account-client-http-fs
cargo build -p ddnet-account-client-reqwest
Expand All @@ -63,13 +67,18 @@ jobs:
run: |
cargo build -p ddnet-account-client --release
cargo build -p ddnet-account-game-server --release
cargo build -p ddnet-account-game-server --release --no-default-features --features sqlite
cargo build -p ddnet-account-game-server --release --no-default-features --features sqlite,mysql
cargo build -p ddnet-accounts-shared --release
cargo build -p ddnet-account-sql --release
cargo build -p ddnet-account-sql --release --no-default-features --features sqlite
cargo build -p ddnet-account-sql --release --no-default-features --features sqlite,mysql
cargo build -p ddnet-accounts-types --release
cargo build -p ddnet-account-client-http-fs --release
cargo build -p ddnet-account-client-reqwest --release
cargo build --release
- name: Test release
run: |
cargo test --release -- --test-threads=1
cargo test --release -p ddnet-accounts -- --test-threads=1
cargo test --release -p ddnet-account-game-server --features sqlite -- --test-threads=1
36 changes: 33 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ members = [

[package]
name = "ddnet-accounts"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Jupeyy"]
license = "MIT OR Apache-2.0"
Expand All @@ -25,7 +25,7 @@ name = "account-server"
[dependencies]
ddnet-accounts-types = { version = "0.1.0", path = "lib/ddnet-accounts-types" }
ddnet-accounts-shared = { version = "0.1.0", path = "lib/ddnet-accounts-shared" }
ddnet-account-sql = { version = "0.1.0", path = "lib/ddnet-account-sql" }
ddnet-account-sql = { version = "0.2.0", path = "lib/ddnet-account-sql", features = ["mysql"] }

tokio = { version = "1.39.3", features = ["rt-multi-thread", "sync", "fs", "time", "macros"] }
axum = "0.7.5"
Expand Down Expand Up @@ -64,10 +64,9 @@ notify = { version = "6.1.1", default-features = false, features = ["macos_kqueu

[dev-dependencies]
ddnet-account-client = { version = "0.1.0", path = "lib/ddnet-account-client" }
ddnet-account-game-server = { version = "0.1.0", path = "lib/ddnet-account-game-server" }
ddnet-account-game-server = { version = "0.2.0", path = "lib/ddnet-account-game-server" }
ddnet-account-client-http-fs = { version = "0.1.0", path = "lib/ddnet-account-client-http-fs" }
ddnet-account-client-reqwest = { version = "0.1.0", path = "lib/ddnet-account-client-reqwest" }

regex = "1.10.6"
tempfile = "3.12.0"

16 changes: 13 additions & 3 deletions lib/ddnet-account-game-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ddnet-account-game-server"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Jupeyy"]
license = "MIT OR Apache-2.0"
Expand All @@ -10,10 +10,20 @@ repository = "https://github.com/ddnet/ddnet-accounts"
[dependencies]
ddnet-accounts-types = { version = "0.1.0", path = "../ddnet-accounts-types" }
ddnet-accounts-shared = { version = "0.1.0", path = "../ddnet-accounts-shared" }
ddnet-account-sql = { version = "0.1.0", path = "../ddnet-account-sql" }
ddnet-account-sql = { version = "0.2.0", path = "../ddnet-account-sql", default-features = false }

# https://github.com/launchbadge/sqlx/issues/2636
sqlx = { version = "=0.6.3", features = ["mysql", "any", "runtime-tokio-rustls", "chrono"] }
sqlx = { version = "=0.6.3", features = ["any", "runtime-tokio-rustls", "chrono"] }
anyhow = { version = "1.0.86", features = ["backtrace"] }
async-trait = "0.1.81"
thiserror = "1.0.63"

[dev-dependencies]
tokio = { version = "1.39.3", features = ["rt-multi-thread", "sync", "fs", "time", "macros"] }
anyhow = { version = "1.0.86", features = ["backtrace"] }

[features]
mysql = ["ddnet-account-sql/mysql", "sqlx/mysql"]
sqlite = ["ddnet-account-sql/sqlite", "sqlx/sqlite"]

default = ["mysql"]
2 changes: 1 addition & 1 deletion lib/ddnet-account-game-server/src/auto_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub async fn auto_login(
};

let res = qry
.query(&shared.db.register_user_statement)
.query(con, &shared.db.register_user_statement)
.execute(&mut *con)
.await
.map_err(|err| AutoLoginError::Database(err.into()))?;
Expand Down
23 changes: 21 additions & 2 deletions lib/ddnet-account-game-server/src/auto_login/queries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ddnet_account_sql::query::Query;
use ddnet_accounts_types::account_id::AccountId;
use anyhow::anyhow;
use async_trait::async_trait;
use ddnet_account_sql::query::Query;
use ddnet_accounts_types::account_id::AccountId;
use sqlx::any::AnyRow;
use sqlx::Executor;
use sqlx::Statement;
Expand All @@ -18,13 +18,23 @@ pub struct RegisterUser<'a> {

#[async_trait]
impl<'a> Query<()> for RegisterUser<'a> {
#[cfg(feature = "mysql")]
async fn prepare_mysql(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
Ok(connection
.prepare(include_str!("mysql/try_insert_user.sql"))
.await?)
}
#[cfg(feature = "sqlite")]
async fn prepare_sqlite(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
Ok(connection
.prepare(include_str!("sqlite/try_insert_user.sql"))
.await?)
}
#[cfg(feature = "mysql")]
fn query_mysql<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
Expand All @@ -33,6 +43,15 @@ impl<'a> Query<()> for RegisterUser<'a> {

statement.query().bind(self.default_name).bind(account_id)
}
#[cfg(feature = "sqlite")]
fn query_sqlite<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Any, sqlx::any::AnyArguments<'b>> {
let account_id = self.account_id;

statement.query().bind(self.default_name).bind(account_id)
}
fn row_data(_row: &AnyRow) -> anyhow::Result<()> {
Err(anyhow!(
"Data rows are not supported for this query.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT
OR IGNORE INTO user (
name,
account_id,
create_time
)
VALUES
(
?,
?,
datetime('now')
);
6 changes: 6 additions & 0 deletions lib/ddnet-account-game-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#![deny(clippy::nursery)]
#![deny(clippy::all)]

#[cfg(not(any(feature = "mysql", feature = "sqlite")))]
std::compile_error!("at least the mysql or sqlite feature must be used.");

/// Data types and operations related to
/// logging in a user to the game server.
pub mod auto_login;
Expand All @@ -27,3 +30,6 @@ pub mod setup;
/// Shared data that is used in the game
/// server implementation.
pub mod shared;

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion lib/ddnet-account-game-server/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub async fn rename(
let qry = RenameUser { account_id, name };

let res = qry
.query(&shared.db.try_rename_statement)
.query(con, &shared.db.try_rename_statement)
.execute(&mut *con)
.await;

Expand Down
23 changes: 21 additions & 2 deletions lib/ddnet-account-game-server/src/rename/queries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ddnet_account_sql::query::Query;
use ddnet_accounts_types::account_id::AccountId;
use anyhow::anyhow;
use async_trait::async_trait;
use ddnet_account_sql::query::Query;
use ddnet_accounts_types::account_id::AccountId;
use sqlx::any::AnyRow;
use sqlx::Executor;
use sqlx::Statement;
Expand All @@ -18,13 +18,23 @@ pub struct RenameUser<'a> {

#[async_trait]
impl<'a> Query<()> for RenameUser<'a> {
#[cfg(feature = "mysql")]
async fn prepare_mysql(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
Ok(connection
.prepare(include_str!("mysql/try_rename.sql"))
.await?)
}
#[cfg(feature = "sqlite")]
async fn prepare_sqlite(
connection: &mut sqlx::AnyConnection,
) -> anyhow::Result<sqlx::any::AnyStatement<'static>> {
Ok(connection
.prepare(include_str!("sqlite/try_rename.sql"))
.await?)
}
#[cfg(feature = "mysql")]
fn query_mysql<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
Expand All @@ -33,6 +43,15 @@ impl<'a> Query<()> for RenameUser<'a> {

statement.query().bind(self.name).bind(account_id)
}
#[cfg(feature = "sqlite")]
fn query_sqlite<'b>(
&'b self,
statement: &'b sqlx::any::AnyStatement<'static>,
) -> sqlx::query::Query<'b, sqlx::Any, sqlx::any::AnyArguments<'b>> {
let account_id = self.account_id;

statement.query().bind(self.name).bind(account_id)
}
fn row_data(_row: &AnyRow) -> anyhow::Result<()> {
Err(anyhow!(
"Data rows are not supported for this query.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE
user
SET
name = ?
WHERE
user.account_id = ?;
Loading

0 comments on commit 394c342

Please sign in to comment.