Problem/Motivation
Static queries should have the field names bracket-encapsulated. In that way static queries cannot fail on a database, because a field name is a reserved keyword for the given database. For more info: https://www.drupal.org/node/2986894.
Proposed resolution
Bracket-encapsulated field names for all static queries located in the directory "core/modules/{every module}" and its sub-directories, but not in the directory "core/modules/{every module}/tests". Static queries can be found by doing a search for "->query(" and "->queryRange(" in the directory "core/modules/{every module}" and its sub-directories, but not in the directory "core/modules/{every module}/tests".
What needs to be changed:
From:
$connection->query('SELECT nid FROM {node} WHERE nid = :id', [':id' => 1']);
To:
$connection->query('SELECT [nid] FROM {node} WHERE [nid] = :id', [':id' => 1']);
After that is done, the core committer @alexpott will review all changes and determine which queries are not in the "critical path" and therefore can/must be changed to a dynamic query. From "->query(" and "->queryRange(" to "->select(".
Remaining tasks
TBD
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
TBD
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | reroll-diff-22-27.txt | 20.94 KB | s_bhandari |
| #27 | 3160271-27.patch | 31.32 KB | s_bhandari |
| #22 | interdiff-19-22.txt | 2.04 KB | megha_kundar |
| #22 | 3160271-22.patch | 30.99 KB | megha_kundar |
Comments
Comment #2
s_bhandari commentedWorking on it.
Thanks.
Comment #3
megha_kundar commentedComment #4
megha_kundar commentedComment #6
daffie commentedThank you @Megha_kundar for working on this. Your patch looks good, only there is some work to be done:
Field names like
t.wordshould be bracket encapsulated as[t].[word]. Also you forgot to bracket encapsulate the fields: "i.word" and "t.word" in the left join part of the query.The change to the query in these files fall outside this issue. From the issue summary: "Static queries can be found by doing a search for "->query(" and "->queryRange(" in the directory "core/modules/{every module}" and its sub-directories, but not in the directory "core/modules/{every module}/tests"."
You forgot to bracket encapsulate the fields: "changed" and "status" in the order by part of the query.
Comment #7
s_bhandari commentedHi @daffie,
I am working on it, will update shortly.
Thanks.
Comment #8
megha_kundar commentedHi @daffie,
Thanks for guiding.
I have re-attached patch. Let me know if any changes here or if i have missed any.
Comment #9
s_bhandari commentedAdded a patch for all the occurrences present. Please review the same and let me know for any observation.
Comment #10
daffie commentedReview of the patch from comment #8. Patch looks better.
This is wrong
[checked + refresh]. It should be:[checked] + [refresh].Can we change
MAX(comment_count)toMAX([comment_count]).Can we change
COUNT(c.cid)toCOUNT([c].[cid]). Same for "h.timestamp".The char "*" does not to have brackets.
@Megha_kundar: Could you add an interdiff.txt with you patch. It makes reviewing the changes you made in the patch a lot easier.
Comment #11
megha_kundar commentedHI @daffie,
Please review this patch.
Comment #12
daffie commentedReview of the patch from comment #9. Patch looks better.
My remark #10.3 also applies to your patch.
I am missing a query that need bracket encapsulation in "core/modules/node/src/Plugin/views/argument/Vid.php".
@S_Bhandari: Could you add an interdiff.txt with you patch. It makes reviewing the changes you made in the patch a lot easier.
Comment #13
s_bhandari commentedHi @daffie,
Please find attached the interdiff (interdiff-4-8.txt).
Thanks.
Comment #14
daffie commented@Megha_kundar: Could you take the patch from comment #9 and work from that. I am now reviewing both you individual patch work. It would be a lot easier for me when we work together on the same patch. I am choosing his patch, because you patch is missing a couple of queries.
Comment #15
megha_kundar commented@daffie sure i will work on it.
Comment #16
s_bhandari commentedHi @daffie,
Adding this patch with the one you have mentioned (core/modules/node/src/Plugin/views/argument/Vid.php), with all the other files. Do let me know please for any other observations. Also, added the interdiff.
Thanks.
Comment #17
daffie commentedAlmost ready for the view from @alexpott.
Can we change
GREATEST(COALESCE(h.timestamp,toGREATEST(COALESCE([h].[timestamp],.Comment #18
megha_kundar commentedHi @daffie,
i have worked upon #9 patch.
please review this patch.
Comment #19
megha_kundar commentedHi @daffie,
Updated patch as mentioned in #17 let me know if any changes.
Comment #20
s_bhandari commented@daffie, I have checked the patches #18, #19, and can see that the observations you have shared in #17 have been addressed in these patches.
Comment #21
daffie commentedThe patch is almost ready for a review by @alexpott. Just 2 remarks left:
Can we add brackets to the field "timestamp".
Why was this change removed? Can you put it back in.
Comment #22
megha_kundar commentedComment #23
daffie commentedAfter talking about this issue with @alexpott on Slack. He wanted to have all queries that are, according to him in the "critical path", to have the field be bracket encapsulated. All the other queries should be changed to dynamic queries. The queries in the current patch have all fields been bracket encapsulated. It is now up to @alexpott to determine which queries are in the "critical path" and which are not. For that am I adding the tag "Needs framework manager review" and changing the status to RTBC.
Comment #24
dwwRe: #17.2: See also #3163934: Remove raw DB queries in AssignOwnerNode::buildConfigurationForm() and always use autocomplete
Although as a bug fix, this should probably be backported, while that would be a UI-change only in 9.1.x. So we can proceed here, then remove all of that crap from AssignOwnerNode::buildConfigurationForm() afterwards.
Meanwhile, I just carefully reviewed the patch. This whole topic is new to me, so mostly these are questions. A few hyper-detail nitpicks, too.
But I'm setting back to NR for confirmation before this is RTBC and a core committer spends time on it.
A) Do we need
as [num_comments]?B) Should we capitalize AS since it's an actual reserved word? ;)
C) I don't understand why we need
[n]. If we have a raw 'n' as the table alias, and that works, how could 'n.' possibly conflict with a reserved word? Can these ben.[nid]?D) If we insist on
[n].[nid], wouldn't we "need" to doFROM {node} [n]for completeness?Since we're changing this line, shouldn't we remove the double space before 'and' and capitalize it?
... :system_schema AND [name] ...?Since we're changing, add the spaces around
=?WHERE [nid] = :nid?s/as/AS/ ?
Thanks!
-Derek
Comment #25
daffie commentedThank you for your review @dww.
For #24.1.A and #24.1.D: This is something we do not do at the moment, but maybe that is something we start doing.
For #24.1.B and #24.4: Already fixed in #3159982: AS keyword should be capitalised in SQL queries.
For #24.1.C: We are adding quotes to field names. The part "n." is part of the field name. I have also no problem with doing "n.[nid]" as you are suggesting.
For #24.2 and #24.3: Could be seen as out of scope, but I have no problem in doing that in this issue.
Comment #26
dwwThanks, @daffie!
Re: #24.1: I definitely prefer C over A and D. ;) So if you're okay with that, there are a *bunch* of instances in the latest patch that can be undone, which will make this a much smaller change/patch for the next review.
If 24.1B and 24.4 are already fixed, this needs a reroll. ;)
Yeah, could go either way on the scope for 24.2 and 24.3. I tend to have the "if we're changing the line, we should make the line correct" approach. Not to go fishing for every whitespace problem in the whole file. But since we're already touching it, seems better to fix the spacing at the same time. Saves all the churn of a whole separate issue which will take a dozen hours of contributor / committer time to fix 3 spaces. ;)
Comment #27
s_bhandari commentedHi,
Four files of Patch #22 is getting failed to apply. Adding this patch with the correction of the same, as well as addressing the changes mentioned in #24 and #25. Also, there are problems occurring during creating interdiff. So, added the git diff output of both patch (#22 and #27).
Please review the same and let me know for any observations.
Thanks.
Comment #29
daffie commentedCould we not add square brackets to alias names. Do
WHERE u.[uid] = :uidinstead ofWHERE [u].[uid] = :uid. All the other changes look good. Points #24.2 and #24.3 are still open.Comment #30
daffie commentedPostponed on #3189880: Use square brackets syntax in sql queries.
Comment #31
daffie commentedThis issue is fixed in #3189880: Use square brackets syntax in sql queries. People in this issue got commit credits in the other issue.