SET CHARACTER SET koi8r;

--disable_warnings
DROP TABLE IF EXISTS таблица, t1, t2;
--enable_warnings

SET CHARACTER SET koi8r;
CREATE TABLE t1 (a CHAR(10) CHARACTER SET cp1251) SELECT _koi8r'проба' AS a;
CREATE TABLE t2 (a CHAR(10) CHARACTER SET utf8);
SHOW CREATE TABLE t1;
SELECT a FROM t1;
SELECT HEX(a) FROM t1;
INSERT t2 SELECT * FROM t1;
SELECT HEX(a) FROM t2;
DROP TABLE t1, t2;


#
# Check that long strings conversion does not fail (bug#2218)
#
CREATE TABLE t1 (description text character set cp1250 NOT NULL);
INSERT INTO t1 (description) VALUES (_latin2'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasssssssssssaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddde');
SELECT description FROM t1;
DROP TABLE t1;

# same with TEXT
CREATE TABLE t1 (a TEXT CHARACTER SET cp1251) SELECT _koi8r'проба' AS a;
CREATE TABLE t2 (a TEXT CHARACTER SET utf8);
SHOW CREATE TABLE t1;
SELECT HEX(a) FROM t1;
INSERT t2 SELECT * FROM t1;
SELECT HEX(a) FROM t2;
DROP TABLE t1, t2;

CREATE TABLE `таблица`
(
  поле CHAR(32) CHARACTER SET koi8r NOT NULL COMMENT "комментарий поля"
) COMMENT "комментарий таблицы";

SHOW TABLES;
SHOW CREATE TABLE таблица;
SHOW FIELDS FROM таблица;

SET CHARACTER SET cp1251;
SHOW TABLES;
SHOW CREATE TABLE РЮАКХЖЮ;
SHOW FIELDS FROM РЮАКХЖЮ;


SET CHARACTER SET utf8;
SHOW TABLES;
SHOW CREATE TABLE я┌п╟п╠п╩п╦я├п╟;
SHOW FIELDS FROM я┌п╟п╠п╩п╦я├п╟;

SET CHARACTER SET koi8r;
DROP TABLE таблица;
SET CHARACTER SET default;

# Test for Item_func_conv_charset::fix_fields (bug #3704)
SET NAMES UTF8;
CREATE TABLE t1 (t text) DEFAULT CHARSET UTF8;
INSERT INTO t1 (t) VALUES ('x');
SELECT 1 FROM t1 WHERE CONCAT(_latin1'x') = t;
DROP TABLE t1;

SET CHARACTER SET koi8r;
CREATE DATABASE тест;
USE тест;
SHOW TABLES;
SHOW TABLES IN тест;
SET CHARACTER SET cp1251;
SHOW TABLES;
SHOW TABLES IN РЕЯР;
SET CHARACTER SET koi8r;
DROP DATABASE тест;

SET NAMES koi8r;
SELECT hex('тест');
SET character_set_connection=cp1251;
SELECT hex('тест');

USE test;

# Bug#4417
# Check that identifiers and strings are not converted 
# when the client character set is binary.

SET NAMES binary;
CREATE TABLE `я┌п╣я│я┌` (`я┌п╣я│я┌` int);
SHOW CREATE TABLE `я┌п╣я│я┌`;
SET NAMES utf8;
SHOW CREATE TABLE `я┌п╣я│я┌`;
DROP TABLE `я┌п╣я│я┌`;
SET NAMES binary;
SET character_set_connection=utf8;
SELECT 'я┌п╣я│я┌' as s;
SET NAMES utf8;
SET character_set_connection=binary;
SELECT 'я┌п╣я│я┌' as s;

# Bug#4417, another aspect:
# Check that both "SHOW CREATE TABLE" and "SHOW COLUMNS"
# return column names and default values in UTF8 after "SET NAMES BINARY"

SET NAMES latin1;
CREATE TABLE t1 (`Д` CHAR(128) DEFAULT 'Д', `Д1` ENUM('Д1','Д2') DEFAULT 'Д2');
SHOW CREATE TABLE t1;
SHOW COLUMNS FROM t1;
SET NAMES binary;
SHOW CREATE TABLE t1;
SHOW COLUMNS FROM t1;
DROP TABLE t1;


#
# Test that we allow only well-formed UTF8 identitiers
#
SET NAMES binary;
--error 1300
CREATE TABLE `goodплохо` (a int);
SET NAMES utf8;
--error 1300
CREATE TABLE `goodплохо` (a int);


#
# Test that we produce a warnign when conversion loses data.
#
set names latin1;
create table t1 (a char(10) character set koi8r, b text character set koi8r);
insert into t1 values ('test','test');
insert into t1 values ('йцук','йцук');
drop table t1;

#
# Try to  apply an automatic conversion in some cases:
# E.g. when mixing a column to a string, the string
# is converted into the column character set.
# If conversion loses data, then error. Otherwise,
# the string is replaced by its converted representation
#
set names koi8r;
create table t1 (a char(10) character set cp1251);
insert into t1 values (_koi8r'вася');
# this is possible:
select * from t1 where a=_koi8r'вася';
# this is possible, because we have a function with constant arguments:
select * from t1 where a=concat(_koi8r'вася');
# this is not posible, cannot convert _latin1'вася' into cp1251:
--error 1267
select * from t1 where a=_latin1'вася';
drop table t1;
set names latin1;

#
# Bug#10446 Illegal mix of collations
#
create table t1 (a char(10) character set utf8 collate utf8_bin);
insert into t1 values ('       xxx');
select * from t1 where a=lpad('xxx',10,' ');
drop table t1;

#
# Check more automatic conversion
#
set names koi8r;
create table t1 (c1 char(10) character set cp1251);
insert into t1 values ('ъ');
select c1 from t1 where c1 between 'ъ' and 'ъ';
select ifnull(c1,'ъ'), ifnull(null,c1) from t1;
select if(1,c1,'Ж'), if(0,c1,'Ж') from t1;
select coalesce('Ж',c1), coalesce(null,c1) from t1;
select least(c1,'Ж'), greatest(c1,'Ж') from t1;
select locate(c1,'ъ'), locate('ъ',c1) from t1;
select field(c1,'ъ'),field('ъ',c1) from t1;
select concat(c1,'Ж'), concat('Ж',c1) from t1;
select concat_ws(c1,'Ж','ъ'), concat_ws('Ж',c1,'ъ') from t1;
select replace(c1,'ъ','Ж'), replace('ъ',c1,'Ж') from t1;
select substring_index(c1,'ЖЖъъ',2) from t1;
select elt(1,c1,'Ж'),elt(1,'Ж',c1) from t1;
select make_set(3,c1,'Ж'), make_set(3,'Ж',c1) from t1;
select insert(c1,1,2,'Ж'),insert('Ж',1,2,c1) from t1;
select trim(c1 from 'ъ'),trim('ъ' from c1) from t1;
select lpad(c1,3,'Ж'), lpad('Ж',3,c1) from t1;
select rpad(c1,3,'Ж'), rpad('Ж',3,c1) from t1;
# TODO
#select case c1 when 'ъ' then 'ъ' when 'Ж' then 'Ж' else 'c' end from t1;
#select export_set(5,c1,'Ж'), export_set(5,'Ж',c1) from t1;

# End of 4.1 tests
