forked from traPtitech/naro-text
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
第一部 - Rust でデータベースを扱う
- Loading branch information
Showing
11 changed files
with
281 additions
and
300 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,56 @@ | ||
use anyhow::Ok; | ||
use sqlx::mysql::MySqlConnectOptions; | ||
use std::env; | ||
|
||
// #region city | ||
#[derive(sqlx::FromRow)] | ||
#[sqlx(rename_all = "PascalCase")] | ||
#[allow(dead_code)] // 使用していないフィールドへの警告を抑制 | ||
struct City { | ||
#[sqlx(rename = "ID")] | ||
id: i32, | ||
name: String, | ||
country_code: String, | ||
district: String, | ||
population: i32, | ||
} | ||
// #endregion city | ||
|
||
fn get_option() -> anyhow::Result<MySqlConnectOptions> { | ||
let host = env::var("DB_HOSTNAME")?; | ||
let port = env::var("DB_PORT")?.parse()?; | ||
let username = env::var("DB_USERNAME")?; | ||
let password = env::var("DB_PASSWORD")?; | ||
let database = env::var("DB_DATABASE")?; | ||
let timezone = Some(String::from("Asia/Tokyo")); | ||
let collation = String::from("utf8mb4_unicode_ci"); | ||
|
||
Ok(MySqlConnectOptions::new() | ||
.host(&host) | ||
.port(port) | ||
.username(&username) | ||
.password(&password) | ||
.database(&database) | ||
.timezone(timezone) | ||
.collation(&collation)) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
let options = get_option()?; | ||
let pool = sqlx::MySqlPool::connect_with(options).await?; | ||
|
||
println!("Connected"); | ||
// #region get | ||
let city = sqlx::query_as::<_, City>("SELECT * FROM city WHERE Name = ?") | ||
.bind("Tokyo") | ||
.fetch_one(&pool) | ||
.await | ||
.map_err(|e| match e { | ||
sqlx::Error::RowNotFound => anyhow::anyhow!("no such city Name = {}\n", "Tokyo"), | ||
_ => anyhow::anyhow!("DB error: {}", e), | ||
})?; | ||
// #endregion get | ||
println!("Tokyoの人口は{}人です", &city.population); | ||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.