Skip to content

Commit

Permalink
Use different character to escape wildcards due to MySQL version diff…
Browse files Browse the repository at this point in the history
…erences
  • Loading branch information
aimeos committed Mar 2, 2017
1 parent 8d37307 commit f28c0bf
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/mwlib/src/MW/Criteria/Expression/Compare/SQL.php
Expand Up @@ -67,7 +67,7 @@ protected function createTerm( $name, $type, $value )
$term = $name . ' ' . self::$operators[$this->getOperator()] . ' ' . $this->escape( $this->getOperator(), $type, $value );

if( in_array( $this->getOperator(), array( '=~', '~=' ), true ) ) {
$term .= ' ESCAPE \'_\'';
$term .= ' ESCAPE \'#\'';
}

return $term;
Expand Down Expand Up @@ -167,7 +167,7 @@ protected function escape( $operator, $type, $value )
case \Aimeos\MW\DB\Statement\Base::PARAM_STR:
if( in_array( $operator, array( '~=', '=~' ), true ) )
{
$value = str_replace( array( '%', '_', '[' ), array( '_%', '__', '_[' ), $this->conn->escape( $value ) );
$value = str_replace( array( '%', '_', '[' ), array( '#%', '#_', '#[' ), $this->conn->escape( $value ) );
$value = '\'%' . $value . '%\''; break;
}
default:
Expand Down
2 changes: 1 addition & 1 deletion lib/mwlib/tests/MW/Criteria/Expression/Combine/SQLTest.php
Expand Up @@ -72,7 +72,7 @@ public function testToString()
$object = new \Aimeos\MW\Criteria\Expression\Combine\SQL( '||', $objects );
$test = new \Aimeos\MW\Criteria\Expression\Combine\SQL( '!', array( $object ) );

$expected = " NOT ( ( list IN ('a','b','c') AND string LIKE '%value%' ESCAPE '_' ) OR ( float < 0.1 AND int > 10 ) )";
$expected = " NOT ( ( list IN ('a','b','c') AND string LIKE '%value%' ESCAPE '#' ) OR ( float < 0.1 AND int > 10 ) )";
$this->assertEquals( $expected, $test->toString( $types ) );

$obj = new \Aimeos\MW\Criteria\Expression\Combine\SQL('&&', array());
Expand Down
4 changes: 2 additions & 2 deletions lib/mwlib/tests/MW/Criteria/Expression/Compare/SQLTest.php
Expand Up @@ -92,7 +92,7 @@ public function testToString()
$this->assertEquals( "t.list NOT IN ('a','b','c')", $expr->toString( $types, $translations ) );

$expr = new \Aimeos\MW\Criteria\Expression\Compare\SQL( $this->conn, '~=', 'string', 'value' );
$this->assertEquals( "t.string LIKE '%value%' ESCAPE '_'", $expr->toString( $types, $translations ) );
$this->assertEquals( "t.string LIKE '%value%' ESCAPE '#'", $expr->toString( $types, $translations ) );

$expr = new \Aimeos\MW\Criteria\Expression\Compare\SQL( $this->conn, '<', 'float', 0.1 );
$this->assertEquals( "t.float < 0.1", $expr->toString( $types, $translations ) );
Expand Down Expand Up @@ -131,7 +131,7 @@ public function testToStringFunction()
$this->assertEquals( "count('name',10,0.1) = 3", $expr->toString( $types, $translations ) );

$expr = new \Aimeos\MW\Criteria\Expression\Compare\SQL( $this->conn, '~=', 'strconcat("hello","world")', 'low' );
$this->assertEquals( "concat('hello','world') LIKE '%low%' ESCAPE '_'", $expr->toString( $types, $translations ) );
$this->assertEquals( "concat('hello','world') LIKE '%low%' ESCAPE '#'", $expr->toString( $types, $translations ) );

$expr = new \Aimeos\MW\Criteria\Expression\Compare\SQL( $this->conn, '==', 'lcounter(["a","b","c","\'d"])', 4 );
$this->assertRegexp( "/^count\(name IN \('a','b','c','('|\\\\)'d'\)\) = 4$/", $expr->toString( $types, $translations ) );
Expand Down
2 changes: 1 addition & 1 deletion lib/mwlib/tests/MW/Criteria/SQLTest.php
Expand Up @@ -101,7 +101,7 @@ public function testGetConditionString()

$expr = array( $this->object->compare( '==', 'int_column', 1 ), $this->object->compare( '~=', 'str_column', array( 't1', 't2', 't3' ) ) );
$this->object->setConditions( $this->object->combine( '&&', $expr ) );
$this->assertEquals( "( int_col = 1 AND (str_col LIKE '%t1%' ESCAPE '_' OR str_col LIKE '%t2%' ESCAPE '_' OR str_col LIKE '%t3%' ESCAPE '_') )", $this->object->getConditionString( $types, $translations ) );
$this->assertEquals( "( int_col = 1 AND (str_col LIKE '%t1%' ESCAPE '#' OR str_col LIKE '%t2%' ESCAPE '#' OR str_col LIKE '%t3%' ESCAPE '#') )", $this->object->getConditionString( $types, $translations ) );

$expr = array( $this->object->compare( '==', 'int_column', 1 ), $this->object->compare( '!=', 'int_column', 2 ) );
$this->object->setConditions( $this->object->combine( '!', array( $this->object->combine( '&&', $expr ) ) ) );
Expand Down

0 comments on commit f28c0bf

Please sign in to comment.