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

add debug_query macro make get raw_sql becomes simply. #189

merged 27 commits into from
Oct 4, 2021

Conversation

baoyachi
Copy link
Contributor

@baoyachi baoyachi commented Sep 23, 2021

Let's take insertion as an example,before we want get sea-orm raw_sql, it's may be import trait namespace with :use sea_orm::QueryTrait;

use sea_orm::QueryTrait;
let pear = fruit::ActiveModel {
            name: Set("Pear".to_owned()),
            ..Default::default() // all other attributes are `Unset`
        };

let raw_sql = Entity::insert(pear).build(DatabaseBackend::MySql).to_string();

Now, you can use debug_query macro get raw_sql becomes simply.It does not need to specify a specific DbBackend,but can be obtained through get_database_backend function with DatabaseConnection. That not import trait namespace, the
debug_query macro make use simply.

let pear = fruit::ActiveModel {
            name: Set("Pear".to_owned()),
            ..Default::default() // all other attributes are `Unset`
        };

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_fmt!(&c,DbBackend::Sqlite);
assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#);
  • debug_query!: support accpeted &DatabaseConnection reference or DbBackend type .
  • debug_query_fmt!: with Statement to_string().

@baoyachi baoyachi changed the title add debug_query make get raw_sql becomes simply. add debug_query macro make get raw_sql becomes simply. Sep 23, 2021
@billy1624
Copy link
Member

billy1624 commented Sep 24, 2021

Hey @baoyachi, if we change the db_backend arg and make it accept anything that impl IntoDbBackend. Would this make the API ergonomic enough?

/// Build the query as [`Statement`]
fn build(&self, db_backend: DbBackend) -> Statement {
let query_builder = db_backend.get_query_builder();
Statement::from_string_values_tuple(
db_backend,
self.as_query().build_any(query_builder.as_ref()),
)
}

Introduce a new trait IntoDbBackend and implement it...

pub trait IntoDbBackend { ... }

impl IntoDbBackend for DatabaseConnection { ... }

impl IntoDbBackend for DatabaseBackend { ... }

Then we can write...

let raw_sql = Entity::insert(pear).build(&db).to_string();

let raw_sql = Entity::insert(pear).build(DatabaseBackend::MySql).to_string();

@baoyachi
Copy link
Contributor Author

@billy1624 That's great. I thought about this before sending PR, which may involve large changes. Now I think I can do that.

@billy1624
Copy link
Member

Any comments? @tyt2y3

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 25, 2021

Okay so, I would prefer debug_query to output a string and remove debug_query_fmt.
This function overloading strategy is the first time I've ever used!

@baoyachi
Copy link
Contributor Author

baoyachi commented Sep 26, 2021

Okay so, I would prefer debug_query to output a string and remove debug_query_fmt.
This function overloading strategy is the first time I've ever used!

@tyt2y3 I have removed debugged_query_fmt macro and use debug_query instead.
Added gen_statment macro.This seems necessary because it receives one of the multiple parameters of DatabaseConnection or DbBackend.It's easy to use.

@billy1624
I try to modify part of the code in 'sea-orm' to IntoDbBackend
However, in the Statement struct object, there are some difficulties in modifying the DbBackend to IntoDbBackend:

  --> src/database/connection.rs:20:8
   |
19 | pub trait IntoDbBackend {
   |           ------------- this trait cannot be made into an object...
20 |     fn build(&self, statement: &impl StatementBuilder) -> Statement;
   |        ^^^^^ ...because method `build` has generic type parameters

So I haven't modified this object for the time being. Let it remain as it is for the time being.

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review. When I go through a PR, I'd actually try to justify every line change.
As such, the more line changes are there, the more effort it takes to go through.
Thank you for your contribution nonetheless!

src/database/connection.rs Outdated Show resolved Hide resolved
src/database/connection.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
@baoyachi baoyachi requested a review from tyt2y3 October 2, 2021 21:57
@tyt2y3
Copy link
Member

tyt2y3 commented Oct 4, 2021

Thank you

@tyt2y3 tyt2y3 changed the base branch from master to debug_query October 4, 2021 04:40
@tyt2y3 tyt2y3 merged commit 49efca7 into SeaQL:debug_query Oct 4, 2021
@tyt2y3
Copy link
Member

tyt2y3 commented Oct 6, 2021

0.2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants