Skip to content

Commit d1385e1

Browse files
committed
sqlx pgsql 测试完成
1 parent a46c1d5 commit d1385e1

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

templates/classic/_base/src/main.rs.liquid

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ use scheduled_thread_pool::ScheduledThreadPool;
88
{%- endif %}
99
{%- endif %}
1010
{%- if db_lib == "sqlx" %}
11+
{%- if db_type == "sqlite" %}
1112
use sqlx::SqlitePool;
13+
{%- elsif db_type == "postgres" %}
14+
use sqlx::PgPool;
15+
{%- elsif db_type == "mysql" %}
16+
use sqlx::mysql::MySqlPool;
17+
{%- endif %}
1218
{%- endif %}
1319
use dotenvy::dotenv;
1420
use salvo::catcher::Catcher;
@@ -87,12 +93,18 @@ async fn main() {
8793
{%- endif %}
8894

8995
{%- elsif db_lib == "sqlx" %}
96+
{%- if db_type == "sqlite" %}
9097
let sqlx_pool = SqlitePool::connect(&config::get().db.url)
98+
{%- elsif db_type == "postgres" %}
99+
let sqlx_pool = PgPool::connect(&config::get().db.url)
100+
{%- elsif db_type == "mysql" %}
101+
let sqlx_pool = MySqlPool::connect(&config::get().db.url)
102+
{%- endif %}
91103
.await
92104
.expect("Database connection failed.");
93105
crate::db::SQLX_POOL
94106
.set(sqlx_pool)
95-
.expect("diesel pool should be set");
107+
.expect("sqlx pool should be set");
96108
{%- endif %}
97109

98110
let _guard = config.log.guard();

templates/classic/_base/src/routers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn root() -> Router {
3030
.get(user::list_users)
3131
.post(user::create_user)
3232
.push(
33-
Router::with_path("{id}")
33+
Router::with_path("{user_id}")
3434
.put(user::update_user)
3535
.delete(user::delete_user),
3636
),

templates/classic/sqlx/src/config/db_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ fn default_connection_timeout() -> u64 {
5353
}
5454
fn default_statement_timeout() -> u64 {
5555
30000
56-
}
56+
}

templates/classic/sqlx/src/db/mod.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::sync::OnceLock;
2+
3+
{%- if db_type == "sqlite" %}
4+
use sqlx::SqlitePool;
5+
pub static SQLX_POOL: OnceLock<SqlitePool> = OnceLock::new();
6+
pub fn pool() -> &'static SqlitePool {
7+
SQLX_POOL.get().expect("sqlx pool should be set")
8+
}
9+
{%- elsif db_type == "postgres" %}
10+
use sqlx::postgres::PgPool;
11+
pub static SQLX_POOL: OnceLock<PgPool> = OnceLock::new();
12+
pub fn pool() -> &'static PgPool {
13+
SQLX_POOL.get().expect("sqlx pool should be set")
14+
}
15+
{%- elsif db_type == "mysql" %}
16+
use sqlx::mysql::MySqlPool;
17+
pub static SQLX_POOL: OnceLock<MySqlPool> = OnceLock::new();
18+
pub fn pool() -> &'static MySqlPool {
19+
SQLX_POOL.get().expect("sqlx pool should be set")
20+
}
21+
{%- endif %}

0 commit comments

Comments
 (0)