Skip to content

Commit

Permalink
DibiPostgreDriver: use 'E' prefix in escapeLike() [Closes dg#159]
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed Jan 22, 2015
1 parent 97b50bd commit 5ee7c79
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
11 changes: 7 additions & 4 deletions dibi/drivers/DibiPostgreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** @var int|FALSE Affected rows */
private $affectedRows = FALSE;

/** @var string or FALSE for PostgreSQL < 8.0 */
private $version;


/**
* @throws DibiNotSupportedException
Expand Down Expand Up @@ -83,6 +86,7 @@ public function connect(array & $config)
if (DibiDriverException::catchError($msg)) {
throw new DibiDriverException($msg, 0);
}
$this->version = pg_parameter_status($this->resource, 'server_version');
}

if (!is_resource($this->connection)) {
Expand Down Expand Up @@ -312,7 +316,7 @@ public function escapeLike($value, $pos)
{
$value = pg_escape_string($this->connection, $value);
$value = strtr($value, array( '%' => '\\\\%', '_' => '\\\\_'));
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
return ($this->version < 8.2 ? '' : 'E') . ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
}


Expand Down Expand Up @@ -445,8 +449,7 @@ public function getResultResource()
*/
public function getTables()
{
$version = pg_parameter_status($this->resource, 'server_version');
if ($version < 7.4) {
if ($this->version < 7.4) {
throw new DibiDriverException('Reflection requires PostgreSQL 7.4 and newer.');
}

Expand All @@ -462,7 +465,7 @@ public function getTables()
WHERE
table_schema = ANY (current_schemas(false))";

if ($version >= 9.3) {
if ($this->version >= 9.3) {
$query .= "
UNION ALL
SELECT
Expand Down
42 changes: 42 additions & 0 deletions tests/dibi/DibiPostgreDriver.like.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @dataProvider ../databases.ini pgsql
*/

use Tester\Assert;

require __DIR__ . '/bootstrap.php';


$danger = '_ % \\';

test(function() use ($config, $danger) {
$conn = new DibiConnection($config);
$conn->query('SET standard_conforming_strings = on');
Assert::false($conn->query("SELECT 'ROLEX' LIKE %~like~", 'ROLE_')->fetchSingle());
Assert::true($conn->query("SELECT 'ROLE_X' LIKE %~like~", 'ROLE_')->fetchSingle());

Assert::same("E'%\\\\_ \\\\% \\%'", $conn->getDriver()->escapeLike($danger, 0));
});


test(function() use ($config, $danger) {
$conn = new DibiConnection($config);
$conn->query('SET standard_conforming_strings = off');
$conn->query('SET escape_string_warning = off'); // do not write into PostgreSQL log

Assert::false($conn->query("SELECT 'ROLEX' LIKE %~like~", 'ROLE_')->fetchSingle());
Assert::true($conn->query("SELECT 'ROLE_X' LIKE %~like~", 'ROLE_')->fetchSingle());

Assert::same("E'%\\\\_ \\\\% \\\\%'", $conn->getDriver()->escapeLike($danger, 0));
});


test(function() use ($config) {
$conn = new DibiConnection($config);

Assert::same("E'%A'", $conn->getDriver()->escapeLike('A', -1));
Assert::same("E'%A%'", $conn->getDriver()->escapeLike('A', 0));
Assert::same("E'A%'", $conn->getDriver()->escapeLike('A', 1));
});

0 comments on commit 5ee7c79

Please sign in to comment.