From ce5258d801859fe38bdcd61531e3a4c14a7b3f03 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 23 May 2014 12:58:17 +0200 Subject: [PATCH 1/7] [ticket/12570] Add test for updating a config with the same value PHPBB3-12570 --- tests/config/db_text_test.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/config/db_text_test.php b/tests/config/db_text_test.php index 354c0efacf..ed5b6e7327 100644 --- a/tests/config/db_text_test.php +++ b/tests/config/db_text_test.php @@ -9,8 +9,8 @@ class phpbb_config_db_text_test extends phpbb_database_test_case { - private $db; - private $config_text; + /** @var \phpbb\config\db_text */ + protected $config_text; public function getDataSet() { @@ -48,6 +48,12 @@ class phpbb_config_db_text_test extends phpbb_database_test_case $this->assertSame('24', $this->config_text->get('foo')); } + public function test_set_same_value_get() + { + $this->config_text->set('foo', '23'); + $this->assertSame('23', $this->config_text->get('foo')); + } + public function test_set_get_long_string() { $expected = str_repeat('ABC', 10000); From 15516bd07853cbda585f1716ce1b614d43cf9d9a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 23 May 2014 16:10:07 +0200 Subject: [PATCH 2/7] [ticket/12570] Add a unit test to show broken sql_affectedrows() PHPBB3-12570 --- tests/dbal/sql_affected_rows_test.php | 73 +++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/dbal/sql_affected_rows_test.php diff --git a/tests/dbal/sql_affected_rows_test.php b/tests/dbal/sql_affected_rows_test.php new file mode 100644 index 0000000000..df008bab9a --- /dev/null +++ b/tests/dbal/sql_affected_rows_test.php @@ -0,0 +1,73 @@ +db = $this->new_dbal(); + } + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function test_select() + { + $sql = 'SELECT * + FROM ' . CONFIG_TABLE; + $this->db->sql_query($sql); + + $this->assertEquals(2, $this->db->sql_affectedrows()); + } + + public function test_update() + { + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = 'bertie'"; + $this->db->sql_query($sql); + + $this->assertEquals(2, $this->db->sql_affectedrows()); + } + + public function test_update_all_matched_unequal_updated() + { + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = 'foo'"; + $this->db->sql_query($sql); + + $this->assertEquals(2, $this->db->sql_affectedrows()); + } + + public function test_update_some_matched_unequal_updated() + { + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = 'foo' + WHERE config_value = 'foo'"; + $this->db->sql_query($sql); + + $this->assertEquals(1, $this->db->sql_affectedrows()); + } + + public function test_insert() + { + $sql = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $this->db->sql_build_array('INSERT', array( + 'config_name' => 'bertie', + 'config_value' => 'rules', + )); + $this->db->sql_query($sql); + + $this->assertEquals(1, $this->db->sql_affectedrows()); + } +} From b92eac71e49f184d1fe451a8b1b1d396768c6fb7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 23 May 2014 16:12:22 +0200 Subject: [PATCH 3/7] [ticket/12570] Fix MySQLi affectedrows by specifying MYSQLI_CLIENT_FOUND_ROWS PHPBB3-12570 --- phpBB/phpbb/db/driver/mysqli.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/driver/mysqli.php b/phpBB/phpbb/db/driver/mysqli.php index 6814599b24..a0df7599f8 100644 --- a/phpBB/phpbb/db/driver/mysqli.php +++ b/phpBB/phpbb/db/driver/mysqli.php @@ -57,7 +57,8 @@ class mysqli extends \phpbb\db\driver\mysql_base } } - $this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket); + $this->db_connect_id = @mysqli_init(); + $this->db_connect_id->real_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS); if ($this->db_connect_id && $this->dbname != '') { From d87dba3b0527ab1590be143c13c0c0d1010e3410 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 23 May 2014 16:13:19 +0200 Subject: [PATCH 4/7] [ticket/12570] Fix MySQL affectedrows We always want the number of matched rows instead of changed rows, when running an update. So when mysql_info() returns the number of matched rows we return that one instead of mysql_affected_rows() PHPBB3-12570 --- phpBB/phpbb/db/driver/mysql.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/driver/mysql.php b/phpBB/phpbb/db/driver/mysql.php index 1a4fd364df..de4d2de9c6 100644 --- a/phpBB/phpbb/db/driver/mysql.php +++ b/phpBB/phpbb/db/driver/mysql.php @@ -207,7 +207,26 @@ class mysql extends \phpbb\db\driver\mysql_base */ function sql_affectedrows() { - return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false; + if ($this->db_connect_id) + { + // We always want the number of matched rows + // instead of changed rows, when running an update. + // So when mysql_info() returns the number of matched rows + // we return that one instead of mysql_affected_rows() + $mysql_info = @mysql_info($this->db_connect_id); + if ($mysql_info !== false) + { + $match = array(); + preg_match('#^Rows matched: (\d)+ Changed: (\d)+ Warnings: (\d)+$#', $mysql_info, $match); + if (isset($match[1])) + { + return $match[1]; + } + } + + return @mysql_affected_rows($this->db_connect_id); + } + return false; } /** From adc62684f0e5e3de44e492d03a0c042741b40ce1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 23 May 2014 16:17:20 +0200 Subject: [PATCH 5/7] [ticket/12570] Add a test for set_array() and updating with the same value PHPBB3-12570 --- tests/config/db_text_test.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/config/db_text_test.php b/tests/config/db_text_test.php index ed5b6e7327..59730edf09 100644 --- a/tests/config/db_text_test.php +++ b/tests/config/db_text_test.php @@ -95,6 +95,8 @@ class phpbb_config_db_text_test extends phpbb_database_test_case 'baby' => 'phpBB', // Entry update 'bar' => '64', + // Entry update - same value + 'foo' => '23', ); $this->config_text->set_array($set_array_param); From 275e56ce70b194ed6712ba30707494607dbdc1cf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 23 May 2014 18:04:40 +0200 Subject: [PATCH 6/7] [ticket/12570] Remove test for affected rows after SELECT It's not supposed to work PHPBB3-12570 --- tests/dbal/sql_affected_rows_test.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/dbal/sql_affected_rows_test.php b/tests/dbal/sql_affected_rows_test.php index df008bab9a..860b8bf237 100644 --- a/tests/dbal/sql_affected_rows_test.php +++ b/tests/dbal/sql_affected_rows_test.php @@ -23,15 +23,6 @@ class phpbb_dbal_sql_affected_rows_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); } - public function test_select() - { - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $this->db->sql_query($sql); - - $this->assertEquals(2, $this->db->sql_affectedrows()); - } - public function test_update() { $sql = 'UPDATE ' . CONFIG_TABLE . " @@ -50,7 +41,7 @@ class phpbb_dbal_sql_affected_rows_test extends phpbb_database_test_case $this->assertEquals(2, $this->db->sql_affectedrows()); } - public function test_update_some_matched_unequal_updated() + public function test_update_same_value_matched_unequal_updated() { $sql = 'UPDATE ' . CONFIG_TABLE . " SET config_value = 'foo' From fcdf1101cba7999081f12bca2f5f96ef7769539c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 24 May 2014 13:06:25 +0200 Subject: [PATCH 7/7] [ticket/12570] Keep MySQLi procedural PHPBB3-12570 --- phpBB/phpbb/db/driver/mysqli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/driver/mysqli.php b/phpBB/phpbb/db/driver/mysqli.php index a0df7599f8..39df175860 100644 --- a/phpBB/phpbb/db/driver/mysqli.php +++ b/phpBB/phpbb/db/driver/mysqli.php @@ -57,8 +57,8 @@ class mysqli extends \phpbb\db\driver\mysql_base } } - $this->db_connect_id = @mysqli_init(); - $this->db_connect_id->real_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS); + $this->db_connect_id = mysqli_init(); + @mysqli_real_connect($this->db_connect_id, $this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS); if ($this->db_connect_id && $this->dbname != '') {