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

Prepare parametrized PG query for showing query parameters in web panel #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

unavailabl3
Copy link

Since Rails 5 has parametrizied query statement like this:
SELECT * FROM 'items' WHERE 'item'.'id' = $1 [['id',123]]
So in panel we see $1 instead of 123
Params are not shown in rack-mini-profiler panel. This commit is a hack (ONLY FOR POSTGRESQL) to see params. Like this we can add for other DBs.
P.S. May be commit needs refactoring for optimization

lib/patches/db/pg.rb Outdated Show resolved Hide resolved
lib/patches/db/pg.rb Outdated Show resolved Hide resolved
counter = 0
loop do
break if !query.include? "$#{counter+=1}"
query = query.sub("$#{counter}",parameters[counter-1].to_s)
Copy link
Member

Choose a reason for hiding this comment

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

subbing is a bit confusing and can cause a nonsense query to be logged in some cases. For example:

$1 -> hello world's, which technically would need to be 'hello world''s' I think

Copy link
Member

Choose a reason for hiding this comment

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

an alternative may be a preamble comment that explains the param so there is not confusion. also I think you technically want gsub here, also what if more than 10 params?

$1 -> \* $1 *\ 'hello-world''s'

Copy link
Author

@unavailabl3 unavailabl3 Oct 9, 2019

Choose a reason for hiding this comment

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

What will happen if more than 10 params?
Example from my project
SELECT tag_id FROM categories_tags WHERE category_id IN (11,3001,8794,8797,8788,8784,8783,3682,8789,3683,3680,3005,3686,3685,3684,3681,3679,8793,8787,3004,3006,8343,8791,8348,8354,3000,3003,3010,3007,3002,2999,8792,8795);

@nateberkopec
Copy link
Collaborator

This is still an issue on master, and there are no conflicts here, so despite the age I will leave this open.

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.

3 participants