--source include/have_utf8mb4.inc

--echo #
--echo # MDEV-27896 Wrong result upon COLLATE latin1_bin CHARACTER SET latin1 on the table or the database level
--echo #

CREATE DATABASE db1 COLLATE latin1_bin CHARACTER SET latin1;
SHOW CREATE DATABASE db1;
DROP DATABASE db1;


CREATE VIEW cscl AS
SELECT
  DEFAULT_CHARACTER_SET_NAME,
  DEFAULT_COLLATION_NAME
FROM
  INFORMATION_SCHEMA.SCHEMATA
WHERE
  SCHEMA_NAME='db1';

SET collation_server=utf8mb4_unicode_ci;
CREATE DATABASE db1 COMMENT 'test';
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 COLLATE DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 COLLATE latin1_bin;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET latin1;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET latin1 COLLATE DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET latin1 COLLATE latin1_bin; 
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 COLLATE DEFAULT CHARACTER SET latin1;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 COLLATE latin1_bin CHARACTER SET latin1;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET DEFAULT CHARACTER SET DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET DEFAULT COLLATE DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 COLLATE DEFAULT CHARACTER SET DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET DEFAULT CHARACTER SET utf8mb4;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET utf8mb4 CHARACTER SET DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

--error ER_CONFLICTING_DECLARATIONS
CREATE DATABASE db1 CHARACTER SET DEFAULT CHARACTER SET latin1;
--error ER_CONFLICTING_DECLARATIONS
CREATE DATABASE db1 CHARACTER SET latin1 CHARACTER SET DEFAULT;

--error ER_COLLATION_CHARSET_MISMATCH
CREATE DATABASE db1 CHARACTER SET DEFAULT COLLATE latin1_bin;
--error ER_COLLATION_CHARSET_MISMATCH
CREATE DATABASE db1 COLLATE latin1_bin CHARACTER SET DEFAULT;

CREATE DATABASE db1 CHARACTER SET DEFAULT COLLATE utf8mb4_bin;
SELECT * FROM cscl;
DROP DATABASE db1;

CREATE DATABASE db1 COLLATE utf8mb4_bin CHARACTER SET DEFAULT;
SELECT * FROM cscl;
DROP DATABASE db1;

DROP VIEW cscl;


--echo #
--echo # MDEV-28117 Multiple conflicting table COLLATE clauses are not rejected
--echo #

--error ER_CONFLICTING_DECLARATIONS
CREATE DATABASE db1 COLLATE latin1_swedish_ci COLLATE latin1_bin;


--echo #
--echo # MDEV-27906 CREATE TABLE/DATABASE .. CHARSET .. COLLATE is not consistent on errors
--echo #

SET collation_server=utf8mb4_unicode_ci;
--error ER_COLLATION_CHARSET_MISMATCH
CREATE DATABASE db1 COLLATE latin1_bin CHARACTER SET DEFAULT;
--error ER_COLLATION_CHARSET_MISMATCH
CREATE DATABASE db1 CHARACTER SET DEFAULT COLLATE latin1_bin;
SET collation_server=DEFAULT;

SET collation_server=latin1_swedish_ci;
CREATE DATABASE db1 COLLATE latin1_bin CHARACTER SET DEFAULT;
SHOW CREATE DATABASE db1;
DROP DATABASE db1;

CREATE DATABASE db1 CHARACTER SET DEFAULT COLLATE latin1_bin;
SHOW CREATE DATABASE db1;
DROP DATABASE db1;
SET collation_server=DEFAULT;
