Skip to content

[feature] Added ->toSql() for raw wheres #1916

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

Closed
wants to merge 10 commits into from
Closed

[feature] Added ->toSql() for raw wheres #1916

wants to merge 10 commits into from

Conversation

rennokki
Copy link
Contributor

@rennokki rennokki commented Jan 20, 2020

See this open PR i closed a while: #1887

It's good to make it work not only because of debugging (either it's not called SQL), but also you can cache query-by-query easier using packages like this: https://github.com/rennokki/laravel-eloquent-query-cache

Needs testing.

Actually it is safe, it detects arrays and automatically serializes them to string, which is exactly what's needed.

@rennokki rennokki changed the title Added ->toSql() for raw wheres [feature] Added ->toSql() for raw wheres Jan 20, 2020
@rennokki rennokki changed the title [feature] Added ->toSql() for raw wheres [feature][WIP] Added ->toSql() for raw wheres Jan 20, 2020
@rennokki rennokki changed the title [feature][WIP] Added ->toSql() for raw wheres [feature] Added ->toSql() for raw wheres Jan 22, 2020
@rennokki
Copy link
Contributor Author

Tests are failing due to Coveralls. This might be needed to PR before re-checking the tests: #1922

@Smolevich Smolevich self-assigned this Jan 24, 2020
Smolevich
Smolevich previously approved these changes Jan 24, 2020
@Smolevich Smolevich removed the request for review from jenssegers January 24, 2020 07:36
@Smolevich Smolevich mentioned this pull request Jan 24, 2020
@Smolevich Smolevich requested a review from jenssegers January 24, 2020 07:51
README.md Outdated
@@ -17,6 +17,7 @@ Table of contents
* [Eloquent](#eloquent)
* [Optional: Alias](#optional-alias)
* [Query Builder](#query-builder)
* [`toSql()`](#to-sql)
Copy link
Contributor

@jenssegers jenssegers Jan 24, 2020

Choose a reason for hiding this comment

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

Maybe we can put this under a category called "debugging" or "inspecting queries".

If I remember correctly there was also a way to get the mongodb query. That could also fit under that category. Will also be nice to be able to send this link to people having issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

Add else info about DB::getQueryLog()

Copy link
Contributor

Choose a reason for hiding this comment

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

It old feature but getQueryLog() return original query struncture to mongodb

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jenssegers maybe fix this after #1917

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Smolevich i will look into it as soon as i get home

Copy link
Contributor

@Smolevich Smolevich Jan 25, 2020

Choose a reason for hiding this comment

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

@rennokki what about to add info about using DB::getQueryLog()
For example

DB::enableQueryLog();
$user = factory(User::class)->create();
$query = DB::connection()->table(config('auth.passwords.users.table'));
dd(DB::getQueryLog());

Example of element of array that function getQueryLog returned

0 => array:3 [
    "query" => "users.insertOne({"name":"Mr. Jayden Cremin Sr.","email":"[email protected]","email_verified_at":{"$date":{"$numberLong":"1579992655000"}},"password":"$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC\/.og\/at2.uheWG\/igi","remember_token":"r6HicH5d78","updated_at":{"$date":{"$numberLong":"1579992655901"}},"created_at":{"$date":{"$numberLong":"1579992655901"}}})"
    "bindings" => []
    "time" => 48.52
  ]
1 => array:3 [
    "query" => "users.find({"email":"[email protected]"},{"limit":1,"typeMap":{"root":"array","document":"array"}})"
    "bindings" => []
    "time" => 4.49
  ]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The scope of this PR was to be able to get the query string before the actual query was executed, so i can be able to add cache query-by-query.

I can add the query log thing in the readme, because that's useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Smolevich @Giacomo92

So far, the single issue here is that the basic get() and the aggregate and groups are treated differently. This means that the final query is split into two methods rather than a single get() one.

I repeat - the purpose of this PR was to get the string for each query, so I can be able to query the cache query-by-query.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Smolevich I think $options and $pipeline has to be attributes in the class and be passed to the generateCacheKey method.

@@ -6,4 +6,19 @@

class Grammar extends BaseGrammar
{
/**
* {@inheritdoc}
Copy link
Contributor

@Smolevich Smolevich Jan 26, 2020

Choose a reason for hiding this comment

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

Add param doc for argument $query and for return type please

@Smolevich
Copy link
Contributor

@rennokki please resolve conflcts

@divine
Copy link
Contributor

divine commented Feb 28, 2020

@rennokki this has been open for almost a month, will you take care?

Looks like no as you've deleted forked repo while at the same trying to reinvent a wheel.

Good luck, closing.

@divine divine closed this Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants