From b221e37b7d96b7d1ae861b2c213e88891856f035 Mon Sep 17 00:00:00 2001
From: "petr@mysql.com" <>
Date: Fri, 3 Feb 2006 13:05:14 +0300
Subject: [PATCH] Fix for Bug#16905 Log tables: unicode statements are logged
 incorrectly

---
 mysql-test/r/log_tables.result | 12 ++++++++++++
 mysql-test/t/log_tables.test   | 18 ++++++++++++++++++
 sql/log.cc                     | 23 +++++++++++++----------
 sql/log.h                      |  9 ++++++---
 4 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/mysql-test/r/log_tables.result b/mysql-test/r/log_tables.result
index caaf0367bb7..f75cf503938 100644
--- a/mysql-test/r/log_tables.result
+++ b/mysql-test/r/log_tables.result
@@ -52,3 +52,15 @@ select "Mark that we woke up from TRUNCATE in the test"
        as "test passed";
 test passed
 Mark that we woke up from TRUNCATE in the test
+use test;
+truncate table mysql.general_log;
+set names utf8;
+create table bug16905 (s char(15) character set utf8 default 'пусто');
+insert into bug16905 values ('новое');
+select * from mysql.general_log;
+event_time	user_host	thread_id	server_id	command_type	argument
+TIMESTAMP	root[root] @ localhost []	2	1	Query	set names utf8
+TIMESTAMP	root[root] @ localhost []	2	1	Query	create table bug16905 (s char(15) character set utf8 default 'пусто')
+TIMESTAMP	root[root] @ localhost []	2	1	Query	insert into bug16905 values ('новое')
+TIMESTAMP	root[root] @ localhost []	2	1	Query	select * from mysql.general_log
+drop table bug16905;
diff --git a/mysql-test/t/log_tables.test b/mysql-test/t/log_tables.test
index 794beaa2a4d..bc5f7f8891a 100644
--- a/mysql-test/t/log_tables.test
+++ b/mysql-test/t/log_tables.test
@@ -144,7 +144,25 @@ reap;
 select "Mark that we woke up from TRUNCATE in the test"
        as "test passed";
 
+connection con1;
+
 disconnect con2;
+
+use test;
+
+#
+# Bug #16905    Log tables: unicode statements are logged incorrectly
+#
+
+truncate table mysql.general_log;
+set names utf8;
+create table bug16905 (s char(15) character set utf8 default 'пусто');
+insert into bug16905 values ('новое');
+--replace_column 1 TIMESTAMP
+select * from mysql.general_log;
+drop table bug16905;
+
+
 disconnect con1;
 --enable_ps_protocol
 
diff --git a/sql/log.cc b/sql/log.cc
index f6fc6d5cd74..bdb924abe26 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -310,7 +310,8 @@ bool Log_to_csv_event_handler::
   log_general(time_t event_time, const char *user_host,
               uint user_host_len, int thread_id,
               const char *command_type, uint command_type_len,
-              const char *sql_text, uint sql_text_len)
+              const char *sql_text, uint sql_text_len,
+              CHARSET_INFO *client_cs)
 {
   TABLE *table= general_log.table;
 
@@ -325,11 +326,11 @@ bool Log_to_csv_event_handler::
   /* set default value (which is CURRENT_TIMESTAMP) */
   table->field[0]->set_null();
 
-  table->field[1]->store(user_host, user_host_len, &my_charset_latin1);
+  table->field[1]->store(user_host, user_host_len, client_cs);
   table->field[2]->store((longlong) thread_id);
   table->field[3]->store((longlong) server_id);
-  table->field[4]->store(command_type, command_type_len, &my_charset_latin1);
-  table->field[5]->store(sql_text, sql_text_len, &my_charset_latin1);
+  table->field[4]->store(command_type, command_type_len, client_cs);
+  table->field[5]->store(sql_text, sql_text_len, client_cs);
   table->file->ha_write_row(table->record[0]);
 
   reenable_binlog(current_thd);
@@ -375,6 +376,7 @@ bool Log_to_csv_event_handler::
 {
   /* table variables */
   TABLE *table= slow_log.table;
+  CHARSET_INFO *client_cs= thd->variables.character_set_client;
 
   DBUG_ENTER("log_slow_to_csv");
 
@@ -395,7 +397,7 @@ bool Log_to_csv_event_handler::
   table->field[0]->set_null();
 
   /* store the value */
-  table->field[1]->store(user_host, user_host_len, &my_charset_latin1);
+  table->field[1]->store(user_host, user_host_len, client_cs);
 
   if (query_start_arg)
   {
@@ -418,7 +420,7 @@ bool Log_to_csv_event_handler::
 
   if (thd->db)
     /* fill database field */
-    table->field[6]->store(thd->db, thd->db_length, &my_charset_latin1);
+    table->field[6]->store(thd->db, thd->db_length, client_cs);
   else
     table->field[6]->set_null();
 
@@ -436,8 +438,7 @@ bool Log_to_csv_event_handler::
   table->field[9]->store((longlong) server_id);
 
   /* sql_text */
-  table->field[10]->store(sql_text,sql_text_len,
-                          &my_charset_latin1);
+  table->field[10]->store(sql_text,sql_text_len, client_cs);
 
   /* write the row */
   table->file->ha_write_row(table->record[0]);
@@ -493,7 +494,8 @@ bool Log_to_file_event_handler::
   log_general(time_t event_time, const char *user_host,
               uint user_host_len, int thread_id,
               const char *command_type, uint command_type_len,
-              const char *sql_text, uint sql_text_len)
+              const char *sql_text, uint sql_text_len,
+              CHARSET_INFO *client_cs)
 {
   return mysql_log.write(event_time, user_host, user_host_len,
                          thread_id, command_type, command_type_len,
@@ -809,7 +811,8 @@ bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
                    user_host_len, id,
                    command_name[(uint) command].str,
                    command_name[(uint) command].length,
-                   message_buff, message_buff_len) || error;
+                   message_buff, message_buff_len,
+                   thd->variables.character_set_client) || error;
     unlock();
   }
   return error;
diff --git a/sql/log.h b/sql/log.h
index 9868fe23292..7f90737c096 100644
--- a/sql/log.h
+++ b/sql/log.h
@@ -368,7 +368,8 @@ public:
   virtual bool log_general(time_t event_time, const char *user_host,
                            uint user_host_len, int thread_id,
                            const char *command_type, uint command_type_len,
-                           const char *sql_text, uint sql_text_len)= 0;
+                           const char *sql_text, uint sql_text_len,
+                           CHARSET_INFO *client_cs)= 0;
   virtual ~Log_event_handler() {}
 };
 
@@ -403,7 +404,8 @@ public:
   virtual bool log_general(time_t event_time, const char *user_host,
                            uint user_host_len, int thread_id,
                            const char *command_type, uint command_type_len,
-                           const char *sql_text, uint sql_text_len);
+                           const char *sql_text, uint sql_text_len,
+                            CHARSET_INFO *client_cs);
   bool flush(THD *thd, TABLE_LIST *close_slow_Log,
              TABLE_LIST* close_general_log);
   void close_log_table(uint log_type, bool lock_in_use);
@@ -431,7 +433,8 @@ public:
   virtual bool log_general(time_t event_time, const char *user_host,
                            uint user_host_len, int thread_id,
                            const char *command_type, uint command_type_len,
-                           const char *sql_text, uint sql_text_len);
+                           const char *sql_text, uint sql_text_len,
+                           CHARSET_INFO *client_cs);
   void flush();
   void init_pthread_objects();
 };