From 39014648959cb95d3609bacee5730a0d3ed02a4c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 13 Feb 2017 17:25:06 +0000 Subject: [PATCH] Issue #2845543 by vaplas, gambry, alexpott: PostgreSQL regular expression match operators works only for text --- .../Core/Database/Driver/pgsql/Connection.php | 13 +++++-------- .../Drupal/KernelTests/Core/Database/SelectTest.php | 7 +++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index f081eff3e63e..59f3bd5a49b5 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -204,14 +204,11 @@ class Connection extends DatabaseConnection { } public function prepareQuery($query) { - // mapConditionOperator converts LIKE operations to ILIKE for consistency - // with MySQL. However, Postgres does not support ILIKE on bytea (blobs) - // fields. - // To make the ILIKE operator work, we type-cast bytea fields into text. - // @todo This workaround only affects bytea fields, but the involved field - // types involved in the query are unknown, so there is no way to - // conditionally execute this for affected queries only. - return parent::prepareQuery(preg_replace('/ ([^ ]+) +(I*LIKE|NOT +I*LIKE) /i', ' ${1}::text ${2} ', $query)); + // mapConditionOperator converts some operations (LIKE, REGEXP, etc.) to + // PostgreSQL equivalents (ILIKE, ~*, etc.). However PostgreSQL doesn't + // automatically cast the fields to the right type for these operators, + // so we need to alter the query and add the type-cast. + return parent::prepareQuery(preg_replace('/ ([^ ]+) +(I*LIKE|NOT +I*LIKE|~\*) /i', ' ${1}::text ${2} ', $query)); } public function queryRange($query, $from, $count, array $args = array(), array $options = array()) { diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php index cc857a27716d..b3eef5463018 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php @@ -509,6 +509,13 @@ class SelectTest extends DatabaseTestBase { $this->assertEqual(count($result), count($test_group['expected']), 'Returns the expected number of rows.'); $this->assertEqual(sort($result), sort($test_group['expected']), 'Returns the expected rows.'); } + + // Ensure that REGEXP filter still works with no-string type field. + $query = $database->select('test', 't'); + $query->addField('t', 'age'); + $query->condition('t.age', '2[6]', 'REGEXP'); + $result = $query->execute()->fetchField(); + $this->assertEquals($result, '26', 'Regexp with number type.'); } /**