Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable raw ident support #96

Merged
merged 1 commit into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
3 changes: 2 additions & 1 deletion sqlx-macros/src/query_macros/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ fn parse_ident(name: &str) -> crate::Result<Ident> {
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);
}
}
Expand Down
57 changes: 57 additions & 0 deletions tests/mysql-macros.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

#[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(())
}
34 changes: 0 additions & 34 deletions tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,40 +87,6 @@ async fn pool_immediately_fails_with_db_error() -> anyhow::Result<()> {
Ok(())
}

#[cfg(feature = "macros")]
abonander marked this conversation as resolved.
Show resolved Hide resolved
#[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<String> {
Ok(dotenv::var("DATABASE_URL")?)
}
Expand Down
26 changes: 26 additions & 0 deletions tests/postgres-macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ async fn test_query_as() -> anyhow::Result<()> {
Ok(())
}

#[derive(Debug)]
struct RawAccount {
r#type: i32,
name: Option<String>,
}

#[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<()> {
Expand Down