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.
- Loading branch information
Showing
7 changed files
with
132 additions
and
139 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.
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,60 @@ | ||
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?; | ||
|
||
let city_name = env::args().nth(1).expect("city name is required"); // [!code ++] | ||
println!("Connected"); | ||
// #region get | ||
let city = sqlx::query_as::<_, City>("SELECT * FROM city WHERE Name = ?") | ||
.bind("Tokyo") // [!code --] | ||
.bind(&city_name) // [!code ++] | ||
.fetch_one(&pool) | ||
.await | ||
.map_err(|e| match e { | ||
sqlx::Error::RowNotFound => anyhow::anyhow!("no such city Name = {}\n", "Tokyo"), // [!code --] | ||
sqlx::Error::RowNotFound => anyhow::anyhow!("no such city Name = {}\n", &city_name), // [!code ++] | ||
_ => anyhow::anyhow!("DB error: {}", e), | ||
})?; | ||
// #endregion get | ||
println!("Tokyoの人口は{}人です", &city.population); // [!code --] | ||
println!("{}の人口は{}人です", &city_name, &city.population); // [!code ++] | ||
Ok(()) | ||
} |