# Replication of character sets.
# This test will fail if the server/client does not support enough charsets.

source include/master-slave.inc;
set timestamp=1000000000;

create database mysqltest2 character set latin2;
set @@character_set_server=latin5;      
create database mysqltest3;
--disable_query_log
select "--- --master--" as "";
--enable_query_log
show create database mysqltest2;
show create database mysqltest3;
--source include/sync_slave_sql_with_master.inc
--disable_query_log
select "--- --slave--" as "";
--enable_query_log
show create database mysqltest2;
show create database mysqltest3;

connection master;
set @@collation_server=armscii8_bin;
drop database mysqltest3;
create database mysqltest3;
--disable_query_log
select "--- --master--" as "";
--enable_query_log
show create database mysqltest3;
--source include/sync_slave_sql_with_master.inc
--disable_query_log
select "--- --slave--" as "";
--enable_query_log
show create database mysqltest3;

connection master;
use mysqltest2;
create table t1 (a int auto_increment primary key, b varchar(100));
set character_set_client=cp850, collation_connection=latin2_croatian_ci;
insert into t1 (b) values(@@character_set_server);
insert into t1 (b) values(@@collation_server);
# character_set_database and collation_database are not tested as they
# needn't be replicated (Bar said in Jan 2005).
insert into t1 (b) values(@@character_set_client);
# collation_client does not exist
insert into t1 (b) values(@@character_set_connection);
insert into t1 (b) values(@@collation_connection);
--echo --- --master--
select * from t1 order by a;
--source include/sync_slave_sql_with_master.inc
--echo --- --slave--
select * from mysqltest2.t1 order by a;

connection master;
set character_set_client=latin1, collation_connection=latin1_german1_ci;
truncate table t1;
insert into t1 (b) values(@@collation_connection);
insert into t1 (b) values(LEAST("Müller","Muffler"));
set collation_connection=latin1_german2_ci;
insert into t1 (b) values(@@collation_connection);
insert into t1 (b) values(LEAST("Müller","Muffler"));
--echo --- --master--
select * from t1 order by a;
--source include/sync_slave_sql_with_master.inc
--echo --- --slave--
select * from mysqltest2.t1 order by a;

# Presently charset info is not logged with LOAD DATA but it will
# change in Jan 2005 when Dmitri pushes his new LOAD DATA,
# before 5.0.3 goes out. When done, LOAD DATA INFILE should be tested
# here.

# See if user var is prefixed with collation in binlog and replicated well.
# Note: replication of user variables is broken as far as derivation is
# concerned. That's because when we store a user variable in the binlog,
# we lose its derivation. So later on the slave, it's impossible to
# know if the collation was explicit or not, so we use DERIVATION_NONE,
# which provokes error messages (like 'Illegal mix of collation') when
# we replay the master's INSERT/etc statements.
connection master;
set @a= _cp850 'Müller' collate cp850_general_ci;
truncate table t1;
insert into t1 (b) values(collation(@a));
--echo --- --master--
select * from t1 order by a;
--source include/sync_slave_sql_with_master.inc
--echo --- --slave--
select * from mysqltest2.t1 order by a;

connection master;
drop database mysqltest2;
drop database mysqltest3;
--source include/sync_slave_sql_with_master.inc

# Check that we can change global.collation_server (since 5.0.3)

set global character_set_server=latin2;
set global character_set_server=latin1; # back
connection master;
set global character_set_server=latin2;
set global character_set_server=latin1; # back

# Test of wrong character set numbers;
error 1115;
set character_set_client=9999999;
error 1273;
set collation_server=9999998;

# This one was contributed by Sergey Petrunia (BUG#3943)

use test;
CREATE TABLE t1 (c1 VARBINARY(255), c2 VARBINARY(255));
SET CHARACTER_SET_CLIENT=koi8r,
 CHARACTER_SET_CONNECTION=cp1251, 
 CHARACTER_SET_RESULTS=koi8r;
INSERT INTO t1 (c1, c2) VALUES ('îĠ, Ú