--source include/have_innodb.inc
--source include/not_embedded.inc
--echo #
--echo # MDEV-14474: Create INFORMATION_SCHEMA.CHECK_CONSTRAINTS
--echo #

CREATE user boo1;
GRANT select,create,alter,drop on foo.* to boo1;
SHOW GRANTS for boo1;
CREATE user boo2;
create database foo;
# Connect with user boo1
CONNECT(con1,localhost, boo1,, foo);

SET check_constraint_checks=1;
CREATE TABLE t0
(
 t int, check (t>32) # table constraint
) ENGINE=myisam;
--sorted_result
SELECT * from information_schema.check_constraints;

ALTER TABLE t0
ADD CONSTRAINT CHK_t0_t CHECK(t<100);
--sorted_result
SELECT * from information_schema.check_constraints;

ALTER TABLE t0
DROP CONSTRAINT CHK_t0_t;
--sorted_result
SELECT * from information_schema.check_constraints;

ALTER TABLE t0
ADD CONSTRAINT CHECK(t<50);
--sorted_result
SELECT * from information_schema.check_constraints;

CREATE TABLE t1
( t int CHECK(t>2), # field constraint
 tt int,
 CONSTRAINT CHECK (tt > 32), CONSTRAINT CHECK (tt <50),# autogenerated names table constraints
 CONSTRAINT CHK_tt CHECK(tt<100) # named table constraint
) ENGINE=InnoDB;
--sorted_result
SELECT * from information_schema.check_constraints;

ALTER TABLE t1
DROP CONSTRAINT CHK_tt;
--sorted_result
SELECT * from information_schema.check_constraints;

CREATE TABLE t2
(
name VARCHAR(30) CHECK(CHAR_LENGTH(name)>2), #field constraint
start_date DATE,
end_date DATE,
CONSTRAINT CHK_dates CHECK(start_date IS NULL) #table constraint
)ENGINE=Innodb;
--sorted_result
SELECT * from information_schema.check_constraints;

ALTER TABLE t1
ADD CONSTRAINT CHK_new_ CHECK(t>tt);
--sorted_result
SELECT * from information_schema.check_constraints;

# Create table with same field and table check constraint name
CREATE TABLE t3
(
a int,
b int check (b>0), # field constraint named 'b'
CONSTRAINT b check (b>10), # table constraint
# `CHECK_CLAUSE` should allow more then `var(64)` constraints
CONSTRAINT b1 check (b<123456789012345678901234567890123456789012345678901234567890123456789)
) ENGINE=InnoDB;
--sorted_result
SELECT * from information_schema.check_constraints;

DISCONNECT con1;
CONNECT(con2, localhost, boo2,,"*NO-ONE*");
--sorted_result
SELECT * from information_schema.check_constraints;

DISCONNECT con2;
CONNECT(con1, localhost, boo1,,foo);
DROP TABLE t0;
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
DROP DATABASE foo;

DISCONNECT con1;
--CONNECTION default
DROP USER boo1;
DROP USER boo2;

--echo #
--echo # MDEV-18440: Information_schema.check_constraints possible data leak
--echo #

CREATE USER foo;
CREATE DATABASE db;
USE db;
CREATE TABLE t1 (a int, b int, CONSTRAINT CHECK (b > 0));
INSERT INTO t1 VALUES (1, 2), (2, 3);
GRANT SELECT (a) ON t1 TO foo;

SHOW GRANTS FOR foo;
--sorted_result
SELECT * FROM information_schema.check_constraints;

CONNECT(con1,localhost, foo,, db);
SELECT a FROM t1;
--sorted_result
SELECT * FROM information_schema.check_constraints;

--CONNECTION default

DROP USER foo;
DROP DATABASE db;

--echo #
--echo # MDEV-24601: INFORMATION_SCHEMA doesn't differentiate between
--echo #             column and table-level CHECK constraints
--echo #

# Mix of table (>0) and field (<0) constraints
# Note that there are 2 constraints `t2` - this is not allowed MDEV-24601
use test;
create table t(check (t0>0),
               t0 int,
               t1 int check (t1<0),
               t2 int check (t2<-1),
               CONSTRAINT tc_1 check(t1 > 1),
               CONSTRAINT t2 check(t2 > 1));
show create table t;
--sorted_result
select * from information_schema.table_constraints where CONSTRAINT_TYPE='CHECK';
--sorted_result
select * from information_schema.check_constraints;
drop table t;
