Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Document that you cannot use the method \Drupal\Core\Database\Connection::escapeLike() in the query builder methods \Drupal\Core\Database\Connection::query() and \Drupal\Core\Database\Connection::queryRange().
You can only use the method \Drupal\Core\Database\Connection::escapeLike() in the query builder \Drupal\Core\Database\Connection::select().
Comment | File | Size | Author |
---|---|---|---|
#24 | 1182428-24-fully-qualified.patch | 1.13 KB | daffie |
Comments
Comment #1
Dave ReidUsing MAMP with SQlite 3.7.3
Comment #2
Damien Tournoud CreditAttribution: Damien Tournoud commentedThat's probably because the ESCAPE clause is missing from your second query.
But given the amount of heavy-lifting required to make LIKE work consistently across database engines, I would recommend sticking with
db_select()
and considering that LIKE is not supported indb_query()
.Comment #3
Dave ReidHere's what happens in DatabaseStatement_sqlite::getStatement():
First query:
Second query:
SELECT name FROM variable WHERE name LIKE :name
Comment #4
Dave ReidYeah, I'm fine documenting that using db_like is only compatible with db_select() - hell the example for db_like() uses db_query()... :)
Comment #5
Dave ReidComment #6
Crell CreditAttribution: Crell commentedHave I mentioned how much I hate SQL databases?
Comment #7
catchThis needed a re-roll for /core, but I just did a cd /core before applying and it still applied with that fortunately.
Seems like we need to backport this to 7.x too, patch here might apply so leaving CNR.
Comment #8
neclimdulIt may be because of the "For example" but I think the documentation would flow better if the new note was after the example instead of before it. The for example references the paragraph describing how to use it, not the noted caveat about its usage we're adding.
Comment #9
xjm#5: 1182428-dblike-does-not-in-fact-like-dbquery.patch queued for re-testing.
Comment #10
mgiffordI think this was supposed to be applied to D8 3 years ago, but somehow wasn't.
Comment #12
daffie CreditAttribution: daffie commentedThis is already fix in Drupal 8.2.x.
Comment #13
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedThis looks like it's in Drupal 8 at https://api.drupal.org/api/drupal/core%21includes%21database.inc/functio... but that function is marked deprecated in favor of https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%... and that one does not document this at all. So I think it just needs to be copied to the new function also.
(Although as part of copying it, the code example probably should be rewritten to actually use escapeLike() rather than db_like()... the current example on escapeLike() actually uses db_like() instead which doesn't make much sense.)
Plus, this issue originally had the backport tag and isn't fixed in Drupal 7 yet.
Comment #14
daffie CreditAttribution: daffie commented@David: I did not thought about Connection::escapeLike(). Good thinking on your part! I made three options for escapeLike. A short , medium and long version. Which do you like. My preference is the medium one.
Comment #15
Crell CreditAttribution: Crell at Platform.sh commentedThe short version is closest, but we shouldn't be recommending/encouraging the use of the global statics, ever. Just use $connection, people will figure out they need to get it from somewhere.
Comment #16
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedTechnically according to https://www.drupal.org/coding-standards/docs#classes documentation is supposed to use the fully-qualified namespace, so actually none of the above are quite right... it should be something like
\Drupal\Core\Database\Connection::select()
instead.(Most important, though, is to make sure it uses something that api.drupal.org can figure out how to link to, so it's clickable when someone reads this on api.drupal.org. The fully-qualified namespace presumably meets that criterion; not sure offhand if anything else does.)
Comment #17
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedActually I guess the coding standards don't quite say you have to use a fully-qualified namespace (just that if you do use one, it should be fully-qualified). Key point is still that it should be clickable on api.drupal.org though... I don't think
->select()
would be (butConnection::select()
might?).Comment #18
daffie CreditAttribution: daffie commentedI did a combination of
\Drupal\Core\Database\Connection::query
and$connection->query()
or$connection::query()
in the hope that people more easily will figure out what the variable $connection means. Which version do you think is better?Comment #19
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedI think it probably should be the opposite actually (
\Drupal\Core\Database\Connection::query()
in the paragraph so that api.drupal.org will turn those into clickable links, then$connection->query()
in the code example since that's what actual code will look like)?The code example also still needs to be updated to change db_like() to $connection->escapeLike().
Comment #20
daffie CreditAttribution: daffie commentedThe automatic linking to api.drupal.org is important, but it reduces the readability of the added documentation a lot. So two new versions.
Comment #21
neclimdul"on all supported database systems" isn't that implied by them implementing the interface. Additionally is it even something we can claim since a database could implement the method with trigger_error() and not support it if they wanted.
These still don't have the leading \
Comment #22
daffie CreditAttribution: daffie commented@neclimdul: Good points.
Comment #23
Crell CreditAttribution: Crell at Platform.sh commentedMaybe I'm misreading, but doesn't the text now say "you can't use query() with escapeLike()", and then show an example that's based on query()?
Comment #24
daffie CreditAttribution: daffie commented@Crell: You are right. Fixed it.
Comment #27
neclimdulThank you helpful merge commits for reminding us all this exists. Looks commitable to me. Good solid docs fixes.
Comment #28
catchThis needs a new issue for the documentation follow-up, and a new issue for the 7.x backport. Moving back to fixed since the original patch was committed in 2011. Please link to the new issues from here if you open them.
Comment #29
daffie CreditAttribution: daffie commentedComment #30
daffie CreditAttribution: daffie commented@catch: I agree with you that there needs to be a new issue. But marking this one as fixed and a automatic closed after 2 weeks makes this issue disappear. And the follow up work that is in this issue with it. Not happy about it. I created a follow up in #2786811: Document that Database/Connection::escapeLike() does not work when used with Database/Connection::query() and Database/Connection::queryRange().
Comment #31
catch@daffie if you look at #10 the follow-up work was already lost for three years because the issue was against the 7.x branch at CNR - it's the re-opening issues for minor changes that causes work to get lost in the cracks, not asking for proper follow-ups to be opened.
Comment #32
YesCT CreditAttribution: YesCT commentedComment #34
apaderno