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

[8.x] Add possibility to output an sql inlining the bindings to the screen #39053

Closed
wants to merge 11 commits into from

Conversation

dietercoopman
Copy link

@dietercoopman dietercoopman commented Oct 2, 2021

What ?

A trait added to the Eloquent Builder and Query Builder to make it possible to echo the current sql with bindings included on the builder

Usage

The Query Builder

DB::table('products')->where('id', '=', 1)->show()->get();

The Eloquent Builder

Product::whereHas('category')->show()->get();

@Jubeki gave some valuable feedback 👏 and I've added the possibilty to pass a callback if you don't want the echo output. So you can decide whatever you want to do with the sql statement.

$callback = function(string $sql){
     Log::info($sql);
};

DB::table('products')->where('id', '=', 1)->show($callback)->get();

Why

We often want to show the query ( including the filled in bindings ) that will run against our database server. You can do that with many of the community driven profiling tools but they are mostly thrown in a container together with other sql statements. So you have to start dig into the bunch of statements ran against your database. I find myself writing a macro over and over again to have the possibility of this pull request , maybe it might be a nice addition to the framework itself.

Thoughts

I now did the output to the screen , that's a choice , but also a doubt. I find it must return the Builder instance and not a string ( because not wanting to break the chain ). The doubt in it is that the user gets no choice ...It's always an output , and not a log entry. EDIT after the feedback from @Jubeki i've made it possible to pass it a closure

Could extend it but might be too much complex for developers using it. What's your opinion on that ?

@dietercoopman dietercoopman changed the title Show [8.x] Add possibility to output a sql including the bindings to the screen Oct 2, 2021
@dietercoopman dietercoopman changed the title [8.x] Add possibility to output a sql including the bindings to the screen [8.x] Add possibility to output an sql inlining the bindings to the screen Oct 2, 2021
@Jubeki
Copy link
Contributor

Jubeki commented Oct 2, 2021

@dietercoopman in my opinion returning a string is better here is different ideas then echoing the sql string and still returning the Query Builder.:

$result = $query->show(function(string $sqlWithBindings) {
    Log::info($sqlWithBindings);
})->get();

If you return a string on the show method it would also be possible for you to write a macro to wrap around that. For example:

Builder::macro('xxx', function($query) {
     $sqlWithBindings = $query->show();

     // Do something with the string

     return $query;
});

I think the second approach would be best. Meaning that show would return a string.

@dietercoopman
Copy link
Author

dietercoopman commented Oct 2, 2021

Hi @Jubeki , thx for your feedback 👌 , I've added the possibility to add a callback , so you can decide not to output the sql statement to the screen but logging it to whatever you want. I'm a big fan of not breaking the chain on the builder, but I Am open for any suggestion and of course will listen to the community wishes 👏

 $callback = function(string $sql){
      Log::info($sql);
  };

 DB::table('products')->where('id', '=', 1)->show($callback)->get();

@chu121su12
Copy link
Contributor

Similar PR has been previously declined in #38027. Please consider some of the implementation there, especially for the binding substitution logic.

@dietercoopman
Copy link
Author

Hi @chu121su12 , thx for your feedback 👏 I've updated the code, so it uses a better substitution logic. Hope we can have a framework included candidate for this functionality. I've already created a package for this but I think this might be a timesaver to have it included.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@dietercoopman
Copy link
Author

I've created a package past week that implements this features and I've enhanced it with the feedback given , thx guys ! 👏

If you are interested ⏬
https://github.com/dietercoopman/laravel-showsql

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.

4 participants