From ff731ce503d891e0d037c5b8def71d4b275be813 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Tue, 28 Jan 2020 18:56:25 -0800 Subject: [PATCH] Enable raw ident support --- Cargo.toml | 4 ++ sqlx-macros/src/query_macros/output.rs | 3 +- tests/mysql-macros.rs | 57 ++++++++++++++++++++++++++ tests/mysql.rs | 34 --------------- tests/postgres-macros.rs | 26 ++++++++++++ 5 files changed, 89 insertions(+), 35 deletions(-) create mode 100644 tests/mysql-macros.rs diff --git a/Cargo.toml b/Cargo.toml index 6b55257e97..d1483a4803 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,6 +62,10 @@ trybuild = "1.0" name = "postgres-macros" required-features = [ "postgres", "macros" ] +[[test]] +name = "mysql-macros" +required-features = [ "mysql", "macros" ] + [[test]] name = "mysql" required-features = [ "mysql" ] diff --git a/sqlx-macros/src/query_macros/output.rs b/sqlx-macros/src/query_macros/output.rs index 68df770ed0..089223517e 100644 --- a/sqlx-macros/src/query_macros/output.rs +++ b/sqlx-macros/src/query_macros/output.rs @@ -68,7 +68,8 @@ fn parse_ident(name: &str) -> crate::Result { let is_valid_ident = name.chars().all(|c| c.is_alphanumeric() || c == '_'); if is_valid_ident { - if let Ok(ident) = syn::parse_str(name) { + let ident = String::from("r#") + name; + if let Ok(ident) = syn::parse_str(&ident) { return Ok(ident); } } diff --git a/tests/mysql-macros.rs b/tests/mysql-macros.rs new file mode 100644 index 0000000000..6d5d4117f2 --- /dev/null +++ b/tests/mysql-macros.rs @@ -0,0 +1,57 @@ +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn macro_select_from_cte() -> anyhow::Result<()> { + let mut conn = connect().await?; + let account = + sqlx::query!("select * from (select (1) as id, 'Herp Derpinson' as name) accounts") + .fetch_one(&mut conn) + .await?; + + println!("{:?}", account); + println!("{}: {}", account.id, account.name); + + Ok(()) +} + +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn macro_select_from_cte_bind() -> anyhow::Result<()> { + let mut conn = connect().await?; + let account = sqlx::query!( + "select * from (select (1) as id, 'Herp Derpinson' as name) accounts where id = ?", + 1i32 + ) + .fetch_one(&mut conn) + .await?; + + println!("{:?}", account); + println!("{}: {}", account.id, account.name); + + Ok(()) +} + +#[derive(Debug)] +struct RawAccount { + r#type: i32, + name: Option, +} + +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_query_as_raw() -> anyhow::Result<()> { + let mut conn = connect().await?; + + let account = sqlx::query_as!( + RawAccount, + "SELECT * from (VALUES (1, null)) accounts(type, name)" + ) + .fetch_one(&mut conn) + .await?; + + assert_eq!(None, account.name); + assert_eq!(1, account.r#type); + + println!("{:?}", account); + + Ok(()) +} diff --git a/tests/mysql.rs b/tests/mysql.rs index d2b38af9a7..b4523f553a 100644 --- a/tests/mysql.rs +++ b/tests/mysql.rs @@ -87,40 +87,6 @@ async fn pool_immediately_fails_with_db_error() -> anyhow::Result<()> { Ok(()) } -#[cfg(feature = "macros")] -#[cfg_attr(feature = "runtime-async-std", async_std::test)] -#[cfg_attr(feature = "runtime-tokio", tokio::test)] -async fn macro_select_from_cte() -> anyhow::Result<()> { - let mut conn = connect().await?; - let account = - sqlx::query!("select * from (select (1) as id, 'Herp Derpinson' as name) accounts") - .fetch_one(&mut conn) - .await?; - - println!("{:?}", account); - println!("{}: {}", account.id, account.name); - - Ok(()) -} - -#[cfg(feature = "macros")] -#[cfg_attr(feature = "runtime-async-std", async_std::test)] -#[cfg_attr(feature = "runtime-tokio", tokio::test)] -async fn macro_select_from_cte_bind() -> anyhow::Result<()> { - let mut conn = connect().await?; - let account = sqlx::query!( - "select * from (select (1) as id, 'Herp Derpinson' as name) accounts where id = ?", - 1i32 - ) - .fetch_one(&mut conn) - .await?; - - println!("{:?}", account); - println!("{}: {}", account.id, account.name); - - Ok(()) -} - fn url() -> anyhow::Result { Ok(dotenv::var("DATABASE_URL")?) } diff --git a/tests/postgres-macros.rs b/tests/postgres-macros.rs index 7ff30ec9b9..82378a5abe 100644 --- a/tests/postgres-macros.rs +++ b/tests/postgres-macros.rs @@ -68,6 +68,32 @@ async fn test_query_as() -> anyhow::Result<()> { Ok(()) } +#[derive(Debug)] +struct RawAccount { + r#type: i32, + name: Option, +} + +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_query_as_raw() -> anyhow::Result<()> { + let mut conn = connect().await?; + + let account = sqlx::query_as!( + RawAccount, + "SELECT * from (VALUES (1, null)) accounts(type, name)" + ) + .fetch_one(&mut conn) + .await?; + + assert_eq!(None, account.name); + assert_eq!(1, account.r#type); + + println!("{:?}", account); + + Ok(()) +} + #[cfg_attr(feature = "runtime-async-std", async_std::test)] #[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn test_query_file_as() -> anyhow::Result<()> {