Skip to content

Commit

Permalink
Fix #105, allow trailing commas in query macros
Browse files Browse the repository at this point in the history
  • Loading branch information
timmythetiny committed Feb 9, 2020
1 parent f8e112f commit cefd377
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ macro_rules! query (
$crate::sqlx_macros::query!($query, $($args),*);
}
macro_result!($($args),*)
});
($query:literal, $($args:expr),*,) => ({
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query!($query, $($args),*);
}
macro_result!($($args),*)
})
);

Expand Down Expand Up @@ -161,6 +168,13 @@ macro_rules! query_file (
$crate::sqlx_macros::query_file!($query, $($args),*);
}
macro_result!($($args),*)
});
($query:literal, $($args:expr),*,) => (#[allow(dead_code)]{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_file!($query, $($args),*);
}
macro_result!($($args),*)
})
);

Expand Down Expand Up @@ -227,6 +241,13 @@ macro_rules! query_as (
$crate::sqlx_macros::query_as!($out_struct, $query, $($args),*);
}
macro_result!($($args),*)
});
($out_struct:path, $query:literal, $($args:expr),*,) => (#[allow(dead_code)] {
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_as!($out_struct, $query, $($args),*);
}
macro_result!($($args),*)
})
);

Expand Down Expand Up @@ -278,5 +299,12 @@ macro_rules! query_file_as (
$crate::sqlx_macros::query_file_as!($out_struct, $query, $($args),*);
}
macro_result!($($args),*)
});
($out_struct:path, $query:literal, $($args:expr),*,) => (#[allow(dead_code)] {
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_file_as!($out_struct, $query, $($args),*);
}
macro_result!($($args),*)
})
);
4 changes: 2 additions & 2 deletions tests/postgres-macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async fn test_query() -> anyhow::Result<()> {

let account = sqlx::query!(
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
1i32
1i32,
)
.fetch_one(&mut conn)
.await?;
Expand Down Expand Up @@ -56,7 +56,7 @@ async fn test_query_as() -> anyhow::Result<()> {

let account = sqlx::query_as!(
Account,
"SELECT * from (VALUES (1, null)) accounts(id, name)"
"SELECT * from (VALUES (1, null)) accounts(id, name)",
)
.fetch_one(&mut conn)
.await?;
Expand Down

0 comments on commit cefd377

Please sign in to comment.