function ConditionCastTest::testRawSqlOperatorCastOnIntegerField
Same name and namespace in other branches
- main core/modules/pgsql/tests/src/Kernel/pgsql/ConditionCastTest.php \Drupal\Tests\pgsql\Kernel\pgsql\ConditionCastTest::testRawSqlOperatorCastOnIntegerField()
Tests LIKE and regex operators in raw SQL on an integer column.
SQL passed as a string does not go through condition compilation, so prepareStatement() adds the '::text' cast to these queries. Relying on this rewrite is deprecated.
Attributes
#[IgnoreDeprecations]
See also
\Drupal\pgsql\Driver\Database\pgsql\Connection::prepareStatement()
File
-
core/
modules/ pgsql/ tests/ src/ Kernel/ pgsql/ ConditionCastTest.php, line 62
Class
- ConditionCastTest
- Tests casting of non-text fields for LIKE and regex operators.
Namespace
Drupal\Tests\pgsql\Kernel\pgsqlCode
public function testRawSqlOperatorCastOnIntegerField() : void {
$this->expectUserDeprecationMessage('Relying on the PostgreSQL database driver to add a ::text type-cast to fields used with LIKE and regular expression operators in SQL passed as a string is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Add the ::text cast to the field explicitly or use the query builder. See https://www.drupal.org/node/3615418');
$statement = $this->connection
->prepareStatement('SELECT COUNT(*) FROM {test} WHERE [age] LIKE :pattern', []);
$this->assertStringContainsString('::text', $statement->getQueryString());
$count = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [age] LIKE :pattern', [
':pattern' => '2%',
])
->fetchField();
$this->assertSame(4, (int) $count);
$count = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [age] ~* :pattern', [
':pattern' => '8$',
])
->fetchField();
$this->assertSame(1, (int) $count);
// A raw condition snippet inside a query builder query.
$query = $this->connection
->select('test');
$query->addField('test', 'name');
$query->where('[age] LIKE :pattern', [
':pattern' => '%6',
]);
$this->assertCount(1, $query->execute()
->fetchAll());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.