sea_orm/query/debug.rs
1use crate::{QueryTrait, Statement, database::*};
2
3/// This structure provides debug capabilities
4#[derive(Debug)]
5pub struct DebugQuery<'a, Q, T> {
6 /// The query to debug
7 pub query: &'a Q,
8 /// The value of the query
9 pub value: T,
10}
11
12macro_rules! debug_query_build {
13 ($impl_obj:ty, $db_expr:expr) => {
14 impl<'a, Q> DebugQuery<'a, Q, $impl_obj>
15 where
16 Q: QueryTrait,
17 {
18 /// This macro builds a [Statement] when invoked
19 pub fn build(&self) -> Statement {
20 let func = $db_expr;
21 let db_backend = func(self);
22 self.query.build(db_backend)
23 }
24 }
25 };
26}
27
28debug_query_build!(DbBackend, |x: &DebugQuery<_, DbBackend>| x.value);
29debug_query_build!(&DbBackend, |x: &DebugQuery<_, &DbBackend>| *x.value);
30debug_query_build!(DatabaseConnection, |x: &DebugQuery<
31 _,
32 DatabaseConnection,
33>| x.value.get_database_backend());
34debug_query_build!(&DatabaseConnection, |x: &DebugQuery<
35 _,
36 &DatabaseConnection,
37>| x.value.get_database_backend());
38
39/// Helper to get a `Statement` from an object that impl `QueryTrait`.
40///
41/// # Example
42///
43/// ```
44/// # #[cfg(feature = "mock")]
45/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, DbBackend};
46/// #
47/// # let conn = MockDatabase::new(DbBackend::Postgres)
48/// # .into_connection();
49/// #
50/// use sea_orm::{debug_query_stmt, entity::*, query::*, tests_cfg::cake};
51///
52/// let c = cake::Entity::insert(cake::ActiveModel {
53/// id: ActiveValue::set(1),
54/// name: ActiveValue::set("Apple Pie".to_owned()),
55/// });
56///
57/// let raw_sql = debug_query_stmt!(&c, &conn).to_string();
58/// assert_eq!(
59/// raw_sql,
60/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#
61/// );
62///
63/// let raw_sql = debug_query_stmt!(&c, conn).to_string();
64/// assert_eq!(
65/// raw_sql,
66/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#
67/// );
68///
69/// let raw_sql = debug_query_stmt!(&c, DbBackend::MySql).to_string();
70/// assert_eq!(
71/// raw_sql,
72/// r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#
73/// );
74///
75/// let raw_sql = debug_query_stmt!(&c, &DbBackend::MySql).to_string();
76/// assert_eq!(
77/// raw_sql,
78/// r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#
79/// );
80/// ```
81#[macro_export]
82macro_rules! debug_query_stmt {
83 ($query:expr,$value:expr) => {
84 $crate::DebugQuery {
85 query: $query,
86 value: $value,
87 }
88 .build();
89 };
90}
91
92/// Helper to get a raw SQL string from an object that impl `QueryTrait`.
93///
94/// # Example
95///
96/// ```
97/// # #[cfg(feature = "mock")]
98/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, DbBackend};
99/// #
100/// # let conn = MockDatabase::new(DbBackend::Postgres)
101/// # .into_connection();
102/// #
103/// use sea_orm::{debug_query, entity::*, query::*, tests_cfg::cake};
104///
105/// let c = cake::Entity::insert(cake::ActiveModel {
106/// id: ActiveValue::set(1),
107/// name: ActiveValue::set("Apple Pie".to_owned()),
108/// });
109///
110/// let raw_sql = debug_query!(&c, &conn);
111/// assert_eq!(
112/// raw_sql,
113/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#
114/// );
115///
116/// let raw_sql = debug_query!(&c, conn);
117/// assert_eq!(
118/// raw_sql,
119/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#
120/// );
121///
122/// let raw_sql = debug_query!(&c, DbBackend::Sqlite);
123/// assert_eq!(
124/// raw_sql,
125/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#
126/// );
127/// ```
128#[macro_export]
129macro_rules! debug_query {
130 ($query:expr,$value:expr) => {
131 $crate::debug_query_stmt!($query, $value).to_string();
132 };
133}