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().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Using MAMP with SQlite 3.7.3

Damien Tournoud’s picture

That'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 in db_query().

Dave Reid’s picture

Here's what happens in DatabaseStatement_sqlite::getStatement():

First query:

SELECT v.name AS name
FROM 
variable v
WHERE  (name LIKE :db_condition_placeholder_0 ESCAPE '\') 

Second query:
SELECT name FROM variable WHERE name LIKE :name

Dave Reid’s picture

Title: db_like() does not work when used with db_query() » Document that db_like() does not work when used with db_query()

Yeah, I'm fine documenting that using db_like is only compatible with db_select() - hell the example for db_like() uses db_query()... :)

Dave Reid’s picture

Component: sqlite database » database system
Status: Active » Needs review
FileSize
1.33 KB
Crell’s picture

Status: Needs review » Reviewed & tested by the community

Have I mentioned how much I hate SQL databases?

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs backport to D7

This 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.

neclimdul’s picture

It 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.

xjm’s picture

mgifford’s picture

Version: 7.x-dev » 8.0.x-dev
Assigned: Dave Reid » Unassigned
Issue summary: View changes
Status: Needs review » Needs work

I think this was supposed to be applied to D8 3 years ago, but somehow wasn't.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

daffie’s picture

Status: Needs work » Closed (fixed)

This is already fix in Drupal 8.2.x.

David_Rothstein’s picture

Title: Document that db_like() does not work when used with db_query() » Document that db_like() and Database/Connection::escapeLike() do not work when used with db_query()
Status: Closed (fixed) » Needs work
Issue tags: +Needs backport to D7

This 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.

daffie’s picture

@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.

Crell’s picture

The 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.

David_Rothstein’s picture

+   * You must use a query builder like ->select() in order to use ->escapeLike() on
...
+   * You must use a query builder like Database::getConnection()->select() in
...
+   * You must use a query builder like
+   * Drupal\Core\Database::getConnection()->select() in

Technically 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.)

David_Rothstein’s picture

Actually 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 (but Connection::select() might?).

daffie’s picture

I 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?

David_Rothstein’s picture

I 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().

daffie’s picture

The automatic linking to api.drupal.org is important, but it reduces the readability of the added documentation a lot. So two new versions.

neclimdul’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Database/Connection.php
    @@ -987,12 +987,20 @@ public function escapeAlias($field) {
    +   * order to use \Drupal\Core\Database\Connection::escapeLike() on all
    +   * supported database systems. Using
    

    "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.

  2. +++ b/core/lib/Drupal/Core/Database/Connection.php
    @@ -987,12 +987,20 @@ public function escapeAlias($field) {
    +   * Drupal\Core\Database\Connection::escapeLike() with
    +   * Drupal\Core\Database\Connection::query() or
    +   * Drupal\Core\Database\Connection::queryRange() is not supported.
    

    These still don't have the leading \

daffie’s picture

@neclimdul: Good points.

Crell’s picture

Maybe 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()?

daffie’s picture

@Crell: You are right. Fixed it.

  • catch committed f28b98c on 8.3.x
    Issue #1182428 by Dave Reid: Fixed Document that db_like() does not work...

  • catch committed f28b98c on 8.3.x
    Issue #1182428 by Dave Reid: Fixed Document that db_like() does not work...
neclimdul’s picture

Status: Needs review » Reviewed & tested by the community

Thank you helpful merge commits for reminding us all this exists. Looks commitable to me. Good solid docs fixes.

catch’s picture

Version: 8.1.x-dev » 8.0.x-dev
Status: Reviewed & tested by the community » Fixed

This 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.

daffie’s picture

Issue summary: View changes
daffie’s picture

@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().

catch’s picture

@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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

apaderno’s picture