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

add debug_query macro make get raw_sql becomes simply. #189

Merged
merged 27 commits into from
Oct 4, 2021
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4377bd4
add debug_query make get raw_sql becomes simply.
baoyachi Sep 23, 2021
8f08529
refactor code with macro debug_query!
baoyachi Sep 23, 2021
aa7e053
update doc
baoyachi Sep 23, 2021
10eeca1
add DbConnection with DebugQuey build overload method. Working for #145
baoyachi Sep 23, 2021
536a900
all rename with DbBackEnd
baoyachi Sep 24, 2021
9af9637
define IntoDbBackEnd trait
baoyachi Sep 24, 2021
849ffeb
fix build error
baoyachi Sep 24, 2021
a1bbb72
fix build error
baoyachi Sep 24, 2021
55ecf46
refactor code IntoDbBackend
baoyachi Sep 25, 2021
08f19cc
cargo fmt
baoyachi Sep 26, 2021
ba6d2d9
rename marco
baoyachi Sep 26, 2021
de0fd2a
Merge branch 'master' of github.com:SeaQL/sea-orm
baoyachi Sep 26, 2021
ece6b62
cargo fmt
baoyachi Sep 26, 2021
d2702dd
fix build test error
baoyachi Sep 27, 2021
fae268a
fix example build error
baoyachi Sep 27, 2021
b2bb259
remove warning
baoyachi Sep 27, 2021
9afd7dd
Merge branch 'adapter_url_scheme'
baoyachi Sep 27, 2021
37133d0
Merge branch 'master' of github.com:SeaQL/sea-orm
baoyachi Sep 27, 2021
1c0be92
Merge branch 'master' of github.com:SeaQL/sea-orm
baoyachi Sep 30, 2021
1083844
remove IntoDbBackend
baoyachi Oct 2, 2021
806f20b
Merge branch 'master' of github.com:SeaQL/sea-orm
baoyachi Oct 2, 2021
f987d3e
cargo fmt
baoyachi Oct 2, 2021
fa9999d
cargo fmt code
baoyachi Oct 2, 2021
bad6d0d
add DbBackend ref with DebugQuery
baoyachi Oct 2, 2021
1b3b5c4
update
baoyachi Oct 2, 2021
897a385
refactor code
baoyachi Oct 2, 2021
0c9334b
refactor code
baoyachi Oct 2, 2021
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
Prev Previous commit
Next Next commit
refactor code with macro debug_query!
ref:#145
  • Loading branch information
baoyachi committed Sep 23, 2021
commit 8f0852958ffe216ab75c276063852c1d30da2dd4
93 changes: 89 additions & 4 deletions src/query/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,99 @@ pub trait QueryTrait {
}
}

#[derive(Debug)]
pub struct DebugQuery<'a, Q, T> {
pub query: &'a Q,
pub value: T,
}

impl<'a, Q> DebugQuery<'a, Q, DbBackend>
where
Q: QueryTrait,
{
pub fn build(&self) -> Statement {
self.query.build(self.value)
}
}

impl<'a, Q> DebugQuery<'a, Q, &DatabaseConnection>
where
Q: QueryTrait,
{
pub fn build(&self) -> Statement {
self.query.build(self.value.get_database_backend())
}
}

/// Make get raw_sql becomes simply. It does not need to specify a specific `DbBackend`,
/// but can be obtained through `get_database_backend` with `DatabaseConnection`.
/// Return a Statement type.
pub fn debug_query(query: &impl QueryTrait, conn: &DatabaseConnection) -> Statement {
query.build(conn.get_database_backend())
///
///
/// # Example
///
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// #
/// # let conn = MockDatabase::new(DbBackend::Postgres)
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query};
///
/// let c = cake::Entity::insert(
/// cake::ActiveModel {
/// id: ActiveValue::set(1),
/// name: ActiveValue::set("Apple Pie".to_owned()),
/// });
///
/// let raw_sql = debug_query!(&c,&conn).to_string();
/// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#);
///
/// let raw_sql = debug_query!(&c,DbBackend::MySql).to_string();
/// assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#);
///
/// ```
#[macro_export]
macro_rules! debug_query {
($query:expr,$value:expr) => {
$crate::DebugQuery {
query: $query,
value: $value,
}
.build();
};
}

/// Use `debug_query` get raw_sql.
pub fn debug_query_fmt(query: &impl QueryTrait, conn: &DatabaseConnection) -> String {
debug_query(query, conn).to_string()
///
/// # Example
///
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// #
/// # let conn = MockDatabase::new(DbBackend::Postgres)
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query_fmt};
///
/// let c = cake::Entity::insert(
/// cake::ActiveModel {
/// id: ActiveValue::set(1),
/// name: ActiveValue::set("Apple Pie".to_owned()),
/// });
///
/// let raw_sql = debug_query_fmt!(&c,&conn);
/// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#);
///
/// let raw_sql = debug_query_fmt!(&c,DbBackend::Sqlite);
/// assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#);
///
/// ```
#[macro_export]
macro_rules! debug_query_fmt {
($query:expr,$value:expr) => {
$crate::debug_query!($query, $value).to_string();
};
}