From 71374078b969ab429f0efcdf99158c1d21b138b6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 01:50:00 +0200 Subject: [PATCH 01/15] [ticket/10942] Add sql_conditional to dbal PHPBB3-10942 --- phpBB/includes/db/dbal.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index cf54d455f7..bf0dc15fad 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -283,6 +283,23 @@ class dbal return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\''); } + /** + * Build a conditional SQL query + * + * @param string $condition The condition which must be true, to use action_true rather then action_else + * @param string $action_true SQL statement that is used, if the condition is true + * @param string $action_else SQL statement that is used, if the condition is false, optional + * @return string CASE expression including the condition and statements + */ + function sql_conditional($condition, $action_true, $action_false = false) + { + $sql_condition = 'CASE WHEN ' . $condition; + $sql_condition .= ' THEN ' . $action_true; + $sql_condition .= ($action_false !== false) ? ' ELSE ' . $action_false : ''; + $sql_condition .= ' END'; + return $sql_condition; + } + /** * Returns whether results of a query need to be buffered to run a transaction while iterating over them. * From dd0da6fffb3358937ebfdb3ff347089d97e64a1a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 01:50:42 +0200 Subject: [PATCH 02/15] [ticket/10942] Add unit tests for sql_conditional PHPBB3-10942 --- tests/dbal/conditional_test.php | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/dbal/conditional_test.php diff --git a/tests/dbal/conditional_test.php b/tests/dbal/conditional_test.php new file mode 100644 index 0000000000..3e01de05f6 --- /dev/null +++ b/tests/dbal/conditional_test.php @@ -0,0 +1,64 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function test_conditional_string() + { + $db = $this->new_dbal(); + + $sql = 'SELECT config_name, ' . $db->sql_conditional('is_dynamic = 1', "'" . $db->sql_escape('true') . "'", "'" . $db->sql_escape('false') . "'") . ' AS string + FROM phpbb_config'; + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array( + 'config_name' => 'config1', + 'string' => 'false', + ), + array( + 'config_name' => 'config2', + 'string' => 'true', + ), + ), + $db->sql_fetchrowset($result) + ); + } + + public function test_conditional_statement() + { + $db = $this->new_dbal(); + + $sql = 'SELECT config_name, ' . $db->sql_conditional('is_dynamic = 1', 'is_dynamic', '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' => 'foo', + ), + array( + 'config_name' => 'config2', + 'string' => '1', + ), + ), + $db->sql_fetchrowset($result) + ); + } +} From 1e3272bfced5e5a2295e6410ef1fec0800b58d99 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 02:10:04 +0200 Subject: [PATCH 03/15] [ticket/10942] Add sql_concatenate to dbal PHPBB3-10942 --- phpBB/includes/db/dbal.php | 12 ++++++++++++ phpBB/includes/db/firebird.php | 8 ++++++++ phpBB/includes/db/mssql.php | 8 ++++++++ phpBB/includes/db/mssql_odbc.php | 8 ++++++++ phpBB/includes/db/mssqlnative.php | 8 ++++++++ phpBB/includes/db/postgres.php | 8 ++++++++ phpBB/includes/db/sqlite.php | 8 ++++++++ 7 files changed, 60 insertions(+) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index bf0dc15fad..c040fa8e2d 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -300,6 +300,18 @@ class dbal return $sql_condition; } + /** + * Build a concatenated string + * + * @param string $string1 Base SQL statement where we append the second one + * @param string $string2 SQL statement that is appended on the first statement + * @return string Concatenated string + */ + function sql_concatenate($string1, $string2) + { + return 'CONCAT(' . $string1 . ', ' . $string2 . ')'; + } + /** * Returns whether results of a query need to be buffered to run a transaction while iterating over them. * diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index 7709e8fdf5..884206d298 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -110,6 +110,14 @@ class dbal_firebird extends dbal return ($raw) ? '2.1' : 'Firebird/Interbase'; } + /** + * {@inheritDoc} + */ + function sql_concatenate($string1, $string2) + { + return $string1 . ' || ' . $string2; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index abeabc389f..16b3b3c600 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -91,6 +91,14 @@ class dbal_mssql extends dbal return ($this->sql_server_version) ? 'MSSQL
' . $this->sql_server_version : 'MSSQL'; } + /** + * {@inheritDoc} + */ + function sql_concatenate($string1, $string2) + { + return $string1 . ' + ' . $string2; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index 6e24f4e9e8..c1ee1b43f0 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -109,6 +109,14 @@ class dbal_mssql_odbc extends dbal return ($this->sql_server_version) ? 'MSSQL (ODBC)
' . $this->sql_server_version : 'MSSQL (ODBC)'; } + /** + * {@inheritDoc} + */ + function sql_concatenate($string1, $string2) + { + return $string1 . ' + ' . $string2; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index 8a4503f111..db1bb86620 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -257,6 +257,14 @@ class dbal_mssqlnative extends dbal return ($this->sql_server_version) ? 'MSSQL
' . $this->sql_server_version : 'MSSQL'; } + /** + * {@inheritDoc} + */ + function sql_concatenate($string1, $string2) + { + return $string1 . ' + ' . $string2; + } + /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index bf22cffafa..dddf615f9c 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -154,6 +154,14 @@ class dbal_postgres extends dbal return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version; } + /** + * {@inheritDoc} + */ + function sql_concatenate($string1, $string2) + { + return $string1 . ' || ' . $string2; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php index 86bfa75a13..38aba0624e 100644 --- a/phpBB/includes/db/sqlite.php +++ b/phpBB/includes/db/sqlite.php @@ -72,6 +72,14 @@ class dbal_sqlite extends dbal return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version; } + /** + * {@inheritDoc} + */ + function sql_concatenate($string1, $string2) + { + return $string1 . ' || ' . $string2; + } + /** * SQL Transaction * @access private From 8cea7b2a5173a69889d76b1e27469cf15e95cefc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 02:10:54 +0200 Subject: [PATCH 04/15] [ticket/10942] Add unit tests for sql_concatenate PHPBB3-10942 --- tests/dbal/concatenate_test.php | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/dbal/concatenate_test.php diff --git a/tests/dbal/concatenate_test.php b/tests/dbal/concatenate_test.php new file mode 100644 index 0000000000..0891fa58a0 --- /dev/null +++ b/tests/dbal/concatenate_test.php @@ -0,0 +1,64 @@ +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) + ); + } +} From 4fbbbfcebb6f8ed9867cb61623767a2f4c191ed6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 02:12:48 +0200 Subject: [PATCH 05/15] [ticket/10942] Fix function name on order_lower_test.php PHPBB3-10942 --- tests/dbal/order_lower_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dbal/order_lower_test.php b/tests/dbal/order_lower_test.php index e16c0c20ee..84d454742f 100644 --- a/tests/dbal/order_lower_test.php +++ b/tests/dbal/order_lower_test.php @@ -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(); From c8e322d88f8195727c18eac90d30a5919e6fb8a0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 12:41:26 +0200 Subject: [PATCH 06/15] [ticket/10942] Fix sql_conditional for mssql, postgre and oracle PHPBB3-10942 --- phpBB/includes/db/mssql.php | 12 ++++++++++++ phpBB/includes/db/mssql_odbc.php | 12 ++++++++++++ phpBB/includes/db/mssqlnative.php | 12 ++++++++++++ phpBB/includes/db/oracle.php | 12 ++++++++++++ phpBB/includes/db/postgres.php | 12 ++++++++++++ 5 files changed, 60 insertions(+) diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index 16b3b3c600..ca8846b60d 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -91,6 +91,18 @@ class dbal_mssql extends dbal return ($this->sql_server_version) ? 'MSSQL
' . $this->sql_server_version : 'MSSQL'; } + /** + * {@inheritDoc} + */ + function sql_conditional($condition, $action_true, $action_false = false) + { + $sql_condition = 'CASE WHEN ' . $condition; + $sql_condition .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; + $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; + $sql_condition .= ' END'; + return $sql_condition; + } + /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index c1ee1b43f0..814dd76373 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -109,6 +109,18 @@ class dbal_mssql_odbc extends dbal return ($this->sql_server_version) ? 'MSSQL (ODBC)
' . $this->sql_server_version : 'MSSQL (ODBC)'; } + /** + * {@inheritDoc} + */ + function sql_conditional($condition, $action_true, $action_false = false) + { + $sql_condition = 'CASE WHEN ' . $condition; + $sql_condition .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; + $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; + $sql_condition .= ' END'; + return $sql_condition; + } + /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index db1bb86620..c6c34c8612 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -257,6 +257,18 @@ class dbal_mssqlnative extends dbal return ($this->sql_server_version) ? 'MSSQL
' . $this->sql_server_version : 'MSSQL'; } + /** + * {@inheritDoc} + */ + function sql_conditional($condition, $action_true, $action_false = false) + { + $sql_condition = 'CASE WHEN ' . $condition; + $sql_condition .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; + $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; + $sql_condition .= ' END'; + return $sql_condition; + } + /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php index 2e801532f0..f09e702951 100644 --- a/phpBB/includes/db/oracle.php +++ b/phpBB/includes/db/oracle.php @@ -89,6 +89,18 @@ class dbal_oracle extends dbal return $this->sql_server_version; } + /** + * {@inheritDoc} + */ + function sql_conditional($condition, $action_true, $action_false = false) + { + $sql_condition = 'CASE WHEN ' . $condition; + $sql_condition .= ' THEN CAST(' . $action_true . ' AS clob)'; + $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS clob)' : ''; + $sql_condition .= ' END'; + return $sql_condition; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index dddf615f9c..a27fbb9d9f 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -154,6 +154,18 @@ class dbal_postgres extends dbal return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version; } + /** + * {@inheritDoc} + */ + function sql_conditional($condition, $action_true, $action_false = false) + { + $sql_condition = 'CASE WHEN ' . $condition; + $sql_condition .= ' THEN CAST(' . $action_true . ' AS TEXT)'; + $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS TEXT)' : ''; + $sql_condition .= ' END'; + return $sql_condition; + } + /** * {@inheritDoc} */ From 79dfdf94063cf7cc64197e50807977acaf9a2e66 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 12:50:12 +0200 Subject: [PATCH 07/15] [ticket/10942] Change term string to expression to avoid confusion PHPBB3-10942 --- phpBB/includes/db/dbal.php | 10 +++++----- phpBB/includes/db/firebird.php | 4 ++-- phpBB/includes/db/mssql.php | 4 ++-- phpBB/includes/db/mssql_odbc.php | 4 ++-- phpBB/includes/db/mssqlnative.php | 4 ++-- phpBB/includes/db/postgres.php | 4 ++-- phpBB/includes/db/sqlite.php | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index c040fa8e2d..e85de28a4c 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -301,15 +301,15 @@ class dbal } /** - * Build a concatenated string + * Build a concatenated expression * - * @param string $string1 Base SQL statement where we append the second one - * @param string $string2 SQL statement that is appended on the first statement + * @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 */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return 'CONCAT(' . $string1 . ', ' . $string2 . ')'; + return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')'; } /** diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index 884206d298..d6b16d0342 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -113,9 +113,9 @@ class dbal_firebird extends dbal /** * {@inheritDoc} */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return $string1 . ' || ' . $string2; + return $expr1 . ' || ' . $expr2; } /** diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index ca8846b60d..c64b289724 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -106,9 +106,9 @@ class dbal_mssql extends dbal /** * {@inheritDoc} */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return $string1 . ' + ' . $string2; + return $expr1 . ' + ' . $expr2; } /** diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index 814dd76373..d026e2d754 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -124,9 +124,9 @@ class dbal_mssql_odbc extends dbal /** * {@inheritDoc} */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return $string1 . ' + ' . $string2; + return $expr1 . ' + ' . $expr2; } /** diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index c6c34c8612..36c52278d6 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -272,9 +272,9 @@ class dbal_mssqlnative extends dbal /** * {@inheritDoc} */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return $string1 . ' + ' . $string2; + return $expr1 . ' + ' . $expr2; } /** diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index a27fbb9d9f..9da2fd0b40 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -169,9 +169,9 @@ class dbal_postgres extends dbal /** * {@inheritDoc} */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return $string1 . ' || ' . $string2; + return $expr1 . ' || ' . $expr2; } /** diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php index 38aba0624e..6eb77e5a70 100644 --- a/phpBB/includes/db/sqlite.php +++ b/phpBB/includes/db/sqlite.php @@ -75,9 +75,9 @@ class dbal_sqlite extends dbal /** * {@inheritDoc} */ - function sql_concatenate($string1, $string2) + function sql_concatenate($expr1, $expr2) { - return $string1 . ' || ' . $string2; + return $expr1 . ' || ' . $expr2; } /** From 089e5f5c79965058a5288590a3a34d94f3bcb833 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 12:57:08 +0200 Subject: [PATCH 08/15] [ticket/10942] Rename method sql_conditional() to sql_case() PHPBB3-10942 --- phpBB/includes/db/dbal.php | 18 +++++++++--------- phpBB/includes/db/mssql.php | 12 ++++++------ phpBB/includes/db/mssql_odbc.php | 12 ++++++------ phpBB/includes/db/mssqlnative.php | 12 ++++++------ phpBB/includes/db/oracle.php | 12 ++++++------ phpBB/includes/db/postgres.php | 12 ++++++------ .../{conditional_test.php => case_test.php} | 10 +++++----- 7 files changed, 44 insertions(+), 44 deletions(-) rename tests/dbal/{conditional_test.php => case_test.php} (69%) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index e85de28a4c..9eb74ab617 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -284,20 +284,20 @@ class dbal } /** - * Build a conditional SQL query + * Build a case expression * * @param string $condition The condition which must be true, to use action_true rather then action_else - * @param string $action_true SQL statement that is used, if the condition is true - * @param string $action_else SQL statement that is used, if the condition is false, optional + * @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 */ - function sql_conditional($condition, $action_true, $action_false = false) + function sql_case($condition, $action_true, $action_false = false) { - $sql_condition = 'CASE WHEN ' . $condition; - $sql_condition .= ' THEN ' . $action_true; - $sql_condition .= ($action_false !== false) ? ' ELSE ' . $action_false : ''; - $sql_condition .= ' END'; - return $sql_condition; + $sql_case = 'CASE WHEN ' . $condition; + $sql_case .= ' THEN ' . $action_true; + $sql_case .= ($action_false !== false) ? ' ELSE ' . $action_false : ''; + $sql_case .= ' END'; + return $sql_case; } /** diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index c64b289724..0345e4f8f8 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -94,13 +94,13 @@ class dbal_mssql extends dbal /** * {@inheritDoc} */ - function sql_conditional($condition, $action_true, $action_false = false) + function sql_case($condition, $action_true, $action_false = false) { - $sql_condition = 'CASE WHEN ' . $condition; - $sql_condition .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; - $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; - $sql_condition .= ' END'; - return $sql_condition; + $sql_case = 'CASE WHEN ' . $condition; + $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; + $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; + $sql_case .= ' END'; + return $sql_case; } /** diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index d026e2d754..07da14bd53 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -112,13 +112,13 @@ class dbal_mssql_odbc extends dbal /** * {@inheritDoc} */ - function sql_conditional($condition, $action_true, $action_false = false) + function sql_case($condition, $action_true, $action_false = false) { - $sql_condition = 'CASE WHEN ' . $condition; - $sql_condition .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; - $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; - $sql_condition .= ' END'; - return $sql_condition; + $sql_case = 'CASE WHEN ' . $condition; + $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; + $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; + $sql_case .= ' END'; + return $sql_case; } /** diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index 36c52278d6..835b86f8c3 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -260,13 +260,13 @@ class dbal_mssqlnative extends dbal /** * {@inheritDoc} */ - function sql_conditional($condition, $action_true, $action_false = false) + function sql_case($condition, $action_true, $action_false = false) { - $sql_condition = 'CASE WHEN ' . $condition; - $sql_condition .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; - $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; - $sql_condition .= ' END'; - return $sql_condition; + $sql_case = 'CASE WHEN ' . $condition; + $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; + $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; + $sql_case .= ' END'; + return $sql_case; } /** diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php index f09e702951..9e08979bc1 100644 --- a/phpBB/includes/db/oracle.php +++ b/phpBB/includes/db/oracle.php @@ -92,13 +92,13 @@ class dbal_oracle extends dbal /** * {@inheritDoc} */ - function sql_conditional($condition, $action_true, $action_false = false) + function sql_case($condition, $action_true, $action_false = false) { - $sql_condition = 'CASE WHEN ' . $condition; - $sql_condition .= ' THEN CAST(' . $action_true . ' AS clob)'; - $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS clob)' : ''; - $sql_condition .= ' END'; - return $sql_condition; + $sql_case = 'CASE WHEN ' . $condition; + $sql_case .= ' THEN CAST(' . $action_true . ' AS clob)'; + $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS clob)' : ''; + $sql_case .= ' END'; + return $sql_case; } /** diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 9da2fd0b40..1ceb0ed0a2 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -157,13 +157,13 @@ class dbal_postgres extends dbal /** * {@inheritDoc} */ - function sql_conditional($condition, $action_true, $action_false = false) + function sql_case($condition, $action_true, $action_false = false) { - $sql_condition = 'CASE WHEN ' . $condition; - $sql_condition .= ' THEN CAST(' . $action_true . ' AS TEXT)'; - $sql_condition .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS TEXT)' : ''; - $sql_condition .= ' END'; - return $sql_condition; + $sql_case = 'CASE WHEN ' . $condition; + $sql_case .= ' THEN CAST(' . $action_true . ' AS TEXT)'; + $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS TEXT)' : ''; + $sql_case .= ' END'; + return $sql_case; } /** diff --git a/tests/dbal/conditional_test.php b/tests/dbal/case_test.php similarity index 69% rename from tests/dbal/conditional_test.php rename to tests/dbal/case_test.php index 3e01de05f6..7e966c0ec1 100644 --- a/tests/dbal/conditional_test.php +++ b/tests/dbal/case_test.php @@ -7,18 +7,18 @@ * */ -class phpbb_dbal_conditional_test extends phpbb_database_test_case +class phpbb_dbal_case_test extends phpbb_database_test_case { public function getDataSet() { return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); } - public function test_conditional_string() + public function test_case_string() { $db = $this->new_dbal(); - $sql = 'SELECT config_name, ' . $db->sql_conditional('is_dynamic = 1', "'" . $db->sql_escape('true') . "'", "'" . $db->sql_escape('false') . "'") . ' AS string + $sql = 'SELECT config_name, ' . $db->sql_case('is_dynamic = 1', "'" . $db->sql_escape('true') . "'", "'" . $db->sql_escape('false') . "'") . ' AS string FROM phpbb_config'; $result = $db->sql_query($sql); @@ -38,11 +38,11 @@ class phpbb_dbal_conditional_test extends phpbb_database_test_case ); } - public function test_conditional_statement() + public function test_case_statement() { $db = $this->new_dbal(); - $sql = 'SELECT config_name, ' . $db->sql_conditional('is_dynamic = 1', 'is_dynamic', 'config_value') . ' AS string + $sql = 'SELECT config_name, ' . $db->sql_case('is_dynamic = 1', 'is_dynamic', 'config_value') . ' AS string FROM phpbb_config'; $result = $db->sql_query($sql); From c71f604327653bde3eea1b5d7150f5cc9c58a712 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 23:30:31 +0200 Subject: [PATCH 09/15] [ticket/10942] Add a comment why we cast to sql_case() PHPBB3-10942 --- phpBB/includes/db/mssql.php | 1 + phpBB/includes/db/mssql_odbc.php | 1 + phpBB/includes/db/mssqlnative.php | 1 + phpBB/includes/db/oracle.php | 1 + phpBB/includes/db/postgres.php | 1 + 5 files changed, 5 insertions(+) diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index 0345e4f8f8..8888bb3939 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -96,6 +96,7 @@ class dbal_mssql extends dbal */ function sql_case($condition, $action_true, $action_false = false) { + // To ensure, that both expressions have the same type, we cast them to varchar manually $sql_case = 'CASE WHEN ' . $condition; $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index 07da14bd53..a14d6adb14 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -114,6 +114,7 @@ class dbal_mssql_odbc extends dbal */ function sql_case($condition, $action_true, $action_false = false) { + // To ensure, that both expressions have the same type, we cast them to varchar manually $sql_case = 'CASE WHEN ' . $condition; $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index 835b86f8c3..0001bc4ba7 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -262,6 +262,7 @@ class dbal_mssqlnative extends dbal */ function sql_case($condition, $action_true, $action_false = false) { + // To ensure, that both expressions have the same type, we cast them to varchar manually $sql_case = 'CASE WHEN ' . $condition; $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php index 9e08979bc1..dc405ab08a 100644 --- a/phpBB/includes/db/oracle.php +++ b/phpBB/includes/db/oracle.php @@ -94,6 +94,7 @@ class dbal_oracle extends dbal */ function sql_case($condition, $action_true, $action_false = false) { + // To ensure, that both expressions have the same type, we cast them to clob manually $sql_case = 'CASE WHEN ' . $condition; $sql_case .= ' THEN CAST(' . $action_true . ' AS clob)'; $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS clob)' : ''; diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 1ceb0ed0a2..c3116601ae 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -159,6 +159,7 @@ class dbal_postgres extends dbal */ function sql_case($condition, $action_true, $action_false = false) { + // To ensure, that both expressions have the same type, we cast them to text manually $sql_case = 'CASE WHEN ' . $condition; $sql_case .= ' THEN CAST(' . $action_true . ' AS TEXT)'; $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS TEXT)' : ''; From 0fd02035d8dd12db4a793af5be564911fca4f900 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 30 Jun 2012 13:00:41 +0200 Subject: [PATCH 10/15] [ticket/10942] Make unit tests for sql_case simpler PHPBB3-10942 --- tests/dbal/case_test.php | 50 +++++++++++++++------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/tests/dbal/case_test.php b/tests/dbal/case_test.php index 7e966c0ec1..684f2c26a9 100644 --- a/tests/dbal/case_test.php +++ b/tests/dbal/case_test.php @@ -18,47 +18,35 @@ class phpbb_dbal_case_test extends phpbb_database_test_case { $db = $this->new_dbal(); - $sql = 'SELECT config_name, ' . $db->sql_case('is_dynamic = 1', "'" . $db->sql_escape('true') . "'", "'" . $db->sql_escape('false') . "'") . ' AS string + $sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '0') . ' AS bool FROM phpbb_config'; - $result = $db->sql_query($sql); + $result = $db->sql_query_limit($sql, 1); - $db->sql_return_on_error(false); + $this->assertEquals(true, (bool) $db->sql_fetchfield('bool')); - $this->assertEquals(array( - array( - 'config_name' => 'config1', - 'string' => 'false', - ), - array( - 'config_name' => 'config2', - 'string' => 'true', - ), - ), - $db->sql_fetchrowset($result) - ); + $sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '0') . ' AS bool + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals(false, (bool) $db->sql_fetchfield('bool')); } public function test_case_statement() { $db = $this->new_dbal(); - $sql = 'SELECT config_name, ' . $db->sql_case('is_dynamic = 1', 'is_dynamic', 'config_value') . ' AS string - FROM phpbb_config'; - $result = $db->sql_query($sql); + $sql = 'SELECT ' . $db->sql_case('is_dynamic = 1', 'is_dynamic', '0') . " AS bool + FROM phpbb_config + WHERE is_dynamic = 1"; + $result = $db->sql_query_limit($sql, 1); - $db->sql_return_on_error(false); + $this->assertEquals(true, (bool) $db->sql_fetchfield('bool')); - $this->assertEquals(array( - array( - 'config_name' => 'config1', - 'string' => 'foo', - ), - array( - 'config_name' => 'config2', - 'string' => '1', - ), - ), - $db->sql_fetchrowset($result) - ); + $sql = 'SELECT ' . $db->sql_case('is_dynamic = 1', '1', 'is_dynamic') . " AS bool + FROM phpbb_config + WHERE is_dynamic = 0"; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals(false, (bool) $db->sql_fetchfield('bool')); } } From a8cf926566c32097c61c14210990f2a7229ddfc7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 2 Jul 2012 11:10:02 +0200 Subject: [PATCH 11/15] [ticket/10942] Require same data type and do not cast expressions automatically PHPBB3-10942 --- phpBB/includes/db/dbal.php | 2 ++ phpBB/includes/db/mssql.php | 13 ------------- phpBB/includes/db/mssql_odbc.php | 13 ------------- phpBB/includes/db/mssqlnative.php | 13 ------------- phpBB/includes/db/oracle.php | 13 ------------- phpBB/includes/db/postgres.php | 13 ------------- 6 files changed, 2 insertions(+), 65 deletions(-) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index 9eb74ab617..8dbcd73b58 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -286,6 +286,8 @@ class dbal /** * 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 diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index 8888bb3939..d24cbf329f 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -91,19 +91,6 @@ class dbal_mssql extends dbal return ($this->sql_server_version) ? 'MSSQL
' . $this->sql_server_version : 'MSSQL'; } - /** - * {@inheritDoc} - */ - function sql_case($condition, $action_true, $action_false = false) - { - // To ensure, that both expressions have the same type, we cast them to varchar manually - $sql_case = 'CASE WHEN ' . $condition; - $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; - $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; - $sql_case .= ' END'; - return $sql_case; - } - /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index a14d6adb14..d64264470e 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -109,19 +109,6 @@ class dbal_mssql_odbc extends dbal return ($this->sql_server_version) ? 'MSSQL (ODBC)
' . $this->sql_server_version : 'MSSQL (ODBC)'; } - /** - * {@inheritDoc} - */ - function sql_case($condition, $action_true, $action_false = false) - { - // To ensure, that both expressions have the same type, we cast them to varchar manually - $sql_case = 'CASE WHEN ' . $condition; - $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; - $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; - $sql_case .= ' END'; - return $sql_case; - } - /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index 0001bc4ba7..31c3a3721a 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -257,19 +257,6 @@ class dbal_mssqlnative extends dbal return ($this->sql_server_version) ? 'MSSQL
' . $this->sql_server_version : 'MSSQL'; } - /** - * {@inheritDoc} - */ - function sql_case($condition, $action_true, $action_false = false) - { - // To ensure, that both expressions have the same type, we cast them to varchar manually - $sql_case = 'CASE WHEN ' . $condition; - $sql_case .= ' THEN CAST(' . $action_true . ' AS VARCHAR)'; - $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS VARCHAR)' : ''; - $sql_case .= ' END'; - return $sql_case; - } - /** * {@inheritDoc} */ diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php index dc405ab08a..2e801532f0 100644 --- a/phpBB/includes/db/oracle.php +++ b/phpBB/includes/db/oracle.php @@ -89,19 +89,6 @@ class dbal_oracle extends dbal return $this->sql_server_version; } - /** - * {@inheritDoc} - */ - function sql_case($condition, $action_true, $action_false = false) - { - // To ensure, that both expressions have the same type, we cast them to clob manually - $sql_case = 'CASE WHEN ' . $condition; - $sql_case .= ' THEN CAST(' . $action_true . ' AS clob)'; - $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS clob)' : ''; - $sql_case .= ' END'; - return $sql_case; - } - /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index c3116601ae..a31181b4d4 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -154,19 +154,6 @@ class dbal_postgres extends dbal return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version; } - /** - * {@inheritDoc} - */ - function sql_case($condition, $action_true, $action_false = false) - { - // To ensure, that both expressions have the same type, we cast them to text manually - $sql_case = 'CASE WHEN ' . $condition; - $sql_case .= ' THEN CAST(' . $action_true . ' AS TEXT)'; - $sql_case .= ($action_false !== false) ? ' ELSE CAST(' . $action_false . ' AS TEXT)' : ''; - $sql_case .= ' END'; - return $sql_case; - } - /** * {@inheritDoc} */ From ad9d6506594f90ac7c6cdf20a5718d28042059c9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 2 Jul 2012 11:10:58 +0200 Subject: [PATCH 12/15] [ticket/10942] Fix up unit tests for sql_case() PHPBB3-10942 --- tests/dbal/case_test.php | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/tests/dbal/case_test.php b/tests/dbal/case_test.php index 684f2c26a9..5219defa06 100644 --- a/tests/dbal/case_test.php +++ b/tests/dbal/case_test.php @@ -14,39 +14,56 @@ class phpbb_dbal_case_test extends phpbb_database_test_case 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 num + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals(1, (int) $db->sql_fetchfield('num')); + + $sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '2') . ' AS num + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals(2, (int) $db->sql_fetchfield('num')); + } + public function test_case_string() { $db = $this->new_dbal(); - $sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '0') . ' AS bool + $sql = 'SELECT ' . $db->sql_case('1 = 1', "'foo'", "'bar'") . ' AS string FROM phpbb_config'; $result = $db->sql_query_limit($sql, 1); - $this->assertEquals(true, (bool) $db->sql_fetchfield('bool')); + $this->assertEquals('foo', $db->sql_fetchfield('string')); - $sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '0') . ' AS bool + $sql = 'SELECT ' . $db->sql_case('1 = 0', "'foo'", "'bar'") . ' AS string FROM phpbb_config'; $result = $db->sql_query_limit($sql, 1); - $this->assertEquals(false, (bool) $db->sql_fetchfield('bool')); + $this->assertEquals('bar', $db->sql_fetchfield('string')); } - public function test_case_statement() + public function test_case_column() { $db = $this->new_dbal(); - $sql = 'SELECT ' . $db->sql_case('is_dynamic = 1', 'is_dynamic', '0') . " AS bool + $sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS string FROM phpbb_config - WHERE is_dynamic = 1"; + WHERE config_name = 'config1'"; $result = $db->sql_query_limit($sql, 1); - $this->assertEquals(true, (bool) $db->sql_fetchfield('bool')); + $this->assertEquals('config1', $db->sql_fetchfield('string')); - $sql = 'SELECT ' . $db->sql_case('is_dynamic = 1', '1', 'is_dynamic') . " AS bool + $sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS string FROM phpbb_config - WHERE is_dynamic = 0"; + WHERE config_value = 'bar'"; $result = $db->sql_query_limit($sql, 1); - $this->assertEquals(false, (bool) $db->sql_fetchfield('bool')); + $this->assertEquals('bar', $db->sql_fetchfield('string')); } } From fe1f21d1a55ac15b278d99c3a73a8e3424a38fd5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 2 Jul 2012 23:11:21 +0200 Subject: [PATCH 13/15] [ticket/10942] Use ANSI SQL standard || in dbal.php PHPBB3-10942 --- phpBB/includes/db/dbal.php | 2 +- phpBB/includes/db/firebird.php | 8 -------- phpBB/includes/db/mysql.php | 8 ++++++++ phpBB/includes/db/mysqli.php | 8 ++++++++ phpBB/includes/db/postgres.php | 8 -------- phpBB/includes/db/sqlite.php | 8 -------- 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index 8dbcd73b58..fded27a001 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -311,7 +311,7 @@ class dbal */ function sql_concatenate($expr1, $expr2) { - return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')'; + return $expr1 . ' || ' . $expr2; } /** diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index d6b16d0342..7709e8fdf5 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -110,14 +110,6 @@ class dbal_firebird extends dbal return ($raw) ? '2.1' : 'Firebird/Interbase'; } - /** - * {@inheritDoc} - */ - function sql_concatenate($expr1, $expr2) - { - return $expr1 . ' || ' . $expr2; - } - /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index eb38e3e913..3cda5cc1e0 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -119,6 +119,14 @@ class dbal_mysql extends dbal return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version; } + /** + * {@inheritDoc} + */ + function sql_concatenate($expr1, $expr2) + { + return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')'; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index 4210a58002..e3828d9599 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -122,6 +122,14 @@ class dbal_mysqli extends dbal return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version; } + /** + * {@inheritDoc} + */ + function sql_concatenate($expr1, $expr2) + { + return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')'; + } + /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index a31181b4d4..bf22cffafa 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -154,14 +154,6 @@ class dbal_postgres extends dbal return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version; } - /** - * {@inheritDoc} - */ - function sql_concatenate($expr1, $expr2) - { - return $expr1 . ' || ' . $expr2; - } - /** * SQL Transaction * @access private diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php index 6eb77e5a70..86bfa75a13 100644 --- a/phpBB/includes/db/sqlite.php +++ b/phpBB/includes/db/sqlite.php @@ -72,14 +72,6 @@ class dbal_sqlite extends dbal return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version; } - /** - * {@inheritDoc} - */ - function sql_concatenate($expr1, $expr2) - { - return $expr1 . ' || ' . $expr2; - } - /** * SQL Transaction * @access private From 6776feb0de667340d640744462d00436f893c45f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 4 Jul 2012 14:05:03 +0200 Subject: [PATCH 14/15] [ticket/10942] Add access modifiers PHPBB3-10942 --- phpBB/includes/db/dbal.php | 4 ++-- phpBB/includes/db/mssql.php | 2 +- phpBB/includes/db/mssql_odbc.php | 2 +- phpBB/includes/db/mssqlnative.php | 2 +- phpBB/includes/db/mysql.php | 2 +- phpBB/includes/db/mysqli.php | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index fded27a001..159703d3be 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -293,7 +293,7 @@ class dbal * @param string $action_else SQL expression that is used, if the condition is false, optional * @return string CASE expression including the condition and statements */ - function sql_case($condition, $action_true, $action_false = false) + public function sql_case($condition, $action_true, $action_false = false) { $sql_case = 'CASE WHEN ' . $condition; $sql_case .= ' THEN ' . $action_true; @@ -309,7 +309,7 @@ class dbal * @param string $expr2 SQL expression that is appended to the first expression * @return string Concatenated string */ - function sql_concatenate($expr1, $expr2) + public function sql_concatenate($expr1, $expr2) { return $expr1 . ' || ' . $expr2; } diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index d24cbf329f..fb044b492f 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -94,7 +94,7 @@ class dbal_mssql extends dbal /** * {@inheritDoc} */ - function sql_concatenate($expr1, $expr2) + public function sql_concatenate($expr1, $expr2) { return $expr1 . ' + ' . $expr2; } diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index d64264470e..64fa9634d1 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -112,7 +112,7 @@ class dbal_mssql_odbc extends dbal /** * {@inheritDoc} */ - function sql_concatenate($expr1, $expr2) + public function sql_concatenate($expr1, $expr2) { return $expr1 . ' + ' . $expr2; } diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php index 31c3a3721a..7878615acc 100644 --- a/phpBB/includes/db/mssqlnative.php +++ b/phpBB/includes/db/mssqlnative.php @@ -260,7 +260,7 @@ class dbal_mssqlnative extends dbal /** * {@inheritDoc} */ - function sql_concatenate($expr1, $expr2) + public function sql_concatenate($expr1, $expr2) { return $expr1 . ' + ' . $expr2; } diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index 3cda5cc1e0..8d1f805870 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -122,7 +122,7 @@ class dbal_mysql extends dbal /** * {@inheritDoc} */ - function sql_concatenate($expr1, $expr2) + public function sql_concatenate($expr1, $expr2) { return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')'; } diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index e3828d9599..e07cd35e24 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -125,7 +125,7 @@ class dbal_mysqli extends dbal /** * {@inheritDoc} */ - function sql_concatenate($expr1, $expr2) + public function sql_concatenate($expr1, $expr2) { return 'CONCAT(' . $expr1 . ', ' . $expr2 . ')'; } From 1b826842aadc16ad35485479f4bb3bdef03b534c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 16 Jul 2012 16:59:40 +0200 Subject: [PATCH 15/15] [ticket/10942] Avoid possible conflicts with magic words in unit tests PHPBB3-10942 --- tests/dbal/case_test.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/dbal/case_test.php b/tests/dbal/case_test.php index 5219defa06..57a1729a39 100644 --- a/tests/dbal/case_test.php +++ b/tests/dbal/case_test.php @@ -18,52 +18,52 @@ class phpbb_dbal_case_test extends phpbb_database_test_case { $db = $this->new_dbal(); - $sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '2') . ' AS num + $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('num')); + $this->assertEquals(1, (int) $db->sql_fetchfield('test_num')); - $sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '2') . ' AS 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('num')); + $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 string + $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('string')); + $this->assertEquals('foo', $db->sql_fetchfield('test_string')); - $sql = 'SELECT ' . $db->sql_case('1 = 0', "'foo'", "'bar'") . ' AS 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('string')); + $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 string + $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('string')); + $this->assertEquals('config1', $db->sql_fetchfield('test_string')); - $sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS 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('string')); + $this->assertEquals('bar', $db->sql_fetchfield('test_string')); } }