Unverified Commit 832b5b5c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2902311 by jhedstrom, dawehner, joachim, daffie, moshe weitzman,...

Issue #2902311 by jhedstrom, dawehner, joachim, daffie, moshe weitzman, longwave, Berdir: add __toString() to the entity Query class

(cherry picked from commit d86ce736)
parent 584f1574
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -326,4 +326,28 @@ public function getTables(SelectInterface $sql_query) {
    return new $class($sql_query);
  }

  /**
   * Implements the magic __toString method.
   */
  public function __toString() {
    // Clone the query so the prepare and compile doesn't get repeated.
    $clone = clone($this);

    $clone->prepare()
      ->compile()
      ->addSort()
      ->finish();

    // Quote arguments so query is able to be run.
    $quoted = [];
    foreach ($clone->sqlQuery->getArguments() as $key => $value) {
      $quoted[$key] = is_null($value) ? 'NULL' : $this->connection->quote($value);
    }

    // Replace table name brackets.
    $sql = $clone->connection->prefixTables((string) $clone->sqlQuery);

    return strtr($sql, $quoted);
  }

}
+32 −0
Original line number Diff line number Diff line
@@ -1201,4 +1201,36 @@ public function testConditionOnRevisionMetadataKeys() {
    $this->assertEquals($entity->id(), reset($result));
  }

  /**
   * Tests __toString().
   */
  public function testToString() {
    $query = $this->storage->getQuery();
    $group_blue = $query->andConditionGroup()->condition("{$this->figures}.color", ['blue'], 'IN');
    $group_red = $query->andConditionGroup()->condition("{$this->figures}.color", ['red'], 'IN');
    $null_group = $query->andConditionGroup()->notExists("{$this->figures}.color");
    $this->queryResults = $query
      ->condition($group_blue)
      ->condition($group_red)
      ->condition($null_group)
      ->sort('id');

    $figures = $this->figures;

    $expected = <<<EOF
SELECT base_table.revision_id AS revision_id, base_table.id AS id
FROM
{entity_test_mulrev} base_table
INNER JOIN {entity_test_mulrev__{$figures}} entity_test_mulrev__$figures ON entity_test_mulrev__$figures.entity_id = base_table.id
INNER JOIN {entity_test_mulrev__{$figures}} entity_test_mulrev__{$figures}_2 ON entity_test_mulrev__{$figures}_2.entity_id = base_table.id
LEFT JOIN {entity_test_mulrev__{$figures}} entity_test_mulrev__{$figures}_3 ON entity_test_mulrev__{$figures}_3.entity_id = base_table.id
WHERE (entity_test_mulrev__{$figures}.{$figures}_color IN ('blue')) AND (entity_test_mulrev__{$figures}_2.{$figures}_color IN ('red')) AND (entity_test_mulrev__{$figures}_3.{$figures}_color IS NULL)
ORDER BY base_table.id ASC
EOF;

    // Apply table prefixes to the expected sql.
    $expected = \Drupal::database()->prefixTables($expected);
    $this->assertEquals($expected, (string) $query);
  }

}