1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-02-24 20:13:22 +01:00

Merge branch 'develop-ascraeus' into develop

* develop-ascraeus:
  [ticket/12671] Possibility to use NOT LIKE expression
  [ticket/12671] Possibility to use NOT LIKE expression
  [ticket/12671] Possibility to use NOT LIKE expression
  [ticket/12671] Possibility to use NOT LIKE expression
  [ticket/12671] Possibility to use NOT LIKE expression
This commit is contained in:
Joas Schilling 2014-08-10 13:35:37 +02:00
commit 489aba31c7
12 changed files with 175 additions and 3 deletions

View File

@ -369,6 +369,17 @@ abstract class driver implements driver_interface
return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\'');
}
/**
* {@inheritDoc}
*/
function sql_not_like_expression($expression)
{
$expression = utf8_str_replace(array('_', '%'), array("\_", "\%"), $expression);
$expression = utf8_str_replace(array(chr(0) . "\_", chr(0) . "\%"), array('_', '%'), $expression);
return $this->_sql_not_like_expression('NOT LIKE \'' . $this->sql_escape($expression) . '\'');
}
/**
* {@inheritDoc}
*/

View File

@ -418,6 +418,16 @@ interface driver_interface
*/
public function sql_like_expression($expression);
/**
* Correctly adjust NOT LIKE expression for special characters
* Some DBMS are handling them in a different way
*
* @param string $expression The expression to use. Every wildcard is
* escaped, except $this->any_char and $this->one_char
* @return string A SQL statement like: "NOT LIKE 'bertie_%'"
*/
public function sql_not_like_expression($expression);
/**
* Explain queries
*

View File

@ -417,6 +417,14 @@ class factory implements driver_interface
return $this->get_driver()->sql_like_expression($expression);
}
/**
* {@inheritdoc}
*/
public function sql_not_like_expression($expression)
{
return $this->get_driver()->sql_not_like_expression($expression);
}
/**
* {@inheritdoc}
*/

View File

@ -365,6 +365,15 @@ class mssql extends \phpbb\db\driver\driver
return $expression . " ESCAPE '\\'";
}
/**
* Build NOT LIKE expression
* @access private
*/
function _sql_not_like_expression($expression)
{
return $expression . " ESCAPE '\\'";
}
/**
* return sql error array
* @access private

View File

@ -51,6 +51,15 @@ abstract class mssql_base extends \phpbb\db\driver\driver
return $expression . " ESCAPE '\\'";
}
/**
* Build NOT LIKE expression
* @access private
*/
function _sql_not_like_expression($expression)
{
return $expression . " ESCAPE '\\'";
}
/**
* Build db-specific query data
* @access private

View File

@ -111,6 +111,15 @@ abstract class mysql_base extends \phpbb\db\driver\driver
return $expression;
}
/**
* Build NOT LIKE expression
* @access private
*/
function _sql_not_like_expression($expression)
{
return $expression;
}
/**
* Build db-specific query data
* @access private

View File

@ -656,6 +656,15 @@ class oracle extends \phpbb\db\driver\driver
return $expression . " ESCAPE '\\'";
}
/**
* Build NOT LIKE expression
* @access private
*/
function _sql_not_like_expression($expression)
{
return $expression . " ESCAPE '\\'";
}
function _sql_custom_build($stage, $data)
{
return $data;

View File

@ -378,6 +378,15 @@ class postgres extends \phpbb\db\driver\driver
return $expression;
}
/**
* Build NOT LIKE expression
* @access private
*/
function _sql_not_like_expression($expression)
{
return $expression;
}
/**
* {@inheritDoc}
*/

View File

@ -280,7 +280,7 @@ class sqlite extends \phpbb\db\driver\driver
*/
function sql_like_expression($expression)
{
// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
// Unlike LIKE, GLOB is unfortunately case sensitive.
// We only catch * and ? here, not the character map possible on file globbing.
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
@ -290,6 +290,23 @@ class sqlite extends \phpbb\db\driver\driver
return 'GLOB \'' . $this->sql_escape($expression) . '\'';
}
/**
* {@inheritDoc}
*
* For SQLite an underscore is a not-known character...
*/
function sql_not_like_expression($expression)
{
// Unlike NOT LIKE, NOT GLOB is unfortunately case sensitive.
// We only catch * and ? here, not the character map possible on file globbing.
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
return 'NOT GLOB \'' . $this->sql_escape($expression) . '\'';
}
/**
* return sql error array
* @access private

View File

@ -265,11 +265,11 @@ class sqlite3 extends \phpbb\db\driver\driver
/**
* {@inheritDoc}
*
* For SQLite an underscore is a not-known character...
* For SQLite an underscore is an unknown character.
*/
public function sql_like_expression($expression)
{
// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
// Unlike LIKE, GLOB is unfortunately case sensitive.
// We only catch * and ? here, not the character map possible on file globbing.
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
@ -279,6 +279,23 @@ class sqlite3 extends \phpbb\db\driver\driver
return 'GLOB \'' . $this->sql_escape($expression) . '\'';
}
/**
* {@inheritDoc}
*
* For SQLite an underscore is an unknown character.
*/
public function sql_not_like_expression($expression)
{
// Unlike NOT LIKE, NOT GLOB is unfortunately case sensitive
// We only catch * and ? here, not the character map possible on file globbing.
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
return 'NOT GLOB \'' . $this->sql_escape($expression) . '\'';
}
/**
* return sql error array
*

View File

@ -233,6 +233,66 @@ class phpbb_dbal_select_test extends phpbb_database_test_case
$db->sql_freeresult($result);
}
public function not_like_expression_data()
{
// * = any_char; # = one_char
return array(
array('barfoo', array(
array('username_clean' => 'foobar'),
array('username_clean' => 'bertie')
)),
array('bar', array(
array('username_clean' => 'barfoo'),
array('username_clean' => 'foobar'),
array('username_clean' => 'bertie'),
)),
array('bar*', array(
array('username_clean' => 'foobar'),
array('username_clean' => 'bertie'))
),
array('*bar*', array(array('username_clean' => 'bertie'))),
array('b*r', array(
array('username_clean' => 'barfoo'),
array('username_clean' => 'foobar'),
array('username_clean' => 'bertie')
)),
array('b*e', array(
array('username_clean' => 'barfoo'),
array('username_clean' => 'foobar')
)),
array('#b*e', array(
array('username_clean' => 'barfoo'),
array('username_clean' => 'foobar'),
array('username_clean' => 'bertie')
)),
array('b####e', array(
array('username_clean' => 'barfoo'),
array('username_clean' => 'foobar')
)),
);
}
/**
* @dataProvider not_like_expression_data
*/
public function test_not_like_expression($like_expression, $expected)
{
$db = $this->new_dbal();
$like_expression = str_replace('*', $db->get_any_char(), $like_expression);
$like_expression = str_replace('#', $db->get_one_char(), $like_expression);
$where = ($like_expression) ? 'username_clean ' . $db->sql_not_like_expression($like_expression) : '';
$result = $db->sql_query('SELECT username_clean
FROM phpbb_users
' . (($where) ? ' WHERE ' . $where : '') . '
ORDER BY user_id ASC');
$this->assertEquals($expected, $db->sql_fetchrowset($result));
$db->sql_freeresult($result);
}
public function in_set_data()
{
return array(

View File

@ -191,6 +191,10 @@ namespace phpbb\db\driver
{
}
function sql_not_like_expression($expression)
{
}
function sql_fetchrowset($query_id = false)
{
return array(