From 294e7c295fe55372fb93d4dbbf3ccb7ac785cb4a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 17 Aug 2012 15:17:29 +0200 Subject: [PATCH] Annother attempt at fixing the mysqli_fetch_field tests Instead of character set detection (which doesn't always work correctly) fetch the character set info using mysqli_get_charset(). To make sure that the returned info applies to all of client, connection and result explicitely set utf8 as charset using mysqli_set_charset() before. I'm not sure whether that last part is really necessary, but included it to be safe. --- ext/mysqli/tests/connect.inc | 93 ------------------- ext/mysqli/tests/mysqli_fetch_field.phpt | 22 +++-- ext/mysqli/tests/mysqli_fetch_field_oo.phpt | 21 +++-- ext/mysqli/tests/mysqli_fetch_fields.phpt | 20 ++-- ext/mysqli/tests/mysqli_field_seek.phpt | 22 +++-- ..._stmt_get_result_metadata_fetch_field.phpt | 21 +++-- 6 files changed, 64 insertions(+), 135 deletions(-) diff --git a/ext/mysqli/tests/connect.inc b/ext/mysqli/tests/connect.inc index 3a9d8ec258c..4acc20cb919 100644 --- a/ext/mysqli/tests/connect.inc +++ b/ext/mysqli/tests/connect.inc @@ -129,99 +129,6 @@ } } - function my_get_charsets($link) { - - /* Those tree are set by SET NAMES */ - $charsets = array( - 'client' => NULL, - 'results' => NULL, - 'connection' => NULL, - ); - - if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE '%character%'"))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - - $names = array(); - while ($row = mysqli_fetch_assoc($res)) { - $names[$row['Variable_name']] = $row['Value']; - } - mysqli_free_result($res); - - if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_client']))) || - !($details = mysqli_fetch_assoc($res))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - mysqli_free_result($res); - - $charsets['client'] = array( - 'charset' => $details['Charset'], - 'desc' => $details['Description'], - 'collation' => $details['Default collation'], - 'maxlen' => $details['Maxlen'], - 'nr' => NULL, - ); - - if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) || - !($collation = mysqli_fetch_assoc($res))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - mysqli_free_result($res); - $charsets['client']['nr'] = $collation['Id']; - - if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_results']))) || - !($details = mysqli_fetch_assoc($res))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - mysqli_free_result($res); - - $charsets['results'] = array( - 'charset' => $details['Charset'], - 'desc' => $details['Description'], - 'collation' => $details['Default collation'], - 'maxlen' => $details['Maxlen'], - 'nr' => NULL, - ); - - if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) || - !($collation = mysqli_fetch_assoc($res))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - mysqli_free_result($res); - $charsets['results']['nr'] = $collation['Id']; - - - if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_connection']))) || - !($details = mysqli_fetch_assoc($res))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - mysqli_free_result($res); - - $charsets['connection'] = array( - 'charset' => $details['Charset'], - 'desc' => $details['Description'], - 'collation' => $details['Default collation'], - 'maxlen' => $details['Maxlen'], - 'nr' => NULL, - ); - - if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) || - !($collation = mysqli_fetch_assoc($res))) { - printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link)); - return $charsets; - } - mysqli_free_result($res); - $charsets['connection']['nr'] = $collation['Id']; - - return $charsets; - } - function have_innodb($link) { if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) && ($row = $res->fetch_row()) && diff --git a/ext/mysqli/tests/mysqli_fetch_field.phpt b/ext/mysqli/tests/mysqli_fetch_field.phpt index d1d358b3421..2b9108072b4 100644 --- a/ext/mysqli/tests/mysqli_fetch_field.phpt +++ b/ext/mysqli/tests/mysqli_fetch_field.phpt @@ -22,7 +22,13 @@ require_once('skipifconnectfailure.inc'); require('table.inc'); - $charsets = my_get_charsets($link); + // Make sure that client, connection and result charsets are all the + // same. Not sure whether this is strictly necessary. + if (!mysqli_set_charset($link, 'utf8')) + printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link)); + + $charsetInfo = mysqli_get_charset($link); + if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) { printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } @@ -34,19 +40,17 @@ require_once('skipifconnectfailure.inc'); /* label column, result set charset */ $tmp = mysqli_fetch_field($res); var_dump($tmp); - if ($tmp->charsetnr != $charsets['results']['nr']) { + if ($tmp->charsetnr != $charsetInfo->number) { printf("[004] Expecting charset %s/%d got %d\n", - $charsets['results']['charset'], - $charsets['results']['nr'], $tmp->charsetnr); + $charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr); } - if ($tmp->length != (1 * $charsets['results']['maxlen'])) { + if ($tmp->length != $charsetInfo->max_length) { printf("[005] Expecting length %d got %d\n", - $charsets['results']['maxlen'], - $tmp->max_length); + $charsetInfo->max_length, $tmp->max_length); } if ($tmp->db != $db) { printf("011] Expecting database '%s' got '%s'\n", - $db, $tmp->db); + $db, $tmp->db); } var_dump(mysqli_fetch_field($res)); @@ -174,4 +178,4 @@ object(stdClass)#%d (13) { [%u|b%"decimals"]=> int(0) } -done! \ No newline at end of file +done! diff --git a/ext/mysqli/tests/mysqli_fetch_field_oo.phpt b/ext/mysqli/tests/mysqli_fetch_field_oo.phpt index 2d5ad261b13..8c5609b163d 100644 --- a/ext/mysqli/tests/mysqli_fetch_field_oo.phpt +++ b/ext/mysqli/tests/mysqli_fetch_field_oo.phpt @@ -27,7 +27,12 @@ require_once('skipifconnectfailure.inc'); if (!is_null($tmp = @$res->fetch_field($link))) printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); - $charsets = my_get_charsets($link); + // Make sure that client, connection and result charsets are all the + // same. Not sure whether this is strictly necessary. + if (!$mysqli->set_charset('utf8')) + printf("[%d] %s\n", $mysqli->errno, $mysqli->errno); + + $charsetInfo = $mysqli->get_charset(); if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) { printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error); @@ -37,18 +42,16 @@ require_once('skipifconnectfailure.inc'); $tmp = $res->fetch_field(); var_dump($tmp); - if ($tmp->charsetnr != $charsets['results']['nr']) { + if ($tmp->charsetnr != $charsetInfo->number) { printf("[005] Expecting charset %s/%d got %d\n", - $charsets['results']['charset'], - $charsets['results']['nr'], $tmp->charsetnr); + $charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr); } - if ($tmp->length != (1 * $charsets['results']['maxlen'])) { + if ($tmp->length != $charsetInfo->max_length) { printf("[006] Expecting length %d got %d\n", - $charsets['results']['maxlen'], - $tmp->max_length); + $charsetInfo->max_length, $tmp->max_length); } if ($tmp->db != $db) { - printf("008] Expecting database '%s' got '%s'\n", + printf("[007] Expecting database '%s' got '%s'\n", $db, $tmp->db); } @@ -126,4 +129,4 @@ object(stdClass)#%d (13) { bool(false) Warning: mysqli_result::fetch_field(): Couldn't fetch mysqli_result in %s on line %d -done! \ No newline at end of file +done! diff --git a/ext/mysqli/tests/mysqli_fetch_fields.phpt b/ext/mysqli/tests/mysqli_fetch_fields.phpt index 479c71cbbc5..6b66d6f231f 100644 --- a/ext/mysqli/tests/mysqli_fetch_fields.phpt +++ b/ext/mysqli/tests/mysqli_fetch_fields.phpt @@ -21,7 +21,13 @@ require_once('skipifconnectfailure.inc'); printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); require('table.inc'); - $charsets = my_get_charsets($link); + + // Make sure that client, connection and result charsets are all the + // same. Not sure whether this is strictly necessary. + if (!mysqli_set_charset($link, 'utf8')) + printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link)); + + $charsetInfo = mysqli_get_charset($link); if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) { printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); @@ -33,14 +39,14 @@ require_once('skipifconnectfailure.inc'); switch ($k) { case 1: /* label column, result set charset */ - if ($field->charsetnr != $charsets['results']['nr']) { + if ($field->charsetnr != $charsetInfo->number) { printf("[004] Expecting charset %s/%d got %d\n", - $charsets['results']['charset'], - $charsets['results']['nr'], $field->charsetnr); + $charsetInfo->charset, + $charsetInfo->number, $field->charsetnr); } - if ($field->length != (1 * $charsets['results']['maxlen'])) { + if ($field->length != $charsetInfo->max_length) { printf("[005] Expecting length %d got %d\n", - $charsets['results']['maxlen'], + $charsetInfo->max_length, $field->max_length); } break; @@ -118,4 +124,4 @@ object(stdClass)#%d (13) { } Warning: mysqli_fetch_fields(): Couldn't fetch mysqli_result in %s on line %d -done! \ No newline at end of file +done! diff --git a/ext/mysqli/tests/mysqli_field_seek.phpt b/ext/mysqli/tests/mysqli_field_seek.phpt index a747bdfa019..449d2f90d41 100644 --- a/ext/mysqli/tests/mysqli_field_seek.phpt +++ b/ext/mysqli/tests/mysqli_field_seek.phpt @@ -66,7 +66,13 @@ require_once('skipifconnectfailure.inc'); printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); require('table.inc'); - $charsets = my_get_charsets($link); + + // Make sure that client, connection and result charsets are all the + // same. Not sure whether this is strictly necessary. + if (!mysqli_set_charset($link, 'utf8')) + printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link)); + + $charsetInfo = mysqli_get_charset($link); if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) { printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); @@ -81,15 +87,13 @@ require_once('skipifconnectfailure.inc'); $field = mysqli_fetch_field($res); var_dump($field); /* label column, result set charset */ - if ($field->charsetnr != $charsets['results']['nr']) { + if ($field->charsetnr != $charsetInfo->number) { printf("[004] Expecting charset %s/%d got %d\n", - $charsets['results']['charset'], - $charsets['results']['nr'], $field->charsetnr); + $charsetInfo->charset, $charsetInfo->number, $field->charsetnr); } - if ($field->length != (1 * $charsets['results']['maxlen'])) { + if ($field->length != $charsetInfo->max_length) { printf("[005] Expecting length %d got %d\n", - $charsets['results']['maxlen'], - $field->max_length); + $charsetInfo->max_length, $field->max_length); } var_dump(mysqli_field_tell($res)); @@ -217,7 +221,7 @@ bool(false) Warning: mysqli_field_seek(): Invalid field offset in %s on line %d bool(false) bool(true) -object(stdClass)#3 (13) { +object(stdClass)#%d (13) { [%u|b%"name"]=> %unicode|string%(5) "_null" [%u|b%"orgname"]=> @@ -248,4 +252,4 @@ object(stdClass)#3 (13) { Warning: mysqli_field_seek(): Couldn't fetch mysqli_result in %s on line %d NULL -done! \ No newline at end of file +done! diff --git a/ext/mysqli/tests/mysqli_stmt_get_result_metadata_fetch_field.phpt b/ext/mysqli/tests/mysqli_stmt_get_result_metadata_fetch_field.phpt index afaccaf3c7a..739bf56ea15 100644 --- a/ext/mysqli/tests/mysqli_stmt_get_result_metadata_fetch_field.phpt +++ b/ext/mysqli/tests/mysqli_stmt_get_result_metadata_fetch_field.phpt @@ -12,7 +12,13 @@ if (!function_exists('mysqli_stmt_get_result')) --FILE-- charsetnr != $charsets['results']['nr']) { + if ($field->charsetnr != $charsetInfo->number) { printf("[004] Expecting charset %s/%d got %d\n", - $charsets['results']['charset'], - $charsets['results']['nr'], $field->charsetnr); + $charsetInfo->charset, + $charsetInfo->number, $field->charsetnr); } - if ($field->length != (1 * $charsets['results']['maxlen'])) { + if ($field->length != $charsetInfo->max_length) { printf("[005] Expecting length %d got %d\n", - $charsets['results']['maxlen'], - $field->max_length); + $charsetInfo->max_length, $field->max_length); } } } @@ -173,4 +178,4 @@ object(stdClass)#%d (13) { [%u|b%"decimals"]=> int(31) } -done! \ No newline at end of file +done!