--
-- ### View checks
--

create table t1 ( primary a int not null, b string(30));
create table t2 ( primary c int not null, d string(30));
create table t3 ( primary e int not null, f string(30));
create table t4 ( primary g int not null, h string(30));
create index t4_idx on t4(h);

create view myview1 as select tx.a as a, ty.d as d from t1 tx, t2 ty where tx.a = ty.c;
create view myview2 as select tx.a as a, ty.e as e from myview1 tx, t3 ty where tx.a = ty.e;

insert into t1 values ( 1, 'xxx');
insert into t1 values ( 2, 'yyy');
insert into t1 values ( 3, 'zzz');

insert into t2 values ( 1, 'xxx');
insert into t2 values ( 2, 'III');
insert into t2 values ( 3, 'OOOO');

insert into t3 values ( 1, 'Iwan');
insert into t3 values ( 2, 'xxx');
insert into t3 values ( 3, 'hurga');


insert into t4 values ( 1, 'alpha');
insert into t4 values ( 2, 'beta');
insert into t4 values ( 3, 'gamma');
insert into t4 values ( 4, 'delta');

-- select * from myview;
-- select * from myview;
plan select * from myview2 mv, t4 where mv.a = t4.g and t4.h = 'beta'; 
select * from myview2 mv, t4 where mv.a = t4.g and t4.h = 'beta'; 


create view v1 as select t1.a as X, t2.d as Y from t1 inner join t2 on t1.a = t2.c;
select X from v1 where Y = 'xxx';


drop view myview1;
drop view myview2;
drop view v1;
drop table t1;
drop table t2;
drop table t3;
drop table t4;


