Skip to main content

sea_orm/query/
debug.rs

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