API page: https://api.drupal.org/api/drupal/core%21includes%21database.inc/group/d...

Enter a descriptive title (above) relating to Database abstraction layer, then describe the problem you have found:

On #2148255: [meta] Make better D8 api.d.o landing page, linked to high-level overview topics, and put it in Core api.php files we are making a better api.drupal.org landing page for Drupal 8. It needs to link to a page on the Database Abstraction Layer, and we already have such a page (see link above).

That page needs to describe PDO and entity field queries, in addition to what it currently covers. Perhaps it should be pared down into an overview with links to separate more detailed topics?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

jhodgdon’s picture

Assigned: Unassigned » jhodgdon

I'm going to work on this one.

jhodgdon’s picture

Status: Active » Needs review
FileSize
16.89 KB

Here's a first pass on this documentation update.

As a note, I also filed:
#2287151: StatementInterface docs have a bunch of obsolete stuff in them
and anyone reviewing this patch might want to take a look at that too?

jhodgdon’s picture

FileSize
16.88 KB
1.4 KB

chx made some suggestions in IRC. New patch.

Crell’s picture

Status: Needs review » Needs work
  1. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * @sec sec_entity Querying entities
    

    I question if this belongs in the database documentation. It's part of the entity system, NOT the Database component. The database component should be seen as a stand-alone library; there's only a few technical reasons it isn't yet, because we've not fully unwound it. Assume that DBTNG will eventually move to \Drupal\Component\ and potentially even its own repository.

    A warning to not use the DB API directly for querying entities is fine, but the actual documentation of how to do so belongs elsewhere.

  2. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * - Retrieve the entity query object (which implements
    + *   \Drupal\Core\Entity\Query\QueryInterface) appropriate to the entity type:
    + *   @code
    + *   // If you have a $container variable:
    + *   $container->get('entity.query')->get('your_entity_type');
    + *   // If you don't:
    + *   $query = \Drupal::entityQuery('your_entity_type');
    + *   @endcode
    

    Except most of the time you should get the entity.query service injected rather than accessing the container directly or calling the global. (This is a global problem of how to document things, not specific to this case.)

  3. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * abstraction layer provides the functions db_query() and db_query_range(),
    

    If we're updating the documentation, we should also not refer to deprecated wrapper functions and use the object form instead.

  4. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * In this context, "simple" means a query that does not involve a join, LIKE,
    

    JOINs are fairly safe in most cases. If you can get away with just using query() you should.

  5. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + *   ->fields('e', array('id', 'title', 'created'))
    

    Perhaps it's time to start using the short-array syntax here, as it's actually considerably more readable. (Fewer things using (), so the code looks less like LISP.)

  6. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * consistently across databases. So, you should never use db_query() to run
    

    "So" is unnecessary in this sentence.

  7. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * an INSERT, UPDATE, or DELETE query. Instead, use functions db_insert(),
    

    As above, we should stop referring to the functions and use the methods instead.

  8. +++ b/core/includes/database.inc
    @@ -18,123 +18,160 @@
    + * Drupal supports transactions, including a transparent fallback for
      * databases that do not support transactions. To start a new transaction,
    

    We're likely to remove the transparent fallback logic, as it's kinda broken right now: #2278971: Deprecate Connection::supportsTransactions()

jhodgdon’s picture

Thanks for the review! I've made some updates... moved the entity stuff into the entity topic, and addressed most of your points (I think).

A few notes:

The functions db_select() and friends are *not* deprecated and are used quite extensively in Core. I did already have a section at the end about the connection objects... I've put in more references to that section. But I don't want to rewrite the entire topic and its examples. I don't think it adds a lot.

Regarding the transaction code... that was already in the topic, and I only cleaned up the wording a bit... I really don't know anything much about transactions. If that other patch lands, it should be updating the documentation, correct? We need to document what's true now, not wait for some other issue to maybe be resolved.

Regarding #5, I have no idea what you mean by "short array syntax", sorry.

Note: The interdiff here is almost as big as the patch; you may have better luck just reviewing the new patch.

chx’s picture

+ * In this context, "simple" means a query that does not involve a LIKE
+ * or any SQL functions.

* For SELECT queries involving LIKE, joins, or any SQL functions, instead of
* using the db_query() and db_query_range() functions,

Complicated. What DBTNG does, if your conditions has a single operator ->condition('d.data', "% $key %", 'LIKE') then yes DBTNG can and will map your LIKE per database driver. However when using functions in the select part db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', then that's fine.

JOINs are very fine in a db_query().

When you absolutely must use the query builder is when the query string (without arguments) is not a fixed string; when the query string itself is dynamic. That immediately mandates db_select because otherwise you will create a sechole (even core did have a weakness relating to that although not an outright sechole).

I hope this explains it; sorry for not being able to put it into succint words. I would be glad helping in wordsmithing this. And yes, this is in the current docs and yes the current docs is incorrect.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
2.11 KB
18.95 KB

Good feedback, thanks! Here's a new patch.

chx’s picture

Status: Needs review » Reviewed & tested by the community

Let's do this, we can bikeshed more later.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

  • Commit e5b3f93 on 8.x by webchick:
    Issue #2191445 by jhodgdon: Fixed Database abstraction layer topic /...

Status: Fixed » Closed (fixed)

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