Skip to content

Commit

Permalink
PostgreDriver: consider 'standard_conforming_strings' settings in esc…
Browse files Browse the repository at this point in the history
…apeLike() [Closes dg#159]
  • Loading branch information
milo committed Jan 22, 2015
1 parent 97b50bd commit 585d9b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dibi/drivers/DibiPostgreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ public function escape($value, $type)
*/
public function escapeLike($value, $pos)
{
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off

$value = pg_escape_string($this->connection, $value);
$value = strtr($value, array( '%' => '\\\\%', '_' => '\\\\_'));
$value = strtr($value, array('%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\'));
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
}

Expand Down
31 changes: 31 additions & 0 deletions tests/dibi/DibiPostgreDriver.like.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

use Tester\Assert;

require __DIR__ . '/bootstrap.php';


$tests = function($conn) {
Assert::false($conn->query("SELECT 'AAxBB' LIKE %~like~", 'A_B')->fetchSingle());
Assert::true( $conn->query("SELECT 'AA_BB' LIKE %~like~", 'A_B')->fetchSingle());

Assert::false($conn->query("SELECT 'AAxBB' LIKE %~like~", 'A%B')->fetchSingle());
Assert::true( $conn->query("SELECT 'AA%BB' LIKE %~like~", 'A%B')->fetchSingle());

Assert::same('AA\\BB', $conn->query("SELECT 'AA\\BB'")->fetchSingle());
Assert::false($conn->query("SELECT 'AAxBB' LIKE %~like~", 'A\\B')->fetchSingle());
Assert::true( $conn->query("SELECT 'AA\\BB' LIKE %~like~", 'A\\B')->fetchSingle());
};

$conn = new DibiConnection($config);
$conn->query('SET standard_conforming_strings = on');
$tests($conn);

$conn = new DibiConnection($config);
$conn->query('SET escape_string_warning = off'); // do not log warnings
$conn->query('SET standard_conforming_strings = off');
$tests($conn);

0 comments on commit 585d9b4

Please sign in to comment.