Browse Source

Testing renames in Enums

master
Julio Biason 2 years ago
parent
commit
16fdd4fa80
  1. 5
      sqlxtest/db/migrations/202202110959_enums.sql
  2. 89
      sqlxtest/src/main.rs

5
sqlxtest/db/migrations/202202110959_enums.sql

@ -0,0 +1,5 @@
CREATE TABLE foo (
id BIGINT,
bar TEXT,
baz TEXT
);

89
sqlxtest/src/main.rs

@ -60,6 +60,29 @@ async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> {
Ok(pool) Ok(pool)
} }
#[derive(Debug, sqlx::Type)]
pub enum BazTypes {
#[sqlx(rename = "foo")]
One,
#[sqlx(rename = "bar")]
Two,
#[sqlx(rename = "baz")]
Three,
}
#[derive(Debug, sqlx::FromRow)]
struct Foo {
id: i32,
#[sqlx(rename = "bar")]
indicator: String,
#[sqlx(rename = "baz")]
foo_type: BazTypes,
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), sqlx::Error> { async fn main() -> Result<(), sqlx::Error> {
let pool = connect().await?; let pool = connect().await?;
@ -74,24 +97,58 @@ async fn main() -> Result<(), sqlx::Error> {
println!("Command: \"{}\"", command); println!("Command: \"{}\"", command);
if command == "add" { match command.as_str() {
println!("Should add \"{}\"", value); "add" => {
let record = Label { println!("Should add \"{}\"", value);
id: None, let record = Label {
description: value.into(), id: None,
}; description: value.into(),
repo.save(&record).await?; };
} else if command == "remove" { repo.save(&record).await?;
println!("Should remove \"{}\"", value); }
match repo.find_by_description(&value).await { "remove" => {
Ok(record) => { println!("Should remove \"{}\"", value);
repo.delete(&record).await?; match repo.find_by_description(&value).await {
println!("Removed"); Ok(record) => {
} repo.delete(&record).await?;
Err(err) => { println!("Removed");
println!("Label does not exist: {:?}", err); }
Err(err) => {
println!("Label does not exist: {:?}", err);
}
} }
} }
"one" => {
let pool = connect().await.unwrap();
let record = Foo {
id: 1,
indicator: value.into(),
foo_type: BazTypes::One,
};
println!("Adding Foo({:?})", record);
sqlx::query("INSERT INTO foo (bar, baz) values ($1, $2);")
.bind(&record.indicator)
.bind(&record.foo_type)
.execute(&pool)
.await
.unwrap();
}
"two" => {
let pool = connect().await.unwrap();
let record = Foo {
id: 1,
indicator: value.into(),
foo_type: BazTypes::Two,
};
println!("Adding Foo({:?})", record);
sqlx::query("INSERT INTO foo (bar, baz) values ($1, $2);")
.bind(&record.indicator)
.bind(&record.foo_type)
.execute(&pool)
.await
.unwrap();
}
_ => println!("Unknonw command"),
} }
Ok(()) Ok(())
} }

Loading…
Cancel
Save