Commit 39014648 authored by alexpott's avatar alexpott

Issue #2845543 by vaplas, gambry, alexpott: PostgreSQL regular expression...

Issue #2845543 by vaplas, gambry, alexpott: PostgreSQL regular expression match operators works only for text
parent 3404c729
......@@ -204,14 +204,11 @@ public function query($query, array $args = array(), $options = array()) {
}
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()) {
......
......@@ -509,6 +509,13 @@ public function testRegexCondition() {
$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.');
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment