1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-02-24 12:03:21 +01:00

Merge remote-tracking branch 'nickvergessen/ticket/10942' into develop

* nickvergessen/ticket/10942:
  [ticket/10942] Avoid possible conflicts with magic words in unit tests
  [ticket/10942] Add access modifiers
  [ticket/10942] Use ANSI SQL standard || in dbal.php
  [ticket/10942] Fix up unit tests for sql_case()
  [ticket/10942] Require same data type and do not cast expressions automatically
  [ticket/10942] Make unit tests for sql_case simpler
  [ticket/10942] Add a comment why we cast to sql_case()
  [ticket/10942] Rename method sql_conditional() to sql_case()
  [ticket/10942] Change term string to expression to avoid confusion
  [ticket/10942] Fix sql_conditional for mssql, postgre and oracle
  [ticket/10942] Fix function name on order_lower_test.php
  [ticket/10942] Add unit tests for sql_concatenate
  [ticket/10942] Add sql_concatenate to dbal
  [ticket/10942] Add unit tests for sql_conditional
  [ticket/10942] Add sql_conditional to dbal
This commit is contained in:
Andreas Fischer 2012-07-17 17:59:56 +02:00
commit 6980b3dcfd
9 changed files with 205 additions and 1 deletions

View File

@ -283,6 +283,37 @@ class dbal
return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\'');
}
/**
* Build a case expression
*
* Note: The two statements action_true and action_false must have the same data type (int, vchar, ...) in the database!
*
* @param string $condition The condition which must be true, to use action_true rather then action_else
* @param string $action_true SQL expression that is used, if the condition is true
* @param string $action_else SQL expression that is used, if the condition is false, optional
* @return string CASE expression including the condition and statements
*/
public function sql_case($condition, $action_true, $action_false = false)
{
$sql_case = 'CASE WHEN ' . $condition;
$sql_case .= ' THEN ' . $action_true;
$sql_case .= ($action_false !== false) ? ' ELSE ' . $action_false : '';
$sql_case .= ' END';
return $sql_case;
}
/**
* Build a concatenated expression
*
* @param string $expr1 Base SQL expression where we append the second one
* @param string $expr2 SQL expression that is appended to the first expression
* @return string Concatenated string
*/
public function sql_concatenate($expr1, $expr2)
{
return $expr1 . ' || ' . $expr2;
}
/**
* Returns whether results of a query need to be buffered to run a transaction while iterating over them.
*

View File

@ -91,6 +91,14 @@ class dbal_mssql extends dbal
return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
}
/**
* {@inheritDoc}
*/
public function sql_concatenate($expr1, $expr2)
{
return $expr1 . ' + ' . $expr2;
}
/**
* SQL Transaction
* @access private

View File

@ -109,6 +109,14 @@ class dbal_mssql_odbc extends dbal
return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
}
/**
* {@inheritDoc}
*/
public function sql_concatenate($expr1, $expr2)
{
return $expr1 . ' + ' . $expr2;
}
/**
* SQL Transaction
* @access private

View File

@ -257,6 +257,14 @@ class dbal_mssqlnative extends dbal
return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
}
/**
* {@inheritDoc}
*/
public function sql_concatenate($expr1, $expr2)
{
return $expr1 . ' + ' . $expr2;
}
/**
* {@inheritDoc}
*/

View File

@ -119,6 +119,14 @@ class dbal_mysql extends dbal
return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
}
/**
* {@inheritDoc}
*/
public function sql_concatenate($expr1, $expr2)
{
return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')';
}
/**
* SQL Transaction
* @access private

View File

@ -122,6 +122,14 @@ class dbal_mysqli extends dbal
return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
}
/**
* {@inheritDoc}
*/
public function sql_concatenate($expr1, $expr2)
{
return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')';
}
/**
* SQL Transaction
* @access private

69
tests/dbal/case_test.php Normal file
View File

@ -0,0 +1,69 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class phpbb_dbal_case_test extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml');
}
public function test_case_int()
{
$db = $this->new_dbal();
$sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '2') . ' AS test_num
FROM phpbb_config';
$result = $db->sql_query_limit($sql, 1);
$this->assertEquals(1, (int) $db->sql_fetchfield('test_num'));
$sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '2') . ' AS test_num
FROM phpbb_config';
$result = $db->sql_query_limit($sql, 1);
$this->assertEquals(2, (int) $db->sql_fetchfield('test_num'));
}
public function test_case_string()
{
$db = $this->new_dbal();
$sql = 'SELECT ' . $db->sql_case('1 = 1', "'foo'", "'bar'") . ' AS test_string
FROM phpbb_config';
$result = $db->sql_query_limit($sql, 1);
$this->assertEquals('foo', $db->sql_fetchfield('test_string'));
$sql = 'SELECT ' . $db->sql_case('1 = 0', "'foo'", "'bar'") . ' AS test_string
FROM phpbb_config';
$result = $db->sql_query_limit($sql, 1);
$this->assertEquals('bar', $db->sql_fetchfield('test_string'));
}
public function test_case_column()
{
$db = $this->new_dbal();
$sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string
FROM phpbb_config
WHERE config_name = 'config1'";
$result = $db->sql_query_limit($sql, 1);
$this->assertEquals('config1', $db->sql_fetchfield('test_string'));
$sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string
FROM phpbb_config
WHERE config_value = 'bar'";
$result = $db->sql_query_limit($sql, 1);
$this->assertEquals('bar', $db->sql_fetchfield('test_string'));
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class phpbb_dbal_concatenate_test extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml');
}
public function test_concatenate_string()
{
$db = $this->new_dbal();
$sql = 'SELECT config_name, ' . $db->sql_concatenate('config_name', "'" . $db->sql_escape('append') . "'") . ' AS string
FROM phpbb_config';
$result = $db->sql_query($sql);
$db->sql_return_on_error(false);
$this->assertEquals(array(
array(
'config_name' => 'config1',
'string' => 'config1append',
),
array(
'config_name' => 'config2',
'string' => 'config2append',
),
),
$db->sql_fetchrowset($result)
);
}
public function test_concatenate_statement()
{
$db = $this->new_dbal();
$sql = 'SELECT config_name, ' . $db->sql_concatenate('config_name', 'config_value') . ' AS string
FROM phpbb_config';
$result = $db->sql_query($sql);
$db->sql_return_on_error(false);
$this->assertEquals(array(
array(
'config_name' => 'config1',
'string' => 'config1foo',
),
array(
'config_name' => 'config2',
'string' => 'config2bar',
),
),
$db->sql_fetchrowset($result)
);
}
}

View File

@ -14,7 +14,7 @@ class phpbb_dbal_order_lower_test extends phpbb_database_test_case
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/styles.xml');
}
public function test_cross_join()
public function test_order_lower()
{
$db = $this->new_dbal();