Skip to content

Commit

Permalink
If you choose SQLx, there will be an error when using MySQL.
Browse files Browse the repository at this point in the history
SQLx official website documentation:
// Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL/MariaDB)
  • Loading branch information
pengchanglu committed Sep 28, 2024
1 parent 546f615 commit ce35cc4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/template/src/services/user.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ pub async fn add_user(req: UserAddRequest) -> AppResult<UserResponse> {
let _ = sqlx::query!(
r#"
INSERT INTO users (id, username, password)
{{#if is_mysql}}
VALUES (?, ?, ?)
{{else}}
VALUES ($1, $2, $3)
{{/if}}
"#,
id,
req.username,
Expand All @@ -56,7 +60,11 @@ pub async fn login(req: UserLoginRequest) -> AppResult<UserLoginResponse> {
User,
r#"
SELECT id, username, password FROM users
{{#if is_mysql}}
WHERE username = ?
{{else}}
WHERE username = $1
{{/if}}
"#,
req.username
)
Expand Down Expand Up @@ -88,8 +96,13 @@ pub async fn update_user(req: UserUpdateRequest) -> AppResult<UserResponse> {
let _ = sqlx::query!(
r#"
UPDATE users
{{#if is_mysql}}
SET username = ?, password = ?
WHERE id = ?
{{else}}
SET username = $1, password = $2
WHERE id = $3
{{/if}}
"#,
req.username,
hash_password,
Expand All @@ -109,7 +122,12 @@ pub async fn delete_user(id: String) -> AppResult<()> {
sqlx::query!(
r#"
DELETE FROM users
{{#if is_mysql}}
WHERE id = ?
{{else}}
WHERE id = $1
{{/if}}

"#,
id,
)
Expand Down

0 comments on commit ce35cc4

Please sign in to comment.